diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 5f41f49f44a..b0e94850398 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1270,7 +1270,12 @@ fn normalize(package_root: &Path, if package_root.join(&default_bin_path.0).exists() { default_bin_path // inferred from bin's name } else { - PathValue(Path::new("src").join("main.rs")) + let path = PathValue(Path::new("src").join("main.rs")); + if package_root.join(&path.0).exists() { + path + } else { + PathValue(Path::new("src").join("bin").join("main.rs")) + } } }); let mut target = Target::bin_target(&bin.name(), package_root.join(&path.0), diff --git a/tests/build.rs b/tests/build.rs index 433e2ed51e0..dfa6241beab 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -2849,3 +2849,24 @@ fn run_proper_binary() { assert_that(p.cargo_process("run").arg("--bin").arg("other"), execs().with_status(0)); } + +#[test] +fn run_proper_binary_main_rs() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + authors = [] + version = "0.0.0" + [[bin]] + name = "foo" + "#) + .file("src/lib.rs", "") + .file("src/bin/main.rs", r#" + fn main() { + } + "#); + + assert_that(p.cargo_process("run").arg("--bin").arg("foo"), + execs().with_status(0)); +}