Skip to content

fix: an embedded module's clients ship inside sdk/ - #60

Merged
TomChv merged 2 commits into
mainfrom
fix/runtime-clients-in-sdk
Sep 18, 2026
Merged

TomChv merged 2 commits into
mainfrom
fix/runtime-clients-in-sdk

Conversation

@TomChv

@TomChv TomChv commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #54.

Problem

A module created without --entrypoint got the same clients/ directory as an entrypoint module — loose files reached by tsconfig alias. But sdk/ is the one directory the engine's builtin [runtime] runtime mounts into node_modules, and it cannot be told to mount anything else; the clients only resolved where the JS runtime happened to honour the alias mechanics. The packaged clients/ layout depends on the entrypoint path, whose generated recipe installs whatever the module declares — the embedded path has no such hook.

Fix

The layouts branch on the loading path:

  • --entrypoint — unchanged: clients/ of self-contained npm packages, wired through package.json.
  • no --entrypoint — every client file and the loader ship flat inside sdk/, beside client.gen.ts, so the runtime's single mount carries them by construction:
<module>/sdk/
  client.gen.ts  core.js  core.d.ts  index.ts  telemetry.ts   the library
  <dep>.gen.ts  <self>.gen.ts  loader.gen.ts                  the clients
  • codegen module --flat-clients emits that shape; --loader-import returns so the legacy __dagger.entrypoint.ts imports ./sdk/loader.gen.js.
  • tsconfig / deno aliases point into sdk/ (@dagger.io/<m> → ./sdk/<m>.gen.ts).
  • A clients/ directory an earlier generation left beside sdk/ is taken with the move on regeneration.

Fix 2: the self client survived init but not use

Loading a [runtime] module executes its TypeScript, and the self client only renders in the second generation pass. Staging the first pass used to strip every existing binding, so the moment a module's source actually imported its own client, generation destroyed it: the import dangled in the staged tree, the load failed and was rescued into an empty self set, the second pass never ran, and the stale-binding prune deleted sdk/<self>.gen.ts — the very file the source needs.

Now the staged tree keeps the previous generation's bindings (a stale unimported binding is inert at load time; its absence is what made the module unloadable), and the prune only runs when the module actually loaded — a failed load degrades to a partial regeneration instead of eating user-visible files. The by-path repair in mod.dang never runs a second pass, so it never prunes.

Verified

On a repro workspace carrying one module of each kind: regeneration migrates the embedded module's clients into sdk/ (rename-only diff plus the two pointer updates) and leaves the entrypoint module byte-quiet; both modules run — including a call through the migrated hello client on the builtin runtime — and regeneration is byte-quiet after the move. For the self-client fix: uncommenting a function that calls the module through its own client now regenerates cleanly (+7/+7, both the entrypoint and sdk/<self>.gen.ts gain the function), the call runs on the builtin runtime, and a further generate is byte-quiet. The full e2e suite passes, including a new regression check (generate-self-import-survives-check) plus the existing stale-prune check; codegen and config-updater suites green.

A [runtime] module's clients landed in clients/, loose files reached by
tsconfig alias — but sdk/ is the one directory the engine's builtin runtime
mounts into node_modules, and it cannot be told to mount anything else, so the
clients only resolved where the JS runtime happened to honour the alias. The
packaged clients/ layout needs the entrypoint path, whose generated recipe
installs whatever the module declares; the embedded path gets no such hook.

The layouts now branch on the loading path:

- entrypoint: unchanged — clients/ of self-contained npm packages, wired
  through package.json.
- embedded [runtime]: every client file and the loader ship flat inside sdk/,
  beside client.gen.ts. `codegen module` grows --flat-clients for that shape,
  the legacy __dagger.entrypoint.ts imports ./sdk/loader.gen.js (the
  --loader-import knob returns for it), the tsconfig aliases point into sdk/,
  and a clients/ directory an earlier generation left is taken with the move.

Verified on a repro carrying one module of each kind: regeneration migrates the
embedded module's clients into sdk/ and leaves the entrypoint module byte-quiet,
both modules run — including a call through the migrated hello client — and
regeneration is byte-quiet after the move. The full e2e suite passes with the
embedded checks pointed at the new location.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv force-pushed the fix/runtime-clients-in-sdk branch from f31df5d to 5d8b7fe Compare September 17, 2026 23:41
Loading a [runtime] module executes its TypeScript, and the self client
only renders in the second pass. Staging the first pass used to strip
every existing binding, so a module whose source imports its own client
could not load: the import dangled, the load was rescued into an empty
self set, the second pass never ran, and the prune then deleted the very
file the source needs. Uncommenting a self-call erased sdk/<self>.gen.ts
on the next generate.

Keep the previous generation's bindings in the staged tree — a stale
unimported binding is inert at load time, while its absence is what made
the module unloadable. And only prune stale bindings when the module
actually loaded (an empty self set on an embedded module means it did
not), so a failed load degrades to a partial regeneration instead of
eating user-visible files. The by-path repair in mod.dang never runs a
second pass, so it never prunes.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv merged commit 249ba5a into main Sep 18, 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