Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toml): Add support for setting the default package version (#6583) #14919

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use cargo_util_schemas::manifest::{RustVersion, StringOrBool};
use itertools::Itertools;
use lazycell::LazyCell;
use pathdiff::diff_paths;
use semver::Version;
use toml_edit::ImDocument;
use url::Url;

Expand Down Expand Up @@ -566,7 +567,16 @@ fn normalize_package_toml<'a>(
.clone()
.map(|value| field_inherit_with(value, "version", || inherit()?.version()))
.transpose()?
.map(manifest::InheritableField::Value),
.map(manifest::InheritableField::Value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, though this PR seems incomplete. How does this one play well with SemVer? How does the env affect the entire dependency graph?

Note that there are a couple of different use cases in #6583. The design of it is not yet settled, so I am going to mark this as draft. In Cargo we encourage discussions before implementations. See https://doc.crates.io/contrib/process/working-on-cargo.html#before-hacking-on-cargo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that its not just S-needs-design or S-needs-team-input but its S-propose-close.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Going to close this as it is incomplete and unlikely to merge. Feel free to put your use cases in #6583 and we may consider changing its label status.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It plays as well as current practices with SemVer. When building multiple packages in a call of cargo, all unversioned packages will be affected -- as many similar tools for other languages. I'll post more details on #6583.

.or_else(|| {
let suggestion = std::env::var("CARGO_SUGGESTED_PKG_VERSION") else {
return;
};
let version = Version::parse(&suggestion.unwrap()) else {
return;
};
Some(manifest::InheritableField::Value(version.unwrap()))
}),
authors: original_package
.authors
.clone()
Expand Down
5 changes: 4 additions & 1 deletion src/doc/src/reference/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ so versions are considered considered [compatible](semver.md) if their left-most
See the [Resolver] chapter for more information on how Cargo uses versions to
resolve dependencies.

This field is optional and defaults to `0.0.0`. The field is required for publishing packages.
This field is optional. It defaults to the value of the `CARGO_SUGGESTED_PKG_VERSION`
environment variable. If the variable is not set or the value is invalid,
it defaults to `0.0.0`. The field is required for publishing packages.

> **MSRV:** Before 1.86, this field defaulted to `0.0.0`
> **MSRV:** Before 1.75, this field was required

[SemVer]: https://semver.org
Expand Down
51 changes: 51 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,57 @@ fn staticlib_rlib_and_bin() {
p.cargo("build -v").run();
}

#[cargo_test]
fn suggested_pkg_version() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "ver"
"#,
)
.file(
"src/main.rs",
r#"
static VERSION: &'static str = env!("CARGO_PKG_VERSION");

fn main() {
println!("{}", VERSION);
}
"#,
)
.build();

p.cargo("build -v").run();
p.process(&p.bin("ver"))
.with_stdout_data(str![[r#"
0.0.0

"#]])
.run();

p.cargo("build -v")
.env("CARGO_SUGGESTED_PKG_VERSION", "X.Y.Z")
.run();
p.process(&p.bin("ver"))
.with_stdout_data(str![[r#"
0.0.0

"#]])
.run();

p.cargo("build -v")
.env("CARGO_SUGGESTED_PKG_VERSION", "1.2.3")
.run();
p.process(&p.bin("ver"))
.with_stdout_data(str![[r#"
1.2.3

"#]])
.run();
}

#[cargo_test]
fn opt_out_of_bin() {
let p = project()
Expand Down
Loading