Skip to content

fix: prune a scope's legacy client packages by ownership, not wholesale - #110

Merged
TomChv merged 1 commit into
mainfrom
fix/prune-client-packages-ownership
Oct 19, 2026
Merged

TomChv merged 1 commit into
mainfrom
fix/prune-client-packages-ownership

Conversation

@TomChv

@TomChv TomChv commented Oct 19, 2026

Copy link
Copy Markdown
Member

Problem

dagger generate silently deletes a user's directory.

Any direct child directory X/ of a TypeScript scope that contains a file named X.gen.ts is removed whole, the user's files included. types/types.gen.ts, api/api.gen.ts and graphql/graphql.gen.ts are the mainstream openapi/graphql codegen layouts, so a project reaches this by accident, with no warning and nothing printed.

Reproduced on the published v1.0.3 against engine v1.0.0-beta.18 (the release v1.0.3 declares as its floor):

git init -q . && dagger init -y && dagger install dagger.io/sdk/[email protected]
dagger module init typescript --name m --path m --install -y
mkdir m/types
echo 'export type A = 1'     > m/types/types.gen.ts
echo 'export const keep = 1' > m/types/keepme.ts
git add -A && git commit -qm x        # tree clean
dagger generate -y                    # exit 0, "✔ 1 passed", prints nothing
$ git status --short
 D m/types/keepme.ts
 D m/types/types.gen.ts
$ ls m/types
ls: m/types: No such file or directory

Control, same tree with types.gen.ts removed and nothing else changed: dagger generate -y exits 0, reports no changes to apply, and m/types/keepme.ts is still there. One filename apart — this is the SDK, not the engine.

--no-apply renders the loss as m/types/ -2 and never names the user's file.

It contradicts this SDK's own documented rule. prunedClientPackages' docstring says "leaving the user's own files untouched", and README.md says "Only what the SDK generates is removed … A file of your own … stays, and so does the directory holding it".

Root cause

typescript-sdk.dang:2515, prunedClientPackages — the migration that removes the client packages an earlier layout wrote directly at a scope root:

.filter { name => existingDir(ws, scopeFile(scope, name)).exists(name + ".gen.ts") }
.reduce(ws) { acc, name => acc.withoutDirectory("/" + scopeFile(scope, name)) }

It recognizes a stale package by that one marker and then takes the directory whole. It was the last surviving wholesale withoutDirectory over a path whose contents are not all SDK-owned: #99 made every sibling prune ownership-based — prunedOwnedFiles, prunedFiles, prunedEmbeddedSdk, withoutLegacyWorkspaceCore — and this one sat outside that work's scope.

It is reached on every run that generates a scope: unconditionally from the module path (moduleFiles, typescript-sdk.dang:1842, the entrypoint layout that is the default since #73) and from both branches of the client-only path (clientScope, :2411 and :2418).

Fix

Route the removal through the ownership rule the siblings use: prunedFiles with the set ownedFiles claims. The package's generated files go, and the directory follows only once nothing is left in it — so a file of the user's beside a types.gen.ts keeps the directory open, and a stale package that holds nothing but generated files still disappears whole.

Ownership is read from a synthetic parent (directory.withDirectory(name, …)) because ownedFiles knows a package's manifest as <name>/package.json — the depth the layout actually gave it. Read at the package's own root, a package.json would look like a file of the user's and the migrated package would linger as a husk, which is the half of the intent that has to survive the fix.

Behaviour for an all-generated legacy package (<name>/<name>.gen.ts + <name>/package.json, what clientPackageJSON wrote) is unchanged: the directory goes.

Verification

Engine v1.0.0-beta.18 throughout, via the e2e harness (.dagger/modules/e2e) on a scratch copy of the branch.

Two new checks, both asserting the resulting tree rather than just the changeset, and both covering the two halves — the user's directory survives, an all-generated package still goes whole:

  • generate:generate-keeps-user-files-in-scope-package-check — a module scope on the entrypoint path (moduleFiles call site).
  • client:generate-client-keeps-user-files-in-scope-package-check — a client-only scope (clientScope call site).

Red without the fix. With typescript-sdk.dang reverted to main and the checks kept, both fail:

== CHECKS ==  ✘ 2 failed
✘ client/generate-client-keeps-user-files-in-scope-package-check  ERROR
✘ generate/generate-keeps-user-files-in-scope-package-check       ERROR
  ! a user's file beside a types.gen.ts must survive regeneration, kept [...]

With the fix, client:* plus the generate checks that exercise the same path:

== CHECKS ==  ✔ 18 passed

covering client:remove-clients-check, client:remove-clients-keeps-user-files-check, client:generate-client-respects-existing-check (the one asserting the legacy .dagger/clients migration), client:generate-client-on-synthetic-workspace-check, generate:generate-check, generate:generate-idempotent-check and generate:generate-every-scope-check.

The repro above was also re-run on the published v1.0.3 to confirm the user-visible symptom and its control, as quoted in Problem.

Not covered

  • withoutLegacyScopeClients (typescript-sdk.dang:2430) is left wholesale, deliberately. It removes <scope>/.dagger/clients, a directory this SDK wrote in full: that layout's package was "self-contained: its own package.json and tsconfig.json, generated" (design/module-max.md), so unlike types/types.gen.ts nothing a third-party codegen writes can land there by accident, and the path is inside the engine's own dot-directory. Making it ownership-based would also need a new rule in ownedFiles — that flat layout's package.json and tsconfig.json sit beside the *.gen.ts files rather than one level down, so no existing rule claims them — which would widen the blast radius to every other prune for a case with no accidental collision. Residual exposure: a file a user dropped into <scope>/.dagger/clients goes with the migration. client:generate-client-respects-existing-check asserts that directory is removed whole.
  • The modules argument of prunedClientPackages is [] at all three call sites; left as is.
  • Full suite not run here — the two groups that exercise this path were, plus the new checks.

`dagger generate` deleted a user's directory. Any direct child of a
TypeScript scope that holds a file named after it — types/types.gen.ts,
api/api.gen.ts, graphql/graphql.gen.ts, the layout an openapi or graphql
codegen writes — was removed whole, every file of the user's in it, exit 0
and nothing printed.

prunedClientPackages migrates the layout that wrote each client as a
package at the scope root, and it recognized one by that marker alone and
took the directory with withoutDirectory. It was the last wholesale
removal over a path whose contents are not all this SDK's: #99 made every
sibling prune ownership-based — prunedOwnedFiles, prunedFiles,
prunedEmbeddedSdk, withoutLegacyWorkspaceCore — and this one was outside
that scope. Its own docstring and the README already claimed the rule it
did not follow.

Route it through prunedFiles with the set ownedFiles claims, the same rule
the siblings use: the generated files go, and the directory follows only
once nothing is left in it. Ownership is read from a synthetic parent
because ownedFiles knows a package's manifest as <name>/package.json, the
depth the layout gave it — so an all-generated package still goes whole,
manifest included, rather than lingering as a husk.

Both call sites are covered by a check that fails without the fix: a
module scope on the entrypoint path, and a client-only scope. Each asserts
the resulting tree, and each asserts the other half too, so the intent
survives.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv merged commit b3c5022 into main Oct 19, 2026
5 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.

1 participant