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 -p does not clean if path contains glob characters #10068

Closed
jonhoo opened this issue Nov 11, 2021 · 0 comments · Fixed by #10072
Closed

cargo clean -p does not clean if path contains glob characters #10068

jonhoo opened this issue Nov 11, 2021 · 0 comments · Fixed by #10072
Labels
C-bug Category: bug Command-clean E-easy Experience: Easy

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Nov 11, 2021

Problem

cargo clean -p fails to delete a number of files from target/ if the path to target contains glob characters. This is due to the use of rm_rf_glob used for cleaning, which does not escape glob characters in the path before adding the pattern:

fn rm_rf_glob(pattern: &Path, config: &Config) -> CargoResult<()> {
// TODO: Display utf8 warning to user? Or switch to globset?
let pattern = pattern
.to_str()
.ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?;
for path in glob::glob(pattern)? {
rm_rf(&path?, config)?;

Here for example:

let pkg_dir = format!("{}-*", pkg.name());
// Clean fingerprints.
for (_, layout) in &layouts_with_host {
rm_rf_glob(&layout.fingerprint().join(&pkg_dir), config)?;
}

Steps

#!/bin/bash
rm -rf glob-in-rm
mkdir glob-in-rm
cd glob-in-rm

for d in '[hello]' 'hello'; do
	mkdir "$d"
	pushd "$d"
	cargo new foo
	cd foo
	cargo check
	cargo clean -p foo
	popd
done

diff -qr '[hello]/foo/target' 'hello/foo/target'
Only in [hello]/foo/target/debug/.fingerprint: foo-8ca57bdc9f429d2a
Only in [hello]/foo/target/debug/deps: foo-8ca57bdc9f429d2a.d
Only in [hello]/foo/target/debug/deps: libfoo-8ca57bdc9f429d2a.rmeta
Only in [hello]/foo/target/debug/incremental: foo-1qp1vfygk93x

Possible Solution(s)

Escape any non-pattern part of the string passed to glob with glob::Pattern::escape.

Notes

No response

Version

cargo 1.56.0 (4ed5d137b 2021-10-04)
release: 1.56.0
commit-hash: 4ed5d137baff5eccf1bae5a7b2ae4b57efad4a7d
commit-date: 2021-10-04
@jonhoo jonhoo added the C-bug Category: bug label Nov 11, 2021
@Eh2406 Eh2406 added E-easy Experience: Easy E-help-wanted labels Nov 11, 2021
@henrifrancois henrifrancois removed their assignment Nov 14, 2021
russweas added a commit to russweas/cargo that referenced this issue Nov 16, 2021
Implement glob escaping for clean -p

Add pattern escape for glob matching cargo clean files

Implement correct solution for rust-lang#10068

Removed superfluous formatting changes

Update rm_rf_glob()'s error handling

Remove dir_glob reference for non-glob function

Added test

Satisfy clippy

Add MSVC support for test
@bors bors closed this as completed in adf601d Nov 17, 2021
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 E-easy Experience: Easy
Projects
None yet
4 participants