Skip to content

fix: refuse a module type named after a core type - #111

Merged
TomChv merged 1 commit into
mainfrom
fix/union-alias-shadowing-core-type
Oct 21, 2026
Merged

TomChv merged 1 commit into
mainfrom
fix/union-alias-shadowing-core-type

Conversation

@TomChv

@TomChv TomChv commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Problem

A type a module declares under a core type's name is silently taken to be that core type. No issue for this; the repro is below.

import { object, func } from "@dagger.io/dagger"

export type Volume = "loud" | "quiet"

@object()
export class Unionapp {
  @func()
  shout(volume: Volume): string { return volume }
}

dagger generate -y exits 0 (== GENERATORS == ✔ 1 passed) and the recipe emits:

.withArg("volume", typeDef.withObject("Volume"), sourceMap: sourceMap("src/index.ts", 8, 9)),

So the module takes a core Volume, which the author never mentioned, and the call fails on it:

$ dagger call unionapp shout --volume loud
✔ Workspace.resolve(value: "loud"): Address! 0.0s
✘ .volume: Volume! 0.0s ERROR
! unsupported volume address "loud": must use sshfs:// or engine-volume://

Passing something the core type does accept makes it unmistakable — the function is handed a core Volume object:

$ dagger call unionapp shout --volume engine-volume://loud
✘ .shout(volume: Address.volume: Volume!): String! ERROR
! convert return value: cannot create String from map[string]interface {}

Controls on unmodified main:

  • Rename the alias to Loudness and fix: refuse a union type alias instead of registering it as a scalar #105's union check refuses it correctly: Unsupported union type alias 'Loudness' in src/index.ts. Declare an enum .... The name, not the shape, decided the outcome.
  • Drop the export. Identical recipe ("no changes to apply"), identical failures.
  • @object class Volume instead of an alias: generates fine, and the object is unqueryable — Cannot query field "level" on type "Volume".

The same collision happens in the other SDKs, with a non-colliding control in the same module:

SDK non-colliding type type named after a core type
Go ✔ .note: GoappNote! → fine Cannot query field "level" on type "Volume"
Python ✔ .note: PyappNote! → fine Cannot query field "peek" on type "Volume"
TypeScript ✔ .loudness.level → loud Cannot query field "level" on type "Volume"

Namespacing exists so that two modules declaring the same type name do not collide with each other. It was never meant to isolate a module from core, and all three SDKs agree, so this is not an engine defect to work around — it is a declaration the SDK should refuse.

Root cause

library/src/module/introspector/dagger_module/module.ts: resolveReferences looks for a ClassDeclaration by name first (:158), finds Volume in the module's own generated client.gen.ts, and because that file is a generated client registers ObjectKind and continues (:163-169). #105's union check lives in the type-alias branch (:337), which a colliding name never reaches. A declared @object class Volume has no reference to resolve at all: it is registered under its own name and the collision only shows up in the served schema.

Fix

DaggerModule.validateNoCoreTypeShadowing (library/src/module/introspector/dagger_module/module.ts:392) refuses the declaration, before anything is resolved:

type alias Volume at src/index.ts:2:0 shadows the core Dagger type Volume. A module's
types are namespaced against other modules but not against core, so the core type
answers for that name wherever it is used. Rename the declaration.
class Volume at src/index.ts:10:0 shadows the core Dagger type Volume. ...

Decisions worth calling out:

  • What counts as a declaration. One rule, no carve-out: a class, enum, interface or type alias the module declares, decorated or not and used by the module's API or not. An undecorated class Volume is the same silent failure one @func argument later, and a rule that holds for a declaration rather than for a use does not depend on which of two declarations the resolver finds first.
  • What counts as a core type. The classes and enums declared in client.gen.ts (AST.coreTypeNames, typescript_module/ast.ts:136). Scoped to that one file deliberately: a name shared with a dependency's client (<dep>.gen.ts) or with the module's own client is namespaced by the engine and stays legal. The file's type aliases are excluded too — the <X>Opts records exist only in TypeScript, to spell optional arguments, and have no schema type to collide with.
  • Checked first, before findClasses. The error has to name the declaration the user wrote, and by the time a reference to it is resolved the core type has already answered for the name.
  • Referencing a core type is untouched. The rule is about a declaration in the module's own source; a @func taking or returning an imported Container/Directory/Volume declares nothing and still resolves to the core object.
  • fix: refuse a union type alias instead of registering it as a scalar #105's union refusal stays for unions whose name does not collide; a colliding one now gets the more specific message instead.
  • No change to reference resolution order. An earlier revision of this PR made local declarations win in AST.findResolvedNodeByName; with the refusal firing first that became dead weight and was dropped.

Two paths fail open, both in the safe direction, neither worth code:

  • coreTypeNames() coming back empty makes the rule a no-op. That happens when client.gen.ts never lands in the program — in the scan container an unresolved @dagger.io/* import does not fail, it degrades into a type that prints as the name that was written.
  • A dependency module literally named client would emit client.gen.ts and so be read as core, making its type names unshadowable.

The introspector ships as a committed prebuilt bundle, so library/bundle/introspector.js is rebuilt here: 4,199,683 → 4,201,506 bytes (+1,823). It is the only bundle artifact that moved.

Verification

Engine v1.0.0-beta.18 throughout (the floor v1.0.3 declares).

Reproduced on unmodified main first: every state above — exported, non-exported, @object class, and the Loudness control.

Nothing in this repo trips the widened rule: cross-checking every class, enum, interface and type alias declared under .dagger/, templates/ and the introspector's testdata/ (117 declarations) against the 113 core class and enum names of beta.18 turns up exactly one collision, Volume, in the fixtures added here — plus #105's flat unionTypeAlias fixture, which has no generated client and so still takes the union path.

With the fix, in the same throwaway workspace:

case result
export type Volume = "loud" | "quiet" type alias Volume at src/index.ts:2:0 shadows the core Dagger type Volume. ...
@object() export class Volume class Volume at src/index.ts:10:0 shadows the core Dagger type Volume. ...
class Volume {} + @func shout(v: Volume), undecorated class Volume at src/index.ts:2:0 shadows the core Dagger type Volume. ...
export type Loudness = "loud" | "quiet" #105's message, unchanged
export type Loudness = { level: string } + dagger call unionapp loudness level loud — a non-colliding module type still works
@func() echo(ctr: Container) + dagger call unionapp echo --ctr alpine:3.21 hi — a real core Container argument, called for real

New coverage:

  • library/src/module/introspector/test/core_type_shadowing.spec.ts — a type alias, a decorated class and an undecorated class, each over a source directory of the new testdata/shadowedCoreType fixture (module source beside a generated client declaring class Volume, modelled on sdkAlias). fix: refuse a union type alias instead of registering it as a scalar #105's fixture is flat, with no generated client, which is why it never caught this — and why it still exercises the plain union path. All three red without the fix: Error: scanning type alias Volume should have failed, scanning class Volume should have failed.
  • .dagger/modules/e2e — generate:generate-refuses-shadowed-core-type-check, over fixtures/shadowed-core-type/app. A scan failure reaches Dang as exit code: 1 with stderr only in the trace, so this asserts the failure and the unit tests assert the message — the split generate-refuses-duplicate-object-check documents. Red without the fix (committed bundle swapped back to main's): generating a module declaring a type named after a core type should fail, got: .../app/__dagger.entrypoint.ts, .../app/sdk/client.gen.ts, ... — i.e. generation succeeded.

Checks run (e2e harness workspace, all green):

check result
generate:generate-refuses-* (5, incl. the new one) ✔ 5 passed
generate:generate-check ✔
packager:library-tests (68 specs) ✔
packager:library-bundle/stale ✔
runtimes:* — the whole execution group, 12 checks, every fixture that declares helper classes and interfaces (interface-returns-check passes a core Container across a module boundary for real) ✔ 12 passed
sdk:fixture-engine-version-check (new fixture added to managedTomlFixtures) ✔

Not covered

  • Core scalars (Platform, Bytes, JSON, the <T>ID aliases) are not in the refused set, because the set is the client's classes and enums. Left out on purpose rather than by oversight: a scalar is string-shaped on both sides, so type Platform = string resolving to the core scalar is near-harmless, unlike an object whose fields then cannot be queried.
  • The refusal fires on a declaration whether or not the module's API ever uses it, so a purely internal interface Env is refused too. Deliberate: it keeps the rule declaration-based, with no dependence on resolution order.
  • Not run: the rest of the suite, engine-e-2-e:*.

@TomChv
TomChv force-pushed the fix/union-alias-shadowing-core-type branch from 9a32fae to a7eeb33 Compare October 19, 2026 13:52
@TomChv TomChv changed the title fix: resolve a type reference to the user's own declaration, not a generated one fix: refuse a module type named after a core type Oct 19, 2026
The engine namespaces a module's types against other modules, not against
core, so a declaration sharing a core type's name does not get a namespace of
its own: the core type answers for the name. Nothing failed. `type Volume =
"loud" | "quiet"` generated a module taking a core Volume the author never
mentioned — `dagger call ... --volume loud` then died on "unsupported volume
address" — and `@object class Volume` generated an object whose own fields
could not be queried. The union refusal added in #105 never fired for such a
name, because the class lookup answered first.

Go and Python modules collide with core the same way, so the scan refuses the
declaration rather than working around it: a class, enum, interface or type
alias named after a core class or enum is reported with its position and told
to rename. It holds for the declaration, decorated or not and used by the
module's API or not — an undecorated `class Volume` is the same silent failure
one `@func` argument later, and a rule about a declaration does not depend on
which of two declarations the resolver finds first.

Referencing a core type is untouched — the rule is about a declaration in the
module's own source, not a use of an imported `Container`. The core set is
read from `client.gen.ts` alone, so a name shared with a dependency's client,
which the engine does namespace, stays legal.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv force-pushed the fix/union-alias-shadowing-core-type branch from a7eeb33 to e6f0597 Compare October 19, 2026 14:08
@TomChv
TomChv merged commit 4a03df2 into main Oct 21, 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