Repository navigation
Don't pass nushell relative envs to external commands - #19473
WindSoilder wants to merge 1 commit into
Conversation
1b2265b to
c69fa3b
Compare
|
Why do we need to do this? I can maybe see the point of not passing closures but I don't see the harm with some of these like exit code and cmd duration. |
|
Just as relative issues mentioned, these env variable are only useful inside nu, I don't think it's necessary to pass to others. |
|
I'm not sure about this because if you're running nushell and you do |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
Names nushell sets itself no longer reach ^ externals, and CI is green. The filter goes by name only, though, so it also strips values nu inherited from its parent or that the user set for the child, and it covers run-external but not exec, plugins or the editors.
Findings: 1
Open (6)
Filter drops values nu inherited or the user set for the child Onlyrun-externalfilters;exec, plugins and editors still pass these varsdebug envno longer shows what externals get Childnuscripts now start with no$env.LAST_EXIT_CODENU_LIB_DIRSandNU_PLUGIN_DIRSaren't exported by default anyway Use the existingenv_var_eqhelper
Notes from my clanker, checked on c69fa3b. Built the branch and ran each case on the branch binary and on 0.116.2 (main 4492226). CI ran clippy and the tests on this head, all green, so I didn't rerun them. The start, editor, plugin and generate completions paths are from reading the code.
| let mut envs = env_to_strings(engine_state, stack)?; | ||
| envs.retain(|env_name, _| { | ||
| !INTERNAL_ENV_VARS | ||
| .iter() | ||
| .any(|internal_name| env_name.eq_ignore_ascii_case(internal_name)) | ||
| }); |
There was a problem hiding this comment.
The filter matches names only, so it also drops values nushell never set. #19455 asks that a child get "just the environment inherited by the shell and any environment variables explicitly set using with-env, etc". On c69fa3b the child loses both.
Values nu inherited from its parent (zsh here) and never touched:
PROMPT_COMMAND='history -a' PROCESS_PATH=/opt/tool CURRENT_FILE=/tmp/doc.md LAST_EXIT_CODE=5 \
nu -n -c "^sh -c 'env | grep -E \"^(PROMPT_COMMAND|PROCESS_PATH|CURRENT_FILE|LAST_EXIT_CODE)=\"'"
# main: CURRENT_FILE=/tmp/doc.md, LAST_EXIT_CODE=5, PROCESS_PATH=/opt/tool, PROMPT_COMMAND=history -a
# branch: (nothing)Values set on purpose for the child. PROMPT_COMMAND is bash's own variable:
with-env {PROMPT_COMMAND: 'history -a'} { ^bash -c 'echo "[$PROMPT_COMMAND]"' }
# main: [history -a]
# branch: []
PROMPT_COMMAND='echo hook' ^bash -c 'echo "[$PROMPT_COMMAND]"'
# main: [echo hook]
# branch: []
LAST_EXIT_CODE=7 ^sh -c 'echo "[$LAST_EXIT_CODE]"'
# main: [7]
# branch: []Nothing warns, and scoped_internal_environment_stays_internal makes the with-env case a tested rule. The first case bites when nu sits in the middle, like nu -c as the shell for just, make, a git hook or a CI step. The grandchild loses a variable the outer tool exported. The only way around it is ^env 'PROMPT_COMMAND=history -a' bash, and Windows has no env.
Which values count as nushell's own needs deciding before merge. Two options:
- Withhold a value only when nushell wrote it (the REPL's
CMD_DURATION_MSandLAST_EXIT_CODE,FILE_PWD/CURRENT_FILE/PROCESS_PATHfromnu script.nu, the prompt defaults). Pass through anything from the parent or fromwith-envandNAME=value cmd. - Keep the name list but pass a var whose value still matches what nu inherited (
std::env::var), and put thewith-envdrop and the^envworkaround in the release notes.
|
|
||
| #[test] | ||
| #[deps(NU, TESTBIN_ECHO_ENV)] | ||
| fn exec_preserves_internal_environment() -> Result { |
There was a problem hiding this comment.
run-external filters; exec, plugins and editors still pass these vars
Only run-external filters. exec still passes all 17 names, and this test makes that a rule:
$env.PROMPT_INDICATOR = 'leak'; exec sh -c 'echo "[$PROMPT_INDICATOR]"'
# main: [leak]
# branch: [leak]So nu x.nu and exec nu x.nu now start the script with different environments. Why should exec behave differently from ^? The PR body doesn't say.
From reading the code, these call plain env_to_strings too and pass everything: start (filesystem/start.rs:118), the config nu editor (env/config/config_.rs:96), the REPL buffer editor (nu-cli/src/repl.rs:832), plugin processes (nu-plugin-engine/src/persistent.rs:362) and generate completions (generate_completions/mod.rs:623). The release note says nushell-defined env is "no longer passed to external child process", which isn't true for any of them.
If the filter stays, moving the list next to var::AUTOMATIC_VARS in nu-engine/src/env.rs and applying it in one exporting variant of env_to_strings would cover every spawn site at once. Otherwise the description should name the paths that keep the vars and say why.
| } | ||
|
|
||
| #[nu_test_support::test] | ||
| fn debug_env_preserves_internal_environment() -> Result { |
There was a problem hiding this comment.
debug env no longer shows what externals get
debug env describes itself as "Show environment variables as external commands would get it." On c69fa3b it still lists the dropped names, and the child gets nothing:
$env.PROMPT_INDICATOR = 'x'; {debug_env: (debug env | get PROMPT_INDICATOR?), child: (^sh -c 'echo $PROMPT_INDICATOR' | str trim)} | to nuon
# main: {debug_env: x, child: x}
# branch: {debug_env: x, child: ""}debug env is what someone runs when a child is missing a variable, and now it says the variable is there. This test asserts the mismatch. Either run debug env through the same filter or change its description, and update the test to match.
| /// <https://docs.astral.sh/uv/reference/environment/#nu_version>. | ||
| const INTERNAL_ENV_VARS: &[&str] = &[ | ||
| "CMD_DURATION_MS", | ||
| "LAST_EXIT_CODE", |
There was a problem hiding this comment.
nu scripts now start with no $env.LAST_EXIT_CODE
On the nu script.nu question in the conversation: a child script sets its own FILE_PWD, CURRENT_FILE and PROCESS_PATH (nu-cli/src/eval_file.rs:77), and an interactive child seeds CMD_DURATION_MS and LAST_EXIT_CODE (nu-cli/src/repl.rs:123). Script and -c mode seed neither, so until now a child script picked up the parent's values:
[(try { ^sh -c 'exit 3' } catch { 'caught' }) $env.LAST_EXIT_CODE (nu -n -c '$env.LAST_EXIT_CODE? | default MISSING')] | to nuon
# main: [caught, 3, "3"]
# branch: [caught, 3, MISSING]Main wasn't right either, since the child got the parent's exit code as a string. But a script that reads $env.LAST_EXIT_CODE before it runs any external now fails with column_not_found when started from nu. It already failed that way when started from bash. Seeding LAST_EXIT_CODE to 0 in script and -c mode, as repl.rs:128 does for the REPL, would stop child scripts depending on what they inherit.
| /// `NU_LIB_DIRS` and `NU_PLUGIN_DIRS` remain exported so child Nushell processes inherit module | ||
| /// and plugin search paths. |
There was a problem hiding this comment.
NU_LIB_DIRS and NU_PLUGIN_DIRS aren't exported by default anyway
The comment says these "remain exported so child Nushell processes inherit module and plugin search paths". With the default config both are lists, and env_to_strings skips anything that isn't a string, so neither reaches a child on main or on this branch:
{lib: ($env.NU_LIB_DIRS | describe), plugin: ($env.NU_PLUGIN_DIRS | describe), exported: (debug env | columns | where $it =~ 'NU_')} | to nuon
# main and branch: {lib: list<string>, plugin: list<any>, exported: [NU_VERSION]}Leaving them off the list only matters for someone who adds a to_string conversion. The comment should say that, and the PR body's "so running nu scripts won't broken" overstates it.
| envs.retain(|env_name, _| { | ||
| !INTERNAL_ENV_VARS | ||
| .iter() | ||
| .any(|internal_name| env_name.eq_ignore_ascii_case(internal_name)) |
There was a problem hiding this comment.
env_var_eq helper
nu_protocol::engine::env_var_eq already does this comparison, and is_automatic_env_var uses it. AGENTS.md: "reuse the helpers that already exist."
| .any(|internal_name| env_name.eq_ignore_ascii_case(internal_name)) | |
| .any(|internal_name| env_var_eq(env_name, internal_name)) |
with engine::env_var_eq added to the nu_protocol import.



Description
This pr is going to make nushell no longer pass nu defined envs to external commands, but except
NU_VERSIONbecause uv uses it via: https://docs.astral.sh/uv/reference/environment/#nu_versionIt also keeps
NU_LIB_DIRSandNU_PLUGIN_DIRS, so running nu scripts won't broken.User-facing changes (Release notes)
Nushell defined env no longer passed to external child process.
Before
After
It outputs nothing
Additional notes