Skip to content

Commit 014e4f3

Browse files
authored
Rollup merge of rust-lang#107099 - edward-shen:edward-shen/rustdoc-remap-path-prefix, r=GuillaumeGomez
rustdoc: Add support for --remap-path-prefix Adds `--remap-path-prefix` as an unstable option. This is implemented to mimic the behavior of `rustc`'s `--remap-path-prefix`. This flag similarly takes in two paths, a prefix to replace and a replacement string. This is useful for build tools (e.g. Buck) other than cargo that can run doc tests. cc: `@dtolnay`
2 parents f2e1a3a + d66718c commit 014e4f3

10 files changed

+139
-18
lines changed

src/librustdoc/config.rs

+26
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub(crate) struct Options {
128128
pub(crate) enable_per_target_ignores: bool,
129129
/// Do not run doctests, compile them if should_test is active.
130130
pub(crate) no_run: bool,
131+
/// What sources are being mapped.
132+
pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
131133

132134
/// The path to a rustc-like binary to build tests with. If not set, we
133135
/// default to loading from `$sysroot/bin/rustc`.
@@ -211,6 +213,7 @@ impl fmt::Debug for Options {
211213
.field("run_check", &self.run_check)
212214
.field("no_run", &self.no_run)
213215
.field("test_builder_wrappers", &self.test_builder_wrappers)
216+
.field("remap-file-prefix", &self.remap_path_prefix)
214217
.field("nocapture", &self.nocapture)
215218
.field("scrape_examples_options", &self.scrape_examples_options)
216219
.field("unstable_features", &self.unstable_features)
@@ -372,6 +375,13 @@ impl Options {
372375
let codegen_options = CodegenOptions::build(early_dcx, matches);
373376
let unstable_opts = UnstableOptions::build(early_dcx, matches);
374377

378+
let remap_path_prefix = match parse_remap_path_prefix(&matches) {
379+
Ok(prefix_mappings) => prefix_mappings,
380+
Err(err) => {
381+
early_dcx.early_fatal(err);
382+
}
383+
};
384+
375385
let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
376386

377387
// check for deprecated options
@@ -772,6 +782,7 @@ impl Options {
772782
run_check,
773783
no_run,
774784
test_builder_wrappers,
785+
remap_path_prefix,
775786
nocapture,
776787
crate_name,
777788
output_format,
@@ -820,6 +831,21 @@ impl Options {
820831
}
821832
}
822833

834+
fn parse_remap_path_prefix(
835+
matches: &getopts::Matches,
836+
) -> Result<Vec<(PathBuf, PathBuf)>, &'static str> {
837+
matches
838+
.opt_strs("remap-path-prefix")
839+
.into_iter()
840+
.map(|remap| {
841+
remap
842+
.rsplit_once('=')
843+
.ok_or("--remap-path-prefix must contain '=' between FROM and TO")
844+
.map(|(from, to)| (PathBuf::from(from), PathBuf::from(to)))
845+
})
846+
.collect()
847+
}
848+
823849
/// Prints deprecation warnings for deprecated options
824850
fn check_deprecated_options(matches: &getopts::Matches, dcx: &rustc_errors::DiagCtxt) {
825851
let deprecated_flags = [];

src/librustdoc/doctest.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_session::config::{self, CrateType, ErrorOutputType};
1616
use rustc_session::parse::ParseSess;
1717
use rustc_session::{lint, Session};
1818
use rustc_span::edition::Edition;
19-
use rustc_span::source_map::SourceMap;
19+
use rustc_span::source_map::{FilePathMapping, SourceMap};
2020
use rustc_span::symbol::sym;
2121
use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
2222
use rustc_target::spec::{Target, TargetTriple};
@@ -128,6 +128,7 @@ pub(crate) fn run(
128128
edition: options.edition,
129129
target_triple: options.target.clone(),
130130
crate_name: options.crate_name.clone(),
131+
remap_path_prefix: options.remap_path_prefix.clone(),
131132
..config::Options::default()
132133
};
133134

@@ -612,7 +613,6 @@ pub(crate) fn make_test(
612613
use rustc_errors::emitter::{Emitter, HumanEmitter};
613614
use rustc_errors::DiagCtxt;
614615
use rustc_parse::parser::ForceCollect;
615-
use rustc_span::source_map::FilePathMapping;
616616

617617
let filename = FileName::anon_source_code(s);
618618
let source = crates + everything_else;
@@ -803,7 +803,6 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
803803
rustc_span::create_session_if_not_set_then(edition, |_| {
804804
use rustc_errors::emitter::HumanEmitter;
805805
use rustc_errors::DiagCtxt;
806-
use rustc_span::source_map::FilePathMapping;
807806

808807
let filename = FileName::anon_source_code(source);
809808
// Any errors in parsing should also appear when the doctest is compiled for real, so just
@@ -1066,7 +1065,7 @@ impl Collector {
10661065
if !item_path.is_empty() {
10671066
item_path.push(' ');
10681067
}
1069-
format!("{} - {item_path}(line {line})", filename.prefer_local())
1068+
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionaly())
10701069
}
10711070

10721071
pub(crate) fn set_position(&mut self, position: Span) {
@@ -1076,11 +1075,15 @@ impl Collector {
10761075
fn get_filename(&self) -> FileName {
10771076
if let Some(ref source_map) = self.source_map {
10781077
let filename = source_map.span_to_filename(self.position);
1079-
if let FileName::Real(ref filename) = filename
1080-
&& let Ok(cur_dir) = env::current_dir()
1081-
&& let Some(local_path) = filename.local_path()
1082-
&& let Ok(path) = local_path.strip_prefix(&cur_dir)
1083-
{
1078+
if let FileName::Real(ref filename) = filename {
1079+
let path = filename.remapped_path_if_available();
1080+
1081+
// Strip the cwd prefix from the path. This will likely exist if
1082+
// the path was not remapped.
1083+
let path = env::current_dir()
1084+
.map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
1085+
.unwrap_or(path);
1086+
10841087
return path.to_owned().into();
10851088
}
10861089
filename
@@ -1107,20 +1110,13 @@ impl Tester for Collector {
11071110
}
11081111

11091112
let path = match &filename {
1110-
FileName::Real(path) => {
1111-
if let Some(local_path) = path.local_path() {
1112-
local_path.to_path_buf()
1113-
} else {
1114-
// Somehow we got the filename from the metadata of another crate, should never happen
1115-
unreachable!("doctest from a different crate");
1116-
}
1117-
}
1113+
FileName::Real(path) => path.remapped_path_if_available().to_path_buf(),
11181114
_ => PathBuf::from(r"doctest.rs"),
11191115
};
11201116

11211117
// For example `module/file.rs` would become `module_file_rs`
11221118
let file = filename
1123-
.prefer_local()
1119+
.prefer_remapped_unconditionaly()
11241120
.to_string_lossy()
11251121
.chars()
11261122
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,14 @@ fn opts() -> Vec<RustcOptGroup> {
556556
unstable("no-run", |o| {
557557
o.optflagmulti("", "no-run", "Compile doctests without running them")
558558
}),
559+
unstable("remap-path-prefix", |o| {
560+
o.optmulti(
561+
"",
562+
"remap-path-prefix",
563+
"Remap source names in compiler messages",
564+
"FROM=TO",
565+
)
566+
}),
559567
unstable("show-type-layout", |o| {
560568
o.optflagmulti("", "show-type-layout", "Include the memory layout of types in the docs")
561569
}),

tests/run-make/issue-88756-default-output/output-default.stdout

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Options:
157157
Comma separated list of types of output for rustdoc to
158158
emit
159159
--no-run Compile doctests without running them
160+
--remap-path-prefix FROM=TO
161+
Remap source names in compiler messages
160162
--show-type-layout
161163
Include the memory layout of types in the docs
162164
--nocapture Don't capture stdout and stderr of tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// FIXME: if/when the output of the test harness can be tested on its own, this test should be
2+
// adapted to use that, and that normalize line can go away
3+
4+
//@ compile-flags:--test -Z unstable-options --remap-path-prefix={{src-base}}=remapped_path --test-args --test-threads=1
5+
//@ rustc-env:RUST_BACKTRACE=0
6+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
7+
//@ failure-status: 101
8+
9+
// doctest fails at runtime
10+
/// ```
11+
/// panic!("oh no");
12+
/// ```
13+
pub struct SomeStruct;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
running 1 test
3+
test remapped_path/remap-path-prefix-failed-doctest-output.rs - SomeStruct (line 10) ... FAILED
4+
5+
failures:
6+
7+
---- remapped_path/remap-path-prefix-failed-doctest-output.rs - SomeStruct (line 10) stdout ----
8+
Test executable failed (exit status: 101).
9+
10+
stderr:
11+
thread 'main' panicked at remapped_path/remap-path-prefix-failed-doctest-output.rs:3:1:
12+
oh no
13+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
14+
15+
16+
17+
failures:
18+
remapped_path/remap-path-prefix-failed-doctest-output.rs - SomeStruct (line 10)
19+
20+
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// FIXME: if/when the output of the test harness can be tested on its own, this test should be
2+
// adapted to use that, and that normalize line can go away
3+
4+
//@ compile-flags:--test -Z unstable-options --remap-path-prefix={{src-base}}=remapped_path --test-args --test-threads=1
5+
//@ rustc-env:RUST_BACKTRACE=0
6+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
7+
//@ failure-status: 101
8+
9+
// doctest fails to compile
10+
/// ```
11+
/// this is not real code
12+
/// ```
13+
pub struct SomeStruct;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
running 1 test
3+
test remapped_path/remap-path-prefix-invalid-doctest.rs - SomeStruct (line 10) ... FAILED
4+
5+
failures:
6+
7+
---- remapped_path/remap-path-prefix-invalid-doctest.rs - SomeStruct (line 10) stdout ----
8+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `is`
9+
--> remapped_path/remap-path-prefix-invalid-doctest.rs:11:6
10+
|
11+
LL | this is not real code
12+
| ^^ expected one of 8 possible tokens
13+
14+
error: aborting due to 1 previous error
15+
16+
Couldn't compile the test.
17+
18+
failures:
19+
remapped_path/remap-path-prefix-invalid-doctest.rs - SomeStruct (line 10)
20+
21+
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
//@ check-run-results
3+
4+
// FIXME: if/when the output of the test harness can be tested on its own, this test should be
5+
// adapted to use that, and that normalize line can go away
6+
7+
//@ compile-flags:--test -Z unstable-options --remap-path-prefix={{src-base}}=remapped_path --test-args --test-threads=1
8+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
9+
10+
// doctest passes at runtime
11+
/// ```
12+
/// assert!(true);
13+
/// ```
14+
pub struct SomeStruct;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test remapped_path/remap-path-prefix-passed-doctest-output.rs - SomeStruct (line 11) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

0 commit comments

Comments
 (0)