Skip to content

Commit 0fda7ac

Browse files
committed
Set CARGO_BIN_NAME environment variable also for binary examples
Extend the existing CARGO_BIN_NAME environment variable to be set when building binary example targets, additional to "normal" binary targets. Closes #11689.
1 parent 39c13e6 commit 0fda7ac

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/cargo/core/compiler/compilation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'cfg> Compilation<'cfg> {
372372
/// Prepares a rustc_tool process with additional environment variables
373373
/// that are only relevant in a context that has a unit
374374
fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
375-
if unit.target.is_bin() {
375+
if unit.target.is_executable() {
376376
let name = unit
377377
.target
378378
.binary_filename()

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ corresponding environment variable is set to the empty string, `""`.
232232
Note that this is the minimum Rust version supported by the package, not the
233233
current Rust version.
234234
* `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.
235-
* `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`.
235+
* `CARGO_BIN_NAME` — The name of the binary that is currently being compiled.
236+
Only set for [binaries] or binary [examples]. This name does not include any
237+
file extension, such as `.exe`.
236238
* `OUT_DIR` — If the package has a build script, this is set to the folder where the build
237239
script should place its output. See below for more information.
238240
(Only set during compilation.)
@@ -256,6 +258,8 @@ corresponding environment variable is set to the empty string, `""`.
256258
manage its content in any way, this is the responsibility of the test code.
257259

258260
[Cargo target]: cargo-targets.md
261+
[binaries]: cargo-targets.md#binaries
262+
[examples]: cargo-targets.md#examples
259263
[integration test]: cargo-targets.md#integration-tests
260264
[`env` macro]: ../../std/macro.env.html
261265

tests/testsuite/build.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,23 @@ fn crate_env_vars() {
14661466
}
14671467
"#,
14681468
)
1469+
.file(
1470+
"examples/ex-env-vars.rs",
1471+
r#"
1472+
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
1473+
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
1474+
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
1475+
1476+
fn main() {
1477+
assert_eq!("foo", PKG_NAME);
1478+
assert_eq!("ex-env-vars", BIN_NAME);
1479+
assert_eq!("ex_env_vars", CRATE_NAME);
1480+
1481+
// Verify CARGO_TARGET_TMPDIR isn't set for examples
1482+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1483+
}
1484+
"#,
1485+
)
14691486
.file(
14701487
"tests/env.rs",
14711488
r#"
@@ -1503,6 +1520,9 @@ fn crate_env_vars() {
15031520
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
15041521
.run();
15051522

1523+
println!("example");
1524+
p.cargo("run --example ex-env-vars -v").run();
1525+
15061526
println!("test");
15071527
p.cargo("test -v").run();
15081528

0 commit comments

Comments
 (0)