From bd47da1ab129ac6c086f74892327f3896f9f847c Mon Sep 17 00:00:00 2001 From: Rustin170506 <29879298+Rustin170506@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:30:01 +0800 Subject: [PATCH] Change config paths to only check CARGO_HOME for cargo-script Signed-off-by: Rustin170506 <29879298+Rustin170506@users.noreply.github.com> --- src/bin/cargo/commands/run.rs | 8 ++----- tests/testsuite/script.rs | 42 +++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index 61feefeb34c..b4873628297 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -171,12 +171,8 @@ pub fn exec_manifest_command(gctx: &mut GlobalContext, cmd: &str, args: &[OsStri let manifest_path = root_manifest(Some(manifest_path), gctx)?; - // 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)?; + // Reload to cargo home. + gctx.reload_rooted_at(gctx.home().clone().into_path_unlocked())?; let mut ws = Workspace::new(&manifest_path, gctx)?; if gctx.cli_unstable().avoid_dev_deps { diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 9173ddda9f3..3adcd93f569 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1,4 +1,7 @@ +use std::fs; + use cargo_test_support::basic_manifest; +use cargo_test_support::paths::cargo_home; use cargo_test_support::prelude::*; use cargo_test_support::registry::Package; use cargo_test_support::str; @@ -300,7 +303,7 @@ msg = hello } #[cargo_test] -fn use_script_config() { +fn use_cargo_home_config() { let script = ECHO_SCRIPT; let _ = cargo_test_support::project() .at("script") @@ -318,26 +321,47 @@ rustc = "non-existent-rustc" .file("script.rs", script) .build(); - // Verify the config is bad + // Verify that the config from the current directory 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] + .with_stdout_data(str![[r#" +bin: [ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] +args: ["-NotAnArg"] "#]]) .run(); - // Verify that the config isn't used + // Verify that the config from the parent directory is not used p.cargo("-Zscript ../script/script.rs -NotAnArg") .masquerade_as_nightly_cargo(&["script"]) .with_stdout_data(str![[r#" bin: [ROOT]/home/.cargo/target/[HASH]/debug/script[EXE] 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(); }