Skip to content

refactor(project): Drop resurrected resources from delta merge - #1465

Merged
RandomByte merged 2 commits into
mainfrom
fix/delta-merge-resurrected-resources
Jul 22, 2026
Merged

refactor(project): Drop resurrected resources from delta merge#1465
RandomByte merged 2 commits into
mainfrom
fix/delta-merge-resurrected-resources

Conversation

@RandomByte

Copy link
Copy Markdown
Member

Problem

When a task runs as a delta, its output is merged with the previous stage's cached output: any previously-written resource whose path the delta task did not re-emit is copied forward. The membership check only asked whether the delta re-emitted the path:

if (!writtenResourcePaths.includes(res.getOriginalPath())) {
    await stageWriter.write(res);
}

A path can be flagged changed yet not re-emitted, because its source is gone or now excluded. The previous stage still holds a copy of that path, so the merge writes it forward and resurrects content that no longer belongs in the output.

Fix

Distinguish "not re-emitted because unchanged" from "not re-emitted because it should no longer exist" by consulting changedProjectResourcePaths:

  • Path re-emitted by the delta -> skip (delta owns it).
  • Path flagged changed but not written back -> drop the stale copy from the merge.
  • Otherwise -> copy forward as before.

The membership check uses a Set (writtenResourcePathSet); the array is retained for the ordered downstream uses (recordStageCache, verbose counts). The perf log gained a dropped count alongside previous and merged.

Tests

New cases in test/lib/build/cache/ProjectBuildCache.js cover the drop path (changed-but-not-written) and confirm unchanged resources still merge forward.

On a delta signature transition whose changedProjectResourcePaths lists a path
the delta task did not re-emit, the merge loop in recordTaskResult replayed the
previous stage's copy into the new stage writer. When a source file is deleted
between builds, the delta task emits nothing for it and the merge resurrects
the pre-deletion output, so downstream readers keep serving the file after its
source is gone. Affects every differential task (replaceCopyright,
replaceVersion, replaceBuildtime, minify).

Filter the merge by cacheInfo.changedProjectResourcePaths: if a path was
flagged changed and the task did not write it, drop the stale copy instead of
replaying it. Paths not flagged changed merge as before. Track the dropped
count alongside the merged count in the perf log.

Add four fail-then-succeed tests covering invariants across a
failed-attempt/retry boundary on one ProjectBuildCache instance:

1. #writtenResultResourcePaths accumulates across failed attempts, because
   allTasksCompleted (which clears it) never runs on a thrown build. The worst
   outcome is redundant I/O on the retry, not wrong output: the retry re-hashes
   each leaked path against its fresh reader, so unchanged content yields the
   same signature and changed content invalidates. Clearing the set cleanly
   needs a separate fail/abort hook, since #initSourceIndex seeds the array
   with delta paths on the first build after loading persistent cache.
2. #currentStageSignatures reflect only the retry's stages.
3. The delta merge does not resurrect a deleted-source resource.
4. #frozenSourceReader is nulled by the retry's initStages call before task
   inputs are computed.
The delta merge in recordTaskResult scanned writtenResourcePaths with
Array.includes once per previous-stage resource: O(n*m) string comparisons on
every incremental serve rebuild, scaling with a project's total written-resource
count. Build a Set for the membership check; the array stays for the ordered
downstream uses.
@RandomByte
RandomByte marked this pull request as ready for review July 21, 2026 19:15
@RandomByte
RandomByte requested a review from a team July 21, 2026 19:15
@RandomByte
RandomByte merged commit ab6740c into main Jul 22, 2026
60 checks passed
@RandomByte
RandomByte deleted the fix/delta-merge-resurrected-resources branch July 22, 2026 08:13
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.

2 participants