Skip to content

Commit 5d3a85e

Browse files
committed
Auto merge of #11901 - jofas:fix-for-11852, r=weihanglo
Documented working directory behaviour for `cargo test`, `cargo bench` and `cargo run` Fixes #11852. Updated the respective man-pages of `cargo test`, `cargo bench` and `cargo run` to include the information of the working directory of the executables. Added a comment in the code of `cargo run` where the default behaviour (the package's root directory) is overridden.
2 parents 39dba01 + 355ddf5 commit 5d3a85e

13 files changed

+79
-0
lines changed

src/cargo/ops/cargo_run.rs

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ pub fn run(
9393
};
9494
let pkg = bins[0].0;
9595
let mut process = compile.target_process(exe, unit.kind, pkg, *script_meta)?;
96+
97+
// Sets the working directory of the child process to the current working
98+
// directory of the parent process.
99+
// Overrides the default working directory of the `ProcessBuilder` returned
100+
// by `compile.target_process` (the package's root directory)
96101
process.args(args).cwd(config.cwd());
97102

98103
config.shell().status("Running", process.to_string())?;

src/doc/man/cargo-bench.md

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ debugger.
5656

5757
[`bench` profile]: ../reference/profiles.html#bench
5858

59+
### Working directory of benchmarks
60+
61+
The working directory of every benchmark is set to the root directory of the
62+
package the benchmark belongs to.
63+
Setting the working directory of benchmarks to the package's root directory
64+
makes it possible for benchmarks to reliably access the package's files using
65+
relative paths, regardless from where `cargo bench` was executed from.
66+
5967
## OPTIONS
6068

6169
### Benchmark Options

src/doc/man/cargo-run.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ All the arguments following the two dashes (`--`) are passed to the binary to
1717
run. If you're passing arguments to both Cargo and the binary, the ones after
1818
`--` go to the binary, the ones before go to Cargo.
1919

20+
Unlike {{man "cargo-test" 1}} and {{man "cargo-bench" 1}}, `cargo run` sets the
21+
working directory of the binary executed to the current working directory, same
22+
as if it was executed in the shell directly.
23+
2024
## OPTIONS
2125

2226
{{> section-options-package }}

src/doc/man/cargo-test.md

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ and may change in the future; beware of depending on it.
5757
See the [rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information
5858
on writing doc tests.
5959

60+
### Working directory of tests
61+
62+
The working directory of every test is set to the root directory of the package
63+
the test belongs to.
64+
Setting the working directory of tests to the package's root directory makes it
65+
possible for tests to reliably access the package's files using relative paths,
66+
regardless from where `cargo test` was executed from.
67+
6068
## OPTIONS
6169

6270
### Test Options

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

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ DESCRIPTION
4949
switch to the dev profile. You can then run the debug-enabled benchmark
5050
within a debugger.
5151

52+
Working directory of benchmarks
53+
The working directory of every benchmark is set to the root directory of
54+
the package the benchmark belongs to. Setting the working directory of
55+
benchmarks to the package’s root directory makes it possible for
56+
benchmarks to reliably access the package’s files using relative
57+
paths, regardless from where cargo bench was executed from.
58+
5259
OPTIONS
5360
Benchmark Options
5461
--no-run

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

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ DESCRIPTION
1313
to run. If you’re passing arguments to both Cargo and the binary, the
1414
ones after -- go to the binary, the ones before go to Cargo.
1515

16+
Unlike cargo-test(1) and cargo-bench(1), cargo run sets the working
17+
directory of the binary executed to the current working directory, same
18+
as if it was executed in the shell directly.
19+
1620
OPTIONS
1721
Package Selection
1822
By default, the package in the current working directory is selected.

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

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ DESCRIPTION
5252
See the rustdoc book <https://doc.rust-lang.org/rustdoc/> for more
5353
information on writing doc tests.
5454

55+
Working directory of tests
56+
The working directory of every test is set to the root directory of the
57+
package the test belongs to. Setting the working directory of tests to
58+
the package’s root directory makes it possible for tests to reliably
59+
access the package’s files using relative paths, regardless from where
60+
cargo test was executed from.
61+
5562
OPTIONS
5663
Test Options
5764
--no-run

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

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ debugger.
5656

5757
[`bench` profile]: ../reference/profiles.html#bench
5858

59+
### Working directory of benchmarks
60+
61+
The working directory of every benchmark is set to the root directory of the
62+
package the benchmark belongs to.
63+
Setting the working directory of benchmarks to the package's root directory
64+
makes it possible for benchmarks to reliably access the package's files using
65+
relative paths, regardless from where `cargo bench` was executed from.
66+
5967
## OPTIONS
6068

6169
### Benchmark Options

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ All the arguments following the two dashes (`--`) are passed to the binary to
1717
run. If you're passing arguments to both Cargo and the binary, the ones after
1818
`--` go to the binary, the ones before go to Cargo.
1919

20+
Unlike [cargo-test(1)](cargo-test.html) and [cargo-bench(1)](cargo-bench.html), `cargo run` sets the
21+
working directory of the binary executed to the current working directory, same
22+
as if it was executed in the shell directly.
23+
2024
## OPTIONS
2125

2226
### Package Selection

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

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ and may change in the future; beware of depending on it.
5757
See the [rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information
5858
on writing doc tests.
5959

60+
### Working directory of tests
61+
62+
The working directory of every test is set to the root directory of the package
63+
the test belongs to.
64+
Setting the working directory of tests to the package's root directory makes it
65+
possible for tests to reliably access the package's files using relative paths,
66+
regardless from where `cargo test` was executed from.
67+
6068
## OPTIONS
6169

6270
### Test Options

src/etc/man/cargo-bench.1

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ optimizations and disables debugging information. If you need to debug a
5757
benchmark, you can use the \fB\-\-profile=dev\fR command\-line option to switch to
5858
the dev profile. You can then run the debug\-enabled benchmark within a
5959
debugger.
60+
.SS "Working directory of benchmarks"
61+
The working directory of every benchmark is set to the root directory of the
62+
package the benchmark belongs to.
63+
Setting the working directory of benchmarks to the package\[cq]s root directory
64+
makes it possible for benchmarks to reliably access the package\[cq]s files using
65+
relative paths, regardless from where \fBcargo bench\fR was executed from.
6066
.SH "OPTIONS"
6167
.SS "Benchmark Options"
6268
.sp

src/etc/man/cargo-run.1

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Run a binary or example of the local package.
1313
All the arguments following the two dashes (\fB\-\-\fR) are passed to the binary to
1414
run. If you\[cq]re passing arguments to both Cargo and the binary, the ones after
1515
\fB\-\-\fR go to the binary, the ones before go to Cargo.
16+
.sp
17+
Unlike \fBcargo\-test\fR(1) and \fBcargo\-bench\fR(1), \fBcargo run\fR sets the
18+
working directory of the binary executed to the current working directory, same
19+
as if it was executed in the shell directly.
1620
.SH "OPTIONS"
1721
.SS "Package Selection"
1822
By default, the package in the current working directory is selected. The \fB\-p\fR

src/etc/man/cargo-test.1

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ and may change in the future; beware of depending on it.
5353
.sp
5454
See the \fIrustdoc book\fR <https://doc.rust\-lang.org/rustdoc/> for more information
5555
on writing doc tests.
56+
.SS "Working directory of tests"
57+
The working directory of every test is set to the root directory of the package
58+
the test belongs to.
59+
Setting the working directory of tests to the package\[cq]s root directory makes it
60+
possible for tests to reliably access the package\[cq]s files using relative paths,
61+
regardless from where \fBcargo test\fR was executed from.
5662
.SH "OPTIONS"
5763
.SS "Test Options"
5864
.sp

0 commit comments

Comments
 (0)