Skip to content

Commit d9f78cb

Browse files
committed
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 but with minor adjustments. This flag similarly takes in two paths, a prefix to replace and a replacement string.
1 parent 212841e commit d9f78cb

11 files changed

+135
-7
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub(crate) fn run(
129129
edition: options.edition,
130130
target_triple: options.target.clone(),
131131
crate_name: options.crate_name.clone(),
132+
remap_path_prefix: options.remap_path_prefix.clone(),
132133
..config::Options::default()
133134
};
134135

@@ -572,7 +573,6 @@ fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
572573
std::env::current_dir().map(|c| c.join(&path)).unwrap_or_else(|_| path)
573574
}
574575
}
575-
576576
struct IndividualTestOptions {
577577
outdir: DirState,
578578
test_id: String,
@@ -651,7 +651,7 @@ impl CreateRunnableDoctests {
651651
if !item_path.is_empty() {
652652
item_path.push(' ');
653653
}
654-
format!("{} - {item_path}(line {line})", filename.prefer_local())
654+
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionaly())
655655
}
656656

657657
fn add_test(&mut self, test: ScrapedDoctest) {

src/librustdoc/doctest/rust.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ struct RustCollector {
2727
impl RustCollector {
2828
fn get_filename(&self) -> FileName {
2929
let filename = self.source_map.span_to_filename(self.position);
30-
if let FileName::Real(ref filename) = filename
31-
&& let Ok(cur_dir) = env::current_dir()
32-
&& let Some(local_path) = filename.local_path()
33-
&& let Ok(path) = local_path.strip_prefix(&cur_dir)
34-
{
30+
if let FileName::Real(ref filename) = filename {
31+
let path = filename.remapped_path_if_available();
32+
// Strip the cwd prefix from the path. This will likely exist if
33+
// the path was not remapped.
34+
let path = env::current_dir()
35+
.map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
36+
.unwrap_or(path);
3537
return path.to_owned().into();
3638
}
3739
filename

src/librustdoc/lib.rs

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

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,14 @@
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+
//@ failure-status: 101
5+
//@ compile-flags:--test -Z unstable-options --remap-path-prefix={{src-base}}=remapped_path --test-args --test-threads=1
6+
//@ rustc-env:RUST_BACKTRACE=0
7+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
8+
//@ normalize-stdout-test "exit (status|code): 101" -> "exit status: 101"
9+
10+
// doctest fails at runtime
11+
/// ```
12+
/// panic!("oh no");
13+
/// ```
14+
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 11) ... FAILED
4+
5+
failures:
6+
7+
---- remapped_path/remap-path-prefix-failed-doctest-output.rs - SomeStruct (line 11) 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 11)
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+
//@ failure-status: 101
5+
//@ compile-flags:--test -Z unstable-options --remap-path-prefix={{src-base}}=remapped_path --test-args --test-threads=1
6+
//@ rustc-env:RUST_BACKTRACE=0
7+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
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)