Skip to content
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,
Comment thread
heisen-li marked this conversation as resolved.
Outdated
/// 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)?;
Comment thread
heisen-li marked this conversation as resolved.
Outdated
}

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() {
Comment thread
heisen-li marked this conversation as 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());
}