Skip to content

Commit 1923b5b

Browse files
committed
feat(toml): Allow versionless packages
This defaults the version to `0.0.0` for most of cargo. It is an error to lack a version and have a package publishable. That means you have to add `publish = false`.
1 parent 0b5ebd4 commit 1923b5b

File tree

6 files changed

+290
-45
lines changed

6 files changed

+290
-45
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,17 @@ impl TomlManifest {
550550
let version = package
551551
.version
552552
.clone()
553-
.resolve("version", || inherit()?.version())?;
553+
.map(|version| version.resolve("version", || inherit()?.version()))
554+
.transpose()?;
554555

555-
package.version = MaybeWorkspace::Defined(version.clone());
556+
package.version = version.clone().map(MaybeWorkspace::Defined);
556557

557-
let pkgid = package.to_package_id(source_id, version)?;
558+
let pkgid = package.to_package_id(
559+
source_id,
560+
version
561+
.clone()
562+
.unwrap_or_else(|| semver::Version::new(0, 0, 0)),
563+
)?;
558564

559565
let edition = if let Some(edition) = package.edition.clone() {
560566
let edition: Edition = edition
@@ -1009,6 +1015,10 @@ impl TomlManifest {
10091015
None | Some(VecStringOrBool::Bool(true)) => None,
10101016
};
10111017

1018+
if version.is_none() && publish != Some(vec![]) {
1019+
bail!("`package.publish` requires `package.version` be specified");
1020+
}
1021+
10121022
if summary.features().contains_key("default-features") {
10131023
warnings.push(
10141024
"`default-features = [\"..\"]` was found in [features]. \
@@ -1659,7 +1669,7 @@ pub struct TomlPackage {
16591669
edition: Option<MaybeWorkspaceString>,
16601670
rust_version: Option<MaybeWorkspaceRustVersion>,
16611671
name: InternedString,
1662-
version: MaybeWorkspaceSemverVersion,
1672+
version: Option<MaybeWorkspaceSemverVersion>,
16631673
authors: Option<MaybeWorkspaceVecString>,
16641674
build: Option<StringOrBool>,
16651675
metabuild: Option<StringOrVec>,

src/doc/src/reference/manifest.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ resolve dependencies, and for guidelines on setting your own version. See the
109109
[SemVer compatibility] chapter for more details on exactly what constitutes a
110110
breaking change.
111111

112+
This field is optional and defaults to `0.0.0`.
113+
112114
[Resolver]: resolver.md
113115
[SemVer compatibility]: semver.md
114116

tests/testsuite/check.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,22 +1506,17 @@ fn versionless_package() {
15061506
[package]
15071507
name = "foo"
15081508
description = "foo"
1509+
publish = false
15091510
"#,
15101511
)
15111512
.file("src/lib.rs", "")
15121513
.build();
15131514
p.cargo("check")
15141515
.with_stderr(
1515-
r#"error: failed to parse manifest at `[CWD]/Cargo.toml`
1516-
1517-
Caused by:
1518-
TOML parse error at line 2, column 17
1519-
|
1520-
2 | [package]
1521-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1522-
missing field `version`
1523-
"#,
1516+
"\
1517+
[CHECKING] foo v0.0.0 ([CWD])
1518+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1519+
",
15241520
)
1525-
.with_status(101)
15261521
.run();
15271522
}

tests/testsuite/metadata.rs

Lines changed: 242 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,6 +4273,7 @@ fn versionless_packages() {
42734273
r#"
42744274
[package]
42754275
name = "bar"
4276+
publish = false
42764277
42774278
[dependencies]
42784279
foobar = "0.0.1"
@@ -4285,6 +4286,7 @@ fn versionless_packages() {
42854286
r#"
42864287
[package]
42874288
name = "baz"
4289+
publish = false
42884290
42894291
[dependencies]
42904292
foobar = "0.0.1"
@@ -4295,20 +4297,247 @@ fn versionless_packages() {
42954297
Package::new("foobar", "0.0.1").publish();
42964298

42974299
p.cargo("metadata -q --format-version 1")
4298-
.with_stderr(
4299-
r#"error: failed to load manifest for workspace member `[CWD]/bar`
4300-
4301-
Caused by:
4302-
failed to parse manifest at `[CWD]/bar/Cargo.toml`
4303-
4304-
Caused by:
4305-
TOML parse error at line 2, column 17
4306-
|
4307-
2 | [package]
4308-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4309-
missing field `version`
4300+
.with_json(
4301+
r#"
4302+
{
4303+
"packages": [
4304+
{
4305+
"name": "bar",
4306+
"version": "0.0.0",
4307+
"id": "bar 0.0.0 [..]",
4308+
"license": null,
4309+
"license_file": null,
4310+
"description": null,
4311+
"source": null,
4312+
"dependencies": [
4313+
{
4314+
"name": "baz",
4315+
"source": null,
4316+
"req": "*",
4317+
"kind": null,
4318+
"rename": null,
4319+
"optional": false,
4320+
"uses_default_features": true,
4321+
"features": [],
4322+
"target": null,
4323+
"registry": null,
4324+
"path": "[..]/baz"
4325+
},
4326+
{
4327+
"name": "foobar",
4328+
"source": "registry+https://github.com/rust-lang/crates.io-index",
4329+
"req": "^0.0.1",
4330+
"kind": null,
4331+
"rename": null,
4332+
"optional": false,
4333+
"uses_default_features": true,
4334+
"features": [],
4335+
"target": null,
4336+
"registry": null
4337+
}
4338+
],
4339+
"targets": [
4340+
{
4341+
"kind": [
4342+
"lib"
4343+
],
4344+
"crate_types": [
4345+
"lib"
4346+
],
4347+
"name": "bar",
4348+
"src_path": "[..]/bar/src/lib.rs",
4349+
"edition": "2015",
4350+
"doc": true,
4351+
"doctest": true,
4352+
"test": true
4353+
}
4354+
],
4355+
"features": {},
4356+
"manifest_path": "[..]/bar/Cargo.toml",
4357+
"metadata": null,
4358+
"publish": [],
4359+
"authors": [],
4360+
"categories": [],
4361+
"keywords": [],
4362+
"readme": null,
4363+
"repository": null,
4364+
"homepage": null,
4365+
"documentation": null,
4366+
"edition": "2015",
4367+
"links": null,
4368+
"default_run": null,
4369+
"rust_version": null
4370+
},
4371+
{
4372+
"name": "baz",
4373+
"version": "0.0.0",
4374+
"id": "baz 0.0.0 [..]",
4375+
"license": null,
4376+
"license_file": null,
4377+
"description": null,
4378+
"source": null,
4379+
"dependencies": [
4380+
{
4381+
"name": "foobar",
4382+
"source": "registry+https://github.com/rust-lang/crates.io-index",
4383+
"req": "^0.0.1",
4384+
"kind": null,
4385+
"rename": null,
4386+
"optional": false,
4387+
"uses_default_features": true,
4388+
"features": [],
4389+
"target": null,
4390+
"registry": null
4391+
}
4392+
],
4393+
"targets": [
4394+
{
4395+
"kind": [
4396+
"lib"
4397+
],
4398+
"crate_types": [
4399+
"lib"
4400+
],
4401+
"name": "baz",
4402+
"src_path": "[..]/baz/src/lib.rs",
4403+
"edition": "2015",
4404+
"doc": true,
4405+
"doctest": true,
4406+
"test": true
4407+
}
4408+
],
4409+
"features": {},
4410+
"manifest_path": "[..]/baz/Cargo.toml",
4411+
"metadata": null,
4412+
"publish": [],
4413+
"authors": [],
4414+
"categories": [],
4415+
"keywords": [],
4416+
"readme": null,
4417+
"repository": null,
4418+
"homepage": null,
4419+
"documentation": null,
4420+
"edition": "2015",
4421+
"links": null,
4422+
"default_run": null,
4423+
"rust_version": null
4424+
},
4425+
{
4426+
"name": "foobar",
4427+
"version": "0.0.1",
4428+
"id": "foobar 0.0.1 [..]",
4429+
"license": null,
4430+
"license_file": null,
4431+
"description": null,
4432+
"source": "registry+https://github.com/rust-lang/crates.io-index",
4433+
"dependencies": [],
4434+
"targets": [
4435+
{
4436+
"kind": [
4437+
"lib"
4438+
],
4439+
"crate_types": [
4440+
"lib"
4441+
],
4442+
"name": "foobar",
4443+
"src_path": "[..]/foobar-0.0.1/src/lib.rs",
4444+
"edition": "2015",
4445+
"doc": true,
4446+
"doctest": true,
4447+
"test": true
4448+
}
4449+
],
4450+
"features": {},
4451+
"manifest_path": "[..]/foobar-0.0.1/Cargo.toml",
4452+
"metadata": null,
4453+
"publish": null,
4454+
"authors": [],
4455+
"categories": [],
4456+
"keywords": [],
4457+
"readme": null,
4458+
"repository": null,
4459+
"homepage": null,
4460+
"documentation": null,
4461+
"edition": "2015",
4462+
"links": null,
4463+
"default_run": null,
4464+
"rust_version": null
4465+
}
4466+
],
4467+
"workspace_members": [
4468+
"bar 0.0.0 [..]",
4469+
"baz 0.0.0 [..]"
4470+
],
4471+
"workspace_default_members": [
4472+
"bar 0.0.0 [..]",
4473+
"baz 0.0.0 [..]"
4474+
],
4475+
"resolve": {
4476+
"nodes": [
4477+
{
4478+
"id": "bar 0.0.0 [..]",
4479+
"dependencies": [
4480+
"baz 0.0.0 [..]",
4481+
"foobar 0.0.1 [..]"
4482+
],
4483+
"deps": [
4484+
{
4485+
"name": "baz",
4486+
"pkg": "baz 0.0.0 [..]",
4487+
"dep_kinds": [
4488+
{
4489+
"kind": null,
4490+
"target": null
4491+
}
4492+
]
4493+
},
4494+
{
4495+
"name": "foobar",
4496+
"pkg": "foobar 0.0.1 [..]",
4497+
"dep_kinds": [
4498+
{
4499+
"kind": null,
4500+
"target": null
4501+
}
4502+
]
4503+
}
4504+
],
4505+
"features": []
4506+
},
4507+
{
4508+
"id": "baz 0.0.0 [..]",
4509+
"dependencies": [
4510+
"foobar 0.0.1 [..]"
4511+
],
4512+
"deps": [
4513+
{
4514+
"name": "foobar",
4515+
"pkg": "foobar 0.0.1 [..]",
4516+
"dep_kinds": [
4517+
{
4518+
"kind": null,
4519+
"target": null
4520+
}
4521+
]
4522+
}
4523+
],
4524+
"features": []
4525+
},
4526+
{
4527+
"id": "foobar 0.0.1 [..]",
4528+
"dependencies": [],
4529+
"deps": [],
4530+
"features": []
4531+
}
4532+
],
4533+
"root": null
4534+
},
4535+
"target_directory": "[..]/foo/target",
4536+
"version": 1,
4537+
"workspace_root": "[..]",
4538+
"metadata": null
4539+
}
43104540
"#,
43114541
)
4312-
.with_status(101)
43134542
.run();
43144543
}

tests/testsuite/package.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,23 +3105,31 @@ fn versionless_package() {
31053105
[package]
31063106
name = "foo"
31073107
description = "foo"
3108+
publish = false
31083109
"#,
31093110
)
31103111
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
31113112
.build();
31123113

31133114
p.cargo("package")
31143115
.with_stderr(
3115-
r#"error: failed to parse manifest at `[CWD]/Cargo.toml`
3116-
3117-
Caused by:
3118-
TOML parse error at line 2, column 17
3119-
|
3120-
2 | [package]
3121-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
3122-
missing field `version`
3123-
"#,
3116+
"\
3117+
warning: manifest has no license, license-file, documentation, homepage or repository.
3118+
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
3119+
Packaging foo v0.0.0 ([CWD])
3120+
Verifying foo v0.0.0 ([CWD])
3121+
Compiling foo v0.0.0 ([CWD]/target/package/foo-0.0.0)
3122+
Finished dev [unoptimized + debuginfo] target(s) in [..]s
3123+
Packaged 4 files, [..]B ([..]B compressed)
3124+
",
31243125
)
3125-
.with_status(101)
31263126
.run();
3127+
3128+
let f = File::open(&p.root().join("target/package/foo-0.0.0.crate")).unwrap();
3129+
validate_crate_contents(
3130+
f,
3131+
"foo-0.0.0.crate",
3132+
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
3133+
&[],
3134+
);
31273135
}

0 commit comments

Comments
 (0)