Skip to content

Commit 7787741

Browse files
committed
Auto merge of #4182 - matklad:simplify, r=alexcrichton
Simplify inferred binary names Fix for rust-lang/rust#42365, which probably breaks other stuff :(
2 parents 50b1c24 + b6aef6b commit 7787741

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

src/cargo/util/toml.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,12 +1415,9 @@ fn normalize(package_root: &Path,
14151415

14161416
if let Some(ref lib) = *lib {
14171417
lib_target(&mut ret, lib);
1418-
bin_targets(&mut ret, bins,
1419-
&mut |bin| inferred_bin_path(bin, package_root, true, bins.len()));
1420-
} else if bins.len() > 0 {
1421-
bin_targets(&mut ret, bins,
1422-
&mut |bin| inferred_bin_path(bin, package_root, false, bins.len()));
14231418
}
1419+
bin_targets(&mut ret, bins,
1420+
&mut |bin| inferred_bin_path(bin, lib.is_some(), package_root, bins.len()));
14241421

14251422

14261423
if let Some(custom_build) = custom_build {
@@ -1443,32 +1440,9 @@ fn normalize(package_root: &Path,
14431440
}
14441441

14451442
fn inferred_bin_path(bin: &TomlBinTarget,
1443+
has_lib: bool,
14461444
package_root: &Path,
1447-
lib: bool,
14481445
bin_len: usize) -> PathBuf {
1449-
// we have a lib with multiple bins, so the bins are expected to be located
1450-
// inside src/bin
1451-
if lib && bin_len > 1 {
1452-
return Path::new("src").join("bin").join(&format!("{}.rs", bin.name()))
1453-
.to_path_buf()
1454-
}
1455-
1456-
// we have a lib with one bin, so it's either src/main.rs, src/bin/foo.rs or
1457-
// src/bin/main.rs
1458-
if lib && bin_len == 1 {
1459-
let path = Path::new("src").join(&format!("main.rs"));
1460-
if package_root.join(&path).exists() {
1461-
return path.to_path_buf()
1462-
}
1463-
1464-
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
1465-
if package_root.join(&path).exists() {
1466-
return path.to_path_buf()
1467-
}
1468-
1469-
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
1470-
}
1471-
14721446
// here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
14731447
// srb/bin/foo.rs or src/bin/main.rs
14741448
if bin_len == 1 {
@@ -1477,9 +1451,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
14771451
return path.to_path_buf()
14781452
}
14791453

1480-
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1481-
if package_root.join(&path).exists() {
1482-
return path.to_path_buf()
1454+
if !has_lib {
1455+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1456+
if package_root.join(&path).exists() {
1457+
return path.to_path_buf()
1458+
}
14831459
}
14841460

14851461
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
@@ -1496,9 +1472,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
14961472
return path.to_path_buf()
14971473
}
14981474

1499-
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1500-
if package_root.join(&path).exists() {
1501-
return path.to_path_buf()
1475+
if !has_lib {
1476+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1477+
if package_root.join(&path).exists() {
1478+
return path.to_path_buf()
1479+
}
15021480
}
15031481

15041482
let path = Path::new("src").join("bin").join(&format!("main.rs"));

tests/build.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,3 +3229,45 @@ fn deterministic_cfg_flags() {
32293229
--cfg cfg_a --cfg cfg_b --cfg cfg_c --cfg cfg_d --cfg cfg_e`
32303230
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]"));
32313231
}
3232+
3233+
#[test]
3234+
fn explicit_bins_without_paths() {
3235+
let p = project("foo")
3236+
.file("Cargo.toml", r#"
3237+
[package]
3238+
name = "foo"
3239+
version = "0.1.0"
3240+
authors = []
3241+
3242+
[[bin]]
3243+
name = "foo"
3244+
3245+
[[bin]]
3246+
name = "bar"
3247+
"#)
3248+
.file("src/lib.rs", "")
3249+
.file("src/main.rs", "fn main() {}")
3250+
.file("src/bin/bar.rs", "fn main() {}");
3251+
3252+
assert_that(p.cargo_process("build"), execs().with_status(0));
3253+
}
3254+
3255+
#[test]
3256+
fn no_bin_in_src_with_lib() {
3257+
let p = project("foo")
3258+
.file("Cargo.toml", r#"
3259+
[package]
3260+
name = "foo"
3261+
version = "0.1.0"
3262+
authors = []
3263+
3264+
[[bin]]
3265+
name = "foo"
3266+
"#)
3267+
.file("src/lib.rs", "")
3268+
.file("src/foo.rs", "fn main() {}");
3269+
3270+
assert_that(p.cargo_process("build"),
3271+
execs().with_status(101)
3272+
.with_stderr_contains(r#"[ERROR] couldn't read "[..]main.rs"[..]"#));
3273+
}

tests/cargotest/support/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,12 @@ impl Execs {
436436
}
437437

438438
if let Some(ref objects) = self.expect_json {
439-
let lines = match str::from_utf8(&actual.stdout) {
440-
Err(..) => return Err("stdout was not utf8 encoded".to_owned()),
441-
Ok(stdout) => stdout.lines().collect::<Vec<_>>(),
442-
};
439+
let stdout = str::from_utf8(&actual.stdout)
440+
.map_err(|_| "stdout was not utf8 encoded".to_owned())?;
441+
let lines = stdout.lines().collect::<Vec<_>>();
443442
if lines.len() != objects.len() {
444-
return Err(format!("expected {} json lines, got {}",
445-
objects.len(), lines.len()));
443+
return Err(format!("expected {} json lines, got {}, stdout:\n{}",
444+
objects.len(), lines.len(), stdout));
446445
}
447446
for (obj, line) in objects.iter().zip(lines) {
448447
self.match_json(obj, line)?;

0 commit comments

Comments
 (0)