Skip to content

Commit c4e271f

Browse files
committed
Enable doctest-in-workspace by default
This stabilizes and enables the `-Z doctest-in-workspace` flag by default. Also adds another testcase to make sure that the `include!()` and `file!()` macros interact well together.
1 parent 0d5370a commit c4e271f

File tree

8 files changed

+148
-15
lines changed

8 files changed

+148
-15
lines changed

src/cargo/core/features.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ unstable_cli_options!(
730730
config_include: bool = ("Enable the `include` key in config files"),
731731
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
732732
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
733-
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
734733
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
735734
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
736735
features: Option<Vec<String>> = (HIDDEN),
@@ -800,6 +799,9 @@ const STABILIZED_NAMED_PROFILES: &str = "The named-profiles feature is now alway
800799
See https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles \
801800
for more information";
802801

802+
const STABILIZED_DOCTEST_IN_WORKSPACE: &str =
803+
"The doctest-in-workspace feature is now always enabled.";
804+
803805
const STABILIZED_FUTURE_INCOMPAT_REPORT: &str =
804806
"The future-incompat-report feature is now always enabled.";
805807

@@ -1080,6 +1082,7 @@ impl CliUnstable {
10801082
"multitarget" => stabilized_warn(k, "1.64", STABILISED_MULTITARGET),
10811083
"sparse-registry" => stabilized_warn(k, "1.68", STABILISED_SPARSE_REGISTRY),
10821084
"terminal-width" => stabilized_warn(k, "1.68", STABILIZED_TERMINAL_WIDTH),
1085+
"doctest-in-workspace" => stabilized_warn(k, "1.72", STABILIZED_DOCTEST_IN_WORKSPACE),
10831086

10841087
// Unstable features
10851088
// Sorted alphabetically:
@@ -1098,7 +1101,6 @@ impl CliUnstable {
10981101
"config-include" => self.config_include = parse_empty(k, v)?,
10991102
"credential-process" => self.credential_process = parse_empty(k, v)?,
11001103
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,
1101-
"doctest-in-workspace" => self.doctest_in_workspace = parse_empty(k, v)?,
11021104
"doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?,
11031105
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
11041106
"gitoxide" => {

src/cargo/ops/cargo_test.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ fn run_doc_tests(
172172
let config = ws.config();
173173
let mut errors = Vec::new();
174174
let doctest_xcompile = config.cli_unstable().doctest_xcompile;
175-
let doctest_in_workspace = config.cli_unstable().doctest_in_workspace;
176175

177176
for doctest_info in &compilation.to_doc_test {
178177
let Doctest {
@@ -215,13 +214,9 @@ fn run_doc_tests(
215214
p.arg("--crate-name").arg(&unit.target.crate_name());
216215
p.arg("--test");
217216

218-
if doctest_in_workspace {
219-
add_path_args(ws, unit, &mut p);
220-
p.arg("--test-run-directory")
221-
.arg(unit.pkg.root().to_path_buf());
222-
} else {
223-
p.arg(unit.target.src_path().path().unwrap());
224-
}
217+
add_path_args(ws, unit, &mut p);
218+
p.arg("--test-run-directory")
219+
.arg(unit.pkg.root().to_path_buf());
225220

226221
if let CompileKind::Target(target) = unit.kind {
227222
// use `rustc_target()` to properly handle JSON target paths

src/doc/man/cargo-test.md

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ Setting the working directory of tests to the package's root directory makes it
6565
possible for tests to reliably access the package's files using relative paths,
6666
regardless from where `cargo test` was executed from.
6767

68+
For documentation tests, the working directory when invoking `rustdoc` is set to
69+
the workspace root directory, and is also the directory `rustdoc` uses as the
70+
compilation directory of each documentation test.
71+
The corking directory when running each documentation test is set to the root
72+
directory of the package the test belongs to, and is controlled via `rustdoc`s
73+
`--test-run-directory` option.
74+
6875
## OPTIONS
6976

7077
### Test Options

src/doc/man/generated_txt/cargo-test.txt

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ DESCRIPTION
5959
access the package’s files using relative paths, regardless from where
6060
cargo test was executed from.
6161

62+
For documentation tests, the working directory when invoking rustdoc is
63+
set to the workspace root directory, and is also the directory rustdoc
64+
uses as the compilation directory of each documentation test. The
65+
corking directory when running each documentation test is set to the
66+
root directory of the package the test belongs to, and is controlled via
67+
rustdocs --test-run-directory option.
68+
6269
OPTIONS
6370
Test Options
6471
--no-run

src/doc/src/commands/cargo-test.md

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ Setting the working directory of tests to the package's root directory makes it
6565
possible for tests to reliably access the package's files using relative paths,
6666
regardless from where `cargo test` was executed from.
6767

68+
For documentation tests, the working directory when invoking `rustdoc` is set to
69+
the workspace root directory, and is also the directory `rustdoc` uses as the
70+
compilation directory of each documentation test.
71+
The corking directory when running each documentation test is set to the root
72+
directory of the package the test belongs to, and is controlled via `rustdoc`s
73+
`--test-run-directory` option.
74+
6875
## OPTIONS
6976

7077
### Test Options

src/doc/src/reference/unstable.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ Each new feature described below should explain how to use it.
8585
* [host-config](#host-config) --- Allows setting `[target]`-like configuration settings for host build targets.
8686
* [target-applies-to-host](#target-applies-to-host) --- Alters whether certain flags will be passed to host build targets.
8787
* rustdoc
88-
* [`doctest-in-workspace`](#doctest-in-workspace) --- Fixes workspace-relative paths when running doctests.
8988
* [rustdoc-map](#rustdoc-map) --- Provides mappings for documentation to link to external sites like [docs.rs](https://docs.rs/).
9089
* [scrape-examples](#scrape-examples) --- Shows examples within documentation.
9190
* `Cargo.toml` extensions
@@ -1793,3 +1792,23 @@ See [Registry Protocols](registries.md#registry-protocols) for more information.
17931792
The [`cargo logout`] command has been stabilized in the 1.70 release.
17941793

17951794
[target triple]: ../appendix/glossary.md#target '"target" (glossary)'
1795+
1796+
1797+
### `doctest-in-workspace`
1798+
1799+
The `-Z doctest-in-workspace` option for `cargo test` has been stabilized and
1800+
enabled by default in the 1.72 release.
1801+
1802+
This changes the behavior of the current working
1803+
directory used when running doctests. Historically, Cargo has run `rustdoc
1804+
--test` relative to the root of the package, with paths relative from that
1805+
root. However, this is inconsistent with how `rustc` and `rustdoc` are
1806+
normally run in a workspace, where they are run relative to the workspace
1807+
root. This inconsistency causes problems in various ways, such as when passing
1808+
RUSTDOCFLAGS with relative paths, or dealing with diagnostic output.
1809+
1810+
Cargo is now running `rustdoc`
1811+
from the root of the workspace. It also passes the `--test-run-directory` to
1812+
`rustdoc` so that when *running* the tests, they are run from the root of the
1813+
package. This preserves backwards compatibility and is consistent with how
1814+
normal unittests are run.

src/etc/man/cargo-test.1

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ the test belongs to.
5959
Setting the working directory of tests to the package\[cq]s root directory makes it
6060
possible for tests to reliably access the package\[cq]s files using relative paths,
6161
regardless from where \fBcargo test\fR was executed from.
62+
.sp
63+
For documentation tests, the working directory when invoking \fBrustdoc\fR is set to
64+
the workspace root directory, and is also the directory \fBrustdoc\fR uses as the
65+
compilation directory of each documentation test.
66+
The corking directory when running each documentation test is set to the root
67+
directory of the package the test belongs to, and is controlled via \fBrustdoc\fRs
68+
\fB\-\-test\-run\-directory\fR option.
6269
.SH "OPTIONS"
6370
.SS "Test Options"
6471
.sp

tests/testsuite/doc.rs

+93-4
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ fn crate_versions_flag_is_overridden() {
20412041
asserts(output_documentation());
20422042
}
20432043

2044-
#[cargo_test(nightly, reason = "-Zdoctest-in-workspace is unstable")]
2044+
#[cargo_test]
20452045
fn doc_test_in_workspace() {
20462046
let p = project()
20472047
.file(
@@ -2087,16 +2087,14 @@ fn doc_test_in_workspace() {
20872087
",
20882088
)
20892089
.build();
2090-
p.cargo("test -Zdoctest-in-workspace --doc -vv")
2091-
.masquerade_as_nightly_cargo(&["doctest-in-workspace"])
2090+
p.cargo("test --doc -vv")
20922091
.with_stderr_contains("[DOCTEST] crate-a")
20932092
.with_stdout_contains(
20942093
"
20952094
running 1 test
20962095
test crate-a/src/lib.rs - (line 1) ... ok
20972096
20982097
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2099-
21002098
",
21012099
)
21022100
.with_stderr_contains("[DOCTEST] crate-b")
@@ -2106,7 +2104,98 @@ running 1 test
21062104
test crate-b/src/lib.rs - (line 1) ... ok
21072105
21082106
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2107+
",
2108+
)
2109+
.run();
2110+
}
2111+
2112+
/// This is a test for <https://github.com/rust-lang/rust/issues/46372>.
2113+
/// The `file!()` macro inside of an `include!()` should output
2114+
/// workspace-relative paths, just like it does in other cases.
2115+
#[cargo_test]
2116+
fn doc_test_include_file() {
2117+
let p = project()
2118+
.file(
2119+
"Cargo.toml",
2120+
r#"
2121+
[workspace]
2122+
members = [
2123+
"child",
2124+
]
2125+
[package]
2126+
name = "root"
2127+
version = "0.1.0"
2128+
"#,
2129+
)
2130+
.file(
2131+
"src/lib.rs",
2132+
r#"
2133+
/// ```
2134+
/// assert_eq!("src/lib.rs", file!().replace("\\", "/"))
2135+
/// ```
2136+
pub mod included {
2137+
include!(concat!("../", file!(), ".included.rs"));
2138+
}
2139+
"#,
2140+
)
2141+
.file(
2142+
"src/lib.rs.included.rs",
2143+
r#"
2144+
/// ```
2145+
/// assert_eq!(1, 1)
2146+
/// ```
2147+
pub fn foo() {}
2148+
"#,
2149+
)
2150+
.file(
2151+
"child/Cargo.toml",
2152+
r#"
2153+
[package]
2154+
name = "child"
2155+
version = "0.1.0"
2156+
"#,
2157+
)
2158+
.file(
2159+
"child/src/lib.rs",
2160+
r#"
2161+
/// ```
2162+
/// assert_eq!("child/src/lib.rs", file!().replace("\\", "/"))
2163+
/// ```
2164+
pub mod included {
2165+
include!(concat!("../../", file!(), ".included.rs"));
2166+
}
2167+
"#,
2168+
)
2169+
.file(
2170+
"child/src/lib.rs.included.rs",
2171+
r#"
2172+
/// ```
2173+
/// assert_eq!(1, 1)
2174+
/// ```
2175+
pub fn foo() {}
2176+
"#,
2177+
)
2178+
.build();
2179+
2180+
p.cargo("test --workspace --doc -vv -- --test-threads=1")
2181+
.with_stderr_contains("[DOCTEST] child")
2182+
.with_stdout_contains(
2183+
"
2184+
running 2 tests
2185+
test child/src/../../child/src/lib.rs.included.rs - included::foo (line 2) ... ok
2186+
test child/src/lib.rs - included (line 2) ... ok
2187+
2188+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2189+
",
2190+
)
2191+
.with_stderr_contains("[DOCTEST] root")
2192+
.with_stdout_contains(
2193+
"
2194+
running 2 tests
2195+
test src/../src/lib.rs.included.rs - included::foo (line 2) ... ok
2196+
test src/lib.rs - included (line 2) ... ok
21092197
2198+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
21102199
",
21112200
)
21122201
.run();

0 commit comments

Comments
 (0)