Skip to content

core: keep withDirectory content-keyed when both directories are - #14999

Closed
sipsma wants to merge 1 commit into
mainfrom
sipsma/withdirectory-content-key
Closed

sipsma wants to merge 1 commit into
mainfrom
sipsma/withdirectory-content-key

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

A directory put together from two content-keyed directories with withDirectory loses its content key. Whatever is read from it afterwards, a subdirectory or a filter, is then keyed by the recipe of every part. So a change to any file in either part re-keys every read from the result, even of files that didn't change.

This matters for dagger/go, the Go module. Every build there reads the workspace from the client twice, one read after the other. The first read is for the package scan; the second reads the build's source, with the patterns the scan found. Both use patterns like **/*.go, so each read walks the client's whole tree. A dagger/go change reads the Go files once, for both, then reads again only what is left, such as embedded files, and merges the two reads with withDirectory. The build keys each package's compile on that package's own files, read from the merged source with directory and filter. Without this change, every edit would rerun every package's compile.

What it does

Directory.withDirectory now gives its result a content digest when the parent and the source both have one. The digest is made from both digests and the copy's arguments: path, owner, permissions, include, exclude and gitignore. An unfiltered Dockerfile COPY is already keyed this way, in __withDirectoryDockerfileCompat. Since the digest comes from the inputs, the copy stays lazy. When either side has no content digest, nothing changes.

The first directory or filter read from such a directory evaluates the copy and content-hashes what it selects. That's what happens today under a host directory.

Numbers

These are builds of dagger's CLI with dagger/go: this change plus the dagger/go change, against neither. 4 runs each, alternating, on one c8gd.4xlarge, with a fresh engine per run.

Build Without With Difference (95% interval)
Edit inside dagger 7.71 s 7.39 s −0.32 s (−0.52 to −0.11)
Edit of the CLI's own package 6.04 s 5.78 s −0.26 s (−0.42 to −0.12)
Unchanged rebuild, after an edit 0.92 s 0.80 s −0.12 s (−0.18 to −0.05)
Cold build 24.68 s 24.55 s not resolved

On the build call's critical path, reads from the client fall from 0.33 s to 0.01 s on an edit. What it costs instead is content-hashing the merged source on each edit, 0.22 s on the path. A host directory gets its hashes from the sync; the merged directory has to hash its files again.

On its own, this change doesn't change any dagger/go build. It's what the dagger/go change needs.

Tests

TestDirectory/TestWithDirectoryContentKeyed: a command over a subdirectory of a merged directory stays cached when a file elsewhere in the directory changes. It fails without the change.

A copy of one content-keyed directory into another now gets a content
digest made from both directories' digests and the copy's arguments, as
a Dockerfile COPY already does. A directory put together from
content-keyed parts, such as two workspace reads, then stays
content-keyed, and what is read from it afterwards, such as a
subdirectory, is keyed by what it holds rather than by every part it was
made from. The digest comes from the inputs' digests, so the copy stays
lazy.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Its commits moved into #15007, which now combines the engine's pending performance changes, to save CI runs.

@sipsma sipsma closed this Nov 2, 2026
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