Skip to content

Commit

Permalink
Change config paths to only check CARGO_HOME for cargo-script
Browse files Browse the repository at this point in the history
Signed-off-by: Rustin170506 <[email protected]>
  • Loading branch information
Rustin170506 committed Nov 3, 2024
1 parent edd36eb commit a61d845
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 25 additions & 11 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -318,7 +321,28 @@ rustc = "non-existent-rustc"
.file("script.rs", script)
.build();

// Verify the config is bad
// Verify that the local config 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)
Expand All @@ -328,16 +352,6 @@ rustc = "non-existent-rustc"
Caused by:
[NOT_FOUND]
"#]])
.run();

// Verify that the config isn't 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();
}
Expand Down

0 comments on commit a61d845

Please sign in to comment.