Skip to content

Commit

Permalink
Auto merge of #11901 - jofas:fix-for-11852, r=weihanglo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Mar 29, 2023
2 parents 39dba01 + 355ddf5 commit 5d3a85e
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ pub fn run(
};
let pkg = bins[0].0;
let mut process = compile.target_process(exe, unit.kind, pkg, *script_meta)?;

// Sets the working directory of the child process to the current working
// directory of the parent process.
// Overrides the default working directory of the `ProcessBuilder` returned
// by `compile.target_process` (the package's root directory)
process.args(args).cwd(config.cwd());

config.shell().status("Running", process.to_string())?;
Expand Down
8 changes: 8 additions & 0 deletions src/doc/man/cargo-bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ debugger.

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

### Working directory of benchmarks

The working directory of every benchmark is set to the root directory of the
package the benchmark belongs to.
Setting the working directory of benchmarks to the package's root directory
makes it possible for benchmarks to reliably access the package's files using
relative paths, regardless from where `cargo bench` was executed from.

## OPTIONS

### Benchmark Options
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/cargo-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ All the arguments following the two dashes (`--`) are passed to the binary to
run. If you're passing arguments to both Cargo and the binary, the ones after
`--` go to the binary, the ones before go to Cargo.

Unlike {{man "cargo-test" 1}} and {{man "cargo-bench" 1}}, `cargo run` sets the
working directory of the binary executed to the current working directory, same
as if it was executed in the shell directly.

## OPTIONS

{{> section-options-package }}
Expand Down
8 changes: 8 additions & 0 deletions src/doc/man/cargo-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ and may change in the future; beware of depending on it.
See the [rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information
on writing doc tests.

### Working directory of tests

The working directory of every test is set to the root directory of the package
the test belongs to.
Setting the working directory of tests to the package's root directory makes it
possible for tests to reliably access the package's files using relative paths,
regardless from where `cargo test` was executed from.

## OPTIONS

### Test Options
Expand Down
7 changes: 7 additions & 0 deletions src/doc/man/generated_txt/cargo-bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ DESCRIPTION
switch to the dev profile. You can then run the debug-enabled benchmark
within a debugger.

Working directory of benchmarks
The working directory of every benchmark is set to the root directory of
the package the benchmark belongs to. Setting the working directory of
benchmarks to the package’s root directory makes it possible for
benchmarks to reliably access the package’s files using relative
paths, regardless from where cargo bench was executed from.

OPTIONS
Benchmark Options
--no-run
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/generated_txt/cargo-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ DESCRIPTION
to run. If you’re passing arguments to both Cargo and the binary, the
ones after -- go to the binary, the ones before go to Cargo.

Unlike cargo-test(1) and cargo-bench(1), cargo run sets the working
directory of the binary executed to the current working directory, same
as if it was executed in the shell directly.

OPTIONS
Package Selection
By default, the package in the current working directory is selected.
Expand Down
7 changes: 7 additions & 0 deletions src/doc/man/generated_txt/cargo-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ DESCRIPTION
See the rustdoc book <https://doc.rust-lang.org/rustdoc/> for more
information on writing doc tests.

Working directory of tests
The working directory of every test is set to the root directory of the
package the test belongs to. Setting the working directory of tests to
the package’s root directory makes it possible for tests to reliably
access the package’s files using relative paths, regardless from where
cargo test was executed from.

OPTIONS
Test Options
--no-run
Expand Down
8 changes: 8 additions & 0 deletions src/doc/src/commands/cargo-bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ debugger.

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

### Working directory of benchmarks

The working directory of every benchmark is set to the root directory of the
package the benchmark belongs to.
Setting the working directory of benchmarks to the package's root directory
makes it possible for benchmarks to reliably access the package's files using
relative paths, regardless from where `cargo bench` was executed from.

## OPTIONS

### Benchmark Options
Expand Down
4 changes: 4 additions & 0 deletions src/doc/src/commands/cargo-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ All the arguments following the two dashes (`--`) are passed to the binary to
run. If you're passing arguments to both Cargo and the binary, the ones after
`--` go to the binary, the ones before go to Cargo.

Unlike [cargo-test(1)](cargo-test.html) and [cargo-bench(1)](cargo-bench.html), `cargo run` sets the
working directory of the binary executed to the current working directory, same
as if it was executed in the shell directly.

## OPTIONS

### Package Selection
Expand Down
8 changes: 8 additions & 0 deletions src/doc/src/commands/cargo-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ and may change in the future; beware of depending on it.
See the [rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information
on writing doc tests.

### Working directory of tests

The working directory of every test is set to the root directory of the package
the test belongs to.
Setting the working directory of tests to the package's root directory makes it
possible for tests to reliably access the package's files using relative paths,
regardless from where `cargo test` was executed from.

## OPTIONS

### Test Options
Expand Down
6 changes: 6 additions & 0 deletions src/etc/man/cargo-bench.1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ optimizations and disables debugging information. If you need to debug a
benchmark, you can use the \fB\-\-profile=dev\fR command\-line option to switch to
the dev profile. You can then run the debug\-enabled benchmark within a
debugger.
.SS "Working directory of benchmarks"
The working directory of every benchmark is set to the root directory of the
package the benchmark belongs to.
Setting the working directory of benchmarks to the package\[cq]s root directory
makes it possible for benchmarks to reliably access the package\[cq]s files using
relative paths, regardless from where \fBcargo bench\fR was executed from.
.SH "OPTIONS"
.SS "Benchmark Options"
.sp
Expand Down
4 changes: 4 additions & 0 deletions src/etc/man/cargo-run.1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Run a binary or example of the local package.
All the arguments following the two dashes (\fB\-\-\fR) are passed to the binary to
run. If you\[cq]re passing arguments to both Cargo and the binary, the ones after
\fB\-\-\fR go to the binary, the ones before go to Cargo.
.sp
Unlike \fBcargo\-test\fR(1) and \fBcargo\-bench\fR(1), \fBcargo run\fR sets the
working directory of the binary executed to the current working directory, same
as if it was executed in the shell directly.
.SH "OPTIONS"
.SS "Package Selection"
By default, the package in the current working directory is selected. The \fB\-p\fR
Expand Down
6 changes: 6 additions & 0 deletions src/etc/man/cargo-test.1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ and may change in the future; beware of depending on it.
.sp
See the \fIrustdoc book\fR <https://doc.rust\-lang.org/rustdoc/> for more information
on writing doc tests.
.SS "Working directory of tests"
The working directory of every test is set to the root directory of the package
the test belongs to.
Setting the working directory of tests to the package\[cq]s root directory makes it
possible for tests to reliably access the package\[cq]s files using relative paths,
regardless from where \fBcargo test\fR was executed from.
.SH "OPTIONS"
.SS "Test Options"
.sp
Expand Down

0 comments on commit 5d3a85e

Please sign in to comment.