Skip to content

Commit 6ec8da9

Browse files
committed
fix(schema): Mark package.name as optional
1 parent 911f174 commit 6ec8da9

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

crates/cargo-util-schemas/manifest.schema.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@
241241
]
242242
},
243243
"name": {
244-
"type": "string"
244+
"type": [
245+
"string",
246+
"null"
247+
]
245248
},
246249
"version": {
247250
"anyOf": [
@@ -475,10 +478,7 @@
475478
}
476479
]
477480
}
478-
},
479-
"required": [
480-
"name"
481-
]
481+
}
482482
},
483483
"InheritableField_for_string": {
484484
"description": "An enum that allows for inheriting keys from a workspace in a Cargo.toml.",

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ pub struct TomlPackage {
178178
pub edition: Option<InheritableString>,
179179
#[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
180180
pub rust_version: Option<InheritableRustVersion>,
181-
#[cfg_attr(feature = "unstable-schema", schemars(with = "String"))]
182-
pub name: PackageName,
181+
#[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
182+
pub name: Option<PackageName>,
183183
pub version: Option<InheritableSemverVersion>,
184184
pub authors: Option<InheritableVecString>,
185185
pub build: Option<StringOrBool>,
@@ -226,7 +226,7 @@ pub struct TomlPackage {
226226
impl TomlPackage {
227227
pub fn new(name: PackageName) -> Self {
228228
Self {
229-
name,
229+
name: Some(name),
230230

231231
edition: None,
232232
rust_version: None,
@@ -263,6 +263,10 @@ impl TomlPackage {
263263
}
264264
}
265265

266+
pub fn normalized_name(&self) -> Result<&PackageName, UnresolvedError> {
267+
self.name.as_ref().ok_or(UnresolvedError)
268+
}
269+
266270
pub fn normalized_edition(&self) -> Result<Option<&String>, UnresolvedError> {
267271
self.edition.as_ref().map(|v| v.normalized()).transpose()
268272
}

src/cargo/util/toml/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ fn normalize_toml(
322322
if let Some(original_package) = original_toml.package() {
323323
let normalized_package =
324324
normalize_package_toml(original_package, manifest_file, is_embedded, gctx, &inherit)?;
325-
let package_name = &normalized_package.name.clone();
325+
let package_name = &normalized_package
326+
.normalized_name()
327+
.expect("previously normalized")
328+
.clone();
326329
let edition = normalized_package
327330
.normalized_edition()
328331
.expect("previously normalized")
@@ -582,7 +585,12 @@ fn normalize_package_toml<'a>(
582585
.map(|value| field_inherit_with(value, "rust-version", || inherit()?.rust_version()))
583586
.transpose()?
584587
.map(manifest::InheritableField::Value);
585-
let name = original_package.name.clone();
588+
let name = Some(
589+
original_package
590+
.name
591+
.clone()
592+
.ok_or_else(|| anyhow::format_err!("missing field `package.name`"))?,
593+
);
586594
let version = original_package
587595
.version
588596
.clone()
@@ -1198,7 +1206,9 @@ pub fn to_real_manifest(
11981206
let normalized_package = normalized_toml
11991207
.package()
12001208
.expect("previously verified to have a `[package]`");
1201-
let package_name = &normalized_package.name;
1209+
let package_name = normalized_package
1210+
.normalized_name()
1211+
.expect("previously normalized");
12021212
if package_name.contains(':') {
12031213
features.require(Feature::open_namespaces())?;
12041214
}

src/cargo/util/toml/targets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(super) fn to_targets(
117117

118118
targets.push(Target::metabuild_target(&format!(
119119
"metabuild-{}",
120-
package.name
120+
package.normalized_name().expect("previously normalized")
121121
)));
122122
}
123123

tests/testsuite/bad_config.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,10 @@ fn cargo_toml_missing_package_name() {
484484
p.cargo("check")
485485
.with_status(101)
486486
.with_stderr_data(str![[r#"
487-
[ERROR] missing field `name`
488-
--> Cargo.toml:2:16
489-
|
490-
2 | [package]
491-
| ^^^^^^^^^
492-
|
487+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
488+
489+
Caused by:
490+
missing field `package.name`
493491
494492
"#]])
495493
.run();

0 commit comments

Comments
 (0)