Skip to content

feat: rebuild(-ish) rustdoc json for different version of same crates#16773

Open
motorailgun wants to merge 2 commits intorust-lang:masterfrom
motorailgun:rustdoc-rebuild
Open

feat: rebuild(-ish) rustdoc json for different version of same crates#16773
motorailgun wants to merge 2 commits intorust-lang:masterfrom
motorailgun:rustdoc-rebuild

Conversation

@motorailgun
Copy link
Copy Markdown
Contributor

@motorailgun motorailgun commented Mar 20, 2026

Fix #16291.

What does this PR try to resolve?

As discussed in the linked issue, cargo rustdoc --output-format=json doesn't rebuild for same crate name but with different versions. This is because current cargo implementation around rustdoc only specifies output paths/names by only its crate names, but not with version(hash) info.

This PR forces new layout for json output, and uplift the file into backward-compatible place.

How to test and review this PR?

  • Can we consider just forcing new layout for --output-format=json? (and stop uplifting)
  • Uplifting will always happen even if uplifted json is fresh, and/or new layout is manually specified. (could not figure out how to prevent); however it is just a copy operation if docs JSON are already built and fresh.

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-layout Area: target output directory layout, naming, and organization S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 20, 2026

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

@motorailgun motorailgun force-pushed the rustdoc-rebuild branch 2 times, most recently from a1ef71b to 04024f4 Compare March 21, 2026 02:47
@motorailgun motorailgun force-pushed the rustdoc-rebuild branch 2 times, most recently from be69519 to 2f32d53 Compare March 26, 2026 18:45
@motorailgun motorailgun requested a review from weihanglo March 26, 2026 19:15
@motorailgun
Copy link
Copy Markdown
Contributor Author

Updated to use new directory layout.

@rustbot

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 1, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

}

#[cargo_test(nightly, reason = "--output-format is unstable")]
fn cargo_rustdoc_json_should_output_to_target_dir() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add new tests first, and the fix and snapshots in the second commit? So that we can see the behavior change in the snapshot updates. See https://epage.github.io/dev/pr-style/#c-test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, sorry about my stupidness of forgetting atomic commits again... thank you for pointing out!

This comment was marked as spam.

@motorailgun
Copy link
Copy Markdown
Contributor Author

Note: updated PR message reflecting changes.

BMG042

This comment was marked as spam.

self.collect_tests_and_executables(unit)?;

// Uplift rustdoc
if is_rustdoc_json_output(&self) {
Copy link
Copy Markdown
Member

@weihanglo weihanglo Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

Sorry I didn't really review this carefully until this round.

I feel like this is the not the correct place to uplift. In Cargo we track whether a OutputFile will be uplifted through its hard_link. If we make rustdoc JSON as part of that, we'll get uplifting logic for free. We would probably get uplifting logic for free if we set it here:


Also, this seems to unconditionally uplift rustdoc JSON for every root unit. Not wrong but a bit error-prone as it assumes every root unit is rustdoc (which is true actually as output-format is available only in cargo rustdoc.

let filename = format!("{crate_name}.json");

let src_path = doc_json_dir.join(&filename);
copy(src_path, doc_dir.join(&filename))?;
Copy link
Copy Markdown
Member

@weihanglo weihanglo Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

We have cargo_util::paths::link_or_copy though we should leverage OutputFile::hardlink

if !gctx.cli_unstable().unstable_options {
tracing::debug!("`unstable-options` is ignored, required -Zunstable-options flag");
return Ok(());
}
Copy link
Copy Markdown
Member

@weihanglo weihanglo Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

I wonder if we actually don't need this guard unstable_optionts here?

The place we set UserIntent::Doc { json: true } has already checked -Zunstable-options presence:

let output_format = if let Some(output_format) = args._value_of("output-format") {
gctx.cli_unstable()
.fail_if_stable_opt("--output-format", 12103)?;
output_format.parse()?
} else {
OutputFormat::Html
};
let mut compile_opts = args.compile_options_for_single_package(
gctx,
UserIntent::Doc {
deps: false,
json: matches!(output_format, OutputFormat::Json),
},
Some(&ws),
ProfileChecking::Custom,
)?;

So that may imply we also don't need the is_rustdoc_json_output at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-layout Area: target output directory layout, naming, and organization S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cargo rustdoc --output-format=json sometimes doesn't rebuild for different versions of same crate name.

7 participants