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 package can clear the document of a single crates #9841

Closed
wants to merge 12 commits into from
7 changes: 7 additions & 0 deletions src/cargo/core/compiler/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub struct Layout {
examples: PathBuf,
/// The directory for rustdoc output: `$root/doc`
doc: PathBuf,
/// The directory for rustdoc output: `$root/doc/src`
src: PathBuf,
heisen-li marked this conversation as resolved.
Show resolved Hide resolved
/// The directory for temporary data of integration tests and benches: `$dest/tmp`
tmp: PathBuf,
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
Expand Down Expand Up @@ -172,6 +174,7 @@ impl Layout {
fingerprint: dest.join(".fingerprint"),
examples: dest.join("examples"),
doc: root.join("doc"),
src: root.join("doc/src"),
tmp: root.join("tmp"),
root,
dest,
Expand Down Expand Up @@ -206,6 +209,10 @@ impl Layout {
pub fn doc(&self) -> &Path {
&self.doc
}
/// Fetch the doc/src path.
pub fn src(&self) -> &Path {
&self.src
}
/// Fetch the root path (`/…/target`).
pub fn root(&self) -> &Path {
&self.root
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
for pkg in packages {
let pkg_dir = format!("{}-*", pkg.name());

// Clean fingerprints.
for (_, layout) in &layouts_with_host {
// Clean fingerprints.
rm_rf_glob(&layout.fingerprint().join(&pkg_dir), config)?;
// Clean target/doc.
rm_rf(&layout.doc(), config)?;
// Clean target/doc/src.
rm_rf(&layout.src(), config)?;
heisen-li marked this conversation as resolved.
Show resolved Hide resolved
}

for target in pkg.targets() {
Expand Down
29 changes: 29 additions & 0 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,32 @@ fn clean_spec_reserved() {
)
.run();
}

#[cargo_test]
fn clean_spec_doc() {
heisen-li marked this conversation as resolved.
Show resolved Hide resolved
// `clean -p package` make target/doc/package clear
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("doc").run();

let doc_path = &p.build_dir().join("doc");
assert!(doc_path.is_dir());

p.cargo("clean -p foo").run();

assert!(!doc_path.join("foo").is_dir());
assert!(!doc_path.join("src").join("foo").is_dir());

assert!(p.build_dir().is_dir());
}