Skip to content

Commit 2c10f26

Browse files
committed
Auto merge of #8675 - weihanglo:fix/name-help, r=Eh2406
Add --name suggestion for cargo new Resolves #8613 Since `check_name` have already got a parameter to show name help, I reuse the logic and sync the behavior between `cargo init` and `cargo new`. The divergence seems to be intentionally made in #7959: _...Only print the --name suggestion for `cargo init`._ Feel free to discuss.
2 parents f110fd9 + c6fcb0e commit 2c10f26

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,19 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
154154
})
155155
}
156156

157-
fn check_name(name: &str, name_help: &str, has_bin: bool, shell: &mut Shell) -> CargoResult<()> {
157+
fn check_name(
158+
name: &str,
159+
show_name_help: bool,
160+
has_bin: bool,
161+
shell: &mut Shell,
162+
) -> CargoResult<()> {
163+
// If --name is already used to override, no point in suggesting it
164+
// again as a fix.
165+
let name_help = if show_name_help {
166+
"\nIf you need a crate name to not match the directory name, consider using --name flag."
167+
} else {
168+
""
169+
};
158170
restricted_names::validate_package_name(name, "crate name", name_help)?;
159171

160172
if restricted_names::is_keyword(name) {
@@ -363,7 +375,12 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
363375
}
364376

365377
let name = get_name(path, opts)?;
366-
check_name(name, "", opts.kind.is_bin(), &mut config.shell())?;
378+
check_name(
379+
name,
380+
opts.name.is_none(),
381+
opts.kind.is_bin(),
382+
&mut config.shell(),
383+
)?;
367384

368385
let mkopts = MkOptions {
369386
version_control: opts.version_control,
@@ -411,13 +428,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
411428
// user may mean "initialize for library, but also add binary target"
412429
}
413430
let has_bin = src_paths_types.iter().any(|x| x.bin);
414-
// If --name is already used to override, no point in suggesting it
415-
// again as a fix.
416-
let name_help = match opts.name {
417-
Some(_) => "",
418-
None => "\nuse --name to override crate name",
419-
};
420-
check_name(name, name_help, has_bin, &mut config.shell())?;
431+
check_name(name, opts.name.is_none(), has_bin, &mut config.shell())?;
421432

422433
let mut version_control = opts.version_control;
423434

tests/testsuite/init.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ fn invalid_dir_name() {
305305
.with_stderr(
306306
"\
307307
[ERROR] invalid character `.` in crate name: `foo.bar`, [..]
308-
use --name to override crate name",
308+
If you need a crate name to not match the directory name, consider using --name flag.
309+
",
309310
)
310311
.run();
311312

@@ -323,7 +324,7 @@ fn reserved_name() {
323324
.with_stderr(
324325
"\
325326
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]\n\
326-
use --name to override crate name
327+
If you need a crate name to not match the directory name, consider using --name flag.
327328
",
328329
)
329330
.run();

tests/testsuite/new.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ fn existing() {
116116
fn invalid_characters() {
117117
cargo_process("new foo.rs")
118118
.with_status(101)
119-
.with_stderr("[ERROR] invalid character `.` in crate name: `foo.rs`, [..]")
119+
.with_stderr(
120+
"\
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.
123+
",
124+
)
120125
.run();
121126
}
122127

123128
#[cargo_test]
124129
fn reserved_name() {
125130
cargo_process("new test")
126131
.with_status(101)
127-
.with_stderr("[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]")
132+
.with_stderr(
133+
"\
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.
136+
",
137+
)
128138
.run();
129139
}
130140

@@ -133,7 +143,10 @@ fn reserved_binary_name() {
133143
cargo_process("new --bin incremental")
134144
.with_status(101)
135145
.with_stderr(
136-
"[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]",
146+
"\
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.
149+
",
137150
)
138151
.run();
139152

@@ -153,7 +166,12 @@ it conflicts with cargo's build directory names
153166
fn keyword_name() {
154167
cargo_process("new pub")
155168
.with_status(101)
156-
.with_stderr("[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword")
169+
.with_stderr(
170+
"\
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.
173+
",
174+
)
157175
.run();
158176
}
159177

@@ -522,7 +540,12 @@ fn restricted_windows_name() {
522540
cargo_process("new nul")
523541
.env("USER", "foo")
524542
.with_status(101)
525-
.with_stderr("[ERROR] cannot use name `nul`, it is a reserved Windows filename")
543+
.with_stderr(
544+
"\
545+
[ERROR] cannot use name `nul`, it is a reserved Windows filename
546+
If you need a crate name to not match the directory name, consider using --name flag.
547+
",
548+
)
526549
.run();
527550
} else {
528551
cargo_process("new nul")
@@ -559,17 +582,23 @@ fn non_ascii_name_invalid() {
559582
.env("USER", "foo")
560583
.with_status(101)
561584
.with_stderr(
562-
"[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
563-
the first character must be a Unicode XID start character (most letters or `_`)",
585+
"\
586+
[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
587+
the first character must be a Unicode XID start character (most letters or `_`)
588+
If you need a crate name to not match the directory name, consider using --name flag.
589+
",
564590
)
565591
.run();
566592

567593
cargo_process("new a¼")
568594
.env("USER", "foo")
569595
.with_status(101)
570596
.with_stderr(
571-
"[ERROR] invalid character `¼` in crate name: `a¼`, \
572-
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)",
597+
"\
598+
[ERROR] invalid character `¼` in crate name: `a¼`, \
599+
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
600+
If you need a crate name to not match the directory name, consider using --name flag.
601+
",
573602
)
574603
.run();
575604
}

0 commit comments

Comments
 (0)