Skip to content

Commit

Permalink
feat(toml): Add support for setting the default package version (rust…
Browse files Browse the repository at this point in the history
…-lang#6583)

Allow changing the default package version by setting the `CARGO_SUGGESTED_PKG_VERSION` environment variable.
  • Loading branch information
sunhaitao committed Dec 12, 2024
1 parent 3cd245c commit 7484618
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
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)
.or_else(|| {
let suggestion = std::env::var("CARGO_SUGGESTED_PKG_VERSION") else {
return None;
};
let version = Version::parse(&suggestion.unwrap()) else {
return None;
};
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

0 comments on commit 7484618

Please sign in to comment.