Skip to content

Commit cb33b41

Browse files
committed
Introduce CARGO_PKG_EDITION env var
Solves #14872
1 parent 206e3fe commit cb33b41

File tree

9 files changed

+20
-5
lines changed

9 files changed

+20
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ anstyle = "1.0.11"
2424
anyhow = "1.0.98"
2525
base64 = "0.22.1"
2626
blake3 = "1.8.2"
27-
build-rs = { version = "0.3.1", path = "crates/build-rs" }
27+
build-rs = { version = "0.3.3", path = "crates/build-rs" }
2828
cargo = { path = "" }
2929
cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
3030
cargo-credential-libsecret = { version = "0.5.2", path = "credential/cargo-credential-libsecret" }

crates/build-rs-test-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ fn smoke_test_inputs() {
5151
dbg!(cargo_manifest_links());
5252
dbg!(cargo_pkg_authors());
5353
dbg!(cargo_pkg_description());
54+
// FIXME: enable once released?
55+
// dbg!(cargo_pkg_edition());
5456
dbg!(cargo_pkg_homepage());
5557
dbg!(cargo_pkg_license());
5658
dbg!(cargo_pkg_license_file());

crates/build-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "build-rs"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
rust-version.workspace = true
55
edition.workspace = true
66
license.workspace = true

crates/build-rs/src/input.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ pub fn cargo_pkg_description() -> Option<String> {
580580
to_opt(var_or_panic("CARGO_PKG_DESCRIPTION")).map(to_string)
581581
}
582582

583+
/// The Rust language edition from the manifest of your package.
584+
#[track_caller]
585+
pub fn cargo_pkg_edition() -> Option<String> {
586+
to_opt(var_or_panic("CARGO_PKG_EDITION")).map(to_string)
587+
}
588+
583589
/// The home page from the manifest of your package.
584590
#[track_caller]
585591
pub fn cargo_pkg_homepage() -> Option<String> {

src/cargo/core/compiler/compilation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ impl<'gctx> Compilation<'gctx> {
362362
// in BuildContext::target_metadata()
363363
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
364364
.env("CARGO_MANIFEST_PATH", pkg.manifest_path())
365+
.env("CARGO_PKG_EDITION", pkg.edition().to_string())
365366
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
366367
.env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string())
367368
.env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string())

src/cargo/core/package.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::core::dependency::DepKind;
2222
use crate::core::resolver::features::ForceAllTargets;
2323
use crate::core::resolver::{HasDevUnits, Resolve};
2424
use crate::core::{
25-
CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency,
26-
SourceId, Target,
25+
CliUnstable, Dependency, Edition, Features, Manifest, PackageId, PackageIdSpec,
26+
SerializedDependency, SourceId, Target,
2727
};
2828
use crate::core::{Summary, Workspace};
2929
use crate::sources::source::{MaybePackage, SourceMap};
@@ -169,6 +169,10 @@ impl Package {
169169
pub fn proc_macro(&self) -> bool {
170170
self.targets().iter().any(|target| target.proc_macro())
171171
}
172+
/// Gets the package's language edition
173+
pub fn edition(&self) -> Edition {
174+
self.manifest().edition()
175+
}
172176
/// Gets the package's minimum Rust version.
173177
pub fn rust_version(&self) -> Option<&RustVersion> {
174178
self.manifest().rust_version()

src/doc/src/reference/environment-variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ corresponding environment variable is set to the empty string, `""`.
245245
* `CARGO_PKG_REPOSITORY` --- The repository from the manifest of your package.
246246
* `CARGO_PKG_LICENSE` --- The license from the manifest of your package.
247247
* `CARGO_PKG_LICENSE_FILE` --- The license file from the manifest of your package.
248+
* `CARGO_PKG_EDITION` --- The Rust language edition from the manifest of your package.
248249
* `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package.
249250
Note that this is the minimum Rust version supported by the package, not the
250251
current Rust version.

tests/testsuite/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,7 @@ fn crate_env_vars() {
16291629
static LICENSE: &'static str = env!("CARGO_PKG_LICENSE");
16301630
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
16311631
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
1632+
static EDITION: &'static str = env!("CARGO_PKG_EDITION");
16321633
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
16331634
static README: &'static str = env!("CARGO_PKG_README");
16341635
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");

0 commit comments

Comments
 (0)