Repository navigation
core: make local dependency generation linear and merge-safe - #13771
Conversation
generateLocalDependencies walked each module's full transitive closure and ran every dependency's SDK generator, which recursively re-staged its own closure against a differently-staged workspace on every path through the dependency DAG — exponential fan-out. The accumulated changesets were also merged as git patches over unioned before directories, which can duplicate content for nested or shared dependency paths. Restructure the walk as a memoized recursion over direct dependencies: each dependency's staging is a dagql call keyed by (dependency path, workspace), so every walk that reaches a dependency shares one cached result, and each dependency is generated against exactly its own staged closure. Workspaces built by this path are marked GeneratedDepsStaged (set via an internal stageGeneratedDeps arg on Workspace.withChanges) so the SDK generator's nested generateLocalDependencies call short-circuits instead of re-walking. Staged results accumulate by overlaying changesets onto the workspace root rather than merging patches, so the returned changeset is a single diff against the unstaged root — a common base for callers to merge on. Dependency cycles are rejected up front since the recursion would otherwise self-wait on its own in-flight call. Signed-off-by: Alex Suraci <[email protected]>
Replace the GeneratedDepsStaged workspace boolean, set by an internal stageGeneratedDeps arg piggybacking on Workspace.withChanges, with a dedicated Dagger-internal field following the existing __ convention: Workspace.__withGeneratedLocalDependencies(module, changes). The field both applies the changeset produced by ModuleSource.generateLocalDependencies and records the module's workspace-relative path in a StagedGeneration set, so the mark cannot exist apart from the staged content it vouches for, and withChanges returns to being purely user-facing. The nested generateLocalDependencies short-circuit now matches on the specific module path instead of a blanket boolean, so a managed module nested beneath a dependency (but outside its marked closure) stages its own dependencies again rather than being silently skipped, restoring full deep-staging durability for that case while staying linear via the memoized recursion. Signed-off-by: Alex Suraci <[email protected]>
generateLocalDependencies handled its direct dependencies strictly one at a time, but sibling dependencies are independent: each stages its own transitive closure and runs its own SDK generator, and siblings that share transitive dependencies converge on the same memoized dagql calls, so concurrency joins in-flight work rather than repeating it. Fan the per-dependency phase out through a util/parallel job pool capped at 8 — the same bound used for parallel changeset work — and fold the overlays afterwards in declaration order so the resulting changeset stays deterministic regardless of completion order. Each recursion level builds its own pool, so nested staging cannot deadlock on a shared semaphore. Signed-off-by: Alex Suraci <[email protected]>
|
Added a third commit staging direct dependencies in parallel through a Measured on the dagger repo: warm full-repo |
|
The companion go-sdk fix (rebasing module changesets onto a common base before merging) is now up as dagger/go-sdk#16. |
Signed-off-by: Alex Suraci <[email protected]>
TomChv
left a comment
There was a problem hiding this comment.
Amazing optimization! Ship it 🚀
Problem
ModuleSource.generateLocalDependencieswalked each module's full transitive closure leaf-first and ran every dependency's SDK generator. Each of those generator runs recursively calledgenerateLocalDependenciesfor its own module against a differently-staged workspace, so the work repeated once per path through the dependency DAG rather than once per module. On the dagger repo itself, a scoped 2-moduledagger generateperformed 21 nested closure walks (one leaf module was regenerated 8×), and a full-repo generate took ~15 minutes.The accumulated staging changesets were also merged via git patches over unioned before directories. Changesets staged against different bases violate the merge's common-base assumption: one changeset's staged dependency codegen leaks into the merge base, and another changeset's patch can then re-apply the same insertions at an offset (generated code is repetitive enough for
git applyto match context past an existing copy), duplicating generated code. This corrupteddagger.gen.gofor a module that is both workspace-managed and a local dependency of sibling modules.Fix
fix(core): make local dep generation linearrestructures the walk as a memoized recursion over direct dependencies:generateLocalDependenciesselect throughworkspace.moduleSource(path), so the dagql call is keyed by (dependency path, workspace) alone and every walk that reaches a given dependency shares one cached result. Each dependency is generated against exactly its own staged closure, making the per-dependency generator invocation identical — and cached — across all dependents.generateLocalDependenciescall short-circuits instead of re-walking the closure the workspace already carries. This is what breaks the exponential fan-out.Directory.withChanges) instead of patch-based changeset merging, so overlapping content (nested module paths, shared transitive dependencies) converges, and the returned changeset is a single diff against the unstaged workspace root — a common base for whatever merge the caller does next.refactor(core): scope the staged-generation markreplaces the initial boolean workspace flag with a dedicated Dagger-internal field following the existing__convention:Workspace.__withGeneratedLocalDependencies(module: String!, changes: ChangesetID!)applies the staged dependency codegen and records the module's workspace-relative path in aStagedGenerationset, so the mark cannot exist apart from the content it vouches for, andWorkspace.withChangesstays purely user-facing. The short-circuit matches on the specific module path rather than a blanket boolean, so a managed module nested beneath a dependency (but outside its marked closure) still stages its own dependencies — deep-staging durability is preserved for every module in the stack.Compatibility
The
generateLocalDependenciescontract is unchanged (a changeset staging the module's transitive local dependency closure, to overlay before generating the module), so existing SDKs work as before; the short-circuit only engages for workspaces the engine itself built via the new internal field. Underscore-prefixed fields are already excluded from the introspected schema consumed by codegen and docs, so no SDK bindings change.Relatedly, the go SDK'sNo longer needed - see #13773generatenow rebases each module changeset onto the shared unstaged workspace before merging (dagger/go-sdk@89edf83), which fixes the duplication at the final merge; this PR fixes the same class of corruption inside the staging accumulation and removes the redundant work.Verification (dagger repo, dev engine)
toolchains/docs-dev: 21 nested walks → 10 (each module once), 1m38s → 1m03s.dagger generate dagger-go-sdk: ~15m → ~2m; regenerated output is byte-identical to the previously corrupted run minus the duplicated 133-line block; rerunning on an already-regenerated tree is a byte-identical no-op.go test ./core/schema/ ./core/workspace/...pass.🤖 Generated with Claude Code