Skip to content

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

@jonhoo

Description

@jonhoo

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions