Skip to content

Commit 58a807f

Browse files
committed
feat: add CARGO_MANIFEST_PATH env variable
1 parent 2f738d6 commit 58a807f

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

src/cargo/core/compiler/compilation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ impl<'gctx> Compilation<'gctx> {
356356
// in BuildContext::target_metadata()
357357
let rust_version = pkg.rust_version().as_ref().map(ToString::to_string);
358358
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
359+
.env("CARGO_MANIFEST_PATH", pkg.manifest_path())
359360
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
360361
.env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string())
361362
.env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string())

src/cargo/core/compiler/custom_build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
279279
let debug = unit.profile.debuginfo.is_turned_on();
280280
cmd.env("OUT_DIR", &script_out_dir)
281281
.env("CARGO_MANIFEST_DIR", unit.pkg.root())
282+
.env("CARGO_MANIFEST_PATH", unit.pkg.manifest_path())
282283
.env("NUM_JOBS", &bcx.jobs().to_string())
283284
.env("TARGET", bcx.target_data.short_name(&unit.kind))
284285
.env("DEBUG", debug.to_string())

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

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ corresponding environment variable is set to the empty string, `""`.
226226

227227
* `CARGO` --- Path to the `cargo` binary performing the build.
228228
* `CARGO_MANIFEST_DIR` --- The directory containing the manifest of your package.
229+
* `CARGO_MANIFEST_PATH` --- The path to the manifest of your package.
229230
* `CARGO_PKG_VERSION` --- The full version of your package.
230231
* `CARGO_PKG_VERSION_MAJOR` --- The major version of your package.
231232
* `CARGO_PKG_VERSION_MINOR` --- The minor version of your package.
@@ -274,6 +275,7 @@ corresponding environment variable is set to the empty string, `""`.
274275
[examples]: cargo-targets.md#examples
275276
[integration test]: cargo-targets.md#integration-tests
276277
[`env` macro]: ../../std/macro.env.html
278+
[Cargo script]: unstable.md#script
277279

278280
### Dynamic library paths
279281

@@ -320,6 +322,7 @@ let out_dir = env::var("OUT_DIR").unwrap();
320322
* `CARGO_MANIFEST_DIR` --- The directory containing the manifest for the package
321323
being built (the package containing the build script). Also note that this is
322324
the value of the current working directory of the build script when it starts.
325+
* `CARGO_MANIFEST_PATH` --- The path to the manifest of your package.
323326
* `CARGO_MANIFEST_LINKS` --- the manifest `links` value.
324327
* `CARGO_MAKEFLAGS` --- Contains parameters needed for Cargo's [jobserver]
325328
implementation to parallelize subprocesses. Rustc or cargo invocations from

src/doc/src/reference/unstable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ This will not affect any hard-coded paths in the source code, such as in strings
14601460
Values in a non-empty array would be joined into a comma-separated list.
14611461
If the build script introduces absolute paths to built artifacts (such as by invoking a compiler),
14621462
the user may request them to be sanitized in different types of artifacts.
1463-
Common paths requiring sanitization include `OUT_DIR` and `CARGO_MANIFEST_DIR`,
1463+
Common paths requiring sanitization include `OUT_DIR`, `CARGO_MANIFEST_DIR` and `CARGO_MANIFEST_PATH`,
14641464
plus any other introduced by the build script, such as include directories.
14651465

14661466
## gc

tests/testsuite/script.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1317,3 +1317,33 @@ fn cmd_publish_with_embedded() {
13171317
"#]])
13181318
.run();
13191319
}
1320+
1321+
#[cargo_test]
1322+
fn manifest_path_env() {
1323+
let p = cargo_test_support::project()
1324+
.file(
1325+
"script.rs",
1326+
r#"#!/usr/bin/env cargo
1327+
1328+
fn main() {
1329+
let path = env!("CARGO_MANIFEST_PATH");
1330+
println!("CARGO_MANIFEST_PATH: {}", path);
1331+
}
1332+
"#,
1333+
)
1334+
.build();
1335+
p.cargo("-Zscript -v script.rs")
1336+
.masquerade_as_nightly_cargo(&["script"])
1337+
.with_stdout_data(str![[r#"
1338+
CARGO_MANIFEST_PATH: [ROOT]/foo/script.rs
1339+
1340+
"#]])
1341+
.with_stderr_data(str![[r#"
1342+
[WARNING] `package.edition` is unspecified, defaulting to `2021`
1343+
[COMPILING] script v0.0.0 ([ROOT]/foo)
1344+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1345+
[RUNNING] `[ROOT]/home/.cargo/target/[HASH]/debug/script[EXE]`
1346+
1347+
"#]])
1348+
.run();
1349+
}

0 commit comments

Comments
 (0)