Skip to content

Commit def153b

Browse files
authored
Preserve some more cargo envs (#25)
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 3361e63 commit def153b

1 file changed

Lines changed: 46 additions & 12 deletions

File tree

src/command.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,23 @@ impl Command {
291291
self
292292
}
293293

294-
/// Clears all `CARGO_` environment variables that will be set for the child process,
295-
/// except for `CARGO_HOME`.
294+
/// Clears Cargo build-context environment variables from the child process.
296295
///
297-
/// This method will remove all environment variables starting with `CARGO_`
298-
/// from the child process, including those that would normally be inherited
299-
/// from the parent process, except for `CARGO_HOME`. Other environment variables
300-
/// will remain unaffected. Environment variables can be added back individually
301-
/// using [`env`].
296+
/// This method removes the `CARGO_` environment variables that Cargo sets
297+
/// during build script execution and crate compilation (e.g. `CARGO_PKG_*`,
298+
/// `CARGO_MANIFEST_*`, `CARGO_CFG_*`, `CARGO_FEATURE_*`, etc.).
299+
///
300+
/// User configuration variables are preserved, including:
301+
/// - `CARGO_HOME` — Cargo home directory
302+
/// - `CARGO_REGISTRIES_*` — Private registry index URLs, tokens, and credential providers
303+
/// - `CARGO_REGISTRY_*` — Default registry and crates.io credentials
304+
/// - `CARGO_HTTP_*` — HTTP/TLS proxy and timeout settings
305+
/// - `CARGO_NET_*` — Network configuration (retry, offline, git-fetch-with-cli)
306+
/// - `CARGO_ALIAS_*` — Command aliases
307+
/// - `CARGO_TERM_*` — Terminal output settings
308+
///
309+
/// Other environment variables will remain unaffected. Environment variables
310+
/// can be added back individually using [`env`].
302311
///
303312
/// This is particularly useful when using cargo-hyperlight from a build script
304313
/// or other cargo-invoked context where `CARGO_` variables may change the behavior
@@ -322,7 +331,7 @@ impl Command {
322331
/// [`env`]: Command::env
323332
pub fn env_clear_cargo(&mut self) -> &mut Self {
324333
self.inherit_cargo_envs = false;
325-
self.envs.retain(|k, _| !is_cargo_env(k));
334+
self.envs.retain(|k, _| should_preserve_cargo_env(k));
326335
self
327336
}
328337

@@ -507,7 +516,7 @@ impl Command {
507516
if !self.inherit_envs {
508517
false
509518
} else if !self.inherit_cargo_envs {
510-
!is_cargo_env(k)
519+
should_preserve_cargo_env(k)
511520
} else {
512521
true
513522
}
@@ -529,7 +538,7 @@ impl Command {
529538
}
530539
if !self.inherit_cargo_envs {
531540
for (k, _) in env::vars_os() {
532-
if is_cargo_env(&k) {
541+
if !should_preserve_cargo_env(&k) {
533542
command.env_remove(k);
534543
}
535544
}
@@ -751,6 +760,31 @@ fn exec(
751760
Err(std::io::Error::last_os_error())
752761
}
753762

754-
fn is_cargo_env(key: &OsStr) -> bool {
755-
key != "CARGO_HOME" && key.starts_with("CARGO_")
763+
/// Returns `true` if the given environment variable should be preserved
764+
/// when clearing Cargo build-context variables.
765+
///
766+
/// Non-`CARGO_` variables are always preserved. The following `CARGO_`
767+
/// user configuration variables are also preserved:
768+
/// - `CARGO_HOME` — Cargo home directory
769+
/// - `CARGO_REGISTRIES_*` — Private registry index URLs, tokens, and credential providers
770+
/// - `CARGO_REGISTRY_*` — Default registry and crates.io credentials
771+
/// - `CARGO_HTTP_*` — HTTP/TLS proxy and timeout settings
772+
/// - `CARGO_NET_*` — Network configuration (retry, offline, git-fetch-with-cli)
773+
/// - `CARGO_ALIAS_*` — Command aliases
774+
/// - `CARGO_TERM_*` — Terminal output settings
775+
///
776+
/// All other `CARGO_*` variables (e.g. `CARGO_PKG_*`, `CARGO_MANIFEST_*`,
777+
/// `CARGO_CFG_*`, `CARGO_FEATURE_*`, `CARGO_MAKEFLAGS`, etc.) are considered
778+
/// build-context variables set by Cargo during compilation and will be cleared.
779+
///
780+
/// See: <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
781+
fn should_preserve_cargo_env(key: &OsStr) -> bool {
782+
!key.starts_with("CARGO_")
783+
|| key == "CARGO_HOME"
784+
|| key.starts_with("CARGO_REGISTRIES_")
785+
|| key.starts_with("CARGO_REGISTRY_")
786+
|| key.starts_with("CARGO_HTTP_")
787+
|| key.starts_with("CARGO_NET_")
788+
|| key.starts_with("CARGO_ALIAS_")
789+
|| key.starts_with("CARGO_TERM_")
756790
}

0 commit comments

Comments
 (0)