Repository navigation
core: merge changesets by content, not patches - #13773
Conversation
Snapshots share file contents via hardlinks, so a file's ctime, inode and device can change out from under a merge workspace when a concurrent snapshot links the same inode, even though the content is untouched. git then sees hundreds of stat-dirty entries, and `git merge` fails during its internal `git stash create`: that call reports changes from a stat-only check, finds none after refreshing the index, and exits non-zero with no output, leaving merge to die with the bewildering "fatal: stash failed". Configure the ephemeral merge repositories with core.checkStat=minimal and core.trustctime=false so git compares only mtime and size, which is all we can trust across snapshot mounts. Signed-off-by: Alex Suraci <[email protected]>
os.Link reports failures as *os.LinkError wrapping the errno, so comparing the error directly against EXDEV/EMLINK never matched and the content-copy fallback was unreachable; cross-device link attempts failed the whole copy. Nobody hit this while every copy targeted the snapshot filesystem itself, but copying into a mounted view (a different superblock) trips it immediately. Signed-off-by: Alex Suraci <[email protected]>
Changeset.withChangeset[s] built each merge branch by git-applying the changeset's patch onto a union of every input's before directory. When the befores diverge -- e.g. generate staging a shared dependency's codegen into each dependent's before -- the union already contains one side's post-state, so insertion hunks double-apply at an offset and modification hunks fail outright, forcing callers to manufacture a common base. Build each branch from the changeset's file-level content instead: overlay the materialized Before.diff(After) directory onto the base (the same content Directory.withChanges applies), delete the removed paths, recreate empty added directories, and commit. Content the base already contains becomes a no-op, so inputs no longer need a common base, while genuinely common-base inputs produce the same branches the patches did. Same-file edits still merge textually through git's ordinary 3-way merge, and the conflict flows are untouched. The overlay writes through the mounted view rather than the snapshot upperdir since git has to see the files it stages, and skips source hardlinks since they always cross devices there. Signed-off-by: Alex Suraci <[email protected]>
Snapshot contents carry normalized timestamps and layercopy preserves them, so overlaying a same-size edit leaves a file's mtime and size identical to its index entry. With core.checkStat=minimal the branch's git add skipped re-hashing such files and silently dropped the edit from the commit; the worktree still carried the content, so the last-applied changeset's version leaked into the merge result and modify/modify conflicts merged cleanly, clobbering one side. The patch flow never hit this because git apply wrote files with fresh mtimes. Bump every applied path's mtime after the overlay. Also add TestSameFileRegionMerge, which pins that changesets editing different regions of the same file all survive two-way and octopus merges, and that overlapping edits still conflict -- the case that caught this. Signed-off-by: Alex Suraci <[email protected]>
The fix itself SGTM, but curious what situation you were hitting it in? The goal of layercopy is to detect bind mounts, overlay mounts, etc. and then find the underlying "real" path so that hardlinks can be made. This is supposed to be always possible in theory since the engine's state is just |
|
@sipsma The way this PR uses Normally we're copying into the overlays upper dir which resides in The fix was to copy into the overlay mount instead, which will write through to the upper dir and not leave But that led to errors trying to hard-link between the overlay mount and After all that it looks like 🤖 just disabled hard links altogether. Not ideal, but we're only copying the delta each time and not entire directory trees so it's probably not too bad. LLM explanations |
|
@vito I see, yeah it's not as trivial in that case if you need to run git before and after. And also not a regression for this case so definitely not a blocker. I think claude is somewhat underestimating the cost of repeated copies in the case of big shared engines that are already disk bandwidth starved, but I otherwise agree with it in this case 🦾 |
Problem
Changeset.withChangeset[s]built each merge branch bygit applying the changeset's patch onto a union of every input's before directory. That silently assumes all inputs share a common base. When the befores diverge — e.g. generate staging a shared dependency's codegen into each dependent's workspace — the union base already contains one side's post-state, so insertion hunks re-apply at an offset (generated code is repetitive enough forgit applyto match context past an existing copy, duplicating code) and modification hunks fail outright witherror: patch failed. This is what forced the go SDK's rebase workaround (dagger/go-sdk@89edf83): manufacturing a common base at the callsite, in the wrong layer. A changeset is its file-level diff —withChangesetshouldn't require callers to curate bases.Complements #13771, which made the staging-side accumulation merge-free; this octopus merge was the last patch-based consumer.
Fix
core: ignore ctime when merging changesets — snapshots share file contents via hardlinks, so a file's ctime, inode, and device can change under a live merge workspace without the content changing. Git sees stat-dirty entries and
git merge's internalgit stash createfails with a bewilderingfatal: stash failed. The ephemeral merge repos now run withcore.checkStat=minimalandcore.trustctime=false.fix(layercopy): unwrap hardlink fallback errors —
os.Linkwraps errnos in*os.LinkError, so the EXDEV/EMLINK comparisons never matched and the content-copy fallback was dead code. Nobody hit this while every copy targeted the snapshot filesystem itself, but copying into a mounted view (a different superblock) trips it immediately.fix(core): merge changesets by content — each merge branch is now built from the changeset's file-level content instead of a patch: materialize
Before.diff(After)(the same contentDirectory.withChangesandChangeset.exportalready apply), delete the removed paths, overlay the diff onto the checked-out base, recreate empty added directories, and commit. Content the base already contains becomes a no-op, so inputs no longer need a common base, while genuinely common-base inputs produce the same branches the patches did. The merge step itself is unchanged: git re-derives line-level hunks against the merge base, so same-file edits in different regions still compose textually and overlapping edits still conflict per theonConflictstrategy.asPatchstays (API-visible); the patch-apply plumbing is deleted.Two constraints baked into the implementation: the copier writes through the mounted view rather than the snapshot upperdir (git must see the files it stages; mutating an overlay's layers behind a live mount leaves the view incoherent), and source hardlinks are disabled (they always cross superblocks into the view).
fix(core): touch merge-applied paths so git sees them — snapshot contents carry normalized timestamps and the copier preserves them, so a same-size edit leaves a file's mtime and size identical to its index entry. With
core.checkStat=minimal,git addskipped re-hashing such files and silently dropped the edit from the branch commit; the worktree still carried the content, so the last-applied changeset's version leaked into the merge result and modify/modify conflicts "merged" cleanly, clobbering one side. Every applied path's mtime is bumped after the overlay. AddsTestSameFileRegionMerge, pinning that same-file different-region edits compose in both two-way and octopus merges and that overlapping edits still conflict — the case that caught this.Compatibility
No API changes. For changesets sharing a common base, branch content is identical to the patch-applied result, so existing merges behave the same. Empty added directories now survive merges (patches couldn't represent them). Once this lands, the go SDK's rebase workaround can be reverted: generate output against the pre-workaround go-sdk is byte-identical to output with it.
Verification (dagger repo, dev engine)
dagger generate dagger-go-sdkagainst the pre-workaround go-sdk (dagger/go-sdk@0c04581): applies cleanly where it previously duplicatedAsVolumebindings or failed hunks, output is byte-identical to the known-good regen, every toolchain compiles, and rerunning is a no-op.TestChangesetintegration suite passes, including the newTestSameFileRegionMergeand allTestChangesetMerge/TestWithChangesetsconflict flows.go test ./core/schema ./core/workspace ./util/layercopypass; polyfill's e2e module-source checks (including thegenerate-merge-checkdisjointness pin) pass 4/4.🤖 Generated with Claude Code