diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 99851347ea5..74cac7de4ea 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -174,16 +174,18 @@ fn check_name( // If --name is already used to override, no point in suggesting it // again as a fix. let name_help = if show_name_help { - "\nIf you need a package name to not match the directory name, consider using --name flag." + "\nnote: the directory name is used as the package name\ + \nhelp: to override the package name, pass `--name `" } else { "" }; let bin_help = || { let mut help = String::from(name_help); - if has_bin && !name.is_empty() { + // Only suggest `bin.name` for valid crate names because it is used for `--crate` + if has_bin && validate_crate_name(name) { help.push_str(&format!( "\n\ - If you need a binary with the name \"{name}\", use a valid package \ + help: to name the binary \"{name}\", use a valid package \ name, and set the binary name to be different from the package. \ This can be done by setting the binary filename to `src/bin/{name}.rs` \ or change the name in Cargo.toml with:\n\ @@ -204,7 +206,7 @@ fn check_name( if restricted_names::is_keyword(name) { anyhow::bail!( - "the name `{}` cannot be used as a package name, it is a Rust keyword{}", + "invalid package name `{}`: it is a Rust keyword{}", name, bin_help() ); @@ -212,14 +214,14 @@ fn check_name( if restricted_names::is_conflicting_artifact_name(name) { if has_bin { anyhow::bail!( - "the name `{}` cannot be used as a package name, \ + "invalid package name `{}`: \ it conflicts with cargo's build directory names{}", name, name_help ); } else { shell.warn(format!( - "the name `{}` will not support binary \ + "package `{}` will not support binary \ executables with that name, \ it conflicts with cargo's build directory names", name @@ -228,14 +230,14 @@ fn check_name( } if name == "test" { anyhow::bail!( - "the name `test` cannot be used as a package name, \ + "invalid package name `test`: \ it conflicts with Rust's built-in test library{}", bin_help() ); } if ["core", "std", "alloc", "proc_macro", "proc-macro"].contains(&name) { shell.warn(format!( - "the name `{}` is part of Rust's standard library\n\ + "package name `{}` may be confused with the package with that name in Rust's standard library\n\ It is recommended to use a different name to avoid problems.{}", name, bin_help() @@ -244,13 +246,13 @@ fn check_name( if restricted_names::is_windows_reserved(name) { if cfg!(windows) { anyhow::bail!( - "cannot use name `{}`, it is a reserved Windows filename{}", + "invalid package name `{}`: it is a reserved Windows filename{}", name, name_help ); } else { shell.warn(format!( - "the name `{}` is a reserved Windows filename\n\ + "package name `{}` is a reserved Windows filename\n\ This package will not work on Windows platforms.", name ))?; @@ -258,7 +260,7 @@ fn check_name( } if restricted_names::is_non_ascii_name(name) { shell.warn(format!( - "the name `{}` contains non-ASCII characters\n\ + "invalid package name `{}`: contains non-ASCII characters\n\ Non-ASCII crate names are not supported by Rust.", name ))?; @@ -266,13 +268,30 @@ fn check_name( let name_in_lowercase = name.to_lowercase(); if name != name_in_lowercase { shell.warn(format!( - "the name `{name}` is not snake_case or kebab-case which is recommended for package names, consider `{name_in_lowercase}`" + "package name `{name}` is not snake_case or kebab-case which is recommended for package names, consider `{name_in_lowercase}`" ))?; } Ok(()) } +// Taken from +fn validate_crate_name(name: &str) -> bool { + if name.is_empty() { + return false; + } + + for c in name.chars() { + if c.is_alphanumeric() || c == '-' || c == '_' { + continue; + } else { + return false; + } + } + + true +} + /// Checks if the path contains any invalid PATH env characters. fn check_path(path: &Path, shell: &mut Shell) -> CargoResult<()> { // warn if the path contains characters that will break `env::join_paths` diff --git a/tests/testsuite/cargo_init/invalid_dir_name/stderr.term.svg b/tests/testsuite/cargo_init/invalid_dir_name/stderr.term.svg index 792170ccaad..946730239cc 100644 --- a/tests/testsuite/cargo_init/invalid_dir_name/stderr.term.svg +++ b/tests/testsuite/cargo_init/invalid_dir_name/stderr.term.svg @@ -1,7 +1,7 @@ - +