Skip to content

Commit 93f08b6

Browse files
committed
Introduce CARGO_PKG_EDITION env var
Solves #14872
1 parent 3908f64 commit 93f08b6

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

crates/build-rs/src/input.rs

+6
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ pub fn cargo_pkg_license_file() -> Option<PathBuf> {
536536
to_opt(var_or_panic("CARGO_PKG_LICENSE_FILE")).map(to_path)
537537
}
538538

539+
/// The Rust language edition from the manifest of your package.
540+
#[track_caller]
541+
pub fn cargo_pkg_edition() -> Option<String> {
542+
to_opt(var_or_panic("CARGO_PKG_EDITION")).map(to_string)
543+
}
544+
539545
/// The Rust version from the manifest of your package. Note that this is the
540546
/// minimum Rust version supported by the package, not the current Rust version.
541547
#[track_caller]

src/cargo/core/compiler/compilation.rs

+2
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl<'gctx> Compilation<'gctx> {
361361
// consider adding the corresponding properties to the hash
362362
// in BuildContext::target_metadata()
363363
let rust_version = pkg.rust_version().as_ref().map(ToString::to_string);
364+
let edition = pkg.edition();
364365
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
365366
.env("CARGO_MANIFEST_PATH", pkg.manifest_path())
366367
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
@@ -390,6 +391,7 @@ impl<'gctx> Compilation<'gctx> {
390391
metadata.license_file.as_ref().unwrap_or(&String::new()),
391392
)
392393
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
394+
.env("CARGO_PKG_EDITION", edition.to_string())
393395
.env(
394396
"CARGO_PKG_RUST_VERSION",
395397
&rust_version.as_deref().unwrap_or_default(),

src/cargo/core/package.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::core::dependency::DepKind;
2323
use crate::core::resolver::features::ForceAllTargets;
2424
use crate::core::resolver::{HasDevUnits, Resolve};
2525
use crate::core::{
26-
CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency,
27-
SourceId, Target,
26+
CliUnstable, Dependency, Edition, Features, Manifest, PackageId, PackageIdSpec,
27+
SerializedDependency, SourceId, Target,
2828
};
2929
use crate::core::{Summary, Workspace};
3030
use crate::sources::source::{MaybePackage, SourceMap};
@@ -167,6 +167,10 @@ impl Package {
167167
pub fn proc_macro(&self) -> bool {
168168
self.targets().iter().any(|target| target.proc_macro())
169169
}
170+
/// Gets the package's language edition
171+
pub fn edition(&self) -> Edition {
172+
self.manifest().edition()
173+
}
170174
/// Gets the package's minimum Rust version.
171175
pub fn rust_version(&self) -> Option<&RustVersion> {
172176
self.manifest().rust_version()

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

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ corresponding environment variable is set to the empty string, `""`.
239239
* `CARGO_PKG_REPOSITORY` --- The repository from the manifest of your package.
240240
* `CARGO_PKG_LICENSE` --- The license from the manifest of your package.
241241
* `CARGO_PKG_LICENSE_FILE` --- The license file from the manifest of your package.
242+
* `CARGO_PKG_EDITION` --- The Rust language edition from the manifest of your package.
242243
* `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package.
243244
Note that this is the minimum Rust version supported by the package, not the
244245
current Rust version.

tests/testsuite/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ fn crate_env_vars() {
16281628
static LICENSE: &'static str = env!("CARGO_PKG_LICENSE");
16291629
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
16301630
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
1631+
static EDITION: &'static str = env!("CARGO_PKG_EDITION");
16311632
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
16321633
static README: &'static str = env!("CARGO_PKG_README");
16331634
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");

0 commit comments

Comments
 (0)