Skip to content

Commit 078a65b

Browse files
committed
Auto merge of #13164 - epage:empty, r=weihanglo
fix: Fill in more empty name holes ### What does this PR try to resolve? This is a follow up to #13152 and expands on work done in #12928. This is also part of #12801 as I am refactoring how we do validation so that it parts of it can move into the schema, removing the last dependency the schema has on the rest of cargo. ### How should we test and review this PR? This prevents empty registry names which should be fine for the same reason as preventing empty feature names in #12928, that this was a regression due to changes in toml parsers ### Additional information
2 parents 8412d30 + a383185 commit 078a65b

File tree

18 files changed

+220
-11
lines changed

18 files changed

+220
-11
lines changed

Diff for: src/cargo/util/restricted_names.rs

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub fn is_conflicting_artifact_name(name: &str) -> bool {
4343
/// elsewhere. `cargo new` has a few restrictions, such as checking for
4444
/// reserved names. crates.io has even more restrictions.
4545
pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<()> {
46+
if name.is_empty() {
47+
bail!("{what} cannot be empty");
48+
}
49+
4650
let mut chars = name.chars();
4751
if let Some(ch) = chars.next() {
4852
if ch.is_digit(10) {

Diff for: src/cargo/util/toml/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,6 @@ pub fn to_real_manifest(
480480
};
481481

482482
let package_name = package.name.trim();
483-
if package_name.is_empty() {
484-
bail!("package name cannot be an empty string")
485-
}
486-
487483
validate_package_name(package_name, "package name", "")?;
488484

489485
let resolved_path = package_root.join("Cargo.toml");

Diff for: src/cargo/util_schemas/core/package_id_spec.rs

-6
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ impl PackageIdSpec {
9797
Some(version) => Some(version.parse::<PartialVersion>()?),
9898
None => None,
9999
};
100-
if name.is_empty() {
101-
bail!("package ID specification must have a name: `{spec}`");
102-
}
103100
validate_package_name(name, "pkgid", "")?;
104101
Ok(PackageIdSpec {
105102
name: String::from(name),
@@ -185,9 +182,6 @@ impl PackageIdSpec {
185182
None => (String::from(path_name), None),
186183
}
187184
};
188-
if name.is_empty() {
189-
bail!("package ID specification must have a name: `{url}`");
190-
}
191185
validate_package_name(name.as_str(), "pkgid", "")?;
192186
Ok(PackageIdSpec {
193187
name,

Diff for: tests/testsuite/alt_registry.rs

+76
Original file line numberDiff line numberDiff line change
@@ -1546,3 +1546,79 @@ or use environment variable CARGO_REGISTRY_TOKEN",
15461546
)
15471547
.run();
15481548
}
1549+
1550+
#[cargo_test]
1551+
fn config_empty_registry_name() {
1552+
let _ = RegistryBuilder::new()
1553+
.no_configure_token()
1554+
.alternative()
1555+
.build();
1556+
let p = project()
1557+
.file("src/lib.rs", "")
1558+
.file(
1559+
".cargo/config.toml",
1560+
"[registry.'']
1561+
",
1562+
)
1563+
.build();
1564+
1565+
p.cargo("publish")
1566+
.arg("--registry")
1567+
.arg("")
1568+
.with_status(101)
1569+
.with_stderr(
1570+
"\
1571+
[ERROR] registry name cannot be empty",
1572+
)
1573+
.run();
1574+
}
1575+
1576+
#[cargo_test]
1577+
fn empty_registry_flag() {
1578+
let p = project().file("src/lib.rs", "").build();
1579+
1580+
p.cargo("publish")
1581+
.arg("--registry")
1582+
.arg("")
1583+
.with_status(101)
1584+
.with_stderr(
1585+
"\
1586+
[ERROR] registry name cannot be empty",
1587+
)
1588+
.run();
1589+
}
1590+
1591+
#[cargo_test]
1592+
fn empty_dependency_registry() {
1593+
let p = project()
1594+
.file(
1595+
"Cargo.toml",
1596+
r#"
1597+
[package]
1598+
name = "foo"
1599+
version = "0.0.1"
1600+
1601+
[dependencies]
1602+
bar = { version = "0.1.0", registry = "" }
1603+
"#,
1604+
)
1605+
.file(
1606+
"src/lib.rs",
1607+
"
1608+
extern crate bar;
1609+
pub fn f() { bar::bar(); }
1610+
",
1611+
)
1612+
.build();
1613+
1614+
p.cargo("check")
1615+
.with_status(101)
1616+
.with_stderr(
1617+
"\
1618+
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
1619+
1620+
Caused by:
1621+
registry name cannot be empty",
1622+
)
1623+
.run();
1624+
}

Diff for: tests/testsuite/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ fn cargo_compile_with_empty_package_name() {
460460
[ERROR] failed to parse manifest at `[..]`
461461
462462
Caused by:
463-
package name cannot be an empty string
463+
package name cannot be empty
464464
",
465465
)
466466
.run();

Diff for: tests/testsuite/cargo_add/empty_dep_name/in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../add-basic.in

Diff for: tests/testsuite/cargo_add/empty_dep_name/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use cargo_test_support::curr_dir;
6+
7+
#[cargo_test]
8+
fn case() {
9+
cargo_test_support::registry::init();
10+
let project = Project::from_template(curr_dir!().join("in"));
11+
let project_root = project.root();
12+
let cwd = &project_root;
13+
14+
snapbox::cmd::Command::cargo_ui()
15+
.arg("add")
16+
.arg_line("@1.2.3")
17+
.current_dir(cwd)
18+
.assert()
19+
.failure()
20+
.stdout_matches_path(curr_dir!().join("stdout.log"))
21+
.stderr_matches_path(curr_dir!().join("stderr.log"));
22+
23+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
24+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"

Diff for: tests/testsuite/cargo_add/empty_dep_name/stderr.log

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: dependency name cannot be empty

Diff for: tests/testsuite/cargo_add/empty_dep_name/stdout.log

Whitespace-only changes.

Diff for: tests/testsuite/cargo_add/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod dev;
1717
mod dev_build_conflict;
1818
mod dev_prefer_existing_version;
1919
mod dry_run;
20+
mod empty_dep_name;
2021
mod empty_dep_table;
2122
mod features;
2223
mod features_activated_over_limit;

Diff for: tests/testsuite/cargo_new/empty_name/in/.keep

Whitespace-only changes.

Diff for: tests/testsuite/cargo_new/empty_name/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::curr_dir;
3+
use cargo_test_support::CargoCommand;
4+
use cargo_test_support::Project;
5+
6+
#[cargo_test]
7+
fn case() {
8+
let project = Project::from_template(curr_dir!().join("in"));
9+
let project_root = project.root();
10+
let cwd = &project_root;
11+
12+
snapbox::cmd::Command::cargo_ui()
13+
.arg("new")
14+
.args(["foo", "--name", ""])
15+
.current_dir(cwd)
16+
.assert()
17+
.failure()
18+
.stdout_matches_path(curr_dir!().join("stdout.log"))
19+
.stderr_matches_path(curr_dir!().join("stderr.log"));
20+
21+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
22+
}

Diff for: tests/testsuite/cargo_new/empty_name/out/.keep

Whitespace-only changes.

Diff for: tests/testsuite/cargo_new/empty_name/stderr.log

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: package name cannot be empty

Diff for: tests/testsuite/cargo_new/empty_name/stdout.log

Whitespace-only changes.

Diff for: tests/testsuite/cargo_new/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod add_members_to_workspace_with_absolute_package_path;
44
mod add_members_to_workspace_with_empty_members;
55
mod add_members_to_workspace_with_exclude_list;
66
mod add_members_to_workspace_with_members_glob;
7+
mod empty_name;
78
mod help;
89
mod inherit_workspace_lints;
910
mod inherit_workspace_package_table;

Diff for: tests/testsuite/patch.rs

+83
Original file line numberDiff line numberDiff line change
@@ -2700,3 +2700,86 @@ perhaps a crate was updated and forgotten to be re-vendored?"#,
27002700
)
27012701
.run();
27022702
}
2703+
2704+
#[cargo_test]
2705+
fn from_config_empty() {
2706+
Package::new("bar", "0.1.0").publish();
2707+
2708+
let p = project()
2709+
.file(
2710+
"Cargo.toml",
2711+
r#"
2712+
[package]
2713+
name = "foo"
2714+
version = "0.0.1"
2715+
authors = []
2716+
2717+
[dependencies]
2718+
bar = "0.1.0"
2719+
"#,
2720+
)
2721+
.file(
2722+
".cargo/config.toml",
2723+
r#"
2724+
[patch.'']
2725+
bar = { path = 'bar' }
2726+
"#,
2727+
)
2728+
.file("src/lib.rs", "")
2729+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.1"))
2730+
.file("bar/src/lib.rs", r#""#)
2731+
.build();
2732+
2733+
p.cargo("check")
2734+
.with_status(101)
2735+
.with_stderr(
2736+
"\
2737+
[ERROR] [patch] entry `` should be a URL or registry name
2738+
2739+
Caused by:
2740+
invalid url ``: relative URL without a base
2741+
",
2742+
)
2743+
.run();
2744+
}
2745+
2746+
#[cargo_test]
2747+
fn from_manifest_empty() {
2748+
Package::new("bar", "0.1.0").publish();
2749+
2750+
let p = project()
2751+
.file(
2752+
"Cargo.toml",
2753+
r#"
2754+
[package]
2755+
name = "foo"
2756+
version = "0.0.1"
2757+
authors = []
2758+
2759+
[dependencies]
2760+
bar = "0.1.0"
2761+
2762+
[patch.'']
2763+
bar = { path = 'bar' }
2764+
"#,
2765+
)
2766+
.file("src/lib.rs", "")
2767+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.1"))
2768+
.file("bar/src/lib.rs", r#""#)
2769+
.build();
2770+
2771+
p.cargo("check")
2772+
.with_status(101)
2773+
.with_stderr(
2774+
"\
2775+
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
2776+
2777+
Caused by:
2778+
[patch] entry `` should be a URL or registry name
2779+
2780+
Caused by:
2781+
invalid url ``: relative URL without a base
2782+
",
2783+
)
2784+
.run();
2785+
}

0 commit comments

Comments
 (0)