Skip to content

Conversation

janis-bhm
Copy link
Contributor

@janis-bhm janis-bhm commented Sep 1, 2025

fixes #144752
related to/builds on #145008

bevy still crashes in the same way as #144752 when building docs on nightly, and from what I can tell the cause seems to be the following (copied from zulip #t-rustdoc > docs on nightly with example scrapes crash):

render_call_locations tries to find the source span of a call to add as an example, but the example files are never actually in the source map from what I can tell, and so it falls back to the default span, which points at the first file in the source map.
Now, the issue that guillaume mentions here adds new files to the source map in order to get them into the dep info, and that leads to some files, namely docs-rs/trait-tags.html in the case of bevy because it's added with --html-after-content, being added before any source files, so then the default span points at them, and when href_from_span tries to find the source file corresponding to the span, the file doesn't belong to local_sources, and it short circuits.
This can be fixed by just not using DUMMY_SP as the default span and calculating, for example, the crates root source file as the span, because I'm not entirely sure what the href from that span is actually used for; it's not what links to the example in the end.
I think the proper way of fixing this would be to make sure the example files are part of the local_sources or at least the source map, but I don't know nearly enough about rust internals to be able to figure out how to fix that.

I've included a test that's mostly copied from #145008's test with the addition of --html-after-content after.html in the RUSTDOCFLAGS, which panics on master in conjunction with the -Zrustdoc-scrape-examples cargo flag.

cc @GuillaumeGomez

@rustbot
Copy link
Collaborator

rustbot commented Sep 1, 2025

r? @jieyouxu

rustbot has assigned @jieyouxu.
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

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Sep 1, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Sep 1, 2025

r? rustdoc

@rustbot rustbot assigned GuillaumeGomez and unassigned jieyouxu Sep 1, 2025
@@ -2829,7 +2829,21 @@ fn render_call_locations<W: fmt::Write>(
file.start_pos + BytePos(byte_max),
))
})()
.unwrap_or(DUMMY_SP);
.unwrap_or_else(|| {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of cloning parts of the above code, I think it'd might be simpler to instead tweak it a bit:

            let local = tcx.sess.local_crate_source_file()?;
            let crate_src = local.into_local_path()?;
            let abs_crate_src = crate_src.canonicalize().ok()?;
            let crate_root = abs_crate_src.parent()?.parent()?;
            let rel_path = path.strip_prefix(crate_root).ok()?;
            let files = source_map.files();
            let file = files.iter().find(|file| match &file.name {
                FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
                _ => false,
            }).or_else(|| files.iter().find(|file| matches!(&file.name, FileName::Real(file_name) if file_name == &local))?;

Copy link
Member

Choose a reason for hiding this comment

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

Then we can replace the unwrap_or(DUMMY_SP) with expect("no local file found").

@rustbot
Copy link
Collaborator

rustbot commented Sep 1, 2025

Some changes occurred in GUI tests.

cc @GuillaumeGomez

@@ -0,0 +1,9 @@
//@ run-flags:-Zrustdoc-scrape-examples
//@ rustc-env: RUSTDOCFLAGS='--html-after-content after.html'
Copy link
Member

Choose a reason for hiding this comment

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

Please pass --html-after-content after.html directly in run-flags.

@GuillaumeGomez
Copy link
Member

Also please squash your commits once done.

@rust-log-analyzer

This comment has been minimized.

@janis-bhm janis-bhm force-pushed the rustdoc-default-span-with-simple-test branch from a177355 to 6a3975e Compare September 1, 2025 20:14
@rustbot
Copy link
Collaborator

rustbot commented Sep 1, 2025

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.

@janis-bhm janis-bhm force-pushed the rustdoc-default-span-with-simple-test branch from 6a3975e to 6c8d64e Compare September 1, 2025 20:32
@rust-log-analyzer

This comment has been minimized.

@janis-bhm janis-bhm force-pushed the rustdoc-default-span-with-simple-test branch from 6c8d64e to 65567a7 Compare September 1, 2025 22:01
@@ -52,7 +52,7 @@ compare-elements-size-near: (
set-window-size: (900, 900)

// First we check the current width, height and position.
assert-css: ("#crate-search", {"width": "159px"})
assert-css: ("#crate-search", {"width": "185px"})
Copy link
Member

Choose a reason for hiding this comment

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

This change is because the longest crate name increased.

@GuillaumeGomez
Copy link
Member

Note for myself, once merged, I'll need to add a test for the extra HTML added.

@rust-log-analyzer

This comment has been minimized.

…file outside of local_sources

add test against crashing with --html-after-content file

correctly add --html-after-content to env not args

formatting fix for rustdoc-call-locations-after-content/rmake.rs

Use local crate source file as default span in `render_call_locations`

- avoids unwrapping the first file added to the source map as a local file in
`href_from_span`

move test to tests/rustdoc-gui, rename to scrape_examples_ice

test link is correct

use rustdocflags, rename path in example, track lock file

factor out duplicate function calls

use compile-flags to make sure the after.html file is actually included in the rustdoc call

fix goml go-to path

increment assert-count in sidebar-source-code.goml

adjust crate-search width in search-result-display.goml

renamed Bar in scrape_examples_ice test

make crate name shorter ..
@janis-bhm janis-bhm force-pushed the rustdoc-default-span-with-simple-test branch from 65567a7 to f67e29e Compare September 1, 2025 23:42
@GuillaumeGomez
Copy link
Member

Thanks for the fix!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Sep 2, 2025

📌 Commit f67e29e has been approved by GuillaumeGomez

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 2, 2025
bors added a commit that referenced this pull request Sep 2, 2025
Rollup of 14 pull requests

Successful merges:

 - #144066 (stabilize c-style varargs for sysv64, win64, efiapi, aapcs)
 - #145783 (add span to struct pattern rest (..))
 - #146034 (Update target spec metadata of Arm64EC Windows and Trusty targets)
 - #146064 (Add compiler error when trying to use concat metavar expr in repetitions)
 - #146070 (rustdoc-search: skip loading unneeded fnData)
 - #146088 (constify impl Try for ControlFlow)
 - #146089 (fix a constness ordering bug in rustfmt)
 - #146091 (fix rustdoc `render_call_locations`  panicking because of default span `DUMMY_SP` pointing at non local-source file)
 - #146094 (Make `Parser::parse_for_head` public for rustfmt usage)
 - #146102 (Remove dead code stemming from an old effects desugaring)
 - #146115 (Add maintainer for VxWorks)
 - #146116 (Adjust issue-118306.rs test after LLVM change)
 - #146117 (Fix search index generation)
 - #146118 (improve process::abort rendering in Miri backtraces)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 15cde9c into rust-lang:master Sep 2, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Sep 2, 2025
rust-timer added a commit that referenced this pull request Sep 2, 2025
Rollup merge of #146091 - janis-bhm:rustdoc-default-span-with-simple-test, r=GuillaumeGomez

fix rustdoc `render_call_locations`  panicking because of default span `DUMMY_SP` pointing at non local-source file

fixes #144752
related to/builds on #145008

bevy still crashes in the same way as #144752 when building docs on nightly, and from what I can tell the cause seems to be the following (copied from zulip [#t-rustdoc > docs on nightly with example scrapes crash](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/docs.20on.20nightly.20with.20example.20scrapes.20crash)):

> render_call_locations tries to [find](https://github.com/rust-lang/rust/blob/84a17470220e7adf249b18d7c0178dfbede89462/src/librustdoc/html/render/mod.rs#L2816) the source span of a call to add as an example, but the example files are never actually in the source map from what I can tell, and so it falls back to the default span, which points at the first file in the source map.
> Now, the issue that guillaume mentions [here](#145008) adds new files to the source map in order to get them into the dep info, and that leads to some files, namely docs-rs/trait-tags.html in the case of bevy because it's added with --html-after-content, being added before any source files, so then the default span points at them, and when href_from_span tries to find the [source file](https://github.com/rust-lang/rust/blob/84a17470220e7adf249b18d7c0178dfbede89462/src/librustdoc/html/render/context.rs#L368) corresponding to the span, the file doesn't belong to local_sources, and it short circuits.
> This can be fixed by just not using DUMMY_SP as the default span and calculating, for example, the crates root source file as the span, because I'm not entirely sure what the href from that span is actually used for; it's not what links to the example in the end.
> I think the proper way of fixing this would be to make sure the example files are part of the local_sources or at least the source map, but I don't know nearly enough about rust internals to be able to figure out how to fix that.

I've included a test that's mostly copied from #145008's test with the addition of `--html-after-content after.html` in the `RUSTDOCFLAGS`, which panics on master in conjunction with the `-Zrustdoc-scrape-examples` cargo flag.

cc `@GuillaumeGomez`
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Sep 3, 2025
Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#144066 (stabilize c-style varargs for sysv64, win64, efiapi, aapcs)
 - rust-lang/rust#145783 (add span to struct pattern rest (..))
 - rust-lang/rust#146034 (Update target spec metadata of Arm64EC Windows and Trusty targets)
 - rust-lang/rust#146064 (Add compiler error when trying to use concat metavar expr in repetitions)
 - rust-lang/rust#146070 (rustdoc-search: skip loading unneeded fnData)
 - rust-lang/rust#146088 (constify impl Try for ControlFlow)
 - rust-lang/rust#146089 (fix a constness ordering bug in rustfmt)
 - rust-lang/rust#146091 (fix rustdoc `render_call_locations`  panicking because of default span `DUMMY_SP` pointing at non local-source file)
 - rust-lang/rust#146094 (Make `Parser::parse_for_head` public for rustfmt usage)
 - rust-lang/rust#146102 (Remove dead code stemming from an old effects desugaring)
 - rust-lang/rust#146115 (Add maintainer for VxWorks)
 - rust-lang/rust#146116 (Adjust issue-118306.rs test after LLVM change)
 - rust-lang/rust#146117 (Fix search index generation)
 - rust-lang/rust#146118 (improve process::abort rendering in Miri backtraces)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 4, 2025
…d-examples, r=lolbinarycat

[rustdoc] Uncomment code to add scraped rustdoc examples in loaded paths

Since the bug was fixed in rust-lang#146091, we can now uncomment the code. :)

r? lolbinarycat
rust-timer added a commit that referenced this pull request Sep 4, 2025
Rollup merge of #146161 - GuillaumeGomez:loaded-paths-scraped-examples, r=lolbinarycat

[rustdoc] Uncomment code to add scraped rustdoc examples in loaded paths

Since the bug was fixed in #146091, we can now uncomment the code. :)

r? lolbinarycat
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Sep 5, 2025
…s, r=lolbinarycat

[rustdoc] Uncomment code to add scraped rustdoc examples in loaded paths

Since the bug was fixed in rust-lang/rust#146091, we can now uncomment the code. :)

r? lolbinarycat
fmease added a commit to fmease/rust that referenced this pull request Sep 5, 2025
… r=lolbinarycat

Ensure that `--html-after-content` option is used to check `scrape_examples_ice` rustdoc GUI test

Follow-up of rust-lang#146091.

This test ensures that the spans are correctly handled when a "local source file" is not the first in the file list. To ensure it's actually what's tested, this test checks that the option is actually used by adding an element.

r? `@lolbinarycat`
rust-timer added a commit that referenced this pull request Sep 5, 2025
Rollup merge of #146242 - GuillaumeGomez:rustdoc-gui-scrape, r=lolbinarycat

Ensure that `--html-after-content` option is used to check `scrape_examples_ice` rustdoc GUI test

Follow-up of #146091.

This test ensures that the spans are correctly handled when a "local source file" is not the first in the file list. To ensure it's actually what's tested, this test checks that the option is actually used by adding an element.

r? `@lolbinarycat`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: only local crates should have sources emitted
6 participants