Skip to content

Commit d5bfd58

Browse files
committed
Auto merge of #4046 - jmatraszek:fix_rust_41797, r=alexcrichton
Change inferring bin's name logic Should fix rust-lang/rust#41797.
2 parents 00af72e + f793373 commit d5bfd58

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/cargo/util/toml.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,11 +1422,25 @@ fn inferred_bin_path(bin: &TomlBinTarget,
14221422
}
14231423

14241424
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
1425+
}
1426+
1427+
// bin_len > 1
1428+
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
1429+
if package_root.join(&path).exists() {
1430+
return path.to_path_buf()
1431+
}
1432+
1433+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1434+
if package_root.join(&path).exists() {
1435+
return path.to_path_buf()
1436+
}
14251437

1438+
let path = Path::new("src").join("bin").join(&format!("main.rs"));
1439+
if package_root.join(&path).exists() {
1440+
return path.to_path_buf()
14261441
}
14271442

1428-
// here we have multiple bins, so they are expected to be located inside src/bin
1429-
Path::new("src").join("bin").join(&format!("{}.rs", bin.name())).to_path_buf()
1443+
return Path::new("src").join(&format!("main.rs")).to_path_buf()
14301444
}
14311445

14321446
fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {

tests/build.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,68 @@ fn run_proper_binary_main_rs() {
30103010
execs().with_status(0));
30113011
}
30123012

3013+
#[test]
3014+
fn run_proper_alias_binary_from_src() {
3015+
let p = project("foo")
3016+
.file("Cargo.toml", r#"
3017+
[package]
3018+
name = "foo"
3019+
authors = []
3020+
version = "0.0.0"
3021+
[[bin]]
3022+
name = "foo"
3023+
[[bin]]
3024+
name = "bar"
3025+
"#)
3026+
.file("src/foo.rs", r#"
3027+
fn main() {
3028+
println!("foo");
3029+
}
3030+
"#).file("src/bar.rs", r#"
3031+
fn main() {
3032+
println!("bar");
3033+
}
3034+
"#);
3035+
3036+
assert_that(p.cargo_process("build")
3037+
.arg("--all"),
3038+
execs().with_status(0)
3039+
);
3040+
assert_that(process(&p.bin("foo")),
3041+
execs().with_status(0).with_stdout("foo\n"));
3042+
assert_that(process(&p.bin("bar")),
3043+
execs().with_status(0).with_stdout("bar\n"));
3044+
}
3045+
3046+
#[test]
3047+
fn run_proper_alias_binary_main_rs() {
3048+
let p = project("foo")
3049+
.file("Cargo.toml", r#"
3050+
[package]
3051+
name = "foo"
3052+
authors = []
3053+
version = "0.0.0"
3054+
[[bin]]
3055+
name = "foo"
3056+
[[bin]]
3057+
name = "bar"
3058+
"#)
3059+
.file("src/main.rs", r#"
3060+
fn main() {
3061+
println!("main");
3062+
}
3063+
"#);
3064+
3065+
assert_that(p.cargo_process("build")
3066+
.arg("--all"),
3067+
execs().with_status(0)
3068+
);
3069+
assert_that(process(&p.bin("foo")),
3070+
execs().with_status(0).with_stdout("main\n"));
3071+
assert_that(process(&p.bin("bar")),
3072+
execs().with_status(0).with_stdout("main\n"));
3073+
}
3074+
30133075
#[test]
30143076
fn run_proper_binary_main_rs_as_foo() {
30153077
let p = project("foo")

0 commit comments

Comments
 (0)