Skip to content

Commit 7cbdcf0

Browse files
committed
Auto merge of #14404 - Ifropc:cargo_manifest_path, r=weihanglo
feat: add CARGO_MANIFEST_PATH env variable Adds `CARGO_MANIFEST_PATH` variable as part of #12207 Context: `CARGO_MANIFEST_DIR` is not very useful, because there is no `Cargo.toml` file when running a cargo script. In cases when multiple scripts are stored in the same folder, we can't tell which script exactly is being run using `CARGO_MANIFEST_DIR`
2 parents 4b81a83 + d4ac929 commit 7cbdcf0

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
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

+2
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.
@@ -320,6 +321,7 @@ let out_dir = env::var("OUT_DIR").unwrap();
320321
* `CARGO_MANIFEST_DIR` --- The directory containing the manifest for the package
321322
being built (the package containing the build script). Also note that this is
322323
the value of the current working directory of the build script when it starts.
324+
* `CARGO_MANIFEST_PATH` --- The path to the manifest of your package.
323325
* `CARGO_MANIFEST_LINKS` --- the manifest `links` value.
324326
* `CARGO_MAKEFLAGS` --- Contains parameters needed for Cargo's [jobserver]
325327
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
@@ -1466,7 +1466,7 @@ This will not affect any hard-coded paths in the source code, such as in strings
14661466
Values in a non-empty array would be joined into a comma-separated list.
14671467
If the build script introduces absolute paths to built artifacts (such as by invoking a compiler),
14681468
the user may request them to be sanitized in different types of artifacts.
1469-
Common paths requiring sanitization include `OUT_DIR` and `CARGO_MANIFEST_DIR`,
1469+
Common paths requiring sanitization include `OUT_DIR`, `CARGO_MANIFEST_DIR` and `CARGO_MANIFEST_PATH`,
14701470
plus any other introduced by the build script, such as include directories.
14711471

14721472
## gc

tests/testsuite/build.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@ fn crate_env_vars() {
16231623
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
16241624
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
16251625
static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
1626+
static CARGO_MANIFEST_PATH: &'static str = env!("CARGO_MANIFEST_PATH");
16261627
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
16271628
static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE");
16281629
static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY");
@@ -1636,9 +1637,9 @@ fn crate_env_vars() {
16361637
16371638
16381639
fn main() {
1639-
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
1640+
let s = format!("{}-{}-{} @ {} in {} file {}", VERSION_MAJOR,
16401641
VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
1641-
CARGO_MANIFEST_DIR);
1642+
CARGO_MANIFEST_DIR, CARGO_MANIFEST_PATH);
16421643
assert_eq!(s, foo::version());
16431644
println!("{}", s);
16441645
assert_eq!("foo", PKG_NAME);
@@ -1672,12 +1673,13 @@ fn crate_env_vars() {
16721673
use std::path::PathBuf;
16731674
16741675
pub fn version() -> String {
1675-
format!("{}-{}-{} @ {} in {}",
1676+
format!("{}-{}-{} @ {} in {} file {}",
16761677
env!("CARGO_PKG_VERSION_MAJOR"),
16771678
env!("CARGO_PKG_VERSION_MINOR"),
16781679
env!("CARGO_PKG_VERSION_PATCH"),
16791680
env!("CARGO_PKG_VERSION_PRE"),
1680-
env!("CARGO_MANIFEST_DIR"))
1681+
env!("CARGO_MANIFEST_DIR"),
1682+
env!("CARGO_MANIFEST_PATH"))
16811683
}
16821684
16831685
pub fn check_no_int_test_env() {
@@ -1793,7 +1795,7 @@ fn crate_env_vars() {
17931795
println!("bin");
17941796
p.process(&p.bin("foo-bar"))
17951797
.with_stdout_data(str![[r#"
1796-
0-5-1 @ alpha.1 in [ROOT]/foo
1798+
0-5-1 @ alpha.1 in [ROOT]/foo file [ROOT]/foo/Cargo.toml
17971799
17981800
"#]])
17991801
.run();
@@ -1861,12 +1863,15 @@ fn cargo_rustc_current_dir_foreign_workspace_dep() {
18611863
fn baz_env() {
18621864
let workspace_dir = Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
18631865
let manifest_dir = Path::new(option_env!("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
1866+
let manifest_path = Path::new(option_env!("CARGO_MANIFEST_PATH").expect("CARGO_MANIFEST_PATH"));
18641867
let current_dir = std::env::current_dir().expect("current_dir");
18651868
let file_path = workspace_dir.join(file!());
18661869
assert!(file_path.exists(), "{}", file_path.display());
18671870
let workspace_dir = std::fs::canonicalize(current_dir.join(workspace_dir)).expect("CARGO_RUSTC_CURRENT_DIR");
1871+
let manifest_path = std::fs::canonicalize(current_dir.join(manifest_dir.clone()).join("Cargo.toml")).expect("CARGO_MANIFEST_PATH");
18681872
let manifest_dir = std::fs::canonicalize(current_dir.join(manifest_dir)).expect("CARGO_MANIFEST_DIR");
1869-
assert_eq!(workspace_dir, manifest_dir);
1873+
assert_eq!(workspace_dir, manifest_dir.clone());
1874+
assert_eq!(manifest_dir.join("Cargo.toml"), manifest_path);
18701875
}
18711876
"#,
18721877
)
@@ -1955,12 +1960,15 @@ fn cargo_rustc_current_dir_non_local_dep() {
19551960
fn bar_env() {
19561961
let workspace_dir = Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
19571962
let manifest_dir = Path::new(option_env!("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
1963+
let manifest_path = Path::new(option_env!("CARGO_MANIFEST_PATH").expect("CARGO_MANIFEST_PATH"));
19581964
let current_dir = std::env::current_dir().expect("current_dir");
19591965
let file_path = workspace_dir.join(file!());
19601966
assert!(file_path.exists(), "{}", file_path.display());
19611967
let workspace_dir = std::fs::canonicalize(current_dir.join(workspace_dir)).expect("CARGO_RUSTC_CURRENT_DIR");
1968+
let manifest_path = std::fs::canonicalize(current_dir.join(manifest_dir.clone()).join("Cargo.toml")).expect("CARGO_MANIFEST_PATH");
19621969
let manifest_dir = std::fs::canonicalize(current_dir.join(manifest_dir)).expect("CARGO_MANIFEST_DIR");
1963-
assert_eq!(workspace_dir, manifest_dir);
1970+
assert_eq!(workspace_dir, manifest_dir.clone());
1971+
assert_eq!(manifest_dir.join("Cargo.toml"), manifest_path);
19641972
}
19651973
"#,
19661974
)

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)