From f756b8bd655ea78987ba1443113f3128a54ee7a4 Mon Sep 17 00:00:00 2001 From: Ifropc Date: Wed, 14 Aug 2024 21:53:03 -0700 Subject: [PATCH] feat: add CARGO_MANIFEST_PATH env variable --- src/cargo/core/compiler/compilation.rs | 1 + src/cargo/core/compiler/custom_build.rs | 1 + .../src/reference/environment-variables.md | 5 +++- src/doc/src/reference/unstable.md | 2 +- tests/testsuite/script.rs | 30 +++++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 405b17b98841..8ec625d77a46 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -356,6 +356,7 @@ impl<'gctx> Compilation<'gctx> { // in BuildContext::target_metadata() let rust_version = pkg.rust_version().as_ref().map(ToString::to_string); cmd.env("CARGO_MANIFEST_DIR", pkg.root()) + .env("CARGO_MANIFEST_PATH", pkg.manifest_path()) .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/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 706783d93328..f36e741c65b6 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -279,6 +279,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul let debug = unit.profile.debuginfo.is_turned_on(); cmd.env("OUT_DIR", &script_out_dir) .env("CARGO_MANIFEST_DIR", unit.pkg.root()) + .env("CARGO_MANIFEST_PATH", unit.pkg.manifest_path()) .env("NUM_JOBS", &bcx.jobs().to_string()) .env("TARGET", bcx.target_data.short_name(&unit.kind)) .env("DEBUG", debug.to_string()) diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 3a7053675cc5..e26a69534162 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -225,7 +225,8 @@ Note that if one of these values is not provided in the manifest, the corresponding environment variable is set to the empty string, `""`. * `CARGO` --- Path to the `cargo` binary performing the build. -* `CARGO_MANIFEST_DIR` --- The directory containing the manifest of your package. +* `CARGO_MANIFEST_DIR` --- The directory containing the manifest of your package. When running a [Cargo script], this variable will be set to the script location. +* `CARGO_MANIFEST_PATH` --- The path to the manifest of your package. * `CARGO_PKG_VERSION` --- The full version of your package. * `CARGO_PKG_VERSION_MAJOR` --- The major version of your package. * `CARGO_PKG_VERSION_MINOR` --- The minor version of your package. @@ -274,6 +275,7 @@ corresponding environment variable is set to the empty string, `""`. [examples]: cargo-targets.md#examples [integration test]: cargo-targets.md#integration-tests [`env` macro]: ../../std/macro.env.html +[Cargo script]: unstable.md#script ### Dynamic library paths @@ -320,6 +322,7 @@ let out_dir = env::var("OUT_DIR").unwrap(); * `CARGO_MANIFEST_DIR` --- The directory containing the manifest for the package being built (the package containing the build script). Also note that this is the value of the current working directory of the build script when it starts. +* `CARGO_MANIFEST_PATH` --- The path to the manifest of your package. * `CARGO_MANIFEST_LINKS` --- the manifest `links` value. * `CARGO_MAKEFLAGS` --- Contains parameters needed for Cargo's [jobserver] implementation to parallelize subprocesses. Rustc or cargo invocations from diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 18be45fc2b03..7e265a4a26db 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -1460,7 +1460,7 @@ This will not affect any hard-coded paths in the source code, such as in strings Values in a non-empty array would be joined into a comma-separated list. If the build script introduces absolute paths to built artifacts (such as by invoking a compiler), the user may request them to be sanitized in different types of artifacts. - Common paths requiring sanitization include `OUT_DIR` and `CARGO_MANIFEST_DIR`, + Common paths requiring sanitization include `OUT_DIR`, `CARGO_MANIFEST_DIR` and `CARGO_MANIFEST_PATH`, plus any other introduced by the build script, such as include directories. ## gc diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index d5a2c5349a80..f63c4131efa8 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -1317,3 +1317,33 @@ fn cmd_publish_with_embedded() { "#]]) .run(); } + +#[cargo_test] +fn manifest_path_env() { + let p = cargo_test_support::project() + .file( + "script.rs", + r#"#!/usr/bin/env cargo + +fn main() { + let path = env!("CARGO_MANIFEST_PATH"); + println!("CARGO_MANIFEST_PATH: {}", path); +} +"#, + ) + .build(); + p.cargo("-Zscript -v script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .with_stdout_data(str![[r#" +CARGO_MANIFEST_PATH: [ROOT]/foo/script.rs + +"#]]) + .with_stderr_data(str![[r#" +[WARNING] `package.edition` is unspecified, defaulting to `2021` +[COMPILING] script v0.0.0 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script` + +"#]]) + .run(); +}