Skip to content

feat: restore pre-1.0 dag.<module>() through a generated compat shim - #78

Merged
TomChv merged 2 commits into
mainfrom
feat/dag-dep-compat
Oct 9, 2026
Merged

TomChv merged 2 commits into
mainfrom
feat/dag-dep-compat

Conversation

@TomChv

@TomChv TomChv commented Oct 8, 2026

Copy link
Copy Markdown
Member

Problem

#26 [TypeScript SDK] v0.21.x dag.<dep>() calls fail only at runtime — case G12 (re-run).
Migrated code that calls a dependency through dag fails with “not a function”. Neither generate nor check catches it.

From the re-run write-up: "In 1.0 the dependency bindings live in sdk/.gen.ts (export function legacyTypescriptDep(), export function alpine(opts)), not on dag. This is an accepted change, but nothing tells the migrating user about it: migration doesn't rewrite the code or warn, and generate/check pass. The 0.21 form still fails after the engineVersion bump." Case C6 saw the same on a fresh 1.0 module: "dag.b() from @dagger.io/dagger compiles to a runtime error dag.b is not a function".

SDK Main-Engine Test Run, 2026-10-07 — issue #26, case G12.

Root cause

Pre-1.0 codegen merged every dependency's root fields into the one core Client, so a module wrote dag.myDep().hello(). Unified clients render each module as its own file with its own Client, its own dag and its own top-level functions, and the core Client in client.gen.ts carries no <module>().

tsx and bun strip types without checking them, so a migrated module compiles, generate and check pass, and the call fails only at dispatch — which is exactly what the report describes.

Fix

A backward-compatibility layer that puts each module's root fields back on the core dag, marked @deprecated.

One shim per module scope — sdk/compat.gen.ts in the flat ([runtime]) layout, clients/compat.gen.ts in the packaged ([entrypoint]) one — rendered whenever at least one module client is, which covers dependencies, recorded targets and the self client alike. For each module client it:

  • imports that client's dag under an alias, plus (type-only) whatever its signatures name;
  • installs <field>() on the core Client.prototype per root field, via Object.defineProperty with enumerable: false, delegating to the client's own dag.<field>(...) — so the client's serve-on-use hook (__serveModule) still runs and the module is served before its first query;
  • declares the type side so editors and tsc see it, one interface Client member per field with an @deprecated note naming the 1.0 import.
import { Client } from "@dagger.io/dagger"
import { dag as __dagDep } from "@dagger.io/dep"
import type { Dep } from "@dagger.io/dep"

declare module "@dagger.io/dagger" {
  interface Client {
    /**
     * @deprecated Reach dep through its own client instead: `import { dep } from "@dagger.io/dep"`.
     */
    dep(): Dep
  }
}
// …__install("dep", …) patching Client.prototype

What the augmentation targets, and why that was in question. The brief allowed for augmenting @dagger.io/dagger to fail, since its Client is re-exported through export * from index.ts rather than declared there, with a fallback of augmenting the declaring module (./client.gen.js). It does not fail: a real tsc run against the module's own tsconfig (moduleResolution: "Node", @dagger.io/dagger → ./sdk/index.ts) merges the interface through the re-export, enforces the declared signature (dag.dep({ nope: 1 }) is rejected as TS2353 against ClientDepOpts), and exposes the @deprecated tag to the language service — checked directly through the TS API, where getJsDocTags() returns ["deprecated"], which is what gives the editor strike-through. So the shim augments @dagger.io/dagger in both layouts: one specifier, and the class it patches is the same one the user's own source imports.

Why a separate file. The core file must not import or mention a module client — core → client is the direction the design forbids (ESM cycle, and sdk/ has to stay a publishable core-only library). So the loader imports the shim instead, keeping the direction entrypoint → loader → compat → clients; the shim's header comment says so.

The shim enumerates module clients directly rather than through the loader's class map, which only covers modules owning an object type — so a dependency contributing nothing but a scalar root field is restored too. There is a golden test for exactly that module.

The entrypoint now imports the loader for its side effect as well as by name. The named import is not always used: a module with no object-typed argument, field or return never calls __loadCoreObject, and a type-stripping loader drops an unused named import outright — which would silently take the loader, the shim and every restored method with it. That is the repro's own shape, so without this the fix would not have worked at all.

Skipped root fields, both still reachable through the module's own client:

  • a name that is a TS keyword — it cannot be a member, the same rule _client.ts.gtpl already applies to its top-level functions;
  • a name a core Query field already has — the shim would otherwise replace a core method on the prototype everything shares.

typescript-sdk.dang learns the new file is not a module client: bindingAliasModules excludes it, so no @dagger.io/compat alias is written, and compat joins loader as a reserved module name. packagedClientNames already globs one level down and so leaves it out. Pruning needs no change — it is a *.gen.ts in the regenerated set, so a scope that loses its last client loses the shim with it, which is what generate-compat-shim-pruned-check asserts.

Verification

Engine v1.0.0-beta.17 / 55e112468d throughout.

Go tests — go test ./... in helpers/codegen (and helpers/config-updater): green. New golden tests pin the shim in both layouts (compat_flat_want.ts, compat_packaged_want.ts) plus the skips, the no-client cases, the reserved compat module name, and the side-effect loader import. The three entrypoint goldens gained exactly one line each (that import).

Checks run (dagger --x-release=55e112468d check … in a scratch workspace on the engine-e2e workspace.toml) — all 7 green, command exited 0:

check result
e-2-e:generate:generate-compat-shim-check ✔
e-2-e:generate:generate-compat-shim-pruned-check ✔
e-2-e:generate:generate-compat-shim-type-checks-check ✔
e-2-e:generate:generate-dependency-bindings-check ✔
e-2-e:generate:generate-prunes-stale-bindings-check ✔
runtimes:dag-dependency-runs-check ✔
runtimes:node-runs-check ✔

Full suite run by the coordinator against this branch. Baseline on unmodified main is 67/68, the one red being packager/library-bindings/stale — expected on an engine newer than the committed library bindings, which are untouched here.

The new check discriminates. On a scratch copy with the shim disabled, runtimes:dag-dependency-runs-check goes red with the reported error:

✘ asModule getModDef
  ┃ /src/.dagger/modules/runtimes/fixtures/dag-dep/src/index.ts:9
  ┃ void dag.runtimeDep()
  ┃          ^
  ┃ TypeError: dag.runtimeDep is not a function
! failed to call module "runtime-dag-dep" to get functions: call constructor: exit code: 1

A first draft of the tsc check narrowed include to the probe file and failed the same way a user would — the augmenting file was not in the program. It now runs the module's own tsconfig unnarrowed, which is what a user gets, and asserts both directions.

Manual repro, two --legacy-runtime modules in a throwaway workspace, app holding a recorded client for dep and calling it the 0.21 way (return await dag.dep().hello()).

Before — generate is clean, the call is not:

$ dagger -y generate
== GENERATORS ==  ✔ 1 passed
✔ dag://?generator=typescript-sdk/generate 54.5s OK

$ dagger call app via-dag
== TRACE ==  ✘ FAILED
! exit code 1
! dag.dep is not a function
✘ .viaDag: String! 1.4s ERROR

After, with no change to the module's source:

$ dagger -y generate
✔ dag://?generator=typescript-sdk/generate 37.3s OK

$ ls .dagger/modules/app/sdk/
app.gen.ts  client.gen.ts  compat.gen.ts  core.d.ts  core.js  dep.gen.ts  index.ts  loader.gen.ts  telemetry.ts

$ dagger call app via-dag
✔ .viaDag: String! 4.6s
hello from dep

And the type side, with the module's own generated tsconfig:

$ npx tsc --noEmit --skipLibCheck -p tsconfig.json   # shim present
TSC_EXIT=0

$ mv sdk/compat.gen.ts /tmp/ && npx tsc --noEmit --skipLibCheck -p tsconfig.json
src/index.ts(9,22): error TS2339: Property 'dep' does not exist on type 'Client'.
TSC_EXIT=2

(--skipLibCheck because the vendored bundle's core.d.ts references graphql-request and @opentelemetry/api, which the module itself does not declare — pre-existing and unrelated; the e2e fixture's tsconfig sets it already.)

Not covered / follow-ups

  • Standalone client scopes are out of scope, as agreed: they have no loader, so nothing would evaluate a shim. TestGenerateModule_CompatShimOnlyWithClients pins that none is emitted there rather than leaving a dead file behind.
  • Keyword-named root fields get no restored method — a keyword cannot be a member name. Unchanged from _client.ts.gtpl, which skips them for its top-level functions for the same reason; the field stays reachable through the module client's own dag.
  • A root field colliding with a core Query field is skipped rather than shadowing core. Worth flagging that this is silent: the pre-1.0 merge would have let the module win, so such a module changes behaviour either way. Surfacing it as a generation warning needs somewhere for an SDK to put one.
  • The packaged layout's introspector staging does not include the shim (moduleTypedefJSON stages only the */*.gen.ts package files, flattened). It makes no difference to any typedef that declares its return type, which the templates and the scan's own requirements make the norm, and the flat layout stages the whole sdk/ so the augmentation resolves during the scan there. Staging it under the packaged layout would need its relative client paths rewritten to match the flattening, so it is left alone.
  • library/src/module/introspector/typescript_module/ast.ts skips client and loader when synthesizing @dagger.io/<name> aliases for the scan, and says it mirrors bindingAliasModules. It now also wants compat. The effect is one unused path alias, so it is harmless, and changing it means rebuilding the committed library/bundle/ through the packager — its own change.

@grouville
grouville self-requested a review October 9, 2026 06:50
TomChv added 2 commits October 9, 2026 13:36
Pre-1.0 codegen merged every dependency's root fields into the one core
Client, so a module wrote `dag.myDep().hello()`. Unified clients render
each module as its own file with its own Client, its own `dag` and its own
top-level functions, and the core Client in client.gen.ts carries no
`<module>()`. tsx and bun strip types without checking them, so a migrated
module compiles, `generate` and `check` pass, and the call fails only at
dispatch with "dag.myDep is not a function".

Each module scope now also gets a compat shim — sdk/compat.gen.ts flat,
clients/compat.gen.ts packaged — rendered whenever at least one module
client is. For every root field of every client it installs a delegating
method on the core Client.prototype and declares the type side through a
`declare module "@dagger.io/dagger"` augmentation, each member carrying an
@deprecated note naming the 1.0 import.

The delegation goes through the client's own `dag`, not a raw query, so the
client's serve-on-use hook still runs and the module is served before the
first query reaches it. Object.defineProperty with enumerable: false keeps
the restored methods out of anything that walks a client's own members:
this is compatibility surface, not API.

A file of its own because core may not reference a module client — that
direction is an ESM cycle and would stop sdk/ being a publishable core-only
library — so the loader imports it instead, keeping the direction
entrypoint -> loader -> compat -> clients. The shim enumerates module
clients directly rather than through the loader's class map, which only
covers modules owning an object type; a dependency contributing nothing but
a scalar root field is restored too.

Two kinds of root field are skipped, both still reachable through the
module's own client: a name that is a TS keyword, which cannot be a member
(the same rule _client.ts.gtpl applies to its top-level functions), and a
name a core Query field already has, which would otherwise replace a core
method on the prototype everything shares.

The entrypoint now imports the loader for its side effect as well as by
name. The named import is not always used — a module with no object-typed
argument, field or return never calls __loadCoreObject — and a
type-stripping loader drops an unused named import outright, which would
take the loader, the shim and the restored methods with it.

typescript-sdk.dang learns that compat.gen.ts is not a module client:
bindingAliasModules excludes it so no @dagger.io/compat alias is written.
Pruning needs no change — it is a *.gen.ts in the regenerated set, so a
scope that loses its last client loses the shim with it.

Signed-off-by: Tom Chauveau <[email protected]>
The reported failure was invisible to every tool a user runs — generate and
check passed, and the module failed only when dispatched — so the coverage
has to reach both halves.

runtimes:dag-dependency-runs is the running half, and the runtimes module is
the only place a check loads a generated module for real. A new fixture pair
— a dependency, and a module reaching it as `dag.runtimeDep()` — is
generated in order and loaded. The fixture resolves the call at module scope
rather than only inside a function: a `dag.<dep>()` that is no longer a
function throws there while the entrypoint is still importing the user's
source, so the module never registers its object and the check goes red.
Confirmed against a scratch copy with the shim disabled, where it fails with
the reported "TypeError: dag.runtimeDep is not a function".

generate-compat-shim-type-checks is the type half, run through the module's
own tsconfig unnarrowed — which is what makes the declaration merge apply at
all, since an augmentation only takes effect when the augmenting file is in
the program and the generated tsconfig carries no `include`. Asserted in
both directions, because a passing compile says nothing on its own: the same
source is rejected with TS2339 once the shim is taken away. tsc prints
nothing for @deprecated; an editor shows the strike-through.

generate-compat-shim pins what the shim contains and, as much, what it does
not touch: the core bindings stay core-only. generate-compat-shim-pruned
covers both ends of when the file exists — an entrypoint scope with no
module client gets none, and one left behind is pruned by the same mechanism
that takes a departed dependency's bindings.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv force-pushed the feat/dag-dep-compat branch from 34f74b6 to 8dfad23 Compare October 9, 2026 11:41
@TomChv

TomChv commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

Tested and it works fine, however during my testing I noticed another issue with module source failing to load when source points to a directory, I'm fixing this as part of this PR since my agent already has a lot of context on that

@TomChv

TomChv commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

I changed my mind, I'll do the fix in a follow up PR since that compat is quite important imo

@TomChv
TomChv merged commit 3d741a5 into main Oct 9, 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.

2 participants