Skip to content

Commit 6633e29

Browse files
committed
Blacklist some binary names in cargo new
1 parent 9fe7f07 commit 6633e29

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

src/cargo/ops/cargo_new.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use git2::Config as GitConfig;
1010
use term::color::BLACK;
1111

1212
use core::Workspace;
13+
use ops::is_bad_artifact_name;
1314
use util::{GitRepo, HgRepo, PijulRepo, CargoResult, human, ChainError, internal};
1415
use util::{Config, paths};
1516

@@ -115,7 +116,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
115116
}
116117
}
117118

118-
fn check_name(name: &str) -> CargoResult<()> {
119+
fn check_name(name: &str, is_bin: bool) -> CargoResult<()> {
119120

120121
// Ban keywords + test list found at
121122
// https://doc.rust-lang.org/grammar.html#keywords
@@ -130,7 +131,7 @@ fn check_name(name: &str) -> CargoResult<()> {
130131
"super", "test", "trait", "true", "type", "typeof",
131132
"unsafe", "unsized", "use", "virtual", "where",
132133
"while", "yield"];
133-
if blacklist.contains(&name) {
134+
if blacklist.contains(&name) || (is_bin && is_bad_artifact_name(name)) {
134135
bail!("The name `{}` cannot be used as a crate name\n\
135136
use --name to override crate name",
136137
name)
@@ -274,7 +275,7 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
274275
}
275276

276277
let name = get_name(&path, &opts, config)?;
277-
check_name(name)?;
278+
check_name(name, opts.bin)?;
278279

279280
let mkopts = MkOptions {
280281
version_control: opts.version_control,
@@ -303,7 +304,7 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
303304
}
304305

305306
let name = get_name(&path, &opts, config)?;
306-
check_name(name)?;
307+
check_name(name, opts.bin)?;
307308

308309
let mut src_paths_types = vec![];
309310

src/cargo/ops/cargo_rustc/layout.rs

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ pub struct Layout {
6767
_lock: FileLock,
6868
}
6969

70+
pub fn is_bad_artifact_name(name: &str) -> bool {
71+
["deps", "examples", "build", "native", "incremental"]
72+
.iter()
73+
.find(|&&reserved| reserved == name)
74+
.is_some()
75+
}
76+
7077
impl Layout {
7178
pub fn new(ws: &Workspace,
7279
triple: Option<&str>,

src/cargo/ops/cargo_rustc/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use self::output_depinfo::output_depinfo;
2323
pub use self::compilation::Compilation;
2424
pub use self::context::{Context, Unit};
2525
pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts};
26+
pub use self::layout::is_bad_artifact_name;
2627

2728
mod compilation;
2829
mod context;

src/cargo/ops/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOpt
33
pub use self::cargo_compile::{CompileFilter, CompileMode, MessageFormat, Packages};
44
pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
55
pub use self::cargo_rustc::{compile_targets, Compilation, Kind, Unit};
6-
pub use self::cargo_rustc::Context;
6+
pub use self::cargo_rustc::{Context, is_bad_artifact_name};
77
pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig};
88
pub use self::cargo_rustc::{Executor, DefaultExecutor};
99
pub use self::cargo_run::run;

src/cargo/util/toml.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::{Summary, Manifest, Target, Dependency, DependencyInner, PackageId};
1515
use core::{EitherManifest, VirtualManifest};
1616
use core::dependency::{Kind, Platform};
1717
use core::manifest::{LibKind, Profile, ManifestMetadata};
18+
use ops::is_bad_artifact_name;
1819
use sources::CRATES_IO;
1920
use util::{self, CargoResult, human, ToUrl, ToSemver, ChainError, Config};
2021

@@ -613,10 +614,8 @@ impl TomlManifest {
613614
None => inferred_bin_targets(&project.name, layout)
614615
};
615616

616-
let blacklist = vec!["build", "deps", "examples", "native"];
617-
618617
for bin in bins.iter() {
619-
if blacklist.iter().find(|&x| *x == bin.name()) != None {
618+
if is_bad_artifact_name(&bin.name()) {
620619
bail!("the binary target name `{}` is forbidden",
621620
bin.name())
622621
}

tests/bench.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::str;
77
use cargo::util::process;
88
use cargotest::is_nightly;
99
use cargotest::support::paths::CargoPathExt;
10-
use cargotest::support::registry::Package;
1110
use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest};
1211
use hamcrest::{assert_that, existing_file};
1312

tests/new.rs

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ fn reserved_name() {
134134
use --name to override crate name"));
135135
}
136136

137+
#[test]
138+
fn reserved_binary_name() {
139+
assert_that(cargo_process("new").arg("--bin").arg("incremental"),
140+
execs().with_status(101)
141+
.with_stderr("\
142+
[ERROR] The name `incremental` cannot be used as a crate name\n\
143+
use --name to override crate name"));
144+
}
145+
137146
#[test]
138147
fn keyword_name() {
139148
assert_that(cargo_process("new").arg("pub"),

0 commit comments

Comments
 (0)