Skip to content

Commit

Permalink
fix(script): Process config relative to script, not CWD
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 22, 2023
1 parent 9d85c89 commit e2ed419
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ pub fn is_manifest_command(arg: &str) -> bool {
|| path.file_name() == Some(OsStr::new("Cargo.toml"))
}

pub fn exec_manifest_command(config: &Config, cmd: &str, args: &[OsString]) -> CliResult {
pub fn exec_manifest_command(config: &mut Config, cmd: &str, args: &[OsString]) -> CliResult {
if !config.cli_unstable().script {
return Err(anyhow::anyhow!("running `{cmd}` requires `-Zscript`").into());
}

let manifest_path = Path::new(cmd);
let manifest_path = root_manifest(Some(manifest_path), config)?;

// Treat `cargo foo.rs` like `cargo install --path foo` and re-evaluate the config based on the
// environment where the script resides, rather than the environment from where its being run.
let parent_path = manifest_path
.parent()
.expect("a file should always have a parent");
config.reload_rooted_at(parent_path)?;

let mut ws = Workspace::new(&manifest_path, config)?;
if config.cli_unstable().avoid_dev_deps {
ws.set_require_optional_deps(false);
Expand Down
3 changes: 3 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,9 @@ A parameter is identified as a manifest-command if it has one of:
- A `.rs` extension
- The file name is `Cargo.toml`

Differences between `cargo run --manifest-path <path>` and `cargo <path>`
- `cargo <path>` runs with the config for `<path>` and not the current dir, more like `cargo install --path <path>`

### `[lints]`

* Tracking Issue: [#12115](https://github.com/rust-lang/cargo/issues/12115)
Expand Down
15 changes: 7 additions & 8 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ rustc = "non-existent-rustc"
.build();

// Verify the config is bad
p.cargo("-Zscript script.rs")
p.cargo("-Zscript script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stderr_contains(
Expand All @@ -362,14 +362,13 @@ rustc = "non-existent-rustc"
)
.run();

// Verify that the config is still used
p.cargo("-Zscript ../script/script.rs")
// Verify that the config isn't used
p.cargo("-Zscript ../script/script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stderr_contains(
"\
[ERROR] could not execute process `non-existent-rustc -vV` (never executed)
",
.with_stdout(
r#"bin: [..]/debug/script[EXE]
args: ["-NotAnArg"]
"#,
)
.run();
}
Expand Down

0 comments on commit e2ed419

Please sign in to comment.