Dagger TypeScript SDK 中 ClientModuleSourceOpts 模块源解析选项详解
2026/9/15 19:54:51 网站建设 项目流程

Dagger TypeScript SDK 中 ClientModuleSourceOpts 模块源解析选项详解

【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger

导读

ClientModuleSourceOpts是 Dagger TypeScript SDK(@dagger.io/dagger)中用于解析模块源(Module Source)引用字符串的配置对象,它直接服务于client.moduleSource(refString, opts?)这一核心 API。本文以官方 API 参考文档为主体,结合仓库内 TypeScript SDK 生成源码与 Dagger 引擎端 GraphQL Schema 实现,逐项拆解其全部选项(allowNotExistsdisableFindUprefPinrequireKind),说明每个选项在底层如何影响模块配置查找、Git 引用解析与错误行为,并给出可直接运行的实战示例。读完本文,你将能够精确控制 Dagger 模块源的加载行为——包括在尚不存在的目录中初始化新模块、禁止向上目录查找dagger.json、锁定 Git 模块版本以及校验模块源类型。

一、类型定义速览

ClientModuleSourceOpts在 TS 中的类型签名为一个可选属性对象(Type Alias):

export type ClientModuleSourceOpts = { allowNotExists?: boolean disableFindUp?: boolean refPin?: string requireKind?: ModuleSourceKind }

该类型定义位于 sdk/typescript/src/api/client.gen.ts,其四个属性全部可选。它作为moduleSource方法的第二个参数出现,而moduleSource是客户端上创建模块源实例的入口方法(sdk/typescript/src/api/client.gen.ts):

moduleSource = ( refString: string, opts?: ClientModuleSourceOpts, ): ModuleSource => { const metadata = { requireKind: { is_enum: true, value_to_name: ModuleSourceKindValueToName, }, } const ctx = this._ctx.select("moduleSource", { refString, ...opts, __metadata: metadata, }) return new ModuleSource(ctx) }

从实现可以看出:opts中的属性会被原样(...opts)注入到 GraphQL 查询的moduleSource字段参数中,其中requireKind因为是枚举类型,需要通过ModuleSourceKindValueToName做枚举值到查询参数的转换。

版本提示:本文基于version-0.19的参考文档。在更新的 SDK 生成版本中(sdk/typescript/src/api/client.gen.ts),该类型还额外包含一个version?: string字段("Version query for a Git module source."),对应引擎端按版本门控(workspace.VersionQueriesVersion)暴露的 Git 版本查询能力,参见 core/schema/modulesource.go。

二、四个选项逐一拆解

2.1 allowNotExists — 允许本地路径尚不存在

allowNotExists?:boolean原文:If true, do not error out if the provided ref string is a local path and does not exist yet. Useful when initializing new modules in directories that don't exist yet.

refString指向一个本地路径,而该路径尚不存在时,引擎默认会报错。设置allowNotExists: true后,即使目录不存在也不会立即失败——这对于在尚未创建的目录中初始化新模块(即"先引用、后创建"的流程)非常关键。

引擎端的行为印证在 core/schema/modulesource.go 的findGitModuleConfig与本地模块源加载逻辑中:当目录中找不到dagger.json配置时,若allowNotExiststrue则返回("", false, nil)静默通过;否则抛出形如git module source %q does not contain a dagger config file的错误。本地路径场景下,allowNotExists同样被透传至localModuleSource/workspaceModuleSourceByName等内部函数(core/schema/modulesource.go 附近),并在case allowNotExists:分支中决定是否容忍缺失的配置。

需要注意:该选项对 Git 类模块源同样适用(源码注释明确说明它"Mirrors localModuleSource's allowNotExists behavior",见 core/schema/modulesource.go),用于把某个 Git 引用仅作为+defaultPath上下文目录加载的场景,此时会返回一个ConfigExists=false的 context-only 模块源。

2.2 disableFindUp — 禁止向上目录查找配置

disableFindUp?:boolean原文:If true, do not attempt to find dagger.json in a parent directory of the provided path. Only relevant for local module sources.

Dagger 在解析本地模块源时默认具备find-up(向上查找)行为:如果给定路径本身没有dagger.json,引擎会沿父目录逐级向上寻找模块配置文件。这在嵌套子目录中直接引用模块时很便利,但也可能造成"意外命中上层模块"的问题。

设置disableFindUp: true后,引擎只会在给定路径精确位置查找模块配置。从引擎实现看,该选项对应gitModuleSource函数中的doFindUp参数——为false时直接调用moduleConfigInDir检查指定子路径,若找不到且未开启allowNotExists即报错(core/schema/modulesource.go);同时源码注释还提醒了一个边界:开启 find-up 时,引擎会先校验给定子路径本身是否存在,防止dagger -m github.com/dagger/dagger/not/a/real/dir这类写法因向上查找到真实模块而意外成功(core/schema/modulesource.go)。

适用前提:该选项"Only relevant for local module sources",仅对本地路径类引用有意义,对 Git 类模块源不适用。

2.3 refPin — 固定模块源版本

refPin?:string原文:The pinned version of the module source

refPin用于为模块源指定一个固定(pin)的版本,典型场景是 Git 模块源:将模块锁定到某个具体 commit/版本,避免随引用分支漂移。引擎端在gitModuleSource中通过parsed.GitRef(ctx, dag, refPin)将 refPin 传入 Git 引用解析(core/schema/modulesource.go),解析结果中的CommitRef.SHA等信息会写入GitModuleSource(core/schema/modulesource.go),从而确定该模块源的具体快照。

在仓库内部,refPin还被广泛用于模块依赖锁定:例如 core/modulesource.go 在加载父模块时传入refPin: parentSrc.Git.CommitdisableFindUp: true,core/modulesource.go 在解析依赖时传入depPin,而 core/schema/modulesource.go 在基于dagger.lock配置重建模块源时也会写入refPin: cfg.Pin。可见refPin是 Dagger 模块锁文件(pin)机制在 API 层面的直接体现。

2.4 requireKind — 强制校验模块源类型

requireKind?:ModuleSourceKind原文:If set, error out if the ref string is not of the provided requireKind.

requireKind用于前置校验:若设置,当refString解析出的模块源类型与指定的ModuleSourceKind不一致时,引擎直接报错。这能在开发早期拦截"传错了引用格式"的失误(例如本应传本地路径却传了 Git 引用)。

ModuleSourceKind枚举(枚举文档)包含三种值,其 SDK 定义为(sdk/typescript/src/api/client.gen.ts):

枚举成员底层字符串值含义
Dir(别名DirSource"DIR_SOURCE"目录型模块源
Git(别名GitSource"GIT_SOURCE"Git 仓库型模块源
Local(别名LocalSource"LOCAL_SOURCE"本地路径型模块源

在引擎端,模块源类型注册于 core/modulesource.go,ModuleSourceKindLocalModuleSourceKindGit分别对应"LOCAL_SOURCE""GIT_SOURCE"。由于requireKind是枚举参数,TS SDK 在构建查询时通过ModuleSourceKindValueToName将其转换为"DIR"/"GIT"/"LOCAL"字符串传入 GraphQL(sdk/typescript/src/api/client.gen.ts)。

三、底层映射:从 TypeScript 到 GraphQL Schema

ClientModuleSourceOpts并非孤立存在,它直接对应引擎端 GraphQL 查询字段Query.moduleSource的入参。Schema 定义位于 core/schema/modulesource.go:

dagql.NodeFunc("moduleSource", s.moduleSource). WithInput(dagql.PerClientInput). Doc(`Create a new module source instance from a source ref string`). Args( dagql.Arg("refString").Doc(`The string ref representation of the module source`), dagql.Arg("version"). Doc(`Version query for a Git module source.`). View(AfterVersion(workspace.VersionQueriesVersion)), dagql.Arg("refPin").Doc(`The pinned version of the module source`), dagql.Arg("disableFindUp").Doc(`If true, do not attempt to find a module config file in a parent directory of the provided path. Only relevant for local module sources.`), dagql.Arg("allowNotExists").Doc(`If true, do not error out if the provided ref string is a local path and does not exist yet. Useful when initializing new modules in directories that don't exist yet.`), dagql.Arg("requireKind").Doc(`If set, error out if the ref string is not of the provided requireKind.`), )

可以看到,TS 侧的每个字段都与 GraphQL 参数一一对应(refString为必填位置参数,其余为可选参数),且引擎端文档字符串与 ClientModuleSourceOpts 参考文档 中的描述完全一致——这正是该 API 参考文档的权威来源。version参数被View(AfterVersion(...))门控,因此旧版本客户端不会查询到它。

四、实战示例:四种典型用法

moduleSource的完整签名与选项语义已确认(sdk/typescript/src/api/client.gen.ts),以下示例可直接在 Dagger TypeScript SDK 中运行:

import { client, ModuleSourceKind } from "@dagger.io/dagger" // 1. 在尚不存在的目录中初始化新模块:先拿到模块源,再调用初始化流程 const pending = client.moduleSource("./my-new-module", { allowNotExists: true, }) // 2. 精确加载指定路径,禁止向上查找父目录中的 dagger.json const exact = client.moduleSource("/tmp/isolated-module", { disableFindUp: true, }) // 3. 加载 Git 模块源并将其固定到具体 commit const pinned = client.moduleSource("github.com/org/example@main", { refPin: "9f1c2d3e4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d", }) // 4. 前置校验引用类型:只接受本地模块源,否则立即报错 const localOnly = client.moduleSource("./some/dir", { requireKind: ModuleSourceKind.LocalSource, })

各选项可以组合使用。例如"初始化一个新模块并精确限定位置"可同时传入allowNotExists: truedisableFindUp: true,避免向上查找到无关配置的同时容忍目标目录尚未创建。

五、跨 SDK 对应:Go 与生成客户端

ClientModuleSourceOpts是 Dagger 各语言 SDK 生成客户端的通用模式之一。以 Go SDK 为例,sdk/go/dagger.gen.go 中存在字段一一对应的ClientModuleSourceOpts结构体(RefPinDisableFindUpAllowNotExistsRequireKind),并在构建查询时按需调用q.Arg("refPin", ...)q.Arg("disableFindUp", ...)等——空值(IsZeroValue)会被自动跳过。仓库内集成测试的生成产物(例如 core/integration/testdata/modules/go/defaults/internal/dagger/dagger.gen.go)也展示了同一套参数注入逻辑,验证了该选项在各 SDK 间的一致性。

因此,本文对四个选项的语义说明同样适用于 Go、Python、Java 等语言的对应生成 API——底层都是同一个 GraphQLmoduleSource字段。

六、常见问题与注意事项

  • disableFindUp与嵌套目录:在多层嵌套子目录中直接引用上层模块时,务必明确是否需要 find-up;若希望引用精确位置,请设置disableFindUp: true,否则引擎可能在父目录找到意料之外的dagger.json
  • allowNotExists只"容忍"不存在的配置:它不会在加载时替你创建任何文件,只是让"先引用、后初始化"的流程得以通过校验;实际初始化仍需调用模块初始化相关流程。
  • requireKind是校验而非转换:它不会把模块源"变成"指定类型,只会因类型不匹配而报错,适合在流水线早期做防御性校验。
  • refPin与锁文件:模块的 pin 信息与dagger.lock联动(见 core/modulesource.go),手动传入refPin时应确保该版本真实存在,否则 Git 解析阶段(parsed.GitRef)会失败。
  • 版本差异version字段属于较新的版本查询能力(受workspace.VersionQueriesVersion门控),在 version-0.19 参考文档对应的类型中尚未出现;升级 SDK 后该字段会自动可用。

七、延伸阅读

  • ClientModuleSourceOpts 官方类型参考:本文所依据的原始参考文档
  • ModuleSourceKind 枚举参考:requireKind的取值定义
  • api/client.gen 索引:moduleSource所属 API 模块的完整索引
  • TypeScript SDK 参考文档首页:@dagger.io/dagger参考总览
  • TS 生成客户端源码:ClientModuleSourceOpts类型(#L2674-L2699)与moduleSource方法(#L13443-L13460)的真实实现
  • 引擎端 GraphQL Schema:moduleSource字段的参数声明(#L62-L77)与allowNotExists/doFindUp/refPin的底层处理逻辑(#L713-L863)
  • Go SDK 生成客户端:Go 语言对应的ClientModuleSourceOpts结构体(#L13737-L13768)

【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询