Repository navigation
Description
What is the issue?
Workspace.withNewDirectory(path, source) does two different things at a path that already
has content, depending on where the workspace came from:
| workspace | behaviour at an existing path |
|---|---|
| host-backed (a local checkout) | replaces it — everything already there is reported removed |
synthetic (Directory.asWorkspace) or git-loaded |
merges into it — nothing is removed |
Nothing in the API distinguishes the two, and withNewDirectory's own doc ("Return this
workspace with a directory added") reads as merge.
This is not academic. Every SDK writes its scaffolding and its generated clients through this
call, so on a local checkout dagger module init deletes whatever is already at the
destination — the user's files, and the module config the engine itself wrote there moments
earlier. It was found and fixed separately in three SDKs
(dagger/go-sdk#30, dagger/python-sdk#14, dagger/typescript-sdk#35), each time by reading the
destination back and layering onto it.
The split also breaks tests in the other direction. A guard that relies on replace to delete
something passes locally and silently stops asserting anything in CI, because the Cloud checks
runner loads the repo as a git-backed workspace. That cost two red CI rounds on
dagger/typescript-sdk#35 before the cause was clear.
Dagger version
v1.0.0-beta.10, and reproduced on main.
Steps to reproduce
Four files, no containers, no network, runs in about a tenth of a second.
mkdir -p /tmp/ws-overlay-repro/.dagger/probe /tmp/ws-overlay-repro/target
cd /tmp/ws-overlay-repro && git init -q .
printf '[modules.probe]\nsource = ".dagger/probe"\n' > dagger.toml
printf 'do not delete me\n' > target/keep.txt
cat > .dagger/probe/dagger.json <<'JSON'
{
"name": "probe",
"engineVersion": "v1.0.0-beta.10",
"sdk": { "source": "dang" },
"source": "."
}
JSON
cat > .dagger/probe/main.dang <<'DANG'
type Probe {
"""
Write a directory over an existing path, on a host-backed workspace and on a
synthetic one, and report what each says it removed.
"""
compare(ws: Workspace!, path: String! = "target"): [String!]! {
let source = directory.withNewFile("new.txt", "written by withNewDirectory")
let synthetic = ws.directory("/").asWorkspace
["host-backed removed:"] + ws.withNewDirectory("/" + path, source).changes(ws).removedPaths +
["synthetic removed:"] + synthetic.withNewDirectory("/" + path, source).changes(synthetic).removedPaths
}
}
DANG
dagger call probe compareLog output
host-backed removed:
target/keep.txt
synthetic removed:
Same call, same source directory, same target path.
Where it comes from
withNewDirectory itself is a merge by construction — it selects Directory.withDirectory on
the overlay base (core/schema/workspace.go, workspaceSchema.withNewDirectory). The
divergence is a level down, in overlayEdit:
- Value, git and rootless workspaces take the
HostPath() == "" || !ClientLocalBase()branch,
where the edit is applied to the full root (workspaceOverlayRootfs).withDirectory
merges onto the existing content. - Host-backed workspaces apply the same edit to an empty delta root, then diff it against a
sparse host base scoped to the touched paths. The delta root only holds what the edit
wrote, so every other file under that path diffs against nothing and comes back as removed.
That the two branches disagree looks unintended rather than designed — #13566 planned one
overlay model for all workspace kinds, and the delta/sparse path came in as a
"don't materialize the whole host tree" optimization. #13947 is in the same area (it notes the
sparse baseline sizing) but fixes changeset application, not this.
Whichever way it is resolved, the two kinds should agree, and the chosen semantic should be
stated on the field. Merge is the one the API description already implies, and the one callers
turn out to want: an SDK that needs replace can express it with withoutDirectory first, while
under replace there is no way to express merge without reading the destination back — which is
what all three SDKs now do.
Activity
Keep existing files when init and client codegen write a directory
Keep existing files when init and client codegen write a directory (#35)
Bump to v1.0.0-beta.11 and write directories with the native workspac…