Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Stablize CARGO_RUSTC_CURRENT_DIR #13644

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/cargo/core/compiler/mod.rs
Expand Up @@ -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()),
Comment on lines +703 to +705
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if it could just be.

Suggested change
base.get_cwd()
.map(|c| c.display().to_string())
.unwrap_or(String::new()),
base.get_cwd().unwrap_or_default(),

(and ditto for CARGO_TARGET_TMPDIR? though that is not relevant to this PR)

);

Ok(base)
}
Expand Down
2 changes: 1 addition & 1 deletion src/doc/src/reference/environment-variables.md
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still feel like people might overlook this, as it isn't an obvious thing easy to relate to their problems. That said, I am fine with the previous explanation. Just forward it here if people have the same question: #12996 (comment).

When we talked about this, Eric was hesitant about locking down the behavior of file! so I left out that use case. It should work and I can't imagine rustc doing anything but absolute or relative to std::env::current_dir but I'm leaving that to people to make the choice that they want to make those assumptions, rather than us advertising it.


[Cargo target]: cargo-targets.md
[binaries]: cargo-targets.md#binaries
Expand Down
41 changes: 4 additions & 37 deletions tests/testsuite/build.rs
Expand Up @@ -1705,30 +1705,22 @@ 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"))
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
.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();
}
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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()
Expand Down