Skip to content
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
8 changes: 6 additions & 2 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ pub fn exec_manifest_command(gctx: &mut GlobalContext, cmd: &str, args: &[OsStri

let manifest_path = root_manifest(Some(manifest_path), gctx)?;

// Reload to cargo home.
gctx.reload_rooted_at(gctx.home().clone().into_path_unlocked())?;
// 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");
gctx.reload_rooted_at(parent_path)?;

let mut ws = Workspace::new(&manifest_path, gctx)?;
if gctx.cli_unstable().avoid_dev_deps {
Expand Down
43 changes: 9 additions & 34 deletions tests/testsuite/script/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::fs;

use crate::prelude::*;
use cargo_test_support::basic_manifest;
use cargo_test_support::paths::cargo_home;
use cargo_test_support::registry::Package;
use cargo_test_support::str;

Expand Down Expand Up @@ -394,7 +391,7 @@ msg = hello
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn use_cargo_home_config() {
fn use_script_config() {
let script = ECHO_SCRIPT;
let _ = cargo_test_support::project()
.at("script")
Expand All @@ -412,49 +409,27 @@ rustc = "non-existent-rustc"
.file("script.rs", script)
.build();

// Verify that the config from the current directory is used
// Verify the config is bad
p.cargo("-Zscript script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/script[EXE]
arg0: [..]
args: ["-NotAnArg"]
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] could not execute process `non-existent-rustc -vV` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();

// Verify that the config from the parent directory is not used
// Verify that the config isn't used
p.cargo("-Zscript ../script/script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/script[EXE]
arg0: [..]
args: ["-NotAnArg"]

"#]])
.run();

// Write a global config.toml in the cargo home directory
let cargo_home = cargo_home();
fs::write(
&cargo_home.join("config.toml"),
r#"
[build]
rustc = "non-existent-rustc"
"#,
)
.unwrap();

// Verify the global config is used
p.cargo("-Zscript script.rs -NotAnArg")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] could not execute process `non-existent-rustc -vV` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}
Expand Down