Skip to content

Commit

Permalink
Auto merge of #6021 - zachlute:validate-package-name, r=alexcrichton
Browse files Browse the repository at this point in the history
Validate that the package name contains no invalid characters.

Fixes #2388.

Invalid characters are currently defined as alphanumeric, _, and -. This matches the rustc restrictions but is not as restrictive as `cargo new` or crates.io.

Mostly this is just so there will be better error messages in the case where characters in the package name aren't valid path characters.
  • Loading branch information
bors committed Sep 13, 2018
2 parents 48e8a8d + dc2d0c0 commit a5d8294
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,16 @@ impl TomlManifest {
bail!("package name cannot be an empty string")
}

for c in package_name.chars() {
if c.is_alphanumeric() {
continue;
}
if c == '_' || c == '-' {
continue;
}
bail!("Invalid character `{}` in package name: `{}`", c, package_name)
}

let pkgid = project.to_package_id(source_id)?;

let edition = if let Some(ref edition) = project.edition {
Expand Down
20 changes: 19 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Caused by:
}

#[test]
fn cargo_compile_with_invalid_package_name() {
fn cargo_compile_with_empty_package_name() {
let p = project()
.file("Cargo.toml", &basic_manifest("", "0.0.0"))
.build();
Expand All @@ -274,6 +274,24 @@ Caused by:
).run();
}

#[test]
fn cargo_compile_with_invalid_package_name() {
let p = project()
.file("Cargo.toml", &basic_manifest("foo::bar", "0.0.0"))
.build();

p.cargo("build")
.with_status(101)
.with_stderr(
"\
[ERROR] failed to parse manifest at `[..]`
Caused by:
Invalid character `:` in package name: `foo::bar`
",
).run();
}

#[test]
fn cargo_compile_with_invalid_bin_target_name() {
let p = project()
Expand Down

0 comments on commit a5d8294

Please sign in to comment.