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

Set CARGO_BIN_NAME environment variable also for binary examples #11705

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl<'cfg> Compilation<'cfg> {
/// Prepares a rustc_tool process with additional environment variables
/// that are only relevant in a context that has a unit
fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
if unit.target.is_bin() {
if unit.target.is_executable() {
epage marked this conversation as resolved.
Show resolved Hide resolved
let name = unit
.target
.binary_filename()
Expand Down
6 changes: 5 additions & 1 deletion src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ corresponding environment variable is set to the empty string, `""`.
Note that this is the minimum Rust version supported by the package, not the
current Rust version.
* `CARGO_CRATE_NAME` — The name of the crate that is currently being compiled. It is the name of the [Cargo target] with `-` converted to `_`, such as the name of the library, binary, example, integration test, or benchmark.
* `CARGO_BIN_NAME` — The name of the binary that is currently being compiled (if it is a binary). This name does not include any file extension, such as `.exe`.
* `CARGO_BIN_NAME` — The name of the binary that is currently being compiled.
Only set for [binaries] or binary [examples]. This name does not include any
file extension, such as `.exe`.
* `OUT_DIR` — If the package has a build script, this is set to the folder where the build
script should place its output. See below for more information.
(Only set during compilation.)
Expand All @@ -256,6 +258,8 @@ corresponding environment variable is set to the empty string, `""`.
manage its content in any way, this is the responsibility of the test code.

[Cargo target]: cargo-targets.md
[binaries]: cargo-targets.md#binaries
[examples]: cargo-targets.md#examples
[integration test]: cargo-targets.md#integration-tests
[`env` macro]: ../../std/macro.env.html

Expand Down
20 changes: 20 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,23 @@ fn crate_env_vars() {
}
"#,
)
.file(
"examples/ex-env-vars.rs",
r#"
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");

fn main() {
assert_eq!("foo", PKG_NAME);
assert_eq!("ex-env-vars", BIN_NAME);
assert_eq!("ex_env_vars", CRATE_NAME);

// Verify CARGO_TARGET_TMPDIR isn't set for examples
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
}
"#,
)
.file(
"tests/env.rs",
r#"
Expand Down Expand Up @@ -1503,6 +1520,9 @@ fn crate_env_vars() {
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
.run();

println!("example");
p.cargo("run --example ex-env-vars -v").run();

println!("test");
p.cargo("test -v").run();

Expand Down