Skip to content

Allow a scope to record a client for its own module - #14188

Merged
eunomie merged 1 commit into
manifest-v2from
fix/sdk-scope-self-client
Sep 17, 2026
Merged

eunomie merged 1 commit into
manifest-v2from
fix/sdk-scope-self-client

Conversation

@TomChv

@TomChv TomChv commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Targets manifest-v2.

Problem

dagger module client add . from inside a module scope fails:

! local SDK generation cycle: .dagger/modules/test -> .dagger/modules/test

sdkModuleGraphDependencies turns every local client target into a generation-dependency edge, including a scope naming its own module — and orderSDKModuleGraph reads the resulting self-loop as a cycle, so the target can never be recorded.

Fix

Skip the edge when the resolved target is the scope itself. A self client is a target like any other for the SDK — the scope's own generation pass renders it — not a generation dependency. This unblocks the unified-clients flow in dagger/typescript-sdk#58, where client add . is how a module opts into calling itself through its generated client.

Real cycles between distinct scopes are still detected: the existing a -> b -> a case in TestValidateSDKModuleGenerationGraph is unchanged, and the test now also covers the self target in both spellings a recorded target can carry ("." and "./<path>").

Everything else on the add . path already handles the self target: resolveWorkspaceClientModuleInput records it as an explicit config-relative path, and the resolved ModuleSource is handed to generateScope like any other client.

Verified

  • go test ./core/schema/ -run 'TestValidateSDKModuleGenerationGraph|TestPlanSDKModuleScopes' passes (run under linux; the package does not build on darwin).
  • E2E against a dev engine build of this branch: dagger module client add . from inside an entrypoint module records clients = ["./.dagger/modules/test"], generation stays byte-quiet, and the module's self-call runs through its generated client.

`dagger module client add .` from inside a module failed with
"local SDK generation cycle: <scope> -> <scope>": the generation graph
turned every local client target into a dependency edge, including a
scope naming its own module, and the topological sort read the self-loop
as a cycle.

A self client is a target like any other for the SDK — the scope's own
generation pass renders it — not a generation dependency, so the edge is
skipped. Real cycles between distinct scopes are still detected, in both
spellings a recorded target can carry ("." and "./<path>").

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Sep 17, 2026
…erywhere

The spec's final form: every scope that has clients carries a clients/
directory of self-contained npm packages — one per client, the scope's own
@dagger.io/dagger package beside them — and a module scope and a client-only
scope generate the identical shape.

- A fresh module gets clients/dagger (installed by default: package.json is the
  resolver, the tsconfig carries no aliases) and clients/<self> (generated by
  default, wired only when the user opts in — npm install, or `dagger module
  client add .`, which records the self client and installs it by default).
  Declared clients land beside them, wired as file:./clients/<name>.
- A client-only scope gets the same clients/ tree, its package.json wired to
  the core and each client.
- codegen grows --packaged-clients: each client nests in its own package
  directory and the loader imports it relatively, so an uninstalled self client
  still resolves at dispatch time. The loader returns to clients/, beside the
  packages.
- The recipe installs from the manifest plus clients/** — the file: targets
  ride with the manifest, npm's relative symlinks resolve under the workspace
  mount, and the CoreDir fold is gone with the workspace-level .dagger/core it
  served (old copies pruned on regeneration, as are root-level packages and the
  embedded sdk/ of earlier layouts).
- config-updater: shared-deps learns keep-only pairs (?name=rel) and a "-" core
  for an emptied scope, treats npm's file:x spelling as equivalent to file:./x
  instead of fighting it, and the second generation pass re-runs the config
  against the user's original files so a keep-only dep survives the first
  pass's ignorance of the self client's name.

Verified end to end on the manifest-v2 engine: init, self-call via npm install,
client add . (against dagger/dagger#14188), a client-only scope consumed with
dagger run npx tsx, byte-quiet regeneration throughout, and the Go suites and
goldens updated to the new recipe shape.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Sep 17, 2026
A fresh module carries clients/dagger and nothing else. Its own client exists
only once the user records it — `module client add .` — and since the ask is
explicit and the SDK owns the module's config, the dep is wired in package.json
too; the user runs npm install and calls themselves. `client rm` takes the
package and the dep back out.

This deletes the keep-only machinery the always-generated self needed: no
"?" pairs from the SDK, no self-name diffing between passes, and
moduleConfigDirectory wires every generated client the same way. A stale
package directory is now swept whole when its files go, rather than leaving an
emptied husk (the on-host directory an applied changeset cannot remove is an
engine limitation, not ours).

Verified on a fresh workspace against the dev engine carrying
dagger/dagger#14188: init has no self client and runs; add . generates
clients/test, wires the dep, self-call answers; rm prunes both; regeneration
is byte-quiet throughout.

Signed-off-by: Tom Chauveau <[email protected]>
@eunomie
eunomie merged commit 2fe71fc into manifest-v2 Sep 17, 2026
86 checks passed
@eunomie
eunomie deleted the fix/sdk-scope-self-client branch September 17, 2026 18:36
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Sep 17, 2026
…shape everywhere (#58)

* design: unified clients — scopes, option C, shared core

Four design notes captured while iterating on the unified-clients model, in
order:

- unified-clients-spec.md: the first pass (clientRoot), kept for the record.
- unified-clients-take2.md: a client is written where it is declared.
- unified-clients-option-c.md: the package manifest is the only link, with the
  drawbacks decomposed and the user journeys walked.
- unified-clients-shared-core.md: one vendored @dagger.io/dagger the whole
  workspace points at, at .dagger/core/<sdk>, until core is published.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: config-updater shared-deps mode wires a scope to the vendored core

The mechanism the unified-clients design hinges on: a scope's package.json is
the only link between a consumer and the clients it resolves. shared-deps
rewrites the @dagger.io/* namespace the SDK owns to exactly what the scope
resolves — the vendored shared core as @dagger.io/dagger, and one
@dagger.io/<name> per declared client, each a file: dependency on a path
relative to the package. Anything else under @dagger.io/* is pruned, so a client
dropped from the scope stops resolving; the user's own dependencies and keys are
left untouched.

Unit-tested; no engine needed. The Dang and entrypoint-recipe wiring that calls
it lands next.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: generate the shared vendored core at .dagger/core/typescript

First step of the shared-core relocation: an entrypoint module scope now writes
the one vendored @dagger.io/dagger — the shipped bundle plus the core bindings —
at .dagger/core/<sdk> beside dagger.toml, idempotently (its contents are a
function of the engine version alone, so writeChanged keeps regeneration
byte-quiet). Written alongside the module's embedded sdk/ for now, so nothing
that resolves @dagger.io/dagger changes yet; switching consumers onto it and
dropping the embedded copy is the next step.

Verified on a repro entrypoint module: generate writes .dagger/core/typescript,
regeneration reports no changes, and a call still runs.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: point entrypoint modules at the shared core, drop the embedded sdk/

An entrypoint module no longer carries its own sdk/ library. It resolves
@dagger.io/dagger from the one vendored core at .dagger/core/typescript:

- the generated container recipe folds the core from there into node_modules
  (new --core-dir on codegen dang-entrypoint; empty keeps a [runtime] module's
  embedded-sdk behavior, so the goldens and legacy path are untouched),
- the module's tsconfig aliases @dagger.io/dagger at the shared core (config-
  updater's tsconfig/deno-config gain a CORE_DIR arg, defaulting to ./sdk),
- the embedded sdk/ is pruned on the switch.

The vendored core is a real @dagger.io/dagger package now — it carries a
package.json with exports/main/types, without which a bare import into the
folded directory resolves to nothing (a self client importing BaseClient fails
at runtime otherwise). The module's own package.json declares no
@dagger.io/dagger dependency: the recipe folds the library and the tsconfig
aliases it, and a file: dep would only break the recipe's isolated install,
which cannot see a path outside the manifest it mounts. The file: link is a
consumer's concern, landing on a client-only scope next.

Verified on a repro entrypoint module against the manifest-v2 engine: generate
writes .dagger/core/typescript and a module with no sdk/, regeneration is
byte-quiet, and `call base-image-address` runs.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: make package.json the install link and the self client opt-in

Two corrections toward the spec, both verified on the repro:

- package.json is the link, not tsconfig. A shared-core module declares
  @dagger.io/dagger as a file: dependency and the generated recipe installs it:
  the install replicates the workspace layout (module manifest at its path, the
  vendored core at CoreDir) so the module's file: specifier resolves, then runs
  the detected manager. The manual node_modules fold and the tsconfig-as-resolver
  are gone; the tsconfig alias stays only for editor resolution before an install.

- The self client is opt-in. A module generates a client for itself only when
  its own path is among the recorded clients (`dagger module client add .`); a
  module that never calls itself carries no self client.

Still on the old per-module format (loose clients/<name>.gen.ts files, loader in
clients/) — per-module client packages at modules/<mod>/<name>/ and the
client-only scope are next.

Signed-off-by: Tom Chauveau <[email protected]>

* fix: client-only scope — packages in the scope, reusing the shared core

A client-only scope was writing <scope>/.dagger/clients/{dagger,<m>} — a
directory nested inside a scope whose name already meant "clients", and a
regenerated dagger/ core beside every client. Both are gone.

Now the scope directory IS the client directory: one @dagger.io/<module> package
per client, directly in it, each declaring @dagger.io/dagger as a file:
dependency on the one shared core at .dagger/core/typescript rather than a copy.
The scope's package.json is wired the same way (shared-deps), so `npm install`
in the scope resolves the clients and the shared core; the user's other files
in the scope are left untouched, and only stale generated package dirs are
pruned.

The shared core is now rendered client-facing (the superset a consumer sees)
from the module's own session-version schema, so a client package resolves every
core type it references and an old git dependency never drags core back a
version.

Verified: generate writes .dagger/clients/hello beside a shared core, npm
install + resolve works, regeneration is byte-quiet, and the module still runs.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: a module's clients are packages at modules/<mod>/<name>, loader at root

The last of the shared-core layout: a module's declared clients are no longer
loose clients/<name>.gen.ts files resolved by tsconfig alias. Each is an
installable package directly under the module — modules/<mod>/<name>/, named
@dagger.io/<name>, depending on the shared core by file: path — the same
packaging a client-only scope gets. The module's package.json names each as a
file: dependency, so `npm install` wires them; the recipe includes the client
packages in its install context so those file: specifiers resolve.

The entrypoint loader moves to the module root (a new --loader-import on codegen
entrypoint points the dispatcher at ./loader.gen.js), so there is no clients/
directory left. An old clients/ dir and the package of a client that has left
the module are pruned on the switch.

Verified end to end on a repro entrypoint module with a git client: generate
writes modules/hello-world/hello as a package beside loader.gen.ts with no
clients/ dir, package.json declares @dagger.io/hello and @dagger.io/dagger as
file: deps, the dispatcher imports ./loader.gen.js, `call` runs, and
regeneration is byte-quiet. The client-only scope and the fresh-module paths
still pass.

Signed-off-by: Tom Chauveau <[email protected]>

* fix: pass CORE_DIR to the template-seed config-updater calls

module init failed because the starter-template config still called
config-updater tsconfig/deno-config with the old CLIENTS_DIR-only signature,
which now reads that argument as CORE_DIR and errors on the missing CLIENTS_DIR.
The seed only lays down a placeholder config that generation overwrites, so it
passes an empty CORE_DIR (the ./sdk default); the real core path is written by
moduleConfigDirectory during generation.

Verified: `dagger module init typescript --entrypoint -n test` on a fresh
workspace creates a module depending only on @dagger.io/dagger (file: shared
core), with no clients/ or sdk/, the loader at the module root, and a call runs.

Signed-off-by: Tom Chauveau <[email protected]>

* fix: resolvable client packages, preserved user deps, no module tsconfig aliases

Three issues installing a client (generated in a client-only scope) into a
module for a self-call:

- The IDE could not resolve @dagger.io/<name>: a client package carried only
  `exports`, which classic `node` moduleResolution ignores. It now carries
  `main`/`types` beside `exports`, like the core package, so a bare import
  resolves under the module's tsconfig.

- `dagger generate` stripped the dep. shared-deps pruned every @dagger.io/*
  dependency not in the module's own recorded clients — clobbering a client the
  user installed from another scope. It now prunes only the SDK's own generated
  clients (the `file:./<name>` deps); a dep the user added pointing elsewhere
  (a `file:../` path or a registry) is left alone.

- The module's tsconfig still carried @dagger.io/dagger path aliases. With a
  shared core, node/bun resolve every @dagger.io/* package through node_modules
  after an install, so the tsconfig now carries none — a new "-" CORE_DIR
  sentinel writes no lib alias and removes one an earlier layout left. Deno keeps
  its import-map entry, since that is its resolver.

Verified end to end: a client-only scope generates a `test` client, `npm install`
of it into the module resolves under `tsc --moduleResolution node` for both
@dagger.io/test and @dagger.io/dagger with no path aliases, `dagger generate`
keeps the dep and empties the tsconfig paths, and a call runs.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: one clients/ directory per scope, the same installable shape everywhere

The spec's final form: every scope that has clients carries a clients/
directory of self-contained npm packages — one per client, the scope's own
@dagger.io/dagger package beside them — and a module scope and a client-only
scope generate the identical shape.

- A fresh module gets clients/dagger (installed by default: package.json is the
  resolver, the tsconfig carries no aliases) and clients/<self> (generated by
  default, wired only when the user opts in — npm install, or `dagger module
  client add .`, which records the self client and installs it by default).
  Declared clients land beside them, wired as file:./clients/<name>.
- A client-only scope gets the same clients/ tree, its package.json wired to
  the core and each client.
- codegen grows --packaged-clients: each client nests in its own package
  directory and the loader imports it relatively, so an uninstalled self client
  still resolves at dispatch time. The loader returns to clients/, beside the
  packages.
- The recipe installs from the manifest plus clients/** — the file: targets
  ride with the manifest, npm's relative symlinks resolve under the workspace
  mount, and the CoreDir fold is gone with the workspace-level .dagger/core it
  served (old copies pruned on regeneration, as are root-level packages and the
  embedded sdk/ of earlier layouts).
- config-updater: shared-deps learns keep-only pairs (?name=rel) and a "-" core
  for an emptied scope, treats npm's file:x spelling as equivalent to file:./x
  instead of fighting it, and the second generation pass re-runs the config
  against the user's original files so a keep-only dep survives the first
  pass's ignorance of the self client's name.

Verified end to end on the manifest-v2 engine: init, self-call via npm install,
client add . (against dagger/dagger#14188), a client-only scope consumed with
dagger run npx tsx, byte-quiet regeneration throughout, and the Go suites and
goldens updated to the new recipe shape.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: the self client is opt-in via `dagger module client add .`

A fresh module carries clients/dagger and nothing else. Its own client exists
only once the user records it — `module client add .` — and since the ask is
explicit and the SDK owns the module's config, the dep is wired in package.json
too; the user runs npm install and calls themselves. `client rm` takes the
package and the dep back out.

This deletes the keep-only machinery the always-generated self needed: no
"?" pairs from the SDK, no self-name diffing between passes, and
moduleConfigDirectory wires every generated client the same way. A stale
package directory is now swept whole when its files go, rather than leaving an
emptied husk (the on-host directory an applied changeset cannot remove is an
engine limitation, not ours).

Verified on a fresh workspace against the dev engine carrying
dagger/dagger#14188: init has no self client and runs; add . generates
clients/test, wires the dep, self-call answers; rm prunes both; regeneration
is byte-quiet throughout.

Signed-off-by: Tom Chauveau <[email protected]>

* fix: point the client e2e checks at the per-scope layout

The client-scope checks asserted the retired <scope>/.dagger/clients layout, so
checks-check went red. They now assert the per-scope shape: packages under
<scope>/clients with the scope's own dagger/ beside them, the scope package.json
carrying the SDK's file: deps while the user's keys survive, a stale client file
in clients/ pruned, and the .dagger/clients directory an earlier layout kept
taken with the move — clientScope now prunes that leftover, and the existing
fixture keeps one to prove it.

The opt-in self client also reached further than intended: the embedded
[runtime] layout lost its always-generated self client, which
generate-self-call-check and generate-source-edit-is-scoped-check assert. The
opt-in now applies to the entrypoint layout only; the embedded path keeps the
behavior it shipped with.

The full e2e suite passes locally against the manifest-v2 engine.

Signed-off-by: Tom Chauveau <[email protected]>

---------

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Sep 17, 2026
…erywhere

Every scope that has clients carries a clients/ directory of self-contained npm
packages — one per client, the scope's own @dagger.io/dagger package beside
them — and a module scope and a client-only scope generate the identical shape.
The package manifest is the only link: package.json file: dependencies, written
by the SDK for what it generates and by npm install for what the user opts into.
No tsconfig aliases, no per-scope .dagger/clients, no embedded sdk/ for
entrypoint modules. The design notes under design/ record how the model got
here; design/unified-clients-per-scope.md is the final form.

- A fresh entrypoint module gets clients/dagger — installed by default, the one
  @dagger.io/* dep it carries — and nothing else. Its tsconfig is
  experimentalDecorators only: package.json is the resolver.
- The self client is opt-in: `dagger module client add .` records it (as the
  resolved path), generates clients/<self>/ and wires the dep; the user runs
  npm install and calls themselves. `client rm` takes the package and the dep
  back out. Needs dagger/dagger#14188, which stops the generation graph from
  reading a scope's own module as a self-cycle. The embedded [runtime] layout
  keeps its always-generated self client unchanged.
- A declared client lands at clients/<name>/, wired as file:./clients/<name>.
  Each package carries main/types/exports and depends on file:../dagger and its
  siblings, so installing one from anywhere pulls everything it references.
- codegen grows --packaged-clients: clients nest in their own package
  directories and the loader imports them relatively, so an uninstalled self
  client still resolves at dispatch time; the loader lives at
  clients/loader.gen.ts beside them.
- The entrypoint recipe installs from the module manifest plus clients/** — the
  file: targets ride with the manifest, so a source edit reinstalls nothing,
  and npm's relative symlinks resolve identically under the runtime's workspace
  mount. requireGenerated checks the packaged files.
- config-updater: shared-deps writes a scope's @dagger.io/* namespace as file:
  deps and prunes only its own (a dep the user pointed elsewhere is theirs);
  npm's file:x spelling is treated as equivalent to file:./x rather than
  fought; "-" empties a scope that lost its last client; tsconfig learns a
  no-alias sentinel. The second generation pass re-runs the config against the
  user's original files, not the first pass's output.
- Migration pruning removes what earlier layouts left: a workspace-level
  .dagger/core, a scope-level .dagger/clients, packages at the scope root, a
  root-level loader, and the embedded sdk/.

Verified end to end on the manifest-v2 engine: init, client add ., self-call,
client rm, a client-only scope consumed with dagger run npx tsx, byte-quiet
regeneration throughout, and the full e2e suite.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Sep 17, 2026
…ectory per scope (#54)

* feat: render every module as its own self-contained client

A TypeScript module's dependencies were grafted onto one core Client: each
per-module file declaration-merged into client.gen.ts, patched Client.prototype
at load time, and the core file re-exported them all. One Client, one namespace,
every module folded in — which is what let two modules collide and what forced
the ESM-cycle workarounds.

Now each module renders as its own self-contained client: its own root Client
built from the fields it contributes to Query, its own dag, a top-level function
per root field, and its own serve. A module's source and a caller outside it
write the same thing —

    import { dag, Container } from "@dagger.io/dagger"  // core
    import { hey } from "@dagger.io/hey"                // a bound module

- _client.ts.gtpl replaces _dep.ts.gtpl + _augmentations.ts.gtpl; the
  declaration-merge / prototype-patch machinery is deleted and the core file
  holds core types only. The library (sdk/) splits from the clients, which live
  beside it, so it can one day be an npm package rather than generated.
- Serve-on-use per client, the only serve: Context carries an optional serve
  spec propagated to every derived context, the first query awaits it, memoized
  per session on Connection, run through the core dag so it cannot recurse. A
  git target serves through dag.moduleSource(ref, { refPin }), a local one
  through a currentWorkspace query — so a client resolves wherever it is used,
  and the dispatcher serves nothing (which also fixes self-calls).
- A module's own API folds into its bindings as a self client, generated in a
  second pass against the staged module's schema.
- clients/loader.gen.ts replaces the entrypoint's namespace lookup: an explicit
  type-name -> class map baked from the schema split.
- config-updater writes one @dagger.io/<module> alias per client into
  tsconfig.json / deno.json, synced; dualClients is removed.
- A standalone client scope converges with a module, packaged for npm install:
  the vendored library as a real @dagger.io/dagger package plus one
  @dagger.io/<module> package per target, wired by file: dependencies.
- Two codegen correctness fixes surfaced by running it: an object type passed
  only as an argument is now imported, and generated optional properties are
  exactOptionalPropertyTypes-clean. library/bundle is rebuilt via the packager.

Breaking: import { Hi } from "@dagger.io/dagger" stops resolving — a module's
types come from @dagger.io/<module>; core imports are unchanged.

Signed-off-by: Tom Chauveau <[email protected]>

* feat: one clients/ directory per scope, the same installable shape everywhere

Every scope that has clients carries a clients/ directory of self-contained npm
packages — one per client, the scope's own @dagger.io/dagger package beside
them — and a module scope and a client-only scope generate the identical shape.
The package manifest is the only link: package.json file: dependencies, written
by the SDK for what it generates and by npm install for what the user opts into.
No tsconfig aliases, no per-scope .dagger/clients, no embedded sdk/ for
entrypoint modules. The design notes under design/ record how the model got
here; design/unified-clients-per-scope.md is the final form.

- A fresh entrypoint module gets clients/dagger — installed by default, the one
  @dagger.io/* dep it carries — and nothing else. Its tsconfig is
  experimentalDecorators only: package.json is the resolver.
- The self client is opt-in: `dagger module client add .` records it (as the
  resolved path), generates clients/<self>/ and wires the dep; the user runs
  npm install and calls themselves. `client rm` takes the package and the dep
  back out. Needs dagger/dagger#14188, which stops the generation graph from
  reading a scope's own module as a self-cycle. The embedded [runtime] layout
  keeps its always-generated self client unchanged.
- A declared client lands at clients/<name>/, wired as file:./clients/<name>.
  Each package carries main/types/exports and depends on file:../dagger and its
  siblings, so installing one from anywhere pulls everything it references.
- codegen grows --packaged-clients: clients nest in their own package
  directories and the loader imports them relatively, so an uninstalled self
  client still resolves at dispatch time; the loader lives at
  clients/loader.gen.ts beside them.
- The entrypoint recipe installs from the module manifest plus clients/** — the
  file: targets ride with the manifest, so a source edit reinstalls nothing,
  and npm's relative symlinks resolve identically under the runtime's workspace
  mount. requireGenerated checks the packaged files.
- config-updater: shared-deps writes a scope's @dagger.io/* namespace as file:
  deps and prunes only its own (a dep the user pointed elsewhere is theirs);
  npm's file:x spelling is treated as equivalent to file:./x rather than
  fought; "-" empties a scope that lost its last client; tsconfig learns a
  no-alias sentinel. The second generation pass re-runs the config against the
  user's original files, not the first pass's output.
- Migration pruning removes what earlier layouts left: a workspace-level
  .dagger/core, a scope-level .dagger/clients, packages at the scope root, a
  root-level loader, and the embedded sdk/.

Verified end to end on the manifest-v2 engine: init, client add ., self-call,
client rm, a client-only scope consumed with dagger run npx tsx, byte-quiet
regeneration throughout, and the full e2e suite.

Signed-off-by: Tom Chauveau <[email protected]>

---------

Signed-off-by: Tom Chauveau <[email protected]>
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.

2 participants