From 81d86bf0ebe38897ba1c79cb9a340a1342e0f22a Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 19 Dec 2025 22:03:57 +0100 Subject: [PATCH 1/3] Add regression test for remap-path-prefix handling in rustdoc for deps --- tests/rustdoc/auxiliary/remapped-paths.rs | 11 +++++++++++ tests/rustdoc/import-remapped-paths.rs | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/rustdoc/auxiliary/remapped-paths.rs create mode 100644 tests/rustdoc/import-remapped-paths.rs diff --git a/tests/rustdoc/auxiliary/remapped-paths.rs b/tests/rustdoc/auxiliary/remapped-paths.rs new file mode 100644 index 0000000000000..f31d2d316f3aa --- /dev/null +++ b/tests/rustdoc/auxiliary/remapped-paths.rs @@ -0,0 +1,11 @@ +//@ compile-flags:-Zunstable-options --remap-path-prefix={{src-base}}= + +pub struct MyStruct { + field: u32, +} + +impl MyStruct { + pub fn new() -> MyStruct { + MyStruct { field: 3 } + } +} diff --git a/tests/rustdoc/import-remapped-paths.rs b/tests/rustdoc/import-remapped-paths.rs new file mode 100644 index 0000000000000..c90ef4df1c0f7 --- /dev/null +++ b/tests/rustdoc/import-remapped-paths.rs @@ -0,0 +1,20 @@ +// This is a regression for `--remap-path-prefix` in an auxiliary dependency. +// +// We want to make sure that we can still have the "Source" links to the dependency +// even if it's paths are remapped. +// +// See also rust-lang/rust#150100 + +//@ aux-build:remapped-paths.rs +//@ build-aux-docs + +#![crate_name = "foo"] + +extern crate remapped_paths; + +//@ has foo/struct.MyStruct.html +// FIXME: Doesn't work! +//@ has - '//a[@href="../src/remapped_paths/remapped-paths.rs.html#3"]' 'Source' +//@ has - '//a[@href="../src/remapped_paths/remapped-paths.rs.html#8"]' 'Source' + +pub use remapped_paths::MyStruct; From 811c241b3d873880fe9bd4b9b36b37a3cdf3f70d Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 18 Dec 2025 23:37:51 +0100 Subject: [PATCH 2/3] Handle remapped paths correctly when generating "Source" links --- src/librustdoc/html/render/context.rs | 17 +++++++++++------ tests/rustdoc/import-remapped-paths.rs | 1 - 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index ee71564a1417a..4d9e352595a1a 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -14,7 +14,7 @@ use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE}; use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustc_span::edition::Edition; -use rustc_span::{BytePos, FileName, Symbol}; +use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Symbol}; use tracing::info; use super::print_item::{full_path, print_item, print_item_path}; @@ -365,7 +365,10 @@ impl<'tcx> Context<'tcx> { // We can safely ignore synthetic `SourceFile`s. let file = match span.filename(self.sess()) { - FileName::Real(ref path) => path.local_path()?.to_path_buf(), + FileName::Real(ref path) => path + .local_path() + .unwrap_or(path.path(RemapPathScopeComponents::MACRO)) + .to_path_buf(), _ => return None, }; let file = &file; @@ -499,10 +502,12 @@ impl<'tcx> Context<'tcx> { } = options; let src_root = match krate.src(tcx) { - FileName::Real(ref p) => match p.local_path().map(|p| p.parent()).flatten() { - Some(p) => p.to_path_buf(), - None => PathBuf::new(), - }, + FileName::Real(ref p) => { + match p.local_path().unwrap_or(p.path(RemapPathScopeComponents::MACRO)).parent() { + Some(p) => p.to_path_buf(), + None => PathBuf::new(), + } + } _ => PathBuf::new(), }; // If user passed in `--playground-url` arg, we fill in crate name here diff --git a/tests/rustdoc/import-remapped-paths.rs b/tests/rustdoc/import-remapped-paths.rs index c90ef4df1c0f7..bce3d782b9c2e 100644 --- a/tests/rustdoc/import-remapped-paths.rs +++ b/tests/rustdoc/import-remapped-paths.rs @@ -13,7 +13,6 @@ extern crate remapped_paths; //@ has foo/struct.MyStruct.html -// FIXME: Doesn't work! //@ has - '//a[@href="../src/remapped_paths/remapped-paths.rs.html#3"]' 'Source' //@ has - '//a[@href="../src/remapped_paths/remapped-paths.rs.html#8"]' 'Source' From c039edd41416c209b6bcdfa02d152a8db64102ba Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 20 Dec 2025 00:32:56 +0100 Subject: [PATCH 3/3] Fix typo --- tests/rustdoc/import-remapped-paths.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rustdoc/import-remapped-paths.rs b/tests/rustdoc/import-remapped-paths.rs index bce3d782b9c2e..9e7518f7c0170 100644 --- a/tests/rustdoc/import-remapped-paths.rs +++ b/tests/rustdoc/import-remapped-paths.rs @@ -1,7 +1,7 @@ // This is a regression for `--remap-path-prefix` in an auxiliary dependency. // // We want to make sure that we can still have the "Source" links to the dependency -// even if it's paths are remapped. +// even if its paths are remapped. // // See also rust-lang/rust#150100