Skip to content

fix(tools): keep file mode and symlinks when writing atomically - #488

Merged
mrnasil merged 2 commits into
mainfrom
fix/atomic-write-keeps-mode
Nov 25, 2026
Merged

mrnasil merged 2 commits into
mainfrom
fix/atomic-write-keeps-mode

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

write_atomic renamed a fresh tempfile over the target. The tempfile is created 0600, and the rename replaces whatever the path was. So every edit made through write, edit, apply_patch, ast_edit and the formatter had three side effects:

  • Scripts lost their execute bit, and 0644 files became 0600.
  • New files were created 0600 instead of the usual umask mode.
  • A symlink was replaced by a regular file, and the file it pointed to was left unchanged.

The fix:

  • A symlink is resolved first, so the write replaces the link's target and the link stays.
  • An existing file's permissions are copied onto the tempfile.
  • New files are created with 0666, filtered by the umask.
  • A dangling symlink is now an error instead of being silently replaced.
  • Windows is unchanged: it has no mode bits to carry, and a read-only tempfile could not be cleaned up.

Tests, Unix only: mode 0751 survives a write; a new file gets the same mode as fs::write gives; writing through a link updates the target and keeps the link; a dangling link and a non-directory parent are reported as errors. Run on Linux (WSL), where atomic.rs has 100% line coverage.

🤖 Generated with Claude Code

write_atomic renamed a fresh tempfile over the target. The tempfile is
created 0600 and the rename replaces whatever the path was, so every
edit made through write/edit/apply_patch/ast_edit/format:

- dropped the execute bit from scripts and narrowed 0644 files to 0600;
- created new files 0600 instead of the usual umask mode;
- turned a symlink into a regular file, leaving the real file untouched.

Follow a symlink to its target before writing, copy an existing file's
permissions onto the tempfile, and create new files with 0666 filtered
by the umask. Windows keeps the old path: it has no mode bits to carry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 14:53
@mrnasil
mrnasil merged commit 80746aa into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/atomic-write-keeps-mode branch November 25, 2026 20:59
@mrnasil mrnasil mentioned this pull request Nov 26, 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