-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: rebuild rustdoc json for different versions of a same crate #16773
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,7 +868,16 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu | |
| add_cap_lints(bcx, unit, &mut rustdoc); | ||
|
|
||
| unit.kind.add_target_arg(&mut rustdoc); | ||
| let doc_dir = build_runner.files().output_dir(unit); | ||
|
|
||
| let doc_dir = if build_runner.bcx.build_config.intent.wants_doc_json_output() { | ||
| // Always use new layout for '--output-format=json'. | ||
| // In fix for https://github.com/rust-lang/cargo/issues/16291 | ||
|
|
||
| build_runner.files().out_dir_new_layout(unit) | ||
| } else { | ||
| build_runner.files().output_dir(unit) | ||
| }; | ||
|
|
||
| rustdoc.arg("-o").arg(&doc_dir); | ||
| rustdoc.args(&features_args(unit)); | ||
| rustdoc.args(&check_cfg_args(unit)); | ||
|
|
@@ -972,6 +981,7 @@ fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<W | |
| let mut rustdoc = prepare_rustdoc(build_runner, unit)?; | ||
|
|
||
| let crate_name = unit.target.crate_name(); | ||
| let is_json_output = build_runner.bcx.build_config.intent.wants_doc_json_output(); | ||
| let doc_dir = build_runner.files().output_dir(unit); | ||
| // Create the documentation directory ahead of time as rustdoc currently has | ||
| // a bug where concurrent invocations will race to create this directory if | ||
|
|
@@ -1055,13 +1065,15 @@ fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<W | |
| } | ||
| } | ||
|
|
||
| let crate_dir = doc_dir.join(&crate_name); | ||
| if crate_dir.exists() { | ||
| // Remove output from a previous build. This ensures that stale | ||
| // files for removed items are removed. | ||
| debug!("removing pre-existing doc directory {:?}", crate_dir); | ||
| paths::remove_dir_all(crate_dir)?; | ||
| } | ||
| if !is_json_output { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If it ever changes to accepts muliple, we'll need to update this (and this place is unlikely the only place need an update). |
||
| let crate_dir = doc_dir.join(&crate_name); | ||
| if crate_dir.exists() { | ||
| // Remove output from a previous build. This ensures that stale | ||
| // files for removed items are removed. | ||
| debug!("removing pre-existing doc directory {:?}", crate_dir); | ||
| paths::remove_dir_all(&crate_dir)?; | ||
| } | ||
| }; | ||
| state.running(&rustdoc); | ||
| let timestamp = paths::set_invocation_time(&fingerprint_dir)?; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,12 +250,6 @@ pub fn add_output_format( | |
| build_runner: &BuildRunner<'_, '_>, | ||
| rustdoc: &mut ProcessBuilder, | ||
| ) -> CargoResult<()> { | ||
| let gctx = build_runner.bcx.gctx; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This is a better to be a refactor commit. |
||
| if !gctx.cli_unstable().unstable_options { | ||
| tracing::debug!("`unstable-options` is ignored, required -Zunstable-options flag"); | ||
| return Ok(()); | ||
| } | ||
|
motorailgun marked this conversation as resolved.
|
||
|
|
||
| if build_runner.bcx.build_config.intent.wants_doc_json_output() { | ||
| rustdoc.arg("-Zunstable-options"); | ||
| rustdoc.arg("--output-format=json"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| //! Tests for the `cargo rustdoc` command. | ||
|
|
||
| use std::fs; | ||
|
|
||
| use crate::prelude::*; | ||
| use cargo_test_support::str; | ||
| use cargo_test_support::{basic_manifest, cross_compile, project}; | ||
|
|
@@ -44,7 +46,24 @@ fn rustdoc_simple_json() { | |
| .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) | ||
| .with_stderr_data(str![[r#" | ||
| [DOCUMENTING] foo v0.0.1 ([ROOT]/foo) | ||
| [RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/doc [..] --output-format=json[..] | ||
| [RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/debug/build/foo-[HASH]/out [..] --output-format=json[..] | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
| [GENERATED] [ROOT]/foo/target/doc/foo.json | ||
|
|
||
| "#]]) | ||
| .run(); | ||
| assert!(p.root().join("target/doc/foo.json").is_file()); | ||
| } | ||
|
|
||
| #[cargo_test(nightly, reason = "--output-format is unstable")] | ||
| fn rustdoc_json_with_new_layout() { | ||
| let p = project().file("src/lib.rs", "").build(); | ||
|
|
||
| p.cargo("rustdoc -Z unstable-options -Z build-dir-new-layout --output-format json -v") | ||
| .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) | ||
| .with_stderr_data(str![[r#" | ||
| [DOCUMENTING] foo v0.0.1 ([ROOT]/foo) | ||
| [RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/debug/build/foo/[HASH]/out [..] --output-format=json[..] | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
| [GENERATED] [ROOT]/foo/target/doc/foo.json | ||
|
|
||
|
|
@@ -322,3 +341,99 @@ fn fail_with_glob() { | |
| "#]]) | ||
| .run(); | ||
| } | ||
|
|
||
| #[cargo_test(nightly, reason = "--output-format is unstable")] | ||
| fn rustdoc_json_same_crate_different_version() { | ||
| let entry = project() | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "entry" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| dep_v1 = { path = "../dep_v1", package = "dep" } | ||
| dep_v2 = { path = "../dep_v2", package = "dep" } | ||
| "#, | ||
| ) | ||
| .file("src/lib.rs", "pub fn entry() {}") | ||
| .build(); | ||
|
|
||
| let _dep_v1 = project() | ||
| .at("dep_v1") | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "dep" | ||
| version = "1.0.0" | ||
| edition = "2021" | ||
| "#, | ||
| ) | ||
| .file("src/lib.rs", "pub fn dep_v1_fn() {}") | ||
| .build(); | ||
|
|
||
| let _dep_v2 = project() | ||
| .at("dep_v2") | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "dep" | ||
| version = "2.0.0" | ||
| edition = "2021" | ||
| "#, | ||
| ) | ||
| .file("src/lib.rs", "pub fn dep_v2_fn() {}") | ||
| .build(); | ||
|
|
||
| entry | ||
| .cargo("rustdoc -v -Z unstable-options --output-format json -p dep@1.0.0") | ||
| .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) | ||
| .with_stderr_data(str![[r#" | ||
| [LOCKING] 2 packages to latest compatible versions | ||
| [DOCUMENTING] dep v1.0.0 ([ROOT]/dep_v1) | ||
| [RUNNING] `rustdoc [..] --crate-name dep [ROOT]/dep_v1/src/lib.rs [..] --output-format=json[..]` | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
| [GENERATED] [ROOT]/foo/target/doc/dep.json | ||
|
|
||
| "#]]) | ||
| .run(); | ||
|
|
||
| let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap(); | ||
| assert!(dep_json.contains("dep_v1_fn")); | ||
| assert!(!dep_json.contains("dep_v2_fn")); | ||
|
|
||
| entry | ||
| .cargo("rustdoc -v -Z unstable-options --output-format json -p dep@2.0.0") | ||
| .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) | ||
| .with_stderr_data(str![[r#" | ||
| [DOCUMENTING] dep v2.0.0 ([ROOT]/dep_v2) | ||
| [RUNNING] `rustdoc [..] --crate-name dep [ROOT]/dep_v2/src/lib.rs [..] --output-format=json[..]` | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
| [GENERATED] [ROOT]/foo/target/doc/dep.json | ||
|
|
||
| "#]]) | ||
| .run(); | ||
|
|
||
| let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap(); | ||
| assert!(!dep_json.contains("dep_v1_fn")); | ||
| assert!(dep_json.contains("dep_v2_fn")); | ||
|
|
||
| entry | ||
| .cargo("rustdoc -v -Z unstable-options --output-format json -p dep@1.0.0") | ||
| .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) | ||
| .with_stderr_data(str![[r#" | ||
| [FRESH] dep v1.0.0 ([ROOT]/dep_v1) | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
| [GENERATED] [ROOT]/foo/target/doc/dep.json | ||
|
|
||
| "#]]) | ||
| .run(); | ||
|
|
||
| let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap(); | ||
| assert!(dep_json.contains("dep_v1_fn")); | ||
| assert!(!dep_json.contains("dep_v2_fn")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test that makes cargo replaces outdated uplifted content! |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.