fix(filesystem): harden file replacement with EPERM fallback#3610
Open
Chelebii wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix(filesystem): harden file replacement with EPERM fallback#3610Chelebii wants to merge 2 commits intomodelcontextprotocol:mainfrom
Chelebii wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Extract replaceFileFromTemp helper that falls back to fs.cp when fs.rename fails with EPERM (Windows locked files). Temp file cleanup is best-effort so a successful write is never masked by a cleanup failure (e.g. antivirus holding the temp file). Changes: - Extract replaceFileFromTemp from inline rename logic in writeFileContent and applyFileEdits - Add EPERM fallback: rename -> cp + best-effort unlink - Document FILE_SHARE_DELETE limitation and non-atomic fallback in JSDoc - Add tests for EPERM fallback through both writeFileContent and applyFileEdits - Add tests for successful writes despite temp cleanup failure Fixes modelcontextprotocol#3430
Contributor
Author
|
Follow-up: all checks are now passing after the lockfile refresh commit (c22fad2). The earlier Python failure was caused by src/fetch/uv.lock being out of sync with src/fetch/pyproject.toml, and that is now resolved.\n\nAt this point the PR looks blocked only on maintainer review / merge, not on CI. |
Contributor
Author
|
@codex review focus on bugs, edge cases and missing tests |
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.
Problem
When \s.rename\ fails with \EPERM\ on Windows (e.g. VS Code or antivirus holding a file lock), both \writeFileContent\ and \�pplyFileEdits\ propagate the error even though the write could succeed via a fallback path. Additionally, if the fallback copy succeeds but temp file cleanup fails (antivirus briefly locks the temp), the user sees an error despite the target file being written correctly.
Ref: #3430, #3199
What changed
eplaceFileFromTemp\ helper from the inline rename logic in \writeFileContent\ and \�pplyFileEdits, eliminating code duplication.
Validation
\
npx vitest run
✓ tests/lib.test.ts (49 tests)
✓ tests/path-utils.test.ts (29 tests)
✓ tests/roots-utils.test.ts (3 tests)
✓ tests/path-validation.test.ts (53 tests)
✓ tests/directory-tree.test.ts (7 tests)
✓ tests/structured-content.test.ts (5 tests)
✓ tests/startup-validation.test.ts (4 tests)
Test Files 7 passed (7)
Tests 150 passed (150)
\\
\ sc --noEmit\ — 0 errors.
Checklist (from #3430)
eplaceFileFromTemp)
Fixes #3430