Skip to content

Commit 2035d0d

Browse files
committed
Auto merge of #12303 - epage:config, r=joshtriplett
fix(script): Process config relative to script, not CWD ### What does this PR try to resolve? This is part of the work for #12207. When you put in your path `foo.rs`: ```rust #!/usr/bin/env cargo fn main() {} ``` You expect it to build once and then repeatedly run the same version. However, `.cargo/config.toml` doesn't work like that (normally). It is an environment file, like `.env`, and is based on your current working directory. So if you run `foo.rs` from within a random project, it might rebuild due to RUSTFLAGS in `.cargo/config.toml`. I had some concern about whether this current behavior is right or not and [noted this in the Pre-RFC](https://github.com/epage/cargo-script-mvs/blob/main/0000-cargo-script.md#unresolved-questions). This came up again while we were [discussing editions on zulip](https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/cargo.20script.20and.20edition). In looking further into this, it turns out we already have precedence for this with `cargo install --path <path>`. ### How should we test and review this PR? The second commit has the fix, the docs, and a change to a test (from the first commit) to show that the fix actually changed behavior.
2 parents 5e842cc + 78334e8 commit 2035d0d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/bin/cargo/commands/run.rs

Lines changed: 9 additions & 1 deletion
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+
// location where the script resides, rather than the environment from where it's 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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,47 @@ fn main() {
332332
.run();
333333
}
334334

335+
#[cargo_test]
336+
fn use_script_config() {
337+
let script = ECHO_SCRIPT;
338+
let _ = cargo_test_support::project()
339+
.at("script")
340+
.file("script.rs", script)
341+
.build();
342+
343+
let p = cargo_test_support::project()
344+
.file(
345+
".cargo/config",
346+
r#"
347+
[build]
348+
rustc = "non-existent-rustc"
349+
"#,
350+
)
351+
.file("script.rs", script)
352+
.build();
353+
354+
// Verify the config is bad
355+
p.cargo("-Zscript script.rs -NotAnArg")
356+
.masquerade_as_nightly_cargo(&["script"])
357+
.with_status(101)
358+
.with_stderr_contains(
359+
"\
360+
[ERROR] could not execute process `non-existent-rustc -vV` (never executed)
361+
",
362+
)
363+
.run();
364+
365+
// Verify that the config isn't used
366+
p.cargo("-Zscript ../script/script.rs -NotAnArg")
367+
.masquerade_as_nightly_cargo(&["script"])
368+
.with_stdout(
369+
r#"bin: [..]/debug/script[EXE]
370+
args: ["-NotAnArg"]
371+
"#,
372+
)
373+
.run();
374+
}
375+
335376
#[cargo_test]
336377
fn test_line_numbering_preserved() {
337378
let script = r#"#!/usr/bin/env cargo

0 commit comments

Comments
 (0)