Skip to content

fix(fs): preserve symlinks in atomicWrite and writeFileAtomicDurable#894

Open
bj456736 wants to merge 1 commit into
MoonshotAI:mainfrom
bj456736:fix/atomic-write-preserve-symlink
Open

fix(fs): preserve symlinks in atomicWrite and writeFileAtomicDurable#894
bj456736 wants to merge 1 commit into
MoonshotAI:mainfrom
bj456736:fix/atomic-write-preserve-symlink

Conversation

@bj456736

Copy link
Copy Markdown
Contributor

Summary

Fixes #751 — when atomicWrite() or writeFileAtomicDurable() wrote to a symlink path, the rename() call replaced the symlink itself with a regular file. This broke setups where config.toml was managed as a symlink (e.g., to iCloud Drive or a dotfiles repo).

Root Cause

Both functions created a temp file next to the given path and then called rename(tmp, path). On POSIX, rename replaces the directory entry, so when path was a symlink, the symlink got overwritten.

Solution

Added a resolveSymlinkTarget() helper that uses lstat + realpath to detect symlinks and resolve them to their underlying target before creating the temp file. Both atomicWrite and writeFileAtomicDurable now write to the resolved target path, leaving the symlink intact.

Changes

  • packages/agent-core/src/utils/fs.ts: added resolveSymlinkTarget helper, applied to both atomic write functions
  • packages/agent-core/test/utils/fs.test.ts: added 6 tests covering:
    • basic write to new file
    • overwrite existing file
    • absolute symlink preservation
    • relative symlink preservation
    • durable write with symlink

Testing

All 6 new tests pass. Existing config and per-id-json-store tests also pass (52 tests).

When atomicWrite() or writeFileAtomicDurable() wrote to a path that
was a symlink, the rename() call replaced the symlink itself with a
regular file. This broke setups where config.toml was a symlink
(e.g., to iCloud Drive or a dotfiles repo).

Now both functions resolve symlinks via realpath() before creating
the temp file and performing the rename, so the symlink remains
intact and only the target file's content is updated.

Fixes MoonshotAI#751
@changeset-bot

changeset-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6fc9d6f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 18, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6fc9d6f
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6fc9d6f

commit: 6fc9d6f

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6fc9d6f8e6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

try {
const stats = await lstat(filePath);
if (stats.isSymbolicLink()) {
return await realpath(filePath);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve dangling symlinks on first write

When filePath is a symlink whose target does not yet exist, lstat succeeds but realpath(filePath) throws ENOENT; the catch then returns the original symlink path, so the later rename(tmpPath, targetPath) replaces the symlink instead of creating/updating its target. This still breaks first-run config/dotfile symlinks to not-yet-created files—the preservation path this change is meant to fix—so resolve the link text rather than falling back to filePath on this ENOENT.

Useful? React with 👍 / 👎.

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.

Bug: atomicWrite replaces symlink with regular file, breaking config.toml sync

1 participant