Skip to content

Commit e2ed419

Browse files
committed
fix(script): Process config relative to script, not CWD
1 parent 9d85c89 commit e2ed419

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/bin/cargo/commands/run.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,21 @@ pub fn is_manifest_command(arg: &str) -> bool {
9393
|| path.file_name() == Some(OsStr::new("Cargo.toml"))
9494
}
9595

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

101101
let manifest_path = Path::new(cmd);
102102
let manifest_path = root_manifest(Some(manifest_path), config)?;
103+
104+
// Treat `cargo foo.rs` like `cargo install --path foo` and re-evaluate the config based on the
105+
// environment where the script resides, rather than the environment from where its being run.
106+
let parent_path = manifest_path
107+
.parent()
108+
.expect("a file should always have a parent");
109+
config.reload_rooted_at(parent_path)?;
110+
103111
let mut ws = Workspace::new(&manifest_path, config)?;
104112
if config.cli_unstable().avoid_dev_deps {
105113
ws.set_require_optional_deps(false);

src/doc/src/reference/unstable.md

+3
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,9 @@ A parameter is identified as a manifest-command if it has one of:
14811481
- A `.rs` extension
14821482
- The file name is `Cargo.toml`
14831483

1484+
Differences between `cargo run --manifest-path <path>` and `cargo <path>`
1485+
- `cargo <path>` runs with the config for `<path>` and not the current dir, more like `cargo install --path <path>`
1486+
14841487
### `[lints]`
14851488

14861489
* Tracking Issue: [#12115](https://github.com/rust-lang/cargo/issues/12115)

tests/testsuite/script.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ rustc = "non-existent-rustc"
352352
.build();
353353

354354
// Verify the config is bad
355-
p.cargo("-Zscript script.rs")
355+
p.cargo("-Zscript script.rs -NotAnArg")
356356
.masquerade_as_nightly_cargo(&["script"])
357357
.with_status(101)
358358
.with_stderr_contains(
@@ -362,14 +362,13 @@ rustc = "non-existent-rustc"
362362
)
363363
.run();
364364

365-
// Verify that the config is still used
366-
p.cargo("-Zscript ../script/script.rs")
365+
// Verify that the config isn't used
366+
p.cargo("-Zscript ../script/script.rs -NotAnArg")
367367
.masquerade_as_nightly_cargo(&["script"])
368-
.with_status(101)
369-
.with_stderr_contains(
370-
"\
371-
[ERROR] could not execute process `non-existent-rustc -vV` (never executed)
372-
",
368+
.with_stdout(
369+
r#"bin: [..]/debug/script[EXE]
370+
args: ["-NotAnArg"]
371+
"#,
373372
)
374373
.run();
375374
}

0 commit comments

Comments
 (0)