Skip to content

Commit d60e025

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 rust-lang#13441. This was added in rust-lang#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 rust-lang#3946 was overly broad and covered many use cases. One of those was for packages to look up information on their dependents. Issue rust-lang#13484 is being left open to track that. Fixes rust-lang#3946
1 parent 84dc5dc commit d60e025

File tree

3 files changed

+13
-48
lines changed

3 files changed

+13
-48
lines changed

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
@@ -267,7 +267,7 @@ corresponding environment variable is set to the empty string, `""`.
267267
where integration tests or benchmarks are free to put any data needed by
268268
the tests/benches. Cargo initially creates this directory but doesn't
269269
manage its content in any way, this is the responsibility of the test code.
270-
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from **(nightly only)**.
270+
* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from
271271

272272
[Cargo target]: cargo-targets.md
273273
[binaries]: cargo-targets.md#binaries

tests/testsuite/build.rs

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

17501750
println!("build");
1751-
p.cargo("build -v")
1752-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1753-
.run();
1751+
p.cargo("build -v").run();
17541752

17551753
println!("bin");
17561754
p.process(&p.bin("foo-bar"))
17571755
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
17581756
.run();
17591757

17601758
println!("example");
1761-
p.cargo("run --example ex-env-vars -v")
1762-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1763-
.run();
1759+
p.cargo("run --example ex-env-vars -v").run();
17641760

17651761
println!("test");
1766-
p.cargo("test -v")
1767-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1768-
.run();
1762+
p.cargo("test -v").run();
17691763

17701764
if is_nightly() {
17711765
println!("bench");
1772-
p.cargo("bench -v")
1773-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
1774-
.run();
1766+
p.cargo("bench -v").run();
17751767
}
17761768
}
17771769

@@ -1857,11 +1849,9 @@ fn cargo_rustc_current_dir_foreign_workspace_dep() {
18571849

18581850
// Verify it works from a different workspace
18591851
foo.cargo("test -p baz")
1860-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
18611852
.with_stdout_contains("running 1 test\ntest baz_env ... ok")
18621853
.run();
18631854
foo.cargo("test -p baz_member")
1864-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
18651855
.with_stdout_contains("running 1 test\ntest baz_member_env ... ok")
18661856
.run();
18671857
}
@@ -1906,33 +1896,10 @@ fn cargo_rustc_current_dir_non_local_dep() {
19061896
.build();
19071897

19081898
p.cargo("test -p bar")
1909-
.masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"])
19101899
.with_stdout_contains("running 1 test\ntest bar_env ... ok")
19111900
.run();
19121901
}
19131902

1914-
#[cargo_test]
1915-
fn cargo_rustc_current_dir_is_not_stable() {
1916-
if is_nightly() {
1917-
return;
1918-
}
1919-
let p = project()
1920-
.file(
1921-
"tests/env.rs",
1922-
r#"
1923-
use std::path::Path;
1924-
1925-
#[test]
1926-
fn env() {
1927-
assert_eq!(option_env!("CARGO_RUSTC_CURRENT_DIR"), None);
1928-
}
1929-
"#,
1930-
)
1931-
.build();
1932-
1933-
p.cargo("test").run();
1934-
}
1935-
19361903
#[cargo_test]
19371904
fn crate_authors_env_vars() {
19381905
let p = project()

0 commit comments

Comments
 (0)