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");