Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documented working directory behaviour for cargo test, cargo bench and cargo run #11901

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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