Repository navigation
feat: ModuleSource.generateLocalDependencies - #13715
Conversation
82a53b7 to
1f72f64
Compare
This comment was marked as outdated.
This comment was marked as outdated.
5340af2 to
077c113
Compare
|
Quick review while this is still WIP: Main change requested: scope each dep's generation to its owning SDKgenerateOneLocalDependency currently runs the workspace's entire generator group per dependency. That relies on every foreign SDK's @generate no-op'ing under a cwd that isn't theirs (unenforced, spread across three SDK The workspace config already declares ownership: [[modules..as-sdk.modules]] entries (ModuleEntry.AsSDK.Modules[].Path, core/workspace/config.go) map workspace-relative module paths to their SDK module. So:
Open decision: what to do when a dep's path isn't claimed by any as-sdk.modules entry — loud error (actionable config-hygiene signal, avoids the silent-empty-changeset → confusing downstream stale-bindings failure) vs. Bugs to fix
Minor
TestsFine to defer E2E to the SDK-wiring phase per the checklist, but localDepClosureLeafFirst is pure and cheap to unit-test now (diamond dedup, leaf-first order, git-skip). Worth adding before this leaves WIP. |
e5b3881 to
def29c4
Compare
|
Thanks @kpenfound — all applied in Main change — scope each dep to its owning SDK: done. Build a
Bugs:
Minors:
Tests: added Re-validated E2E against a dev engine on the |
|
go:generate-dagger-runtimes Fails but I tried : |
| Args( | ||
| dagql.Arg("changes").Doc("Changes to apply."), | ||
| ), | ||
| dagql.NodeFunc("withCwd", s.withCwd). |
There was a problem hiding this comment.
I feel like this should mirror withWorkdir on Container 🤔
Maybe we should do Workspace.cwd => Workspace.workdir too? Wouldn't block this PR on that since we've got enough to coordinate, but in the long run it feels weird to have different terminology for the same concept
There was a problem hiding this comment.
Agreed on mirroring Container.withWorkdir. Renamed the new setter withCwd → withWorkdir (f736136100).
Left the pre-existing Workspace.cwd getter (and its ~39 doc mentions) as-is for now: renaming it to workdir breaks the ws.cwd reads in 4 SDK dang modules (go-sdk go-sdk.dang/mod.dang, typescript-sdk typescript-sdk.dang/mod.dang), which is the coordination you didn't want to block on. So there's a temporary withWorkdir-sets / cwd-reads asymmetry that a follow-up can resolve atomically across engine + SDKs. I'll track that.
f3a271c to
245624e
Compare
Adds ModuleSource.generateLocalDependencies(workspace), which generates a module's transitive local dependency closure leaf-first, so a set of inter-dependent local modules can be codegen'd in the correct order. TOML modules build from committed files and never runtime-codegen, and module loading is recursive. Editing several inter-dependent local modules at once therefore deadlocks: to codegen a module the engine loads its dependencies, and stale committed bindings can't be repaired at load time. generateLocalDependencies walks the closure leaf-first and, for each dependency, runs the workspace's SDK generators against a workspace scoped to that dependency (its path as the working directory) with the already-generated dependencies overlaid, folding each changeset into an accumulator. The module's own generator overlays that before generating itself. Ordering is introduced locally, per module, on demand; the global generator scheduler stays concurrent. The constructed workspace reaches the SDK @generate functions through a new GeneratorGroup.WorkspaceOverride, applied to the context in Run and read by loadWorkspaceArg in place of the ambient currentWorkspace. It is opt-in via an internal withWorkspace arg on Workspace.generators, so the ordinary dagger generate flow is unchanged. Engine changes: - core/schema/modulesource.go: generateLocalDependencies(workspace) field + handler (leaf-first closure, per-dep scoped/overlaid workspace, fold changesets), gated AfterVersion("v1.0.0-0") - core/generators.go: GeneratorGroup.WorkspaceOverride - core/schema/workspace.go: internal withWorkspace arg on generators(); new withWorkdir setter - core/modfunc.go: loadWorkspaceArg returns the override first - core/workspace.go: ContextWithWorkspaceOverride / WorkspaceOverrideFromContext Design: hack/designs/module-source-generate-local-dependencies.md Closes #13706. Part of #13662. Signed-off-by: Tom Chauveau <[email protected]>
245624e to
0c1ca57
Compare
vito
left a comment
There was a problem hiding this comment.
Some nits, nothing blocking - the withWorkdir thing is a cheap clean-up though if you want to get it out of the way, the rest is mostly "hey my PR did this too but in some other way" which doesn't make sense to block on (I can just address it in my own PR).
| for _, dep := range deps { | ||
| depPath := dep.Self().SourceRootSubpath | ||
| depChanges, err := s.generateOneLocalDependency(ctx, dag, workspace, depPath, acc, owners) | ||
| if err != nil { | ||
| return res, fmt.Errorf("generate local dependency %q: %w", depPath, err) | ||
| } | ||
| if depChanges.Self() == nil { | ||
| continue // dependency not managed by any SDK; skipped with a warning | ||
| } | ||
| if acc.Self() == nil { | ||
| acc = depChanges | ||
| continue | ||
| } | ||
| acc, err = mergeChangesets(ctx, dag, acc, depChanges) | ||
| if err != nil { | ||
| return res, fmt.Errorf("merge local dependency %q changes: %w", depPath, err) | ||
| } | ||
| } |
There was a problem hiding this comment.
nit: would be nice to do this in parallel, but I guess that's complicated when there are dependencies between them; you'd need DAG-derived grouped parallelism of some sort
| Field: "generators", | ||
| Args: []dagql.NamedInput{ | ||
| {Name: "include", Value: dagql.Opt(dagql.ArrayInput[dagql.String]{dagql.String(owner)})}, | ||
| {Name: "withWorkspace", Value: dagql.Opt(dagql.NewID[*core.Workspace](wsDID))}, |
There was a problem hiding this comment.
nit: what is this withWorkspace argument doing? How does it compare to the Workspace it's being called against? 🤔 Trying to sus out why we can't just call generators against it. I ran into something kind of similar in #13635 and resolved it by storing the BoundWorkspace on GeneratorGroup (and related): f454788#diff-9623ae30098c2cd5d35ce1d7cd378ef0b10344322868e5de6434c2d30fbcf2c0 - wondering if the same approach would have solved this.
(Not blocking though if so, I can clean it up in my PR.)
| }, nil) | ||
| } | ||
|
|
||
| func (s *workspaceSchema) withWorkdir( |
There was a problem hiding this comment.
nit: technically if I do Workspace.withWorkdir("a") and then withWorkdir("b") that should give me a/b to be consistent with Container. Right now it gives you b. (If I withWorkdir("/b") that would yield /b)
| @@ -90,6 +90,14 @@ type GeneratorGroup struct { | |||
| // workspace.generators().run()) would execute in the calling module's runtime | |||
| // client, where workspace auto-injection is disabled. | |||
| WorkspaceClientID string `json:"workspaceClientID,omitempty"` | |||
There was a problem hiding this comment.
Continuing the note from before, just for context - in my PR I got rid of WorkspaceClientID in all these places, since it's just an inferior form of storing the entire BoundWorkspace which stores any ClientID it depends on anyway (if any)
| // a workspace's generators against a scoped/overlaid workspace the engine | ||
| // constructed — e.g. ModuleSource.generateLocalDependencies staging a | ||
| // dependency's codegen before generating the dependent. | ||
| func ContextWithWorkspaceOverride(ctx context.Context, ws dagql.ObjectResult[*Workspace]) context.Context { |
There was a problem hiding this comment.
Haha OK yeah we both ended up doing this style of solution - my PR also introduced WorkspaceToContext/WorkspaceFromContext, only it was LLM-motivated and not transitive-codegen-motivated.
|
Thanks for the review, I let you take care of these nits in your follow up so I can continue my work on SDK |
|
Validation report from testing the two SDK PRs that consume this API (dagger/go-sdk#12, dagger/typescript-sdk#12) against
FailureTwo distinct symptoms: doubly-prefixed paths ( Minimal repro — 4 plain Go modules, no TypeScript neededmkdir -p repro/.dagger/modules/{a,b,e,f} && cd repro && git init -q # .git is required
for m in a:. b:a e:b f:e; do
n=${m%%:*}; dep=${m##*:}
printf 'package main\n\ntype %s struct{}\n' "$(echo $n | tr a-z A-Z)" > .dagger/modules/$n/main.go
{ echo "name = \"$n\""; echo 'engineVersion = "v1.0.0-beta.8"'; echo; echo '[runtime]'; echo ' source = "go"'
[ "$dep" != "." ] && { echo; echo '[[dependencies]]'; echo " name = \"$dep\""; echo " source = \"../$dep\""; }
} > .dagger/modules/$n/dagger-module.toml
done
{ echo '[modules.dagger-go-sdk]'; echo 'source = "github.com/dagger/go-sdk@main"'; echo
for m in a b e f; do echo "[modules.$m]"; echo "source = \".dagger/modules/$m\""; echo; done
echo '[modules.dagger-go-sdk.as-sdk]'; echo 'name = "go"'; echo
for m in a b e f; do echo '[[modules.dagger-go-sdk.as-sdk.modules]]'; echo "path = \".dagger/modules/$m\""; echo; done
} > dagger.toml
dagger generate --no-apply # -> apply ours patch: ... already exists in working directoryDropping Bisect matrix
Trigger: closure size ≥3 with at least one Go module anywhere in the chain. Pure-TS chains are immune at the same depth; cross-SDK is not required (pure Go reproduces it). Root-cause hypothesisIn withWorkdir(cwd = depPath) // workdir becomes .dagger/modules/b
withChanges(acc) // acc is workspace-root-relativeIf Swapping the order ( What works — please don't regress it
Unrelated footgun spottedA workspace without |
|
Root-caused and fixed — thanks for the thorough report and bisect, they pinned it exactly. The fix is entirely in dagger/go-sdk#12 ( Not the engine's Actual cause: the Go SDK's
This is exactly why pure-TS was immune: the TS SDK's Fix (go-sdk#12): fold each module's changes onto Validated against a dev engine on both failing shapes from your matrix:
Real apply lands all files at the right paths with 0 nested-leak dirs, and a second run reports |
Adds
ModuleSource.generateLocalDependencies(workspace), which generates a module's transitive local dependency closure, leaf-first, so a set of inter-dependent local modules can be codegen'd in the correct order.Problem
TOML modules build from committed files and never runtime-codegen, and module loading is recursive (
asModule->loadDependencyModules). So editing several inter-dependent local modules at once deadlocks: to codegenapp-py(python ->lib-go, go) the engine loadslib-go; if its committed bindings are stale they can't be repaired at load, so codegen fails.lib-gomust be generated first, but the generator scheduler has no ordering, no staged visibility, and no cross-SDK view.Approach
generateLocalDependencies(workspace)walks the module's local dependency closure leaf-first and, for each dependency, runs the workspace's SDK generators against a workspace scoped to that dependency (its path as the working directory) with the already-generated dependencies overlaid. It folds each dependency's changeset into an accumulator and returns it; the module's own generator overlays that before generating itself. Ordering is introduced locally, per module, on demand — the global generator scheduler stays concurrent.The constructed workspace reaches the SDK
@generatefunctions through a newGeneratorGroup.WorkspaceOverride, applied to the context inRunand read byloadWorkspaceArgin place of the ambientcurrentWorkspace. It is opt-in via an internalwithWorkspacearg onWorkspace.generators, so the ordinarydagger generateflow is unchanged. Cache-safe: the injected workspace lands in each generator's call ID.Naming: the new per-dependency scoping setter is
Workspace.withWorkdir(mirrorsContainer.withWorkdir, per review). The pre-existingWorkspace.cwdgetter is left as-is — renaming it toworkdirtouches the go/typescript SDK dang modules that readws.cwd, so it's a coordinated follow-up.Design:
hack/designs/module-source-generate-local-dependencies.md.Engine changes
core/schema/modulesource.gogenerateLocalDependencies(workspace)field + handler (leaf-first closure, per-dep scoped/overlaid workspace, fold changesets). GatedAfterVersion("v1.0.0-0")core/generators.goGeneratorGroup.WorkspaceOverride— applied to ctx inRun, persisted, attached as a dependency resultcore/schema/workspace.gowithWorkspacearg ongenerators()-> sets the override; newwithWorkdirsettercore/modfunc.goloadWorkspaceArgreturns the override first (bypasses the module-function guard)core/workspace.goContextWithWorkspaceOverride/WorkspaceOverrideFromContextChecklist
generateLocalDependenciesengine API + schema (version-gated; regenerated goldens)@generatewiring to call it — go + typescript (external SDK repos); python is a follow-upa -> b -> cclosure against a dev engine) +TestLocalDepClosureLeafFirstunit testCloses #13706. Part of #13662.
PR chain
ModuleSource.generateLocalDependencies(+ the runtime fixes found in validation)@generateuses the API (draft)@generateuses the API (draft)The two SDK PRs depend on this engine PR. Related: #13727 (
asSDKexplicit workspace) is now ✅ merged into main, and itsworkspacearg landed optional — so the SDK-side asSDK-workspace changes (go-sdk#14 + TS/Python follow-ups) can merge independently of this chain.