Skip to content

Support multiple crate versions in --extern-html-root-url #143465

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kornelski
Copy link
Contributor

@kornelski kornelski commented Jul 5, 2025

Rustdoc's --extern-html-root-url used to use tcx.crate_name() to identify crates, but that used crates' internal names from their metadata, instead of names given to them in --extern. That was confusing, because both --extern… arguments seem related and use similar syntax. Crucially, this didn't work correctly with Cargo's package aliases or multiple versions of crates.

sess.opts.externs lacks CrateNum, and Resolver.extern_prelude gets destroyed before rustdoc has a chance to see it, so I've had to save this mapping in CStore.

Just in case, I've kept the previous mapping by crate name as a fallback for crates that weren't matched by their extern name.

Fixes #76296

@rustbot
Copy link
Collaborator

rustbot commented Jul 5, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
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 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 Jul 5, 2025
@kornelski kornelski changed the title Avoid redundant lookup in CrateLoader::existing_match Support multiple crate versions in --extern-html-root-url Jul 5, 2025
@kornelski
Copy link
Contributor Author

And f9f6a70 is just a bit of redundant code I've noticed in the process, too small to give it a separate PR.

@rust-log-analyzer

This comment has been minimized.

@jyn514
Copy link
Member

jyn514 commented Jul 5, 2025

cc @aDotInTheVoid @obi1kenobi - i haven’t checked if this will help distinguish versions for the json backend too but i think it might

@kornelski
Copy link
Contributor Author

Yes, the URL is in the JSON, and can be creatively used to identify the crates.

@jyn514
Copy link
Member

jyn514 commented Jul 5, 2025

to be clear i do not think html-root-url should be used for this, i’m saying the new metadata you’ve added to CrateRoot might allow the json backend to emit structured metadata.

@Nemo157
Copy link
Member

Nemo157 commented Jul 5, 2025

#76296 doesn't demonstrate the entirety of how rustdoc has issues with resolving crate versions. There's also the case of non-renamed transitively reëxported multiple versions of a single package name.

@lolbinarycat
Copy link
Contributor

I think the name resolution method used here should be documented in the rustdoc book

@petrochenkov petrochenkov self-assigned this Jul 5, 2025
kornelski added 4 commits July 6, 2025 00:45
Tracks association between `self.sess.opts.externs` (aliases in `--extern alias=rlib`) and resolved `CrateNum`

Intended to allow Rustdoc match the aliases in `--extern-html-root-url` rust-lang#76296

Force-injected extern crates aren't included, since they're meant for the linker only
Comment on lines +398 to +399
Crate names in this flag refer to the names in the [extern prelude], and support crates renamed
via the `--extern` `rustc` flag.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there's a few improvements that could be made here:

  1. the original crate name is used as a fallback if there is no match in the extern prelude
  2. the fact that renaming crates via --extern logically follows from the fact we are using the extern prelude
  3. renaming via extern crate as is also supported.
  4. note that cargo's package renaming works via --extern and is thus supported.

I think this should be split into at least two sentences, one tersely and precisely describing the behavior (extern prelude with fallback), and another describing the consequence of this behavior (e.g. support for various methods of renaming, backwards compatibility)

name: Symbol,
extern_crate: ExternCrate,
) {
debug_assert_eq!(extern_crate.dependency_of, LOCAL_CRATE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debug_assert_eq!(extern_crate.dependency_of, LOCAL_CRATE);
// this function should not be called on transitive dependencies
debug_assert_eq!(extern_crate.dependency_of, LOCAL_CRATE);

cx.cache.extern_locations.insert(e.crate_num, location);
}
}

// Cache where all our extern crates are located
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
Copy link
Contributor

Choose a reason for hiding this comment

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

if the doc url is emitted in the json, doesn't that make this fixme comment wrong? if so, i would replace it with a note saying that this needs to be common because it is actually emitted by both backends.

Comment on lines +2 to +8
//@ aux-crate:extern_prelude_name=empty.rs

extern crate extern_prelude_name as internal_ident_name;

//@ has extern_html_alias/index.html
//@ has - '//a/@href' 'https://renamed.example.com/empty/index.html'
pub use internal_ident_name as yet_different_name;
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. internal_ident_name is a misleading name, as it actually does exist in extern prelude as an alias (i.e. ::internal_ident_name would be valid here, in this case both exist in the extern prelude)
  2. we should test that extern crate as aliases are handled correctly
  3. we should test the fallback behavior (using crate name)

Doing all this would probably require at least a second aux-crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--extern-html-root-url uses library names to identify dependencies, failing to handle multiple versions
9 participants