Skip to content

Fix relative symlink handling in write_atomic - #17362

Open
cuishuang wants to merge 1 commit into
rust-lang:masterfrom
cuishuang:master
Open

Fix relative symlink handling in write_atomic#17362
cuishuang wants to merge 1 commit into
rust-lang:masterfrom
cuishuang:master

Conversation

@cuishuang

Copy link
Copy Markdown
Contributor

What does this PR try to resolve?

Fixes #17361.

cargo_util::paths::write_atomic follows symlinks before performing an atomic write. However, fs::read_link returns a relative target exactly as stored in the symlink.

The returned target was previously used directly, causing subsequent filesystem operations to interpret it relative to Cargo's process working directory. This could make the write fail or overwrite an unrelated file.

This change resolves the target against the symlink's parent directory:

resolved_path = path.parent().unwrap().join(target);

Path::join preserves absolute targets, so the existing behavior for absolute symlinks is unchanged.

How to test and review this PR?

A new unit test creates a symlink with a relative target in a directory different from the process working directory. The test verifies that:

  • the actual target receives the updated contents;
  • the symlink remains a symlink;
  • the stored relative target remains unchanged.

I ran:

cargo +1.97.1 test -p cargo-util
cargo fmt --all -- --check
image

All 12 unit tests and the cargo-util doc-test passed.

Signed-off-by: cuishuang <imcusg@gmail.com>
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 14, 2026
@rustbot

rustbot commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

r? @weihanglo

rustbot has assigned @weihanglo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @epage, @weihanglo
  • @epage, @weihanglo expanded to epage, weihanglo
  • Random selection from epage, weihanglo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

write_atomic interprets relative symlink targets against the process working directory

3 participants