From e7f96b4676a64477d26e1f092ec123cdbfc41200 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 | 2 ++ src/cargo/core/package.rs | 8 ++++++-- src/doc/src/reference/environment-variables.md | 1 + tests/testsuite/build.rs | 1 + 8 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c981033f846..3acf5da0058 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,7 +254,7 @@ dependencies = [ [[package]] name = "build-rs" -version = "0.2.0" +version = "0.2.1" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index f54a6c5d642..1a70da4f247 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ anstyle = "1.0.8" anyhow = "1.0.86" base64 = "0.22.1" blake3 = "1.5.2" -build-rs = { version = "0.2.0", path = "crates/build-rs" } +build-rs = { version = "0.2.1", path = "crates/build-rs" } bytesize = "1.3" cargo = { path = "" } cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" } diff --git a/crates/build-rs/Cargo.toml b/crates/build-rs/Cargo.toml index e2e8eb9fccf..e84d03fb105 100644 --- a/crates/build-rs/Cargo.toml +++ b/crates/build-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "build-rs" -version = "0.2.0" +version = "0.2.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 6f564f8c68b..02c70315e44 100644 --- a/crates/build-rs/src/input.rs +++ b/crates/build-rs/src/input.rs @@ -536,6 +536,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 7531f0fa61b..90a4dbdca28 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -361,6 +361,7 @@ impl<'gctx> Compilation<'gctx> { // consider adding the corresponding properties to the hash // in BuildContext::target_metadata() let rust_version = pkg.rust_version().as_ref().map(ToString::to_string); + let edition = pkg.edition(); cmd.env("CARGO_MANIFEST_DIR", pkg.root()) .env("CARGO_MANIFEST_PATH", pkg.manifest_path()) .env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string()) @@ -390,6 +391,7 @@ impl<'gctx> Compilation<'gctx> { metadata.license_file.as_ref().unwrap_or(&String::new()), ) .env("CARGO_PKG_AUTHORS", &pkg.authors().join(":")) + .env("CARGO_PKG_EDITION", edition.to_string()) .env( "CARGO_PKG_RUST_VERSION", &rust_version.as_deref().unwrap_or_default(), diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 3d7c84a4266..0588f268897 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -23,8 +23,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 c47c5d178d0..23ee7d14f34 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 5b5ad0dfc3c..e31a5bb30d7 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1628,6 +1628,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");