Skip to content

cargo clean -p package can clear the document of a single crates #9841

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

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`
doc_src: PathBuf,
/// 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"),
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 doc_src(&self) -> &Path {
&self.doc_src
}
/// Fetch the root path (`/…/target`).
pub fn root(&self) -> &Path {
&self.root
Expand Down
18 changes: 17 additions & 1 deletion src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,24 @@ 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 entries of workspace members under target/doc.
for _ in pkg.targets().iter().filter(|t| t.documented()) {
if ws.is_member(pkg) {
rm_rf(&layout.doc().join(pkg.name()), config)?;
rm_rf(&layout.doc_src().join(pkg.name()), config)?;
}
}

// Clean lib of non-members under target/doc.
for _ in pkg.targets().iter().find(|t| t.is_lib()) {
if ws.is_member(pkg) {
rm_rf(&layout.doc().join(pkg.name()), config)?;
rm_rf(&layout.doc_src().join(pkg.name()), config)?;
}
}

// Clean fingerprints.
let dir = escape_glob_path(layout.fingerprint())?;
rm_rf_glob(&Path::new(&dir).join(&pkg_dir), config)?;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,44 @@ fn clean_spec_reserved() {
)
.run();
}

#[cargo_test]
fn clean_spec_doc() {
// `clean -p package` make target/doc/package clear
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []

[dependencies.bar]
path = "bar"
"#,
)
.file("src/lib.rs", "extern crate bar; pub fn foo() {}")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("bar/src/lib.rs", "pub fn bar() {}")
.build();

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

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

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

assert!(!foo_doc_path.is_dir());
assert!(bar_doc_path.is_dir());

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

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