Skip to content

core: make local dependency generation linear and merge-safe - #13771

Merged
vito merged 4 commits into
dagger:mainfrom
vito:generate-local-deps-linear
Jul 30, 2026
Merged

vito merged 4 commits into
dagger:mainfrom
vito:generate-local-deps-linear

Conversation

@vito

@vito vito commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

ModuleSource.generateLocalDependencies walked each module's full transitive closure leaf-first and ran every dependency's SDK generator. Each of those generator runs recursively called generateLocalDependencies for 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-module dagger generate performed 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 apply to match context past an existing copy), duplicating generated code. This corrupted dagger.gen.go for a module that is both workspace-managed and a local dependency of sibling modules.

Fix

fix(core): make local dep generation linear restructures the walk as a memoized recursion over direct dependencies:

  • Each dependency's transitive staging is computed by a recursive generateLocalDependencies select through workspace.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.
  • Workspaces built by this staging path are marked, so the dependency's SDK generator's own nested generateLocalDependencies call short-circuits instead of re-walking the closure the workspace already carries. This is what breaks the exponential fan-out.
  • Staged results accumulate by overlaying changesets onto the workspace root directory (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.
  • Dependency cycles are rejected up front, since the recursion would otherwise self-wait on its own in-flight dagql call.

refactor(core): scope the staged-generation mark replaces 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 a StagedGeneration set, so the mark cannot exist apart from the content it vouches for, and Workspace.withChanges stays 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 generateLocalDependencies contract 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's generate now 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. No longer needed - see #13773

Verification (dagger repo, dev engine)

  • Scoped generate from toolchains/docs-dev: 21 nested walks → 10 (each module once), 1m38s → 1m03s.
  • Full-repo 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

vito added 3 commits July 29, 2026 12:26
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]>
@vito

vito commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Added a third commit staging direct dependencies in parallel through a util/parallel pool capped at 8 (matching the parallel-changeset bound). Sibling dependencies are independent and shared transitive dependencies converge on the same memoized dagql calls, so concurrency joins in-flight work; overlays are folded afterwards in declaration order to keep the resulting changeset deterministic, and each recursion level builds its own pool so nested staging can't deadlock on a shared semaphore.

Measured on the dagger repo: warm full-repo dagger generate dagger-go-sdk drops from ~1m40s to ~48s; first run against a fresh engine is roughly unchanged (~2m) since the deep dependency chains are inherently serial. Output remains byte-identical and idempotent.

@vito

vito commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

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 TomChv 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.

Amazing optimization! Ship it 🚀

@vito
vito merged commit 352dd3d into dagger:main Jul 30, 2026
87 checks passed
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