Fix cargo add overwriting symlinked Cargo.toml files
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:
- Creates a symlinked Cargo.toml file
- Runs
cargo addto add a dependency - 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.
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
@RaghavenderSingh you requested a review but it doesn't look like my review comments have been addressed.