Skip to content

Commit 5629df7

Browse files
authored
fix(script): Default bin.name to package.name (#16064)
### What does this PR try to resolve? The current behavior breaks from how implicit bins normally work when `package.name` is set. When it isn't set, they were using the same logic, so that should be fine. ### How to test and review this PR?
2 parents 45941d6 + b00064c commit 5629df7

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,7 @@ fn normalize_toml(
379379
warnings,
380380
)?;
381381
let original_toml_bin = if is_embedded {
382-
let manifest_file_stem = manifest_file
383-
.file_stem()
384-
.expect("file name enforced previously");
385-
let name = embedded::sanitize_name(manifest_file_stem.to_string_lossy().as_ref());
382+
let name = package_name.as_ref().to_owned();
386383
let manifest_file_name = manifest_file
387384
.file_name()
388385
.expect("file name enforced previously");

tests/testsuite/script/cargo.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,70 @@ args: []
673673
.run();
674674
}
675675

676+
#[cargo_test(nightly, reason = "-Zscript is unstable")]
677+
fn test_name_is_deps_dir_implicit() {
678+
let script = ECHO_SCRIPT;
679+
let p = cargo_test_support::project()
680+
.file("deps.rs", script)
681+
.build();
682+
683+
p.cargo("-Zscript -v deps.rs")
684+
.masquerade_as_nightly_cargo(&["script"])
685+
.with_stdout_data(str![[r#"
686+
current_exe: [ROOT]/home/.cargo/target/[HASH]/debug/deps-[EXE]
687+
arg0: [..]
688+
args: []
689+
690+
"#]])
691+
.with_stderr_data(str![[r#"
692+
[WARNING] `package.edition` is unspecified, defaulting to `2024`
693+
[COMPILING] deps- v0.0.0 ([ROOT]/foo/deps.rs)
694+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
695+
[RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/deps-[EXE]`
696+
697+
"#]])
698+
.run();
699+
}
700+
701+
#[cargo_test(nightly, reason = "-Zscript is unstable")]
702+
fn test_name_is_deps_dir_explicit() {
703+
let script = r#"#!/usr/bin/env cargo
704+
---
705+
package.name = "deps"
706+
---
707+
708+
fn main() {
709+
let current_exe = std::env::current_exe().unwrap().to_str().unwrap().to_owned();
710+
let mut args = std::env::args_os();
711+
let arg0 = args.next().unwrap().to_str().unwrap().to_owned();
712+
let args = args.collect::<Vec<_>>();
713+
println!("current_exe: {current_exe}");
714+
println!("arg0: {arg0}");
715+
println!("args: {args:?}");
716+
}
717+
718+
#[test]
719+
fn test () {}
720+
"#;
721+
let p = cargo_test_support::project()
722+
.file("deps.rs", script)
723+
.build();
724+
725+
p.cargo("-Zscript -v deps.rs")
726+
.masquerade_as_nightly_cargo(&["script"])
727+
.with_status(101)
728+
.with_stdout_data(str![""])
729+
.with_stderr_data(str![[r#"
730+
[WARNING] `package.edition` is unspecified, defaulting to `2024`
731+
[ERROR] failed to parse manifest at `[ROOT]/foo/deps.rs`
732+
733+
Caused by:
734+
the binary target name `deps` is forbidden, it conflicts with cargo's build directory names
735+
736+
"#]])
737+
.run();
738+
}
739+
676740
#[cargo_test(nightly, reason = "-Zscript is unstable")]
677741
fn script_like_dir() {
678742
let p = cargo_test_support::project()

0 commit comments

Comments
 (0)