Skip to content

Commit 5a1fa7f

Browse files
committed
feat: Stablize CARGO_RUSTC_CURRENT_DIR
This provides what cargo sets as the `current_dir` for the `rustc` process. While `std::file!` is unspecified in what it is relative to, it is relatively safe, it is generally relative to `rustc`s `current_dir`. This can be useful for snapshot testing. For example, `snapbox` has been using this macro on nightly since assert-rs/snapbox#247, falling back to finding a parent of `CARGO_MANIFEST_DIR`, if present. This has been in use in Cargo since #13441. This was added in #12996. Relevant points discussed in that issue: - This diverged from the original proposal from the Cargo team of having a `CARGO_WORKSPACE_DIR` that is the "workspace" of the package being built (ie registry packages would map to `CARGO_MANIFEST_DIR`). In looking at the `std::file!` use case, `CARGO_MANIFEST_DIR`, no matter how we defined it, would only sort of work because no sane definition of that maps to `rustc`'s `current_dir`.a This instead focuses on the mechanism currently being used. - Using "current dir" in the name is meant to be consistent with `std::env::current_dir`. - I can go either way on `CARGO_RUSTC` vs `RUSTC`. Existing related variables: - `RUSTC` - `RUSTC_WRAPPER` - `RUSTC_WORKSPACE_WRAPPER` - `RUSTFLAGS` (no `C`) - `CARGO_CACHE_RUSTC_INFO` Note that #3946 was overly broad and covered many use cases. One of those was for packages to look up information on their dependents. Issue #13484 is being left open to track that. Fixes #3946
1 parent a510712 commit 5a1fa7f

3 files changed

Lines changed: 13 additions & 48 deletions

File tree

src/cargo/core/compiler/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,16 +696,14 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
696696
let tmp = build_runner.files().layout(unit.kind).prepare_tmp()?;
697697
base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
698698
}
699-
if build_runner.bcx.gctx.nightly_features_allowed {
700-
// This must come after `build_base_args` (which calls `add_path_args`) so that the `cwd`
701-
// is set correctly.
702-
base.env(
703-
"CARGO_RUSTC_CURRENT_DIR",
704-
base.get_cwd()
705-
.map(|c| c.display().to_string())
706-
.unwrap_or(String::new()),
707-
);
708-
}
699+
// This must come after `build_base_args` (which calls `add_path_args`) so that the `cwd`
700+
// is set correctly.
701+
base.env(
702+
"CARGO_RUSTC_CURRENT_DIR",
703+
base.get_cwd()
704+
.map(|c| c.display().to_string())
705+
.unwrap_or(String::new()),
706+
);
709707

710708
Ok(base)
711709
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ corresponding environment variable is set to the empty string, `""`.
265265
where integration tests or benchmarks are free to put any data needed by
266266
the tests/benches. Cargo initially creates this directory but doesn't
267267
manage its content in any way, this is the responsibility of the test code.
268-
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from **(nightly only)**.
268+
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from
269269

270270
[Cargo target]: cargo-targets.md
271271
[binaries]: cargo-targets.md#binaries

tests/testsuite/build.rs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,30 +1705,22 @@ fn crate_env_vars() {
17051705
};
17061706

17071707
println!("build");
1708-
p.cargo("build -v")
1709-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1710-
.run();
1708+
p.cargo("build -v").run();
17111709

17121710
println!("bin");
17131711
p.process(&p.bin("foo-bar"))
17141712
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
17151713
.run();
17161714

17171715
println!("example");
1718-
p.cargo("run --example ex-env-vars -v")
1719-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1720-
.run();
1716+
p.cargo("run --example ex-env-vars -v").run();
17211717

17221718
println!("test");
1723-
p.cargo("test -v")
1724-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1725-
.run();
1719+
p.cargo("test -v").run();
17261720

17271721
if is_nightly() {
17281722
println!("bench");
1729-
p.cargo("bench -v")
1730-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1731-
.run();
1723+
p.cargo("bench -v").run();
17321724
}
17331725
}
17341726

@@ -1814,11 +1806,9 @@ fn cargo_rustc_current_dir_foreign_workspace_dep() {
18141806

18151807
// Verify it works from a different workspace
18161808
foo.cargo("test -p baz")
1817-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
18181809
.with_stdout_contains("running 1 test\ntest baz_env ... ok")
18191810
.run();
18201811
foo.cargo("test -p baz_member")
1821-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
18221812
.with_stdout_contains("running 1 test\ntest baz_member_env ... ok")
18231813
.run();
18241814
}
@@ -1863,33 +1853,10 @@ fn cargo_rustc_current_dir_non_local_dep() {
18631853
.build();
18641854

18651855
p.cargo("test -p bar")
1866-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
18671856
.with_stdout_contains("running 1 test\ntest bar_env ... ok")
18681857
.run();
18691858
}
18701859

1871-
#[cargo_test]
1872-
fn cargo_rustc_current_dir_is_not_stable() {
1873-
if is_nightly() {
1874-
return;
1875-
}
1876-
let p = project()
1877-
.file(
1878-
"tests/env.rs",
1879-
r#"
1880-
use std::path::Path;
1881-
1882-
#[test]
1883-
fn env() {
1884-
assert_eq!(option_env!("CARGO_RUSTC_CURRENT_DIR"), None);
1885-
}
1886-
"#,
1887-
)
1888-
.build();
1889-
1890-
p.cargo("test").run();
1891-
}
1892-
18931860
#[cargo_test]
18941861
fn crate_authors_env_vars() {
18951862
let p = project()

0 commit comments

Comments
 (0)