Skip to content

fix: make dagger module init work from a subdirectory workspace - #13890

Merged
eunomie merged 4 commits into
dagger:mainfrom
eunomie:fix-module-init-from-subdirectory-5edc3918
Aug 18, 2026
Merged

eunomie merged 4 commits into
dagger:mainfrom
eunomie:fix-module-init-from-subdirectory-5edc3918

Conversation

@eunomie

@eunomie eunomie commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #13889.

dagger module init misplaces or rejects files whenever you run it from
anywhere other than the workspace root. Three fixes here, all in the same
corner of init; they're separate commits and can be read independently.

1. Init is anchored at the workspace root

Init writes files the caller doesn't own — the workspace dagger.toml, the new
module's dagger-module.toml, and whatever the SDK scaffolds — then exports
them itself at the workspace root (Workspace.export writes to
ExportHostPath). Two things were measured from the caller's cwd instead.

The SDK stages its files relative to Workspace.cwd, because a changeset
returned from an ordinary function call is applied wherever the client stands.
Handed the caller's workspace, it strips the cwd prefix, so the module comes out
split — engine config in one directory, sources in another:

$ cd subdir && dagger module init go my-module --path subdir/plop
dagger.toml                    +2
plop/main.go                   +34     ← SDK, cwd-stripped
subdir/plop/dagger-module.toml +5      ← engine, root-relative

Workspace.changes re-roots to the caller's cwd for the same reason, so the
workspace init hands back can't describe its own edits from a subdirectory — the
workspace dagger.toml sits above the caller and has no cwd-relative spelling:

$ cd subdir && dagger module init go my-module
Error: changes fall outside the current directory "subdir": ..., dagger.toml

Both are now anchored at the root: the SDK gets a workspace whose cwd matches
where its changeset lands, and the workspace init returns measures its changes
from the same place it exports them. Nothing changes for changesets that really
are applied at the caller's cwd — dagger generate included — and neither the
polyfill nor any SDK is touched.

Note this is not specific to a dagger.toml in a subdirectory. A config at the
repo root with the caller one directory down hits it identically; a subdirectory
config just guarantees it, since the workspace root is the git root.

2. The default module path follows its dagger.toml

.dagger/modules/<name> was joined against the workspace root, but
[modules.<name>].source is resolved against the config directory. With a
config in a subdirectory those disagree, and init would record an entry pointing
outside its own project. The default is now <config dir>/.dagger/modules/<name>,
with the recorded source made config-dir-relative — the same conversion
dagger install already does for a local ref. Nothing changes for a workspace
whose config is at the root.

3. Init paths are resolved against the caller

module init --path and api client init <path> were workspace-root-relative,
while every other path you type at the workspace — Workspace.directory,
Workspace.file, a local ref given to dagger install — is relative to where
you are standing, with a leading / meaning the workspace root. From a
subdirectory that made --path ci mean <root>/ci rather than ./ci,
silently, and left no spelling for the root at all.

Both now go through resolveWorkspacePath, the resolver dagger install
already uses:

dagger module init go my-module --path ci      # ./ci
dagger module init go my-module --path /ci     # <workspace root>/ci
dagger module init go my-module --path ../ci   # climbs, still inside the root

Everything downstream still sees a workspace-root-relative path. Escaping the
root is still rejected, as is a client path resolving to the root itself.

This is a behaviour change for anyone passing these paths from a subdirectory.
At the workspace root, where cwd and root coincide, nothing moves.

Tests

TestInitFromSubdirectoryWorkspace and TestInitFromSubdirectoryCwd cover the
two ways a caller ends up below the workspace root — a subdirectory config, and
a root config with the caller in a subdirectory — for module init, client init,
and --path resolved relatively, rooted with a leading /, climbing with
.., and rejected when it escapes the workspace.

The fixture SDK now mirrors the polyfill's cwd rebasing, so it reproduces the
real failure rather than a synthetic one.

@eunomie eunomie added this to the v1.0.0 milestone Aug 13, 2026
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch from 1fbeedb to 9901d2c Compare August 13, 2026 16:14
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch 2 times, most recently from d38bb22 to a3e1762 Compare August 14, 2026 15:12
@eunomie
eunomie marked this pull request as ready for review August 14, 2026 15:45
@eunomie
eunomie requested a review from a team as a code owner August 14, 2026 15:45
@eunomie
eunomie marked this pull request as draft August 14, 2026 16:39
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch from a3e1762 to 05312c7 Compare August 18, 2026 10:19
@eunomie
eunomie marked this pull request as ready for review August 18, 2026 10:37
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch 3 times, most recently from b318e1b to 99b2dc0 Compare August 18, 2026 14:47
@tiborvass

Copy link
Copy Markdown
Contributor

I found two issues worth addressing:

  1. The returned workspace loses the caller’s cwd. Both withInitModule and withInitClient pass the root-anchored workspace into workspaceWithChangeset and return that result. When init starts below the workspace root, the returned workspace therefore has cwd == "/". For example, ws.withWorkdir("sub").withInitModule(...).file("x") unexpectedly resolves x from the workspace root instead of sub. Root-relative change handling should be separated from the caller-visible cwd, or the original cwd should otherwise be preserved.

  2. The public API documentation still describes these paths as workspace-relative. The implementation now resolves withInitModule.path and withInitClient.path relative to the workspace cwd, with a leading / meaning the workspace root. However, the argument documentation in core/schema/workspace.go still says “Workspace-relative.” It should be updated so the generated API documentation reflects the new path contract.

Init writes files the caller does not own: the workspace dagger.toml, the
new module's dagger-module.toml, and whatever the SDK scaffolds. It then
exports them itself, at the workspace root -- Workspace.export writes to
ExportHostPath. Everything about that flow is measured from the root.

Two things were measured from the caller's cwd instead.

The SDK stages its files relative to Workspace.cwd, because a changeset
returned from an ordinary function call is applied wherever the client is
standing. Handed the caller's workspace, it strips the cwd prefix and
scatters its files away from the config the engine wrote. Give it a
root-anchored workspace, so the cwd it stages against matches where its
changeset lands.

Reading the diff had the same problem from the other side: Workspace.changes
re-roots to the caller's cwd, and init's root-owned edits -- the workspace
dagger.toml above all -- have no cwd-relative spelling from a subdirectory,
so the preview failed with

    changes fall outside the current directory "sub": ..., dagger.toml

That belongs to whoever pairs changes() with export(), not to init: the CLI
asks for the root-measured view, exactly as `dagger setup` already does for
migration. The workspace init returns is the caller's, moved only by the
changeset -- withInitModule is not a chdir.

Neither the polyfill nor any SDK changes, and the cwd-relative rule stays as
it is for changesets that really are applied at the caller's cwd.

Signed-off-by: Yves Brissaud <[email protected]>
The default module path was joined against the workspace root, while
[modules.<name>].source is resolved against the directory holding the
dagger.toml. With a config in a subdirectory the two point at different
places, so the install entry init writes would name a module outside the
project the config belongs to.

Anchor the default at the config directory and convert the recorded
source back to config-directory-relative, the same conversion
`dagger install` performs when it records a local ref. At the workspace
root -- where every existing workspace has its config -- both are what
they were.

--path keeps its documented workspace-root-relative meaning; the CLI help
and the Go SDK page now say so outright, because `--path ci` from a
subdirectory means <workspace root>/ci and reads like ./ci.

Signed-off-by: Yves Brissaud <[email protected]>
The init fixture SDK wrote its scaffold at whatever workspace-root-relative
path the engine handed it, so it could not observe the cwd the engine shows
an SDK. Give it the polyfill's rebasing -- resolve the path against
Workspace.cwd, refuse anything outside it -- and it reproduces the real
failure: the SDK's file lands cwd-stripped while the engine's config stays
put, leaving a split, non-loadable module.

Two suites, for the two ways a caller ends up below the workspace root:

  - a dagger.toml in a subdirectory of the repo, the monorepo layout, with
    --path cases inside and outside the caller's cwd
  - a dagger.toml at the root with the caller in a subdirectory, which is
    the same mismatch with none of the monorepo setup

The fixture SDK module moves off .dagger/ to a dot-free path: a client's
module ref is classified as local by a leading "." or "/" and otherwise by
the absence of a dot, so a nested "common/.dagger/init-fixture" would be
taken for a remote ref.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch from 99b2dc0 to 3df428f Compare August 18, 2026 15:17
@eunomie

eunomie commented Aug 18, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @tiborvass — both fixed in 3df428f2e. (sorry I wasn't expected this to be published by the agent 😅 )

1. The returned workspace no longer moves the cwd. You were right that this needed separating rather than papering over. withInitModule/withInitClient now build their result from the caller's workspace again, so ws.withWorkdir("sub").withInitModule(...).file("x") resolves x from sub. The root-anchored workspace stays internal — it is still what the SDK stages against, which is the half that genuinely needs it.

The root-measured view moved to the caller instead: the CLI does .WithWorkdir(".") before handing the workspace to handleWorkspaceResponse, which is the function that pairs changes() with export(). That is where the two have to agree, since export always writes at ExportHostPath. dagger setup already does exactly this for migration (setup.go, "Migration can move config above the current directory. Use the workspace root so changes() includes it."), so this follows an existing pattern rather than inventing one.

Added a regression test asserting the workspace before and after init share a cwd, so the leak cannot come back quietly.

2. Arg docs updated. withInitModule.path and withInitClient.path now describe the real contract — relative to the workspace cwd, leading / for the workspace root — so the generated API reference matches the implementation. That was a plain miss on my side; I had updated the CLI help and the SDK docs and forgot the schema.

Full TestGenerators plus TestWorkspaceModuleInstall: 139 passed, 0 failed. Each commit builds standalone.

One thing I did not change, in case you have a view: withInitClient.module is still documented and resolved workspace-root-relative. It is a ref rather than an output path, and it also feeds as-sdk.clients[].module in dagger.toml, which is tangled up with the separate as-sdk.modules[].path anchoring question still being coordinated. Felt wrong to flip unilaterally mid-flight, but happy to fold it in if you would rather it be consistent now.

`module init --path` and `api client init <path>` were
workspace-root-relative, while every other path a user types at the
workspace -- Workspace.directory, Workspace.file, a local ref given to
`dagger install` -- is relative to where they are standing, with a leading
"/" meaning the workspace root. From a subdirectory that made `--path ci`
mean <root>/ci rather than ./ci, silently, and left no spelling at all for
"the workspace root".

Route both through resolveWorkspacePath, the resolver `dagger install`
already uses. `ci` now means ./ci, `/ci` means <root>/ci, and `../ci`
climbs; everything downstream still sees a workspace-root-relative path.
Escaping the root is still rejected, as is a client path resolving to the
root itself.

The argument docs say so too, so the published API reference matches; the
generated schema and SDK clients carry those strings and are regenerated
here.

This is a behaviour change for anyone passing these paths from a
subdirectory; at the workspace root, where cwd and root coincide, nothing
moves.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie force-pushed the fix-module-init-from-subdirectory-5edc3918 branch from 3df428f to 75d96de Compare August 18, 2026 15:41
@eunomie
eunomie requested review from a team as code owners August 18, 2026 15:41
@eunomie
eunomie merged commit 5090ac1 into dagger:main Aug 18, 2026
88 checks passed
TomChv added a commit to dagger/sdk-sdk that referenced this pull request Aug 20, 2026
dagger/dagger#13890 landed in 1.0.0-beta.10 and fixes the three checks this
group added red: init changesets are now applied at the workspace root with
the SDK shown a matching cwd, and the default module path is anchored at the
directory holding the dagger.toml being edited.

It also changed `module init --path` deliberately. The path now resolves
against the caller's cwd, with a leading slash meaning the workspace root,
like every other workspace path a user types. explicit-path-keeps-module-whole
passed a workspace-root-relative path naming the config directory, which under
the new resolver means a directory of that name *below* the config directory,
so it asserted the old spelling. Pass a relative path instead and assert it
lands under the caller's cwd.

Add rooted-path-anchors-module-at-root for the other half of the resolver.
With the default path now anchored beside its dagger.toml and a relative
--path resolved against the caller, a leading slash is the only remaining way
to hand the SDK a module path the caller's cwd does not contain — the shape a
cwd-scoped SDK cannot stage at all. beta.9 rejected it outright as absolute.

Five of the six now fail on beta.9 and all six pass on beta.10.

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.

🐞 dagger module init fails "outside changeset root" when dagger.toml is in a subdirectory (monorepo layout)

2 participants