Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ impl Layout {
benches: benches,
}
}

fn main(&self) -> Option<&PathBuf> {
self.bins.iter().find(|p| {
match p.file_name().and_then(|s| s.to_str()) {
Some(s) => s == "main.rs",
None => false
}
})
}
}

fn try_add_file(files: &mut Vec<PathBuf>, file: PathBuf) {
Expand Down Expand Up @@ -475,22 +466,10 @@ impl TomlManifest {

let bins = match self.bin {
Some(ref bins) => {
let bin = layout.main();

for target in bins {
target.validate_binary_name()?;
}

bins.iter().map(|t| {
if bin.is_some() && t.path.is_none() {
TomlTarget {
path: bin.as_ref().map(|&p| PathValue::Path(p.clone())),
.. t.clone()
}
} else {
t.clone()
}
}).collect()
};
bins.clone()
}
None => inferred_bin_targets(&project.name, layout)
};
Expand Down Expand Up @@ -1147,7 +1126,11 @@ fn normalize(package_root: &Path,
default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
for bin in bins.iter() {
let path = bin.path.clone().unwrap_or_else(|| {
PathValue::Path(default(bin))
let default_bin_path = PathValue::Path(default(bin));
match package_root.join(default_bin_path.to_path()).exists() {
true => default_bin_path, // inferred from bin's name
false => PathValue::Path(Path::new("src").join("main.rs"))
}
});
let mut target = Target::bin_target(&bin.name(), package_root.join(path.to_path()));
configure(bin, &mut target);
Expand Down
27 changes: 27 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2769,3 +2769,30 @@ fn build_all_member_dependency_same_name() {
[..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n"));
}

#[test]
fn run_proper_binary() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
authors = []
version = "0.0.0"
[[bin]]
name = "main"
[[bin]]
name = "other"
"#)
.file("src/lib.rs", "")
.file("src/bin/main.rs", r#"
fn main() {
panic!("This should never be run.");
}
"#)
.file("src/bin/other.rs", r#"
fn main() {
}
"#);

assert_that(p.cargo_process("run").arg("--bin").arg("other"),
execs().with_status(0));
}
2 changes: 2 additions & 0 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest,
#[test]
fn cargo_metadata_simple() {
let p = project("foo")
.file("src/foo.rs", "")
.file("Cargo.toml", &basic_bin_manifest("foo"));

assert_that(p.cargo_process("metadata"), execs().with_json(r#"
Expand Down Expand Up @@ -52,6 +53,7 @@ fn cargo_metadata_simple() {
#[test]
fn cargo_metadata_with_deps_and_version() {
let p = project("foo")
.file("src/foo.rs", "")
.file("Cargo.toml", r#"
[project]
name = "foo"
Expand Down