cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Fix cargo add overwriting symlinked Cargo.toml files

Open RaghavenderSingh opened this issue 10 months ago • 2 comments

What does this PR try to resolve?

This PR fixes a bug where cargo add breaks symlinks to Cargo.toml files. Currently, when Cargo.toml is a symlink and cargo add is used to add a dependency, the symlink is replaced with a regular file, breaking the link to the original target file.

This issue was reported in #15241 where a user who relies on symlinked Cargo.toml files found that cargo add breaks their workflow.

Fixes #15241

How should we test and review this PR?

I've modified LocalManifest::write() to check if the path is a symlink, and if so, follow it to get the actual target path. This ensures we write to the actual file rather than replacing the symlink.

I've also added a test in tests/testsuite/cargo_add/symlink.rs that:

  1. Creates a symlinked Cargo.toml file
  2. Runs cargo add to add a dependency
  3. Verifies the symlink is preserved and the dependency is added to the target file

I've manually tested this fix and confirmed it works correctly.

RaghavenderSingh avatar Mar 08 '25 14:03 RaghavenderSingh

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

rustbot avatar Mar 08 '25 14:03 rustbot

@RaghavenderSingh you requested a review but it doesn't look like my review comments have been addressed.

epage avatar Mar 24 '25 14:03 epage