Skip to content

Commit 414ccb5

Browse files
committed
Auto merge of #9098 - ehuss:extra-new-name-help, r=alexcrichton
Add some extra help to `cargo new` and invalid package names. Binaries are not as restricted as package names, so provide some help in case the user really wants a binary with that name. Closes #8829
2 parents 17a7b07 + fdb8ea1 commit 414ccb5

File tree

6 files changed

+141
-33
lines changed

6 files changed

+141
-33
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,42 @@ fn check_name(
163163
// If --name is already used to override, no point in suggesting it
164164
// again as a fix.
165165
let name_help = if show_name_help {
166-
"\nIf you need a crate name to not match the directory name, consider using --name flag."
166+
"\nIf you need a package name to not match the directory name, consider using --name flag."
167167
} else {
168168
""
169169
};
170-
restricted_names::validate_package_name(name, "crate name", name_help)?;
170+
let bin_help = || {
171+
let mut help = String::from(name_help);
172+
if has_bin {
173+
help.push_str(&format!(
174+
"\n\
175+
If you need a binary with the name \"{name}\", use a valid package \
176+
name, and set the binary name to be different from the package. \
177+
This can be done by setting the binary filename to `src/bin/{name}.rs` \
178+
or change the name in Cargo.toml with:\n\
179+
\n \
180+
[bin]\n \
181+
name = \"{name}\"\n \
182+
path = \"src/main.rs\"\n\
183+
",
184+
name = name
185+
));
186+
}
187+
help
188+
};
189+
restricted_names::validate_package_name(name, "package name", &bin_help())?;
171190

172191
if restricted_names::is_keyword(name) {
173192
anyhow::bail!(
174-
"the name `{}` cannot be used as a crate name, it is a Rust keyword{}",
193+
"the name `{}` cannot be used as a package name, it is a Rust keyword{}",
175194
name,
176-
name_help
195+
bin_help()
177196
);
178197
}
179198
if restricted_names::is_conflicting_artifact_name(name) {
180199
if has_bin {
181200
anyhow::bail!(
182-
"the name `{}` cannot be used as a crate name, \
201+
"the name `{}` cannot be used as a package name, \
183202
it conflicts with cargo's build directory names{}",
184203
name,
185204
name_help
@@ -195,16 +214,17 @@ fn check_name(
195214
}
196215
if name == "test" {
197216
anyhow::bail!(
198-
"the name `test` cannot be used as a crate name, \
217+
"the name `test` cannot be used as a package name, \
199218
it conflicts with Rust's built-in test library{}",
200-
name_help
219+
bin_help()
201220
);
202221
}
203222
if ["core", "std", "alloc", "proc_macro", "proc-macro"].contains(&name) {
204223
shell.warn(format!(
205224
"the name `{}` is part of Rust's standard library\n\
206-
It is recommended to use a different name to avoid problems.",
207-
name
225+
It is recommended to use a different name to avoid problems.{}",
226+
name,
227+
bin_help()
208228
))?;
209229
}
210230
if restricted_names::is_windows_reserved(name) {
@@ -781,7 +801,7 @@ mod tests {
781801

782802
if let Err(e) = Workspace::new(&path.join("Cargo.toml"), config) {
783803
crate::display_warning_with_error(
784-
"compiling this new crate may not work due to invalid \
804+
"compiling this new package may not work due to invalid \
785805
workspace configuration",
786806
&e,
787807
&mut config.shell(),

src/cargo/util/toml/targets.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ fn clean_bins(
289289
}
290290

291291
if restricted_names::is_conflicting_artifact_name(&name) {
292-
anyhow::bail!("the binary target name `{}` is forbidden", name)
292+
anyhow::bail!(
293+
"the binary target name `{}` is forbidden, \
294+
it conflicts with with cargo's build directory names",
295+
name
296+
)
293297
}
294298
}
295299

tests/testsuite/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn cargo_compile_with_forbidden_bin_target_name() {
366366
[ERROR] failed to parse manifest at `[..]`
367367
368368
Caused by:
369-
the binary target name `build` is forbidden
369+
the binary target name `build` is forbidden, it conflicts with with cargo's build directory names
370370
",
371371
)
372372
.run();

tests/testsuite/init.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,17 @@ fn invalid_dir_name() {
304304
.with_status(101)
305305
.with_stderr(
306306
"\
307-
[ERROR] invalid character `.` in crate name: `foo.bar`, [..]
308-
If you need a crate name to not match the directory name, consider using --name flag.
307+
[ERROR] invalid character `.` in package name: `foo.bar`, [..]
308+
If you need a package name to not match the directory name, consider using --name flag.
309+
If you need a binary with the name \"foo.bar\", use a valid package name, \
310+
and set the binary name to be different from the package. \
311+
This can be done by setting the binary filename to `src/bin/foo.bar.rs` \
312+
or change the name in Cargo.toml with:
313+
314+
[bin]
315+
name = \"foo.bar\"
316+
path = \"src/main.rs\"
317+
309318
",
310319
)
311320
.run();
@@ -323,8 +332,17 @@ fn reserved_name() {
323332
.with_status(101)
324333
.with_stderr(
325334
"\
326-
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]\n\
327-
If you need a crate name to not match the directory name, consider using --name flag.
335+
[ERROR] the name `test` cannot be used as a package name, it conflicts [..]\n\
336+
If you need a package name to not match the directory name, consider using --name flag.
337+
If you need a binary with the name \"test\", use a valid package name, \
338+
and set the binary name to be different from the package. \
339+
This can be done by setting the binary filename to `src/bin/test.rs` \
340+
or change the name in Cargo.toml with:
341+
342+
[bin]
343+
name = \"test\"
344+
path = \"src/main.rs\"
345+
328346
",
329347
)
330348
.run();

tests/testsuite/new.rs

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ fn invalid_characters() {
118118
.with_status(101)
119119
.with_stderr(
120120
"\
121-
[ERROR] invalid character `.` in crate name: `foo.rs`, [..]
122-
If you need a crate name to not match the directory name, consider using --name flag.
121+
[ERROR] invalid character `.` in package name: `foo.rs`, [..]
122+
If you need a package name to not match the directory name, consider using --name flag.
123+
If you need a binary with the name \"foo.rs\", use a valid package name, \
124+
and set the binary name to be different from the package. \
125+
This can be done by setting the binary filename to `src/bin/foo.rs.rs` \
126+
or change the name in Cargo.toml with:
127+
128+
[bin]
129+
name = \"foo.rs\"
130+
path = \"src/main.rs\"
131+
123132
",
124133
)
125134
.run();
@@ -131,8 +140,17 @@ fn reserved_name() {
131140
.with_status(101)
132141
.with_stderr(
133142
"\
134-
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]
135-
If you need a crate name to not match the directory name, consider using --name flag.
143+
[ERROR] the name `test` cannot be used as a package name, it conflicts [..]
144+
If you need a package name to not match the directory name, consider using --name flag.
145+
If you need a binary with the name \"test\", use a valid package name, \
146+
and set the binary name to be different from the package. \
147+
This can be done by setting the binary filename to `src/bin/test.rs` \
148+
or change the name in Cargo.toml with:
149+
150+
[bin]
151+
name = \"test\"
152+
path = \"src/main.rs\"
153+
136154
",
137155
)
138156
.run();
@@ -144,8 +162,8 @@ fn reserved_binary_name() {
144162
.with_status(101)
145163
.with_stderr(
146164
"\
147-
[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]
148-
If you need a crate name to not match the directory name, consider using --name flag.
165+
[ERROR] the name `incremental` cannot be used as a package name, it conflicts [..]
166+
If you need a package name to not match the directory name, consider using --name flag.
149167
",
150168
)
151169
.run();
@@ -168,8 +186,17 @@ fn keyword_name() {
168186
.with_status(101)
169187
.with_stderr(
170188
"\
171-
[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword
172-
If you need a crate name to not match the directory name, consider using --name flag.
189+
[ERROR] the name `pub` cannot be used as a package name, it is a Rust keyword
190+
If you need a package name to not match the directory name, consider using --name flag.
191+
If you need a binary with the name \"pub\", use a valid package name, \
192+
and set the binary name to be different from the package. \
193+
This can be done by setting the binary filename to `src/bin/pub.rs` \
194+
or change the name in Cargo.toml with:
195+
196+
[bin]
197+
name = \"pub\"
198+
path = \"src/main.rs\"
199+
173200
",
174201
)
175202
.run();
@@ -183,6 +210,16 @@ fn std_name() {
183210
"\
184211
[WARNING] the name `core` is part of Rust's standard library
185212
It is recommended to use a different name to avoid problems.
213+
If you need a package name to not match the directory name, consider using --name flag.
214+
If you need a binary with the name \"core\", use a valid package name, \
215+
and set the binary name to be different from the package. \
216+
This can be done by setting the binary filename to `src/bin/core.rs` \
217+
or change the name in Cargo.toml with:
218+
219+
[bin]
220+
name = \"core\"
221+
path = \"src/main.rs\"
222+
186223
[CREATED] binary (application) `core` package
187224
",
188225
)
@@ -528,8 +565,19 @@ fn explicit_invalid_name_not_suggested() {
528565
cargo_process("new --name 10-invalid a")
529566
.with_status(101)
530567
.with_stderr(
531-
"[ERROR] the name `10-invalid` cannot be used as a crate name, \
532-
the name cannot start with a digit",
568+
"\
569+
[ERROR] the name `10-invalid` cannot be used as a package name, \
570+
the name cannot start with a digit\n\
571+
If you need a binary with the name \"10-invalid\", use a valid package name, \
572+
and set the binary name to be different from the package. \
573+
This can be done by setting the binary filename to `src/bin/10-invalid.rs` \
574+
or change the name in Cargo.toml with:
575+
576+
[bin]
577+
name = \"10-invalid\"
578+
path = \"src/main.rs\"
579+
580+
",
533581
)
534582
.run();
535583
}
@@ -615,7 +663,7 @@ fn restricted_windows_name() {
615663
.with_stderr(
616664
"\
617665
[ERROR] cannot use name `nul`, it is a reserved Windows filename
618-
If you need a crate name to not match the directory name, consider using --name flag.
666+
If you need a package name to not match the directory name, consider using --name flag.
619667
",
620668
)
621669
.run();
@@ -655,9 +703,18 @@ fn non_ascii_name_invalid() {
655703
.with_status(101)
656704
.with_stderr(
657705
"\
658-
[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
706+
[ERROR] invalid character `Ⓐ` in package name: `ⒶⒷⒸ`, \
659707
the first character must be a Unicode XID start character (most letters or `_`)
660-
If you need a crate name to not match the directory name, consider using --name flag.
708+
If you need a package name to not match the directory name, consider using --name flag.
709+
If you need a binary with the name \"ⒶⒷⒸ\", use a valid package name, \
710+
and set the binary name to be different from the package. \
711+
This can be done by setting the binary filename to `src/bin/ⒶⒷⒸ.rs` \
712+
or change the name in Cargo.toml with:
713+
714+
[bin]
715+
name = \"ⒶⒷⒸ\"
716+
path = \"src/main.rs\"
717+
661718
",
662719
)
663720
.run();
@@ -667,9 +724,18 @@ If you need a crate name to not match the directory name, consider using --name
667724
.with_status(101)
668725
.with_stderr(
669726
"\
670-
[ERROR] invalid character `¼` in crate name: `a¼`, \
727+
[ERROR] invalid character `¼` in package name: `a¼`, \
671728
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
672-
If you need a crate name to not match the directory name, consider using --name flag.
729+
If you need a package name to not match the directory name, consider using --name flag.
730+
If you need a binary with the name \"\", use a valid package name, \
731+
and set the binary name to be different from the package. \
732+
This can be done by setting the binary filename to `src/bin/a¼.rs` \
733+
or change the name in Cargo.toml with:
734+
735+
[bin]
736+
name = \"\"
737+
path = \"src/main.rs\"
738+
673739
",
674740
)
675741
.run();

tests/testsuite/workspaces.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ fn new_warns_you_this_will_not_work() {
10341034
.env("USER", "foo")
10351035
.with_stderr(
10361036
"\
1037-
warning: compiling this new crate may not work due to invalid workspace configuration
1037+
warning: compiling this new package may not work due to invalid workspace configuration
10381038
10391039
current package believes it's in a workspace when it's not:
10401040
current: [..]
@@ -1056,7 +1056,7 @@ fn new_warning_with_corrupt_ws() {
10561056
.env("USER", "foo")
10571057
.with_stderr(
10581058
"\
1059-
[WARNING] compiling this new crate may not work due to invalid workspace configuration
1059+
[WARNING] compiling this new package may not work due to invalid workspace configuration
10601060
10611061
failed to parse manifest at `[..]foo/Cargo.toml`
10621062

0 commit comments

Comments
 (0)