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

View all comments

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
Comment thread src/cargo/core/compiler/build_runner/compilation_files.rs Outdated
@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.

Comment thread src/cargo/core/compiler/build_runner/mod.rs Outdated
Comment thread src/cargo/core/compiler/rustdoc.rs Outdated
Comment thread tests/testsuite/build_dir.rs
@motorailgun motorailgun force-pushed the rustdoc-rebuild branch 2 times, most recently from 939c055 to 79399ac Compare April 6, 2026 23:18
@motorailgun
Copy link
Copy Markdown
Contributor Author

Note: updated PR message reflecting changes.

BMG042

This comment was marked as spam.

Comment thread src/cargo/core/compiler/build_runner/mod.rs Outdated
Comment thread src/cargo/core/compiler/build_runner/mod.rs Outdated
Comment thread src/cargo/core/compiler/rustdoc.rs
Comment thread src/cargo/core/compiler/mod.rs Outdated
}

let crate_dir = doc_dir.join(&crate_name);
let crate_dir = if is_json_output {
Copy link
Copy Markdown
Member

@weihanglo weihanglo Apr 15, 2026

Choose a reason for hiding this comment

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

We don't need to clean up doc.json file for the new layout, as they are isolated by units.

That said, I do want to know how this would affect cargo clean --doc. We should at least track it in the tracking issue

View changes since the review

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.

From my quick observation, doc.json file will escape normal cargo clean --doc operation? Will dig into tomorrow.

if opts.doc {
if !opts.spec.is_empty() {
// FIXME: https://github.com/rust-lang/cargo/issues/8790
// This should support the ability to clean specific packages
// within the doc directory. It's a little tricky since it
// needs to find all documentable targets, but also consider
// the fact that target names might overlap with dependency
// names and such.
bail!("--doc cannot be used with -p");
}
// If the doc option is set, we just want to delete the doc directory.
target_dir = target_dir.join("doc");
clean_ctx.remove_paths(&[target_dir.into_path_unlocked()])?;

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.

Tracking issue btw: #13283

Though I feel like we probably don't need to deal with cargo clean --doc. So the behavior doesn't need to be "cleaning anything related to artifacts."

--doc
    This option will cause cargo clean to remove only the doc directory in the target directory.

That makes me wonder -Zrustdoc-mergeable-info also doesn't need to support cargo clean.

@ranger-ross any thoughts?

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.

I meant, we may leave it for future. No need to worry about it now

Comment thread tests/testsuite/build_dir.rs Outdated
Comment thread tests/testsuite/build_dir_legacy.rs Outdated
@motorailgun motorailgun force-pushed the rustdoc-rebuild branch 2 times, most recently from e727c71 to 225d510 Compare April 15, 2026 14:01
@motorailgun motorailgun force-pushed the rustdoc-rebuild branch 2 times, most recently from 86007ed to ca752fa Compare April 16, 2026 03:12
Copy link
Copy Markdown
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

View changes since this review

debug!("removing pre-existing doc directory {:?}", crate_dir);
paths::remove_dir_all(crate_dir)?;
}
if !is_json_output {
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.

rustdoc --output-format currently only accepts one format and each option is mutually exclusive, so this check should be fine for now.

If it ever changes to accepts muliple, we'll need to update this (and this place is unlikely the only place need an update).

build_runner: &BuildRunner<'_, '_>,
rustdoc: &mut ProcessBuilder,
) -> CargoResult<()> {
let gctx = build_runner.bcx.gctx;
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.

nit: This is a better to be a refactor commit.

assert!(!dep_json.contains("dep_v1_fn"));
assert!(dep_json.contains("dep_v2_fn"));
assert!(dep_json.contains("dep_v1_fn"));
assert!(!dep_json.contains("dep_v2_fn"));
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.

Nice test that makes cargo replaces outdated uplifted content!

@weihanglo
Copy link
Copy Markdown
Member

BTW, if follows conventional commit message stricter, the first commit should be test: and the second one should be fix:.

Anyway, thanks!

@weihanglo weihanglo added this pull request to the merge queue Apr 17, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 17, 2026
@motorailgun
Copy link
Copy Markdown
Contributor Author

Oh, is that failing test just flaky? If so I'd love to take some time to amend the commits (messages and histories).

@weihanglo
Copy link
Copy Markdown
Member

Oh, is that failing test just flaky? If so I'd love to take some time to amend the commits (messages and histories).

Yeah definitely flaky.

Feel free to amend the commits if you'd like to. I will merge this on Friday anyway before the next Cargo submodule sync in rust-lang/rust (which is probably tomorrow within 24hrs)

---- util::local_poll_adapter::tests::deferred_success stdout ----

thread 'util::local_poll_adapter::tests::deferred_success' (9550) panicked at src/cargo/util/local_poll_adapter.rs:177:9:
assertion `left == right` failed
  left: Ready(Ok(50))
 right: Pending

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