Skip to content

Commit

Permalink
fix(new): Catch empty package names earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 13, 2023
1 parent 9de03cd commit 2db5c9d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::util::important_paths::find_root_manifest_for_wd;
use crate::util::toml_mut::is_sorted;
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{restricted_names, Config};
use anyhow::{anyhow, Context};
use anyhow::{anyhow, bail, Context};
use cargo_util::paths::{self, write_atomic};
use serde::de;
use serde::Deserialize;
Expand Down Expand Up @@ -197,6 +197,9 @@ fn check_name(
}
help
};
if name.is_empty() {
bail!("package name cannot be empty");
}
restricted_names::validate_package_name(name, "package name", &bin_help())?;

if restricted_names::is_keyword(name) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_new/empty_name/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn case() {
.args(["foo", "--name", ""])
.current_dir(cwd)
.assert()
.success()
.failure()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

Expand Down
8 changes: 1 addition & 7 deletions tests/testsuite/cargo_new/empty_name/stderr.log
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
warning: compiling this new package may not work due to invalid workspace configuration

failed to parse manifest at `[ROOT]/case/foo/Cargo.toml`

Caused by:
package name cannot be an empty string
Created binary (application) `` package
error: package name cannot be empty

0 comments on commit 2db5c9d

Please sign in to comment.