fix: fall back to copy+unlink when rename fails with EXDEV - #241
Open
dyk1454683243-sudo wants to merge 1 commit into
Open
dyk1454683243-sudo wants to merge 1 commit into
dyk1454683243-sudo wants to merge 1 commit into
Conversation
When fs.rename fails with EXDEV (Windows AppX virtualized paths, or a true cross-device move), write the temp file with copy+unlink so writeFile / writeFileSync can still succeed. Rename remains the default atomic path. Other rename errors are unchanged. Fixes npm#71 Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
fs.rename/fs.renameSyncfails withEXDEV(cross-device link not permitted),writeFileandwriteFileSyncnow fall back tocopyFile+unlinkso the write can still succeed.This is the failure mode in #71: Windows AppX virtualization can make a temp file and destination look like they are in the same directory while the rename is treated as a cross-device move. The same
EXDEValso happens on true cross-device dest/tmp pairs (e.g. separate volumes).Rename remains the default path and stays atomic. The copy fallback is not atomic; that is called out in the README.
Other rename errors (
EPERM,ENORENAME, …) still propagate. A failedunlinkof the temp file after a successful copy does not fail the write.Test plan
EXDEVonrename/renameSyncwith a real filesystem copy (test/exdev.js): dest content is written, temp file is removed, existing dest is replacedEXDEVrename errors still fail and do not copycopyFilefailures afterEXDEVstill propagateunlinkfails after a successful copynpx tapon Node 22.22.2: all tests pass,lib/index.jsat 100% coveragenpm run eslintpassestemplate-oss-checkonly fails locally on this fork because it wantspackage.json#repository.urlrewritten to the fork; that check is unchanged onnpm/write-file-atomicCIFixes #71