Skip to content

Commit a5d8294

Browse files
committed
Auto merge of #6021 - zachlute:validate-package-name, r=alexcrichton
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.
2 parents 48e8a8d + dc2d0c0 commit a5d8294

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,16 @@ impl TomlManifest {
810810
bail!("package name cannot be an empty string")
811811
}
812812

813+
for c in package_name.chars() {
814+
if c.is_alphanumeric() {
815+
continue;
816+
}
817+
if c == '_' || c == '-' {
818+
continue;
819+
}
820+
bail!("Invalid character `{}` in package name: `{}`", c, package_name)
821+
}
822+
813823
let pkgid = project.to_package_id(source_id)?;
814824

815825
let edition = if let Some(ref edition) = project.edition {

tests/testsuite/build.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Caused by:
257257
}
258258

259259
#[test]
260-
fn cargo_compile_with_invalid_package_name() {
260+
fn cargo_compile_with_empty_package_name() {
261261
let p = project()
262262
.file("Cargo.toml", &basic_manifest("", "0.0.0"))
263263
.build();
@@ -274,6 +274,24 @@ Caused by:
274274
).run();
275275
}
276276

277+
#[test]
278+
fn cargo_compile_with_invalid_package_name() {
279+
let p = project()
280+
.file("Cargo.toml", &basic_manifest("foo::bar", "0.0.0"))
281+
.build();
282+
283+
p.cargo("build")
284+
.with_status(101)
285+
.with_stderr(
286+
"\
287+
[ERROR] failed to parse manifest at `[..]`
288+
289+
Caused by:
290+
Invalid character `:` in package name: `foo::bar`
291+
",
292+
).run();
293+
}
294+
277295
#[test]
278296
fn cargo_compile_with_invalid_bin_target_name() {
279297
let p = project()

0 commit comments

Comments
 (0)