Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cargo add overwriting symlinked Cargo.toml files #15281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RaghavenderSingh
Copy link

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.

@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2025

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 rustbot added A-manifest Area: Cargo.toml issues S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 8, 2025
@RaghavenderSingh RaghavenderSingh force-pushed the fix-cargo-add-symlinks branch from a45d668 to 53fdb96 Compare March 8, 2025 19:25
@RaghavenderSingh RaghavenderSingh force-pushed the fix-cargo-add-symlinks branch 3 times, most recently from 465de3c to 1708e12 Compare March 9, 2025 20:04
Copy link
Contributor

Choose a reason for hiding this comment

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

This commit does not change the test. Does the previous commit fail cargo test or is it testing the wrong thing? Each commit should be atomic which includes passing tests. When we ask for a test commit to be split out first, it is to show the current behavior. The commit with the fix will also change the test and that diff shows how behavior changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this is still not addressed.

Copy link
Contributor

Choose a reason for hiding this comment

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

From #15281 (comment)

Hi @epage, I implemented symlink preservation in write_atomic(). preview

The test now shows how cargo add handles symlinks correctly. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

So the test fails for the first commit and then passes for the second commit? In that case, these commits are not atomic, as in they do not stand on their own with every commit passing tests.

We ask for the splitting of test/fix commits so that the second commit shows how the behavior changes by how the test body changes to ensure it still passes. This is a clear way of communicating the way the behavior changed.

@epage
Copy link
Contributor

epage commented Mar 24, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manifest Area: Cargo.toml issues 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.

cargo add overwrites a symlink Cargo.toml
4 participants