From 5a1fa7f26baa398653d737d024f22c318c927661 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Mar 2024 20:19:43 -0500 Subject: [PATCH] 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/trycmd#247, falling back to finding a parent of `CARGO_MANIFEST_DIR`, if present. This has been in use in Cargo since rust-lang/cargo#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 --- src/cargo/core/compiler/mod.rs | 18 ++++---- .../src/reference/environment-variables.md | 2 +- tests/testsuite/build.rs | 41 ++----------------- 3 files changed, 13 insertions(+), 48 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index bed881d50db1..ac8b17680b29 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -696,16 +696,14 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult let tmp = build_runner.files().layout(unit.kind).prepare_tmp()?; base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string()); } - if build_runner.bcx.gctx.nightly_features_allowed { - // This must come after `build_base_args` (which calls `add_path_args`) so that the `cwd` - // is set correctly. - base.env( - "CARGO_RUSTC_CURRENT_DIR", - base.get_cwd() - .map(|c| c.display().to_string()) - .unwrap_or(String::new()), - ); - } + // This must come after `build_base_args` (which calls `add_path_args`) so that the `cwd` + // is set correctly. + base.env( + "CARGO_RUSTC_CURRENT_DIR", + base.get_cwd() + .map(|c| c.display().to_string()) + .unwrap_or(String::new()), + ); Ok(base) } diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 40a0d32f1ae5..a00399ddfe52 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -265,7 +265,7 @@ corresponding environment variable is set to the empty string, `""`. where integration tests or benchmarks are free to put any data needed by the tests/benches. Cargo initially creates this directory but doesn't manage its content in any way, this is the responsibility of the test code. -* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from **(nightly only)**. +* `CARGO_RUSTC_CURRENT_DIR` --- This is a path that `rustc` is invoked from [Cargo target]: cargo-targets.md [binaries]: cargo-targets.md#binaries diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 17965aa8dcf9..f1addda28aeb 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1705,9 +1705,7 @@ fn crate_env_vars() { }; println!("build"); - p.cargo("build -v") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) - .run(); + p.cargo("build -v").run(); println!("bin"); p.process(&p.bin("foo-bar")) @@ -1715,20 +1713,14 @@ fn crate_env_vars() { .run(); println!("example"); - p.cargo("run --example ex-env-vars -v") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) - .run(); + p.cargo("run --example ex-env-vars -v").run(); println!("test"); - p.cargo("test -v") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) - .run(); + p.cargo("test -v").run(); if is_nightly() { println!("bench"); - p.cargo("bench -v") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) - .run(); + p.cargo("bench -v").run(); } } @@ -1814,11 +1806,9 @@ fn cargo_rustc_current_dir_foreign_workspace_dep() { // Verify it works from a different workspace foo.cargo("test -p baz") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) .with_stdout_contains("running 1 test\ntest baz_env ... ok") .run(); foo.cargo("test -p baz_member") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) .with_stdout_contains("running 1 test\ntest baz_member_env ... ok") .run(); } @@ -1863,33 +1853,10 @@ fn cargo_rustc_current_dir_non_local_dep() { .build(); p.cargo("test -p bar") - .masquerade_as_nightly_cargo(&["CARGO_RUSTC_CURRENT_DIR"]) .with_stdout_contains("running 1 test\ntest bar_env ... ok") .run(); } -#[cargo_test] -fn cargo_rustc_current_dir_is_not_stable() { - if is_nightly() { - return; - } - let p = project() - .file( - "tests/env.rs", - r#" - use std::path::Path; - - #[test] - fn env() { - assert_eq!(option_env!("CARGO_RUSTC_CURRENT_DIR"), None); - } - "#, - ) - .build(); - - p.cargo("test").run(); -} - #[cargo_test] fn crate_authors_env_vars() { let p = project()