Skip to content

cargo clean with target as a symlink fails #12473

Closed
@mathstuf

Description

@mathstuf

Problem

When target is a symlink, cargo clean removes it and then tries to remove files from underneath it:

% cargo clean
    Removing /home/boeckb/code/depot/group-kitware/proj-kwrobot/ghostflow-director/src/target
error: failed to remove build artifact

Caused by:
  failed to remove file `/home/boeckb/code/depot/group-kitware/proj-kwrobot/ghostflow-director/src/target/.rustc_info.json`

Caused by:
  No such file or directory (os error 2)

Prior to 1.62, the symlink was removed and then nothing else was attempted. If it is a symlink, it should be removed last so that any other file deletions can still work.

Steps

% mkdir cargo-clean
% cd cargo-clean
% cargo init
     Created binary (application) package
% mkdir build
% ln -s build target
% ls -l
total 4
drwxr-x---. 2 user user   6 Aug  9 20:20 build
-rw-r-----. 1 user user 180 Aug  9 20:20 Cargo.toml
drwxr-x---. 2 user user  21 Aug  9 20:20 src
lrwxrwxrwx. 1 user user   5 Aug  9 20:20 target -> build
% cargo build
   Compiling cargo-clean v0.1.0 (/home/boeckb/misc/code/sb/cargo-clean)
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
% cargo clean -v
    Removing /home/boeckb/misc/code/sb/cargo-clean/target
error: failed to remove build artifact

Caused by:
  failed to remove file `/home/boeckb/misc/code/sb/cargo-clean/target/.rustc_info.json`

Caused by:
  No such file or directory (os error 2)

Possible Solution(s)

No response

Notes

See also #7510 and #7527.

Version

% cargo version --verbose
cargo 1.71.0
release: 1.71.0
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.1 vendored)
libcurl: 7.85.0 (sys:0.4.61+curl-8.0.1 system ssl:OpenSSL/3.0.9)
os: Fedora 37.0.0 [64-bit]

Also tested with rustup from 1.53 to stable: 1.61 is fine and 1.62 has this behavior.

Metadata

Metadata

Assignees

Labels

C-bugCategory: bugCommand-cleanS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions