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

fix(script): Process config relative to script, not CWD #12303

Merged
merged 2 commits into from
Jun 22, 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
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
// location where the script resides, rather than the environment from where it's 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
41 changes: 41 additions & 0 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,47 @@ fn main() {
.run();
}

#[cargo_test]
fn use_script_config() {
let script = ECHO_SCRIPT;
let _ = cargo_test_support::project()
.at("script")
.file("script.rs", script)
.build();

let p = cargo_test_support::project()
.file(
".cargo/config",
r#"
[build]
rustc = "non-existent-rustc"
"#,
)
.file("script.rs", script)
.build();

// Verify the config is bad
p.cargo("-Zscript 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)
",
)
.run();

// Verify that the config isn't used
p.cargo("-Zscript ../script/script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout(
r#"bin: [..]/debug/script[EXE]
args: ["-NotAnArg"]
"#,
)
.run();
}

#[cargo_test]
fn test_line_numbering_preserved() {
let script = r#"#!/usr/bin/env cargo
Expand Down