Skip to content

core: replace entrypoint resources with sources - #14693

Merged
eunomie merged 12 commits into
dagger:mainfrom
eunomie:entrypoint-sources-uniform
Oct 15, 2026
Merged

eunomie merged 12 commits into
dagger:mainfrom
eunomie:entrypoint-sources-uniform

Conversation

@eunomie

@eunomie eunomie commented Oct 15, 2026

Copy link
Copy Markdown
Member

A Dang entrypoint can declare sources(workspace), which returns the files the module builds from at workspace-root paths. The engine calls it at module load; the result becomes the module context, so outside files reach every call key. types and call get no workspace, so results are reused across sessions. This removes resources and replaces #14661.

pub sources(workspace: Workspace!): Directory! {
  directory
    .withDirectory("lib", workspace.directory("/lib", include: ["greet/*.go"]))
    .withDirectory(workspace.cwd, workspace.directory("."))
}
  • With no outside files, it returns only the module directory.
  • A module without sources is unchanged.
  • The module's own files win; .git is dropped.
  • Decided: sources runs with the trust of module code.

Tests: Dang conformance, 48 integration tests, go-sdk e2e matrix.
Not covered: caching sources itself for git sources.

Walkthrough: https://gistpreview.github.io/?3f013b572771b17c2d23d1681ded91e9

The entrypoint sources member replaces it.

Signed-off-by: Yves Brissaud <[email protected]>
The returned files join the module context, so they reach the module
digest and every function call key without a workspace argument.

Signed-off-by: Yves Brissaud <[email protected]>

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM based on our discussion on Discord

TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
dagger/dagger#14693 replaces the `resources` member this PR was built on. The
entrypoint now returns the data the module builds from rather than a list of
paths for the engine to interpret:

```dang
pub sources(workspace: Workspace!): Directory! {
  directory
    .withDirectory(workspace.cwd + "/..", workspace.directory("..", include: ["shared"], ...))
    .withDirectory(workspace.cwd, workspace.directory(".", exclude: ["**/.git", "**/node_modules"]))
}
```

The engine merges that into the module context before anything reads the module
digest, so the files reach every call's cache key as they did under `resources`.
What changes is everything around it:

- `types` and `call` take no workspace. Declaring `sources` is what selects that
  interface. Under `resources` both still received one, and that workspace sat
  outside every cache key — a read through it could be served stale, which is
  the hole the replacement closes.
- `sources` runs with API access and receives a workspace, so it returns a
  directory instead of static text. The workspace is the module's own — a local
  checkout, the workspace it was loaded from, or a Git commit — never the
  caller's.
- `call` gains `callDigest`, passed to the dispatcher as
  DAGGER_MODULE_CALL_DIGEST, matching the Go SDK's generated entrypoint.

The member is emitted for every module, not only one with outside paths: one
shape for every module is the engine's choice, and it is what the interface
keys on. A module declaring nothing returns its own directory alone.

Declared paths are read through their parent directory with an include filter
rather than named directly, because a pattern can be a directory, a file
(`../tsconfig.base.json`) or a glob (`../shared/*.ts`) and the parent read
spells all three the same way. Patterns are grouped by that parent, and an
exclude is rebased onto the read it applies to. The module's own directory comes
last so it wins a collision, and is read without .gitignore since a module may
ignore the generated clients/ it cannot run without.

The floor moves to beta.18, with every managed fixture. There is no compatible
shape: an engine without `sources` requires the workspace argument `types` no
longer takes, so the module does not load. The nullable-workspace escape the
engine PR allows is deliberately not taken — it cannot be tested against a
beta.18 engine from here, and a form that misfires on a new engine is worse than
one that fails loudly on an old one. The Go SDK made the same choice.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

Signed-off-by: Tom Chauveau <[email protected]>
@eunomie
eunomie merged commit 300cd37 into dagger:main Oct 15, 2026
94 checks passed
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
beta.18 ships the `sources` contract (dagger/dagger#14693) and the explicit-TTL
call-digest fix (dagger/dagger#14694), so the floor this branch already declared
is now a release that exists.

What moves with it, the way #96 moved the last one: `engine-e2e`'s `engine-dev`
pin to beta.18's commit, and the committed bindings and bundle regenerated
against that schema — `packager:library-bindings/stale` regenerates from
whatever engine runs the checks, so the two cannot be bumped apart. The delta is
six lines, a `readOnly` option on a file mount, plus its five rolled-up
declarations.

#14694 is the reason `call` declares `callDigest` and sets it as
DAGGER_MODULE_CALL_DIGEST on the container before the exec: the engine passes it
only to an entrypoint that declares it, and an entrypoint that declares it has
to key its module process exec on it, or an explicit `+cache` TTL expires while
the exec still answers from cache. That PR notes the TypeScript entrypoint did
not declare it yet.

The whole suite now runs: 98 checks pass on beta.18, including the five that
execute a generated entrypoint and could not load one before — `include-runs`,
`include-cache-key`, `entrypoint-error-text`, `secondary-constructor-runs` and
the generation checks that load a staged module.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
dagger/dagger#14693 replaces the `resources` member this PR was built on. The
entrypoint now returns the data the module builds from rather than a list of
paths for the engine to interpret:

```dang
pub sources(workspace: Workspace!): Directory! {
  directory
    .withDirectory(workspace.cwd + "/..", workspace.directory("..", include: ["shared"], ...))
    .withDirectory(workspace.cwd, workspace.directory(".", exclude: ["**/.git", "**/node_modules"]))
}
```

The engine merges that into the module context before anything reads the module
digest, so the files reach every call's cache key as they did under `resources`.
What changes is everything around it:

- `types` and `call` take no workspace. Declaring `sources` is what selects that
  interface. Under `resources` both still received one, and that workspace sat
  outside every cache key — a read through it could be served stale, which is
  the hole the replacement closes.
- `sources` runs with API access and receives a workspace, so it returns a
  directory instead of static text. The workspace is the module's own — a local
  checkout, the workspace it was loaded from, or a Git commit — never the
  caller's.
- `call` gains `callDigest`, passed to the dispatcher as
  DAGGER_MODULE_CALL_DIGEST, matching the Go SDK's generated entrypoint.

The member is emitted for every module, not only one with outside paths: one
shape for every module is the engine's choice, and it is what the interface
keys on. A module declaring nothing returns its own directory alone.

Declared paths are read through their parent directory with an include filter
rather than named directly, because a pattern can be a directory, a file
(`../tsconfig.base.json`) or a glob (`../shared/*.ts`) and the parent read
spells all three the same way. Patterns are grouped by that parent, and an
exclude is rebased onto the read it applies to. The module's own directory comes
last so it wins a collision, and is read without .gitignore since a module may
ignore the generated clients/ it cannot run without.

The floor moves to beta.18, with every managed fixture. There is no compatible
shape: an engine without `sources` requires the workspace argument `types` no
longer takes, so the module does not load. The nullable-workspace escape the
engine PR allows is deliberately not taken — it cannot be tested against a
beta.18 engine from here, and a form that misfires on a new engine is worse than
one that fails loudly on an old one. The Go SDK made the same choice.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
beta.18 ships the `sources` contract (dagger/dagger#14693) and the explicit-TTL
call-digest fix (dagger/dagger#14694), so the floor this branch already declared
is now a release that exists.

What moves with it, the way #96 moved the last one: `engine-e2e`'s `engine-dev`
pin to beta.18's commit, and the committed bindings and bundle regenerated
against that schema — `packager:library-bindings/stale` regenerates from
whatever engine runs the checks, so the two cannot be bumped apart. The delta is
six lines, a `readOnly` option on a file mount, plus its five rolled-up
declarations.

#14694 is the reason `call` declares `callDigest` and sets it as
DAGGER_MODULE_CALL_DIGEST on the container before the exec: the engine passes it
only to an entrypoint that declares it, and an entrypoint that declares it has
to key its module process exec on it, or an explicit `+cache` TTL expires while
the exec still answers from cache. That PR notes the TypeScript entrypoint did
not declare it yet.

The whole suite now runs: 98 checks pass on beta.18, including the five that
execute a generated entrypoint and could not load one before — `include-runs`,
`include-cache-key`, `entrypoint-error-text`, `secondary-constructor-runs` and
the generation checks that load a staged module.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 15, 2026
Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

Signed-off-by: Tom Chauveau <[email protected]>
TomChv added a commit to dagger/typescript-sdk that referenced this pull request Oct 16, 2026
* feat: derive a module's resources from what its source imports

An experiment on top of #86: instead of asking for a list in `dagger.include`,
read the module's imports — which already say what it needs.

```typescript
import { greeting } from "../shared/greet.js"
```

is enough. Generation derives `["../shared"]` and writes it into the module's
`resources`, with nothing declared anywhere.

Three sources, all of them the user's own code or config:

- a relative specifier that resolves outside the module directory,
- a `file:` or `link:` dependency in package.json,
- a tsconfig `paths` target outside the module directory.

A bare specifier with none of those is deliberately left alone. It resolves
inside node_modules, which in a monorepo sits at the repository root — outside
the module and gitignored, so chasing it would name a path the engine drops from
the context anyway.

`ts.preProcessFile` does the extraction: a lexical scan that needs no program,
no checker, and none of the files being imported. That last part is what breaks
the circularity, since the files outside the module are exactly what this is
trying to discover. It also sees type-only imports, `export * from` and dynamic
`import()`, and ignores a commented-out import or a lookalike string literal.

Directories, not files: a specifier names `./greet.js` where the file is
`greet.ts`, so the resolved path often does not exist as written. The directory
covers both spellings, and whatever that file reads from beside it — which is
how the runtimes fixture's `readFileSync("../shared/greeting.txt")` resolves
with nothing importing it. That fixture's `dagger.include` is removed here, and
`runtimes:include-runs` and `runtimes:include-cache-key` pass without it.

`dagger.include` stays, unioned in, because static analysis cannot see a read
from a directory nothing imports. The premise that it could be dropped
entirely does not hold: a module cannot reach the workspace API either
(`dag.currentWorkspace is not a function` — the engine hides
`Query.currentWorkspace` from the module-facing schema), so resources are the
only way such a file arrives.

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

* feat: derive tsconfig extends and triple-slash references too

Correcting the premise this experiment was written against. A module *can*
reach any workspace file — not through `dag.currentWorkspace`, which the engine
hides from the module-facing schema, but by taking a `Workspace` argument,
which the caller passes:

```typescript
@func()
async extra(ws: Workspace): Promise<string> {
  return await ws.directory("/").file("data/extra.txt").contents()
}
```

Verified against a module with no resources at all: that returns the file,
where `readFileSync("../data/extra.txt")` on the same path fails. So reading a
file was never what resources are for, and `readFileSync` on an outside path is
the wrong shape — it only works where derivation happened to include the
directory anyway.

That leaves resources for build-time paths alone, which is exactly what
deriving from imports targets. Two more such paths, both missed before:

- `tsconfig.json` `extends` pointing outside the module. Not a quiet loss: tsx
  reads the chain at call time and dies with
  `File '../tsconfig.base.json' not found.` The file is derived, not its
  directory — `../tsconfig.base.json` sits in the module's parent, and
  including that whole directory would pull in the repository.
- `/// <reference path="../x.d.ts" />`, which the same scan already hands back
  as `referencedFiles`.

With those, nothing in this repository needs `dagger.include`: the runtimes
fixture's declaration is already gone, and the playground case now generates
`["../shared", "../tsconfig.base.json"]` from an empty package.json and answers
every function. The field stays as a fallback for what static analysis cannot
see — a Deno import map, a `workspace:` dependency, a path computed at run time.

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

* feat: derive a Deno module's resources from its import map

Deno is where derivation has the least to go on. A Deno module has no
package.json and no tsconfig, so `deno.json`'s `imports` is its only alias
mechanism — and a module importing through one names no path outside itself:

```typescript
import { greet } from "@shared/greet"
```

Before this, nothing was derived for that and the call died with
`Module not found "file:///workspace/shared/greet.ts"`. The map entry is now
read like a tsconfig `paths` target.

A relative import already worked, since `preProcessFile` is a lexical scan and
knows nothing about runtimes — verified separately with a Deno module importing
`../../shared/greet.ts`, which derives `../shared` and runs.

Granularity follows the config rather than being imposed: `"@shared/greet":
"../shared/greet.ts"` derives the file, `"@shared/": "../shared/"` derives the
directory. An import still contributes its target's directory, because there
the extension gets rewritten and the resolved path often does not exist as
written. `npm:`, `jsr:` and `https:` targets are specifiers, not paths, and are
left alone.

Covered by `runtimes:include-deno-runs`, whose fixture imports only through the
map and asserts both the derived member and a real call, and by a unit fixture
pinning what is and is not read from a map.

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

* fix: name the sources member, not the resources it replaced

Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

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

* fix: assert the read the Deno import map's file target actually produces

A file target groups under its own parent with the file as the include, not
under the module's parent with a path as the include — that is what the parent
read with an include filter means, and the renamed assertion guessed the wrong
one. Behaviour was right throughout; `runtimes:include-deno-runs` now pins it.

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

* feat: follow imports transitively, and drop dagger.include

Acting on the review of #106.

**Transitive imports.** Stopping at the first hop only worked by accident: a
sibling directory importing from a third one scanned fine and failed to compile
in the container. The scanner now resolves each specifier to a real file and
follows it, breadth-first, until nothing new turns up — so `../shared/greet.ts`
importing `../deeper/punctuate.js` contributes `../deeper` too, attributed to
the import that reached it rather than to the module's own source.

Following needs the files, so the scanner reads the workspace instead of the
module alone. Narrowed to what an import can resolve to and the configs an alias
can come from, not the whole tree: this runs on every generate, and a monorepo's
assets have no bearing on what the module imports. It stays the cheap half — a
lexical scan per file, no TypeScript program — and the expensive typedef scan
downstream is still keyed on the module and the derived paths.

A config target is followed the same way, which caught a bug of mine on the
first real run: a Deno import map naming a file marked that file seen before
enumerating it, so the one file the alias pointed at was never followed.
`runtimes:include-deno-runs` pins it now.

**`dagger.include` is gone.** Nothing needed it: every case is either derived or
better written as a `Workspace` argument. That removes the field, `ModConfig`'s
reader, `module-config`'s `get-include` resolver and its tests, the two e2e
checks that exercised the field, and `docs/include.md` — which becomes
`docs/module-sources.md`, since it no longer documents a field.

**tsconfig `baseUrl`** is honoured: `paths` resolve against it when set. Without
that, `["x/*"]` under `baseUrl: ".."` resolved inside the module and was dropped
as already-part-of-the-source, which is the opposite of what setting it means.

**Implicitness** stays — it is the point — but each derived path is now written
to stderr with the import behind it, so `dagger generate` shows in its trace
what the code asked for. Implicit is not the same as invisible.

`workspace:` dependencies (#108) and `deno.jsonc` (#109) stay unsupported, as
agreed, and now have issues.

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 Oct 16, 2026
…s workspace (#86)

* feat: mount only the module, and dagger.include for files outside it

A generated entrypoint's call() mounted `workspace.directory("/")` — the
caller's whole workspace. Every unrelated file in the repository went into
the module's container (.git, build output, a root .env) and keyed it, so an
edit anywhere in the repository rebuilt it. It now mounts the module's own
directory and nothing else.

A module that needs more declares it with `dagger.include` in its
package.json, or its deno.json under Deno — the nested `dagger` object
`baseImage` already lives in. Entries are paths relative to the module
directory, `!`-prefixed to exclude, globs allowed, resolved at generation by
module-config and baked into the recipe.

One list reaches three places, which is the point: the scan runs against those
paths, so an import leaving the module directory resolves to what it will
resolve to at run time; the recipe mounts them beside the module; and the
install reads them, so a file: dependency outside the module directory
resolves. .gitignore applies to them and not to the module's own directory,
which may ignore the generated clients/ it cannot run without.

Refused rather than silently ignored: an absolute path, one that climbs past
the workspace root, one inside the module directory, one matching nothing, a
module at the workspace root, and a [runtime] module — whose manifest has an
`include` field for this.

A module at the workspace root generates byte-identical output; for a nested
one the mount narrows and the install moves to the module's path under /deps,
so the relative symlink npm leaves for a file: dependency resolves the same
way under both.

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

* refactor: build a call from currentModule, and declare includes as resources

The recipe read the caller's workspace. It now reads the module context the
engine assembles, and declares what belongs in it.

dagger/dagger#14639 gives a Dang entrypoint a `resources` member. The engine
reads it while it loads the module source and adds the listed paths to the
module context *and the module digest*, before anything reads that digest. So:

- A call mounts `currentModule.contextDirectory` and works from
  `currentModule.sourceSubpath`. The engine decides what is in it — the module
  directory, the manifest, and whatever resources lists — so the narrowing is
  the engine's rather than a filter of ours.
- A change to an included file gives a call a new cache key. Read from the
  workspace, those files sat outside everything the key covers, and a function
  with the default policy kept returning its first answer. This is the reason
  for the refactor.
- The context comes from the module's own source, not the caller's, so the
  recipe no longer depends on running from the module's own checkout.
- sourceSubpath replaces the baked module path: it is the module's path inside
  its context, which is not the workspace-relative one when the context root is
  not the workspace root.

`dagger.include` is unchanged for users. Generation still resolves it from the
workspace for its own scan — the member it would otherwise read is the one it is
about to write — and the patterns are rebased to the module-relative form the
engine resolves for the member itself. Single patterns now: a directory pattern
covers the directory in that grammar, so nothing is expanded.

runtime, dependencies and requireGenerated lose their workspace parameter;
call() keeps its own, which the interface mandates, and no longer uses it.

All three fields need beta.17, which #96 already pairs this SDK with, so the
only engine-related change here is the floor the targetEngineVersion docstring
records. The new include fixture's manifest names that release like every other
managed one.

Tests: the four Go suites; `runtimes:include-cache-key`, which rewrites the
included file between two loads and fails if the stale result comes back;
`runtimes:include-runs`, whose fixture imports a function and a type from the
included directory and reads a file there at call time; and the full check
suite on beta.17.

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

* feat: return a sources directory instead of listing resource patterns

dagger/dagger#14693 replaces the `resources` member this PR was built on. The
entrypoint now returns the data the module builds from rather than a list of
paths for the engine to interpret:

```dang
pub sources(workspace: Workspace!): Directory! {
  directory
    .withDirectory(workspace.cwd + "/..", workspace.directory("..", include: ["shared"], ...))
    .withDirectory(workspace.cwd, workspace.directory(".", exclude: ["**/.git", "**/node_modules"]))
}
```

The engine merges that into the module context before anything reads the module
digest, so the files reach every call's cache key as they did under `resources`.
What changes is everything around it:

- `types` and `call` take no workspace. Declaring `sources` is what selects that
  interface. Under `resources` both still received one, and that workspace sat
  outside every cache key — a read through it could be served stale, which is
  the hole the replacement closes.
- `sources` runs with API access and receives a workspace, so it returns a
  directory instead of static text. The workspace is the module's own — a local
  checkout, the workspace it was loaded from, or a Git commit — never the
  caller's.
- `call` gains `callDigest`, passed to the dispatcher as
  DAGGER_MODULE_CALL_DIGEST, matching the Go SDK's generated entrypoint.

The member is emitted for every module, not only one with outside paths: one
shape for every module is the engine's choice, and it is what the interface
keys on. A module declaring nothing returns its own directory alone.

Declared paths are read through their parent directory with an include filter
rather than named directly, because a pattern can be a directory, a file
(`../tsconfig.base.json`) or a glob (`../shared/*.ts`) and the parent read
spells all three the same way. Patterns are grouped by that parent, and an
exclude is rebased onto the read it applies to. The module's own directory comes
last so it wins a collision, and is read without .gitignore since a module may
ignore the generated clients/ it cannot run without.

The floor moves to beta.18, with every managed fixture. There is no compatible
shape: an engine without `sources` requires the workspace argument `types` no
longer takes, so the module does not load. The nullable-workspace escape the
engine PR allows is deliberately not taken — it cannot be tested against a
beta.18 engine from here, and a form that misfires on a new engine is worse than
one that fails loudly on an old one. The Go SDK made the same choice.

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

* chore: pair with the released v1.0.0-beta.18

beta.18 ships the `sources` contract (dagger/dagger#14693) and the explicit-TTL
call-digest fix (dagger/dagger#14694), so the floor this branch already declared
is now a release that exists.

What moves with it, the way #96 moved the last one: `engine-e2e`'s `engine-dev`
pin to beta.18's commit, and the committed bindings and bundle regenerated
against that schema — `packager:library-bindings/stale` regenerates from
whatever engine runs the checks, so the two cannot be bumped apart. The delta is
six lines, a `readOnly` option on a file mount, plus its five rolled-up
declarations.

#14694 is the reason `call` declares `callDigest` and sets it as
DAGGER_MODULE_CALL_DIGEST on the container before the exec: the engine passes it
only to an entrypoint that declares it, and an entrypoint that declares it has
to key its module process exec on it, or an explicit `+cache` TTL expires while
the exec still answers from cache. That PR notes the TypeScript entrypoint did
not declare it yet.

The whole suite now runs: 98 checks pass on beta.18, including the five that
execute a generated entrypoint and could not load one before — `include-runs`,
`include-cache-key`, `entrypoint-error-text`, `secondary-constructor-runs` and
the generation checks that load a staged module.

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

* feat: derive a module's sources from what its code imports (#106)

* feat: derive a module's resources from what its source imports

An experiment on top of #86: instead of asking for a list in `dagger.include`,
read the module's imports — which already say what it needs.

```typescript
import { greeting } from "../shared/greet.js"
```

is enough. Generation derives `["../shared"]` and writes it into the module's
`resources`, with nothing declared anywhere.

Three sources, all of them the user's own code or config:

- a relative specifier that resolves outside the module directory,
- a `file:` or `link:` dependency in package.json,
- a tsconfig `paths` target outside the module directory.

A bare specifier with none of those is deliberately left alone. It resolves
inside node_modules, which in a monorepo sits at the repository root — outside
the module and gitignored, so chasing it would name a path the engine drops from
the context anyway.

`ts.preProcessFile` does the extraction: a lexical scan that needs no program,
no checker, and none of the files being imported. That last part is what breaks
the circularity, since the files outside the module are exactly what this is
trying to discover. It also sees type-only imports, `export * from` and dynamic
`import()`, and ignores a commented-out import or a lookalike string literal.

Directories, not files: a specifier names `./greet.js` where the file is
`greet.ts`, so the resolved path often does not exist as written. The directory
covers both spellings, and whatever that file reads from beside it — which is
how the runtimes fixture's `readFileSync("../shared/greeting.txt")` resolves
with nothing importing it. That fixture's `dagger.include` is removed here, and
`runtimes:include-runs` and `runtimes:include-cache-key` pass without it.

`dagger.include` stays, unioned in, because static analysis cannot see a read
from a directory nothing imports. The premise that it could be dropped
entirely does not hold: a module cannot reach the workspace API either
(`dag.currentWorkspace is not a function` — the engine hides
`Query.currentWorkspace` from the module-facing schema), so resources are the
only way such a file arrives.

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

* feat: derive tsconfig extends and triple-slash references too

Correcting the premise this experiment was written against. A module *can*
reach any workspace file — not through `dag.currentWorkspace`, which the engine
hides from the module-facing schema, but by taking a `Workspace` argument,
which the caller passes:

```typescript
@func()
async extra(ws: Workspace): Promise<string> {
  return await ws.directory("/").file("data/extra.txt").contents()
}
```

Verified against a module with no resources at all: that returns the file,
where `readFileSync("../data/extra.txt")` on the same path fails. So reading a
file was never what resources are for, and `readFileSync` on an outside path is
the wrong shape — it only works where derivation happened to include the
directory anyway.

That leaves resources for build-time paths alone, which is exactly what
deriving from imports targets. Two more such paths, both missed before:

- `tsconfig.json` `extends` pointing outside the module. Not a quiet loss: tsx
  reads the chain at call time and dies with
  `File '../tsconfig.base.json' not found.` The file is derived, not its
  directory — `../tsconfig.base.json` sits in the module's parent, and
  including that whole directory would pull in the repository.
- `/// <reference path="../x.d.ts" />`, which the same scan already hands back
  as `referencedFiles`.

With those, nothing in this repository needs `dagger.include`: the runtimes
fixture's declaration is already gone, and the playground case now generates
`["../shared", "../tsconfig.base.json"]` from an empty package.json and answers
every function. The field stays as a fallback for what static analysis cannot
see — a Deno import map, a `workspace:` dependency, a path computed at run time.

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

* feat: derive a Deno module's resources from its import map

Deno is where derivation has the least to go on. A Deno module has no
package.json and no tsconfig, so `deno.json`'s `imports` is its only alias
mechanism — and a module importing through one names no path outside itself:

```typescript
import { greet } from "@shared/greet"
```

Before this, nothing was derived for that and the call died with
`Module not found "file:///workspace/shared/greet.ts"`. The map entry is now
read like a tsconfig `paths` target.

A relative import already worked, since `preProcessFile` is a lexical scan and
knows nothing about runtimes — verified separately with a Deno module importing
`../../shared/greet.ts`, which derives `../shared` and runs.

Granularity follows the config rather than being imposed: `"@shared/greet":
"../shared/greet.ts"` derives the file, `"@shared/": "../shared/"` derives the
directory. An import still contributes its target's directory, because there
the extension gets rewritten and the resolved path often does not exist as
written. `npm:`, `jsr:` and `https:` targets are specifiers, not paths, and are
left alone.

Covered by `runtimes:include-deno-runs`, whose fixture imports only through the
map and asserts both the derived member and a real call, and by a unit fixture
pinning what is and is not read from a map.

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

* fix: name the sources member, not the resources it replaced

Follow-on from the rebase: the derived paths now feed `sources`
(dagger/dagger#14693) rather than the `resources` list it replaced, so the
checks assert the read the member makes and the docs stop naming a member the
engine no longer has.

Nothing about derivation itself changes — it produced workspace-relative
patterns before and still does, and codegen is what turns them into the reads
`sources` performs.

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

* fix: assert the read the Deno import map's file target actually produces

A file target groups under its own parent with the file as the include, not
under the module's parent with a path as the include — that is what the parent
read with an include filter means, and the renamed assertion guessed the wrong
one. Behaviour was right throughout; `runtimes:include-deno-runs` now pins it.

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

* feat: follow imports transitively, and drop dagger.include

Acting on the review of #106.

**Transitive imports.** Stopping at the first hop only worked by accident: a
sibling directory importing from a third one scanned fine and failed to compile
in the container. The scanner now resolves each specifier to a real file and
follows it, breadth-first, until nothing new turns up — so `../shared/greet.ts`
importing `../deeper/punctuate.js` contributes `../deeper` too, attributed to
the import that reached it rather than to the module's own source.

Following needs the files, so the scanner reads the workspace instead of the
module alone. Narrowed to what an import can resolve to and the configs an alias
can come from, not the whole tree: this runs on every generate, and a monorepo's
assets have no bearing on what the module imports. It stays the cheap half — a
lexical scan per file, no TypeScript program — and the expensive typedef scan
downstream is still keyed on the module and the derived paths.

A config target is followed the same way, which caught a bug of mine on the
first real run: a Deno import map naming a file marked that file seen before
enumerating it, so the one file the alias pointed at was never followed.
`runtimes:include-deno-runs` pins it now.

**`dagger.include` is gone.** Nothing needed it: every case is either derived or
better written as a `Workspace` argument. That removes the field, `ModConfig`'s
reader, `module-config`'s `get-include` resolver and its tests, the two e2e
checks that exercised the field, and `docs/include.md` — which becomes
`docs/module-sources.md`, since it no longer documents a field.

**tsconfig `baseUrl`** is honoured: `paths` resolve against it when set. Without
that, `["x/*"]` under `baseUrl: ".."` resolved inside the module and was dropped
as already-part-of-the-source, which is the opposite of what setting it means.

**Implicitness** stays — it is the point — but each derived path is now written
to stderr with the import behind it, so `dagger generate` shows in its trace
what the code asked for. Implicit is not the same as invisible.

`workspace:` dependencies (#108) and `deno.jsonc` (#109) stay unsupported, as
agreed, and now have issues.

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

---------

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

* fix: stop naming a field that no longer exists

Merge debris from #106, which deleted `dagger.include`: eight places still
named it, in text the merge never touched.

The one that mattered is user-facing — the raise when the derived paths match
nothing in the workspace told people to look at a field that is not there. The
rest are docstrings, including one in the generated entrypoint itself, which
told a reader its `sources` member returns "whatever its `dagger.include`
declares".

Also the two manifest docstrings describing what a module loses when it moves
between a `[runtime]` and an entrypoint: an `include` no longer becomes a
package.json field, it becomes nothing to declare.

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