Skip to content

Workspace.withNewDirectory replaces, and a new Workspace.withDirectory merges - #13956

Merged
eunomie merged 4 commits into
dagger:mainfrom
eunomie:withnewdirectory-replace-merge-fix-lead-1faef34f
Aug 25, 2026
Merged

eunomie merged 4 commits into
dagger:mainfrom
eunomie:withnewdirectory-replace-merge-fix-lead-1faef34f

Conversation

@eunomie

@eunomie eunomie commented Aug 24, 2026

Copy link
Copy Markdown
Member

Fixes #13955.

Same call, same source, same path — two different answers depending on where the
workspace came from:

$ dagger call probe compare
host-backed removed:
target/keep.txt
synthetic removed:

A host-backed checkout replaced whatever was at the path; a workspace loaded from
a Directory or from git merged into it. Every SDK writes its scaffolding and its
generated clients through Workspace.withNewDirectory, so on a real checkout
dagger module init deleted whatever was already at the destination — the user's
files, and the module config the engine itself had just written. It was found and
worked around separately in dagger/go-sdk#30, dagger/python-sdk#14 and
dagger/typescript-sdk#35, each time by reading the destination back and layering
onto it by hand.

The split also hides in the other direction: a guard that leans on the
replacement passes locally and quietly stops asserting anything in CI, where the
same repo arrives as a git workspace.

What changes

Workspace.withNewDirectory now replaces on every kind of workspace, and the new
Workspace.withDirectory merges on every kind — the same contract
Directory.withDirectory has. Callers pick the one they mean instead of the one
their workspace happens to give them:

# the source becomes the path's entire contents
withNewDirectory(path: "mod", source: $scaffold)

# the source lands on top of what the path already holds
withDirectory(path: "mod", source: $scaffold)

The read-back the three SDKs each wrote by hand is now the second field.

Where the split came from

overlayEdit hands the edit two different bases. A value or git workspace edits
its full read root, where the path holds the workspace's own content. A
host-backed one edits a delta root holding only what earlier edits wrote — it
never references the host tree, which is what keeps an edit from uploading the
whole checkout — and its changeset comes from diffing that against a host base
scoped to the touched paths. An edit that layers onto the path finds it empty
there, so everything the host has under it diffs against nothing and reads as
removed.

So:

  • withNewDirectory clears the path before it copies, on either branch. An edit
    whose result owes nothing to what the base held reads the same on both.
  • overlayEdit gained an explicit readsExisting — the touched paths whose
    existing content an edit builds on — and seeds the host-backed delta root with
    them first. That invariant is what was missing.

The seed comes out of the very base the changeset is diffed against, and the
overlay records which paths it seeded so every later edit re-seeds them from its
own base. Content the caller never wrote must not sit pinned on the after side: a
file they edit on disk meanwhile would come back reported as modified to whatever
the host held at seeding time, and exporting would write that over their edit.

Tests

Both fields, against all three workspace kinds (host-backed, value, git): writing
over existing content, writing to a path that does not exist, composing with
earlier overlay edits, and not resurrecting content an earlier edit removed. Plus
the issue's own comparison, the host-content-staleness regression above, and
dagger module init end to end against a destination that already holds a file.

Reverting the fix fails exactly the predicted subtests — withNewDirectory
merging on value and git, withDirectory replacing on host-backed — and the
module-init test deletes the user's file.

Writing a directory into a workspace path had one spelling, withNewDirectory,
and it meant two things. On a host-backed checkout it replaced what was there;
on a workspace loaded from a Directory or from git it merged. Every SDK writes
its scaffolding and its generated clients through that call, so which one you
got decided whether `dagger module init` left the user's files alone or deleted
them.

Add withDirectory for the merge, defined the way Directory.withDirectory is:
the source lands on top of what the path already holds and takes nothing else
with it.

The two overlay branches underneath disagree because they hand the edit
different bases. A value or git workspace edits its full read root, where the
path holds the workspace's own content. A host-backed one edits a delta root
holding only what earlier edits wrote — it never references the host tree, which
is what keeps an edit from uploading the whole checkout — and its changeset
comes from diffing that against a host base scoped to the touched paths. An edit
that layers onto the path finds it empty there, so everything the host has under
it diffs against nothing and reads as removed.

overlayEdit now takes the paths whose existing content an edit builds on, and
seeds the delta root with them first. The seed comes out of the very base the
changeset is diffed against, and the overlay records which paths it seeded so
that every later edit re-seeds them from its own base. Content the caller never
wrote must not sit pinned on the after side: a file they edit on disk meanwhile
would come back reported as modified to whatever the host held at seeding time,
and exporting would write that over their edit.

Signed-off-by: Yves Brissaud <[email protected]>
withNewDirectory replaced what a path held on a host-backed checkout and merged
into it on a workspace loaded from a Directory or from git. Nothing in the API
said which you would get; only where the workspace came from decided. A guard
that leaned on the replacement passed locally and quietly stopped asserting
anything in CI, where the same repo arrives as a git workspace, and the reverse
cost the SDKs the files a user already had at the destination.

Settle it as replacement everywhere. It is the one of the two that cannot be
built from the other: layering onto a path is now withDirectory, while emptying
a path first, then writing into it, would have to read the destination back to
know what to clear.

So the edit clears the path before it copies, on either overlay branch. That is
also what makes the two agree: an edit whose result at the path owes nothing to
what the base held there reads the same on a full read root as on a host
overlay's delta root, which holds only what earlier edits wrote. Replacement now
covers those earlier edits too — the path ends up holding the source and nothing
else, whichever kind of workspace it is.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie force-pushed the withnewdirectory-replace-merge-fix-lead-1faef34f branch from f524506 to 1fa82bb Compare August 24, 2026 16:43
@eunomie
eunomie marked this pull request as ready for review August 24, 2026 17:34
@eunomie
eunomie requested review from a team as code owners August 24, 2026 17:34

@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.

Tested with my repro and it fixes both inconsistency in withNewDirectory + withDirectory work as expected.

withNewDirectory - host-backed removed:
target/keep.txt
withNewDirectory - synthetic removed:
target/keep.txt
withDirectory - host-backed removed:
withDirectory - synthetic removed:

Great fixes

@eunomie
eunomie merged commit ac53a63 into dagger:main Aug 25, 2026
89 checks passed
@eunomie
eunomie deleted the withnewdirectory-replace-merge-fix-lead-1faef34f branch August 25, 2026 10:20
eunomie added a commit to dagger/python-sdk that referenced this pull request Aug 27, 2026
Both places this SDK writes a directory into a workspace wanted a merge, not a
replacement, and beta.10 only had `Workspace.withNewDirectory`, which replaces.
Each therefore read the destination back out of the workspace, merged the new
content onto it by hand, and wrote the result: init so it never deletes files a
user already put at the module path, generation so a generated context holding
only generated files does not remove everything else the module owns.

`dagger/dagger#13956` made `withNewDirectory` replace consistently — it merged
on synthetic workspaces and replaced on host-backed ones — and added
`Workspace.withDirectory`, which merges with `Directory.withDirectory`'s
semantics: what is already at the path stays, the source wins on conflict. That
is exactly what the read-back-and-merge was reproducing, so call it directly and
drop the scaffolding, including the `existingDir` helper that had no other
caller.

The `generatedContextDirectory` overlay in `Mod.generate` stays as it is: it
works around `dagger/dagger#13947`, a different defect, still open on beta.11.

Verified on a real workspace with beta.11: `dagger module init python` onto an
empty path scaffolds the module, and onto a path already holding
`keepme.txt`, `src/user_notes.py` and a stray `sdk/stray.txt` leaves all three
in place while laying the template and the vendored library over them.

Signed-off-by: Yves Brissaud <[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.

🐞 Workspace.withNewDirectory replaces on a host-backed workspace and merges on a synthetic one

2 participants