From 1548a7e04ffbda1a85643229083fde0c7d53f769 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 29 Nov 2024 16:55:09 -0500 Subject: [PATCH] Introduce `CARGO_PKG_EDITION` env var Solves #14872 --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/build-rs/Cargo.toml | 2 +- crates/build-rs/src/input.rs | 6 ++++++ src/cargo/core/compiler/compilation.rs | 1 + src/cargo/core/package.rs | 8 ++++++-- src/doc/src/reference/environment-variables.md | 1 + tests/testsuite/build.rs | 1 + 8 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6b1518eea8..3b4c48d9931 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,7 +249,7 @@ dependencies = [ [[package]] name = "build-rs" -version = "0.3.0" +version = "0.3.1" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index 435bc4d478d..980d688e273 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ anstyle = "1.0.10" anyhow = "1.0.95" base64 = "0.22.1" blake3 = "1.5.5" -build-rs = { version = "0.3.0", path = "crates/build-rs" } +build-rs = { version = "0.3.1", path = "crates/build-rs" } cargo = { path = "" } cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" } cargo-credential-libsecret = { version = "0.4.13", path = "credential/cargo-credential-libsecret" } diff --git a/crates/build-rs/Cargo.toml b/crates/build-rs/Cargo.toml index 24e42b09863..643d61bee69 100644 --- a/crates/build-rs/Cargo.toml +++ b/crates/build-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "build-rs" -version = "0.3.0" +version = "0.3.1" rust-version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/build-rs/src/input.rs b/crates/build-rs/src/input.rs index c07264a4f76..ec3d3bef82e 100644 --- a/crates/build-rs/src/input.rs +++ b/crates/build-rs/src/input.rs @@ -604,6 +604,12 @@ pub fn cargo_pkg_license_file() -> Option { to_opt(var_or_panic("CARGO_PKG_LICENSE_FILE")).map(to_path) } +/// The Rust language edition from the manifest of your package. +#[track_caller] +pub fn cargo_pkg_edition() -> Option { + to_opt(var_or_panic("CARGO_PKG_EDITION")).map(to_string) +} + /// The Rust version from the manifest of your package. Note that this is the /// minimum Rust version supported by the package, not the current Rust version. #[track_caller] diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index c30343ef16c..e16f7a8c728 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -360,6 +360,7 @@ impl<'gctx> Compilation<'gctx> { // in BuildContext::target_metadata() cmd.env("CARGO_MANIFEST_DIR", pkg.root()) .env("CARGO_MANIFEST_PATH", pkg.manifest_path()) + .env("CARGO_PKG_EDITION", pkg.edition().to_string()) .env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string()) .env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string()) .env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string()) diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 54bbea21e10..2415607fd3f 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -22,8 +22,8 @@ use crate::core::dependency::DepKind; use crate::core::resolver::features::ForceAllTargets; use crate::core::resolver::{HasDevUnits, Resolve}; use crate::core::{ - CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency, - SourceId, Target, + CliUnstable, Dependency, Edition, Features, Manifest, PackageId, PackageIdSpec, + SerializedDependency, SourceId, Target, }; use crate::core::{Summary, Workspace}; use crate::sources::source::{MaybePackage, SourceMap}; @@ -167,6 +167,10 @@ impl Package { pub fn proc_macro(&self) -> bool { self.targets().iter().any(|target| target.proc_macro()) } + /// Gets the package's language edition + pub fn edition(&self) -> Edition { + self.manifest().edition() + } /// Gets the package's minimum Rust version. pub fn rust_version(&self) -> Option<&RustVersion> { self.manifest().rust_version() diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 7232934f0ae..5cd0304516c 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -239,6 +239,7 @@ corresponding environment variable is set to the empty string, `""`. * `CARGO_PKG_REPOSITORY` --- The repository from the manifest of your package. * `CARGO_PKG_LICENSE` --- The license from the manifest of your package. * `CARGO_PKG_LICENSE_FILE` --- The license file from the manifest of your package. +* `CARGO_PKG_EDITION` --- The Rust language edition from the manifest of your package. * `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package. Note that this is the minimum Rust version supported by the package, not the current Rust version. diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index b37ae9fc63f..5bbfc2da0f4 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1626,6 +1626,7 @@ fn crate_env_vars() { static LICENSE: &'static str = env!("CARGO_PKG_LICENSE"); static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE"); static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION"); + static EDITION: &'static str = env!("CARGO_PKG_EDITION"); static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION"); static README: &'static str = env!("CARGO_PKG_README"); static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");