Skip to content

[1.0-beta] Go SDK: no-codegen-at-runtime + single-pass codegen - #13210

Closed
eunomie wants to merge 13 commits into
dagger:1.0-betafrom
eunomie:workspace-go-no-codegen-at-runtime
Closed

eunomie wants to merge 13 commits into
dagger:1.0-betafrom
eunomie:workspace-go-no-codegen-at-runtime

Conversation

@eunomie

@eunomie eunomie commented May 22, 2026 •

Copy link
Copy Markdown
Member

What this does

Reworks the Go SDK codegen path so that:

  1. A module can opt out of codegen at runtime. With
    codegen.legacyCodegenAtRuntime=false in dagger.json, the Go SDK trusts
    the committed dagger.gen.go + internal/dagger files and skips the
    in-container codegen pass at runtime (it errors with an actionable message
    if the committed files are missing).
  2. generate-module runs once for Go modules, including self-calls.
    Previously self-calls modules ran it twice (the engine built+ran the module
    via asModule just to discover its own types, then ran codegen again). Now
    the Go codegen discovers its own types from the existing packages.Load
    analysis, emits them as introspection JSON, and merges them into the deps
    schema via the engine's new schema(json).merge(...) tool — in a single
    pass. No AST analyzer; packages.Load is retained.
  3. moduleTypes is dropped from the Go SDK. Type discovery happens through
    the generated invoke() dispatcher's empty-parentName arm. Other SDKs
    (Python, TS) keep moduleTypes and the engine-side self-append.
  4. dagger generate <module> no longer eagerly loads unrelated modules.
    A workspace generate scoped to one module narrows the pending module set
    by the requested include patterns, so a broken/stale sibling module no
    longer blocks regenerating a good one.

Commits (read in order)

  1. core/modules: add legacyCodegenAtRuntime codegen config field
  2. feat: add schema merge tool for codegen — engine-side
    schema(json).merge(...).contents, exposed over dagql for all SDKs. Kept
    minimal: only the schema / merge / contents the codegen needs (no
    listTypes / hasType / describeType or IntrospectionType graph).
  3. core/sdk/go_sdk: skip codegen at runtime when opted in
  4. cmd/codegen/generator/go: emit empty-parentName module dispatch
  5. cmd/codegen + core: single-pass Go codegen, drop moduleTypes
  6. test: add e2e tests for legacyCodegenAtRuntime config — config guardrail
    • missing-generated-files error.
  7. engine/server: narrow workspace module loading by include patterns
  8. cmd/dagger: scope generate module loading to requested generators
  9. go: fix Go self-call schema names and cover dependency use (#2)
  10. go: match engine name casing for self-call enum values, args and types —
    enum values → SCREAMING_SNAKE, args → lowerCamel, type names/refs → Camel,
    matching what the engine exposes from the same Go source.

Status / notes

  • Rebased onto current workspace. Engine builds; core/...,
    cmd/codegen/... and engine/server unit tests pass; TestSelfCalls
    passes (18/18) against a dev engine built from this branch.
  • The net new engine API surface is small: only a Schema object exposing
    merge / contents (plus the schema(json) constructor) surfaces in the
    SDKs, so the SDK regen diff is correspondingly small.
  • The earlier eager-load limitation (a stale/broken sibling module blocking
    dagger generate of a good one) is now addressed by commits 7–8.
  • Design specs + implementation plans were kept local (not in this PR) — happy
    to share separately.

Test plan

  • Self-calls Go module: dagger generate runs codegen once (no asModule
    build/run), produces a compilable dag.MyModule() self binding, self types
    land in internal/dagger/<module>.gen.go.
  • Non-self-calls Go module: unchanged, single pass.
  • Python/TS modules: unaffected (still use moduleTypes).
  • legacyCodegenAtRuntime=false with committed files: runtime skips codegen.
  • schemaTools merge — unit (core/schematool_test.go) + integration
    (TestSchemaTools) cover merge, idempotency, conflict, contents round-trip,
    and the live engine schema.
  • legacyCodegenAtRuntime e2e (TestRuntimeCodegen): config guardrail and
    the missing-generated-files error.
  • Workspace generate narrowing — unit
    (TestFilterPendingWorkspaceModulesByInclude) covers narrowing the pending
    module set by include patterns.

@grouville
grouville force-pushed the workspace branch 2 times, most recently from d9e8204 to b1caab7 Compare May 22, 2026 23:01
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch 3 times, most recently from 400a8d0 to 1107213 Compare May 26, 2026 09:27
@eunomie
eunomie marked this pull request as ready for review May 26, 2026 11:30
@eunomie
eunomie requested review from a team as code owners May 26, 2026 11:30

@grouville grouville left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feature, I found one small regression and tightened the test coverage on eunomie#2

@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from 93bf755 to cc40189 Compare May 27, 2026 10:03
@vito
vito requested a review from a team as a code owner May 27, 2026 17:23
@grouville
grouville self-requested a review May 27, 2026 19:24
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch 2 times, most recently from 9abdda9 to a383ce1 Compare May 28, 2026 17:44
@dagger-codex dagger-codex Bot changed the title Go SDK: no-codegen-at-runtime + single-pass codegen [workspace] Go SDK: no-codegen-at-runtime + single-pass codegen May 28, 2026
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from a383ce1 to 4b796e0 Compare May 29, 2026 07:55
@tiborvass
tiborvass force-pushed the workspace branch 2 times, most recently from b9b4b5f to 4e36540 Compare May 30, 2026 05:10
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch 2 times, most recently from 7a16535 to c9932d8 Compare May 30, 2026 08:30
@eunomie
eunomie force-pushed the workspace branch 2 times, most recently from 74f8372 to 70d629a Compare May 30, 2026 20:53
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from 1c77309 to b753f71 Compare June 2, 2026 15:16
@dagger-codex dagger-codex Bot changed the title [workspace] Go SDK: no-codegen-at-runtime + single-pass codegen [1.0-beta] [workspace] Go SDK: no-codegen-at-runtime + single-pass codegen Jun 3, 2026
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch 3 times, most recently from 81f1be4 to ad74b21 Compare June 4, 2026 11:36
@dagger-codex dagger-codex Bot changed the title [1.0-beta] [workspace] Go SDK: no-codegen-at-runtime + single-pass codegen [1.0-beta] Go SDK: no-codegen-at-runtime + single-pass codegen Jun 4, 2026
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from ad74b21 to a5d3be7 Compare June 5, 2026 09:41
@eunomie

eunomie commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Benefit examples:

  • run a function on a generate module
dagger ~/src/empty $ dagger -m .dagger/modules/my-module/ call container-echo --string-arg plop stdout
✔ connect 0.0s
✔ load workspace: . 0.2s
✔ parsing command line arguments 0.0s

✔ myModule: MyModule! 0.0s
✔ .containerEcho(stringArg: "plop"): Container! 2.8s
✔ .stdout: String! 1.0s

plop
  • edit the main.go, change the body of a function (no signature changes)
dagger ~/src/empty $ vi .dagger/modules/my-module/main.go
  • run again the function -> the code will be re-built as it has changed, but there's no codegen -> 0.7s
dagger ~/src/empty $ dagger -m .dagger/modules/my-module/ call container-echo --string-arg bla stdout
✔ connect 0.0s
✔ load workspace: . 0.7s
✔ parsing command line arguments 0.0s

✔ myModule: MyModule! 0.0s
✔ .containerEcho(stringArg: "bla"): Container! 1.4s
✔ .stdout: String! 0.1s

plop: bla
  • edit, add a new function
dagger ~/src/empty $ vi .dagger/modules/my-module/main.go
  • re-generate: only the dagger.gen.go is modified as there's no new dependencies
dagger ~/src/empty $ dagger generate
● ✔ go-sdk:generate-all 2.7s ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⣿⣿⣿⡆⡄⡀⣿⡇

┃ Apply changes?
┃ .dagger/modules/my-module/dagger.gen.go +22 -3
┃
┃ 1 file changed, +22 -3 lines
┃
┃               Apply     Discard
  • call it -> 0.7s, just the build as everything is already generated
dagger ~/src/empty $ dagger -m .dagger/modules/my-module/ call echo --string-arg hello
✔ connect 0.0s
✔ load workspace: . 0.7s
✔ parsing command line arguments 0.0s

✔ myModule: MyModule! 0.0s
✔ .echo(stringArg: "hello"): String! 0.1s

hello
  • add a new function, using self calls
dagger ~/src/empty $ vi .dagger/modules/my-module/main.go
  • enable self calls
dagger ~/src/empty $ vi .dagger/modules/my-module/dagger.json
  • re-generate: new my-module file, this is the module itself as seen as a dependency
dagger ~/src/empty $ dagger generate
● ✔ go-sdk:generate-all 2.7s ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⡆⡄⡀⣿⡇

┃ Apply changes?
┃ .dagger/modules/my-module/dagger.gen.go                    +22 -3
┃ .dagger/modules/my-module/internal/dagger/my-module.gen.go +92
┃
┃ 2 files changed, +114 -3 lines
┃
┃                        Apply     Discard
  • call: 1.2s (probably because there's the new dependency)
dagger ~/src/empty $ dagger -m .dagger/modules/my-module/ call print --string-arg foo
✔ connect 0.0s
✔ load workspace: . 1.2s
✔ parsing command line arguments 0.0s

✔ myModule: MyModule! 0.0s
✔ .print(stringArg: "foo"): String! 1.2s

plop: foo

Code with the self call is just this:

package main

import (
	"context"
	"dagger/my-module/internal/dagger"
)

type MyModule struct{}

// Returns a container that echoes whatever string argument is provided
func (m *MyModule) ContainerEcho(stringArg string) *dagger.Container {
	return dag.Container().From("alpine:latest").WithExec([]string{"echo", "plop:", stringArg})
}

func (m *MyModule) Echo(stringArg string) string {
	return stringArg
}

func (m *MyModule) Print(ctx context.Context, stringArg string) (string, error) {
	return dag.MyModule().ContainerEcho(stringArg).Stdout(ctx)
}

// Returns lines that match a pattern in the files of the provided Directory
func (m *MyModule) GrepDir(ctx context.Context, directoryArg *dagger.Directory, pattern string) (string, error) {
	return dag.Container().
		From("alpine:latest").
		WithMountedDirectory("/mnt", directoryArg).
		WithWorkdir("/mnt").
		WithExec([]string{"grep", "-R", pattern, "."}).
		Stdout(ctx)
}

@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from bc46928 to 549f8db Compare June 8, 2026 08:43
eunomie and others added 13 commits June 8, 2026 10:46
Introduce codegen.legacyCodegenAtRuntime in dagger.json. When false,
the Go SDK trusts the committed dagger.gen.go + internal/dagger files
and skips the runtime codegen pass. Validated at module load:
false requires automaticGitignore=false (generated files must be
committed). Defaults (nil/true) preserve the legacy behavior.

Signed-off-by: Yves Brissaud <[email protected]>
Add an engine-side schemaTools surface exposed over dagql: load an
introspection schema (dag.schema(json)) and merge a module's
introspection-shaped types into it (merge(moduleTypes, moduleName)),
reading the result back via .contents. The merge stamps
@sourceModuleName and @sourcemap on the merged types so SDK
file-splitters can place them.

Kept minimal — only schema(json).merge(...).contents, which is all the
codegen needs. Provided by the engine so every SDK reasons about
schemas through one implementation. Includes the regenerated SDK
clients and reference docs for the new Schema type.

Signed-off-by: Yves Brissaud <[email protected]>
When dagger.json sets codegen.legacyCodegenAtRuntime=false, Go SDK's
Runtime() skips the in-container codegen pass and builds straight from
the committed dagger.gen.go + internal/dagger files. A baseWithoutCodegen
helper mounts the source as-is (with gitconfig + SSH parity for private
modules) and returns the SSH-unset selectors to bracket the go build.
A precheck errors with an actionable message (self-calls aware) when the
committed files are missing. Codegen() and the default Runtime() path are
unchanged.

Signed-off-by: Yves Brissaud <[email protected]>
Restore the typedef-registration branch generate-module emitted before
moduleTypes was split out. The generated invoke() dispatcher now handles
parentName == "" by returning a *dagger.Module built via
dag.Module().WithObject(...) from baked-in TypeDefCode, so the engine can
materialise the module's typedefs via the Runtime + empty-function-name
path. Adds TypeDefCode() to the parsed object/interface/enum/function/
type-spec types alongside the existing TypeDef() helpers.

Signed-off-by: Yves Brissaud <[email protected]>
Make codegen generate-module run once for Go modules, including
self-calls. The Go codegen emits its own module's types as introspection
JSON (new introspect_emit.go, reusing the packages.Load-parsed types)
and, when --self-calls is set, merges them into the deps schema via the
engine's schemaTools (dag.schema(deps).merge(...)) in a single pass.

Drops the Go SDK's moduleTypes implementation (AsModuleTypes returns
nil,false; ModuleTypes method removed). The engine's runCodegen keeps
its self-append only for SDKs that implement moduleTypes (Python, TS);
Go discovers its types via the empty-parentName dispatcher and merges
codegen-side, so the engine no longer builds+runs the module via
asModule during codegen.

Adds the --self-calls flag, SelfCalls config field, and
experimentalPrivilegedNesting on the codegen exec so the in-container
dag client can call schemaTools.

Signed-off-by: Yves Brissaud <[email protected]>
Cover the Go SDK no-codegen-at-runtime feature end to end:

- the config guardrail rejecting codegen.legacyCodegenAtRuntime=false
  unless codegen.automaticGitignore is also false, enforced during
  module load.
- the missing-generated-files error surfaced by the no-codegen runtime
  path, which points the user at `dagger generate`.

Signed-off-by: Yves Brissaud <[email protected]>
Add a WorkspaceModulesInclude client-metadata hint. When set, the engine
narrows the pending workspace modules to those named by the patterns (the
segment before ':' in each, e.g. "go-sdk" in "go-sdk:generate") before
loading them, so an unrelated broken or stale workspace module cannot block
the operation. A module's own dependencies still load transitively; a bare
pattern (no module segment) or one matching no module disables the
narrowing and loads everything.

This complements the single-query lazy-load (which is gated on SingleQuery
and the peeked root fields): `dagger generate` keeps the session open and
roots its query at currentWorkspace, so it needs an explicit hint rather
than the document peek.

Signed-off-by: Yves Brissaud <[email protected]>
`dagger generate <module:generator>` now passes the patterns to the engine
as WorkspaceModulesInclude, so only the named generators' modules (plus
their dependencies) are loaded. Previously every primary workspace module
was loaded just to enumerate generators, so one stale/broken module blocked
regenerating any other — including the very module you wanted to fix.

Signed-off-by: Yves Brissaud <[email protected]>
* go: fix self-call field names

Problem:
When a Go module has SELF_CALLS enabled, the Go SDK writes the module schema
that is then used to generate the module's own Go client.

For exported fields, that schema was using the Go name directly. So a field
called Message was exposed as Message.

The engine exposes that same field as message. Because of that mismatch, the
generated Go client could be wrong. In the case we found, it could generate a
field and a method with the same Go name, which does not compile.

Fix:
Emit exported fields with the same name the engine uses. Message now becomes
message.

Test:
Add a Message field to the existing Go self-call fixture and query message from
the integration test. This proves the generated Go code compiles and the field
is available under the expected name.

Signed-off-by: Guillaume de Rouville <[email protected]>

* tests: cover self-calls from dependencies

Problem:
The review raised a possible failure where a Go module with SELF_CALLS works
when called directly, but breaks when another module depends on it.

If that happened, the caller could still generate Go code for dag.Dep(), but
the running engine would not expose dep at runtime. The user would only see the
failure when calling the function.

We did not reproduce that exact failure, but this is still a useful area to
cover because it is easy to miss. A module dependency is loaded through a
different path than the module being called directly.

Solution:
Add two small Go fixtures:

- self-calls-as-dep: caller -> dep
- self-calls-transitive: caller -> middle -> dep

Only dep has SELF_CALLS enabled. That keeps the tests focused on the dependency
case from the review.

Tests:
viaDep calls a dependency function that calls itself through dag.Dep().

viaDepContainer returns a container created by that self-call and then reads
stdout from the caller. This proves the returned object is still usable.

viaDepWorker calls back into dag.Dep() from a secondary object. This proves the
case is not limited to methods on the root object.

viaTransitiveDep calls through caller -> middle -> dep. This proves the
self-call still works when the module with SELF_CALLS is a dependency of a
dependency.

Signed-off-by: Guillaume de Rouville <[email protected]>

---------

Signed-off-by: Yves Brissaud <[email protected]>
The self-call introspection emitter must reproduce exactly the names the
engine exposes (the TypeDef path passes raw Go names and lets the engine
normalize them). PR #2 fixed exported struct fields. Apply the same to the
remaining names:

- enum values -> ToScreamingSnake (Active -> ACTIVE); previously always
  mismatched, so a self-called enum value sent the wrong wire name.
- function/constructor arguments -> ToLowerCamel (matches the engine; only
  bit non-lowerCamel params such as acronyms).
- object/interface/enum type names and their type references -> ToCamel
  (only bit acronym type names, e.g. HTTPProxy -> HttpProxy).

Add a Color enum to the Go self-calls fixture and a self-call that passes an
enum argument, covering the enum-value casing end to end.

Signed-off-by: Yves Brissaud <[email protected]>
Rebasing onto 1.0-beta hit a conflict in the generated PHP reference
(Binding/Client/Env.html), which was resolved to the 1.0-beta side and
so dropped the Schema type entries added by the schema-merge-tool patch.

Re-run `dagger generate` (engine built from source, so the schema
includes the new Schema type) to restore them: Client.schema()/
loadSchemaFromID and the Binding/Env schema accessors. Also refreshes
the doctum traits.html so php-sdk:api matches.

Signed-off-by: Yves Brissaud <[email protected]>
…enAtRuntime

The Go SDK no-codegen-at-runtime opt-in was a separate
codegen.legacyCodegenAtRuntime flag that was only valid alongside
codegen.automaticGitignore=false, enforced by a load-time guardrail. That
coupling was redundant: opting out of the generated .gitignore already means
the module commits its generated files, so the runtime can trust them instead
of regenerating.

Collapse the two into a single signal. The Go SDK now skips the runtime codegen
pass when codegen.automaticGitignore=false and builds straight from the
committed dagger.gen.go + internal/dagger; unset/true keeps the legacy
runtime-codegen behavior. This also makes the committed files honest: what is
committed is what runs, instead of being regenerated at call time and possibly
differing from what the user sees in git.

- core/modules: drop the LegacyCodegenAtRuntime field, its Clone entry and the
  Validate guardrail; document the dual meaning of AutomaticGitignore.
- core/schema/modulesource: drop the now-empty codegen Validate call.
- core/sdk/go_sdk: key useRuntimeCodegen off AutomaticGitignore and reword the
  missing-generated-files prechecks.
- tests + generated config schema: follow the rename.

Signed-off-by: Yves Brissaud <[email protected]>
useRuntimeCodegen previously skipped the runtime codegen pass for any Go
module with codegen.automaticGitignore=false, trusting the committed
generated files as-is. But the Go SDK's generated module dispatch is
engine-version specific: the empty-parentName typedef registration is only
emitted by the current codegen, so files generated by an older engine fail
to load against a newer one with "unknown object".

The repo's own dev modules pin engineVersion v0.21.4 (the last stable
release) yet run against the in-development engine, so their committed files
don't speak the running engine's module protocol. Keying skip-codegen purely
off automaticGitignore therefore broke every check that loads those modules
(php-sdk:api, ci:bootstrap, the test-split suite, release).

Only honor the opt-out when the module's pinned engineVersion is at least as
new as the running engine; on version skew, fall back to runtime codegen so
the committed files are regenerated to match. This keeps the single-flag
model while making it safe by construction: stale or mismatched committed
files can never silently run.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie force-pushed the workspace-go-no-codegen-at-runtime branch from 549f8db to dcaa047 Compare June 8, 2026 08:53
@tiborvass
tiborvass deleted the branch dagger:1.0-beta June 8, 2026 21:52
@tiborvass tiborvass closed this Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants