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

Cargo clean removes target directory, even when it is a softlink #13670

Closed
SeseMueller opened this issue Mar 30, 2024 · 3 comments
Closed

Cargo clean removes target directory, even when it is a softlink #13670

SeseMueller opened this issue Mar 30, 2024 · 3 comments
Labels
C-bug Category: bug Command-clean S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@SeseMueller
Copy link

Problem

Running cargo clean deletes the target directory, which is usually fine, because it is regenerated on the next cargo build.
However, I used a symlink for my target directory because I want my code to be synced across devices, but not bloat it with the build artifacts.

cargo clean simply removed the symlink.
In my opinion, it should either give a warning or follow the symlink; not fail silently.

Steps

  1. Create a new empty project
  2. Create a sylink for the target directory to somewhere else
  3. Run cargo build
  4. Run cargo clean

The symlink is gone and on the next cargo build, it will be a regular directory instead.

Possible Solution(s)

Always following symlinks might not be a good idea, so it should probably warn the user and abort. (To work proberly, that would require a new flag for cargo clean though)

Notes

No response

Version

cargo 1.79.0-nightly (d438c80c4 2024-03-19)
release: 1.79.0-nightly
commit-hash: d438c80c45c24be676ef5867edc79d0a14910efe
commit-date: 2024-03-19
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.4.0 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.4.1 [64-bit]
@SeseMueller SeseMueller added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Mar 30, 2024
@weihanglo
Copy link
Member

See #7510 (comment) and dicussions in the closed PR, which consider this as an expected behavior. See also alternaive designs like rust-lang/rfcs#3371 for templating target directories.

Besides alternatives, is there any specific reason you can't set CARGO_TARGET_DIR to the actual directory instead of using a symlink?

@weihanglo weihanglo added Command-clean S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. and removed S-triage Status: This issue is waiting on initial triage. labels Mar 31, 2024
@SeseMueller
Copy link
Author

See #7510 (comment) and dicussions in the closed PR, which consider this as an expected behavior. See also alternaive designs like rust-lang/rfcs#3371 for templating target directories.

Ok, yeah. That makes sense. But given that cargo clean is mostly a manual command, maybe it should warn the user, but proceed as normal?

Besides alternatives, is there any specific reason you can't set CARGO_TARGET_DIR to the actual directory instead of using a symlink?

I didn't realize that setting the target-dir would propagate through modules in my context, that did in fact work. Thanks for pointing that out.

@weihanglo
Copy link
Member

From the manual:

With no options, cargo clean will delete the entire target directory.

Basically cargo clean is an rm -rf taking effect like immediately, so when seeing a warning it's too late. There is a --dry-run flag. When with doubt, users can dry-run a clean before the actual clean, such as cargo clean --verbose --dry-run. Interactive console is also an option, but due to compatibility reasons, that won't become the default behavior of cargo clean in my opinion.

Going to close this in favor of rust-lang/rfcs#3371 and CARGO_TARGET_DIR. Thank you for the report :)

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-clean S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants