Skip to content

Commit 965e265

Browse files
authored
[nextest-runner] set NEXTEST_BIN_EXE_ env vars also with underscores (#2777)
Turns out that some shells and debuggers just drop any environment variables with hyphens in them. Provide an alternative that replaces hyphens with underscores.
1 parent f84914d commit 965e265

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

fixtures/nextest-tests/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::path::Path;
1010
pub fn test_execute_bin_helper() {
1111
let binary_path = std::env::var("NEXTEST_BIN_EXE_nextest-tests")
1212
.expect("NEXTEST_BIN_EXE_nextest-tests should be present");
13+
let with_underscores = std::env::var("NEXTEST_BIN_EXE_nextest_tests")
14+
.expect("NEXTEST_BIN_EXE_nextest_tests (with underscores) should be present");
15+
assert_eq!(binary_path, with_underscores);
1316
assert!(
1417
Path::new(&binary_path).is_absolute(),
1518
"binary path {} is absolute",

nextest-runner/src/test_command.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,21 @@ impl TestCommand {
114114

115115
apply_ld_dyld_env(&mut cmd, lctx.dylib_path);
116116

117-
// Expose paths to non-test binaries at runtime so that relocated paths work.
118-
// These paths aren't exposed by Cargo at runtime, so use a NEXTEST_BIN_EXE prefix.
117+
// Expose paths to non-test binaries at runtime so that relocated paths
118+
// work. These paths aren't exposed by Cargo at runtime, so use a
119+
// NEXTEST_BIN_EXE prefix.
119120
for (name, path) in non_test_binaries {
121+
// Some shells and debuggers have been known to drop environment
122+
// variables with hyphens in their names. Provide an alternative
123+
// name with underscores instead.
124+
//
125+
// See
126+
// https://unix.stackexchange.com/questions/23659/can-shell-variable-name-include-a-hyphen-or-dash.
127+
let with_underscores = name.replace('-', "_");
120128
cmd.env(format!("NEXTEST_BIN_EXE_{name}"), path);
129+
if &with_underscores != name {
130+
cmd.env(format!("NEXTEST_BIN_EXE_{with_underscores}"), path);
131+
}
121132
}
122133

123134
let double_spawn = lctx.double_spawn.spawn_context();

site/src/docs/configuration/env-vars.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Nextest exposes these environment variables to your tests _at runtime only_. The
111111
Binaries are automatically built when the test is built, unless the binary has required features that are not enabled.
112112

113113
When [reusing builds](../ci-features/archiving.md) from an archive, this is set to the remapped path within the target directory.
114+
115+
<!-- md:version 0.9.112 --> Because some shells and [debuggers](../integrations/debuggers.md) drop [environment variables with hyphens in their names](https://unix.stackexchange.com/a/23714), nextest also sets an alternative form of these variables where hyphens in the name are replaced with underscores. For example, for a binary named `my-program`, the environment variable `NEXTEST_BIN_EXE_my_program` is also set to the absolute path of the executable.
114116

115117
`NEXTEST_LD_*` and `NEXTEST_DYLD_*`
116118
: Replicate the values of any environment variables that start with the prefixes `LD_` or `DYLD_`, such as `LD_PRELOAD` or `DYLD_FALLBACK_LIBRARY_PATH`.

0 commit comments

Comments
 (0)