Skip to content

Commit c416fb6

Browse files
committed
Auto merge of #3898 - jmatraszek:fix_rust_40955, r=alexcrichton
Fix rust-lang/rust 40955 Proposed fix for rust-lang/rust#40955. I could also work on adding some additional tests, so we have all cases covered and automatically tested.
2 parents f5348cc + 2c9184d commit c416fb6

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

src/cargo/util/toml.rs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,17 +1273,7 @@ fn normalize(package_root: &Path,
12731273
default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
12741274
for bin in bins.iter() {
12751275
let path = bin.path.clone().unwrap_or_else(|| {
1276-
let default_bin_path = PathValue(default(bin));
1277-
if package_root.join(&default_bin_path.0).exists() {
1278-
default_bin_path // inferred from bin's name
1279-
} else {
1280-
let path = PathValue(Path::new("src").join("main.rs"));
1281-
if package_root.join(&path.0).exists() {
1282-
path
1283-
} else {
1284-
PathValue(Path::new("src").join("bin").join("main.rs"))
1285-
}
1286-
}
1276+
PathValue(default(bin))
12871277
});
12881278
let mut target = Target::bin_target(&bin.name(), package_root.join(&path.0),
12891279
bin.required_features.clone());
@@ -1359,14 +1349,13 @@ fn normalize(package_root: &Path,
13591349
if let Some(ref lib) = *lib {
13601350
lib_target(&mut ret, lib);
13611351
bin_targets(&mut ret, bins,
1362-
&mut |bin| Path::new("src").join("bin")
1363-
.join(&format!("{}.rs", bin.name())));
1352+
&mut |bin| inferred_bin_path(bin, package_root, true, bins.len()));
13641353
} else if bins.len() > 0 {
13651354
bin_targets(&mut ret, bins,
1366-
&mut |bin| Path::new("src")
1367-
.join(&format!("{}.rs", bin.name())));
1355+
&mut |bin| inferred_bin_path(bin, package_root, false, bins.len()));
13681356
}
13691357

1358+
13701359
if let Some(custom_build) = custom_build {
13711360
custom_build_target(&mut ret, &custom_build);
13721361
}
@@ -1394,6 +1383,59 @@ fn normalize(package_root: &Path,
13941383
ret
13951384
}
13961385

1386+
fn inferred_bin_path(bin: &TomlBinTarget,
1387+
package_root: &Path,
1388+
lib: bool,
1389+
bin_len: usize) -> PathBuf {
1390+
// we have a lib with multiple bins, so the bins are expected to be located
1391+
// inside src/bin
1392+
if lib && bin_len > 1 {
1393+
return Path::new("src").join("bin").join(&format!("{}.rs", bin.name()))
1394+
.to_path_buf()
1395+
}
1396+
1397+
// we have a lib with one bin, so it's either src/main.rs, src/bin/foo.rs or
1398+
// src/bin/main.rs
1399+
if lib && bin_len == 1 {
1400+
let path = Path::new("src").join(&format!("main.rs"));
1401+
if package_root.join(&path).exists() {
1402+
return path.to_path_buf()
1403+
}
1404+
1405+
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
1406+
if package_root.join(&path).exists() {
1407+
return path.to_path_buf()
1408+
}
1409+
1410+
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
1411+
}
1412+
1413+
// here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
1414+
// srb/bin/foo.rs or src/bin/main.rs
1415+
if bin_len == 1 {
1416+
let path = Path::new("src").join(&format!("main.rs"));
1417+
if package_root.join(&path).exists() {
1418+
return path.to_path_buf()
1419+
}
1420+
1421+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1422+
if package_root.join(&path).exists() {
1423+
return path.to_path_buf()
1424+
}
1425+
1426+
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
1427+
if package_root.join(&path).exists() {
1428+
return path.to_path_buf()
1429+
}
1430+
1431+
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
1432+
1433+
}
1434+
1435+
// here we have multiple bins, so they are expected to be located inside src/bin
1436+
Path::new("src").join("bin").join(&format!("{}.rs", bin.name())).to_path_buf()
1437+
}
1438+
13971439
fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
13981440
let profiles = profiles.as_ref();
13991441
let mut profiles = Profiles {

tests/build.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,3 +2870,28 @@ fn run_proper_binary_main_rs() {
28702870
assert_that(p.cargo_process("run").arg("--bin").arg("foo"),
28712871
execs().with_status(0));
28722872
}
2873+
2874+
#[test]
2875+
fn run_proper_binary_main_rs_as_foo() {
2876+
let p = project("foo")
2877+
.file("Cargo.toml", r#"
2878+
[package]
2879+
name = "foo"
2880+
authors = []
2881+
version = "0.0.0"
2882+
[[bin]]
2883+
name = "foo"
2884+
"#)
2885+
.file("src/foo.rs", r#"
2886+
fn main() {
2887+
panic!("This should never be run.");
2888+
}
2889+
"#)
2890+
.file("src/main.rs", r#"
2891+
fn main() {
2892+
}
2893+
"#);
2894+
2895+
assert_that(p.cargo_process("run").arg("--bin").arg("foo"),
2896+
execs().with_status(0));
2897+
}

0 commit comments

Comments
 (0)