Repository navigation
Skip environment variables whose value is undefined - #1022
Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Conversation
A common way to start a process without one variable is `{ ...process.env, NAME: undefined }`. `child_process.spawn` leaves such variables out, and the `env` option of `spawn` is typed `{ [key: string]: string | undefined }`. node-pty passes them to the child as the text `undefined`. For example, `pty.spawn('git', ['rev-parse', '--is-inside-work-tree'], { env: { ...process.env, GIT_DIR: undefined } })` prints `fatal: not a git repository: 'undefined'`, where `child_process` prints `true`. This happens because `Terminal._parseEnv` checks `keys[i] === undefined`. A key is never undefined, so every pair gets added, and `env[key]` becomes `'undefined'` when it is joined into `KEY=value`. The check was added with the `| undefined` type in 0588def, which suggests it was meant to test the value.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Federico Brancasi (federicobrancasi)
requested a balanced review from Copilot
November 29, 2026 20:13
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Windows runtime validation is needed to confirm CreateProcessW safely handles the single-NUL environment buffer produced when all values are undefined, without unintended environment inheritance.
No open issues 🎉
What changed in this PR
Fixes shared environment parsing so node-pty omits variables whose values are undefined, matching child_process.spawn behavior on Unix and Windows.
Changes:
- Checks environment values rather than keys before adding entries.
- Adds a regression test covering undefined values while preserving defined values and empty strings.
| File | Description |
|---|---|
src/terminal.ts |
Skips environment entries with undefined values. |
src/terminal.test.ts |
Adds parser coverage for undefined, defined, and empty-string values. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A common way to start a process without one variable is
{ ...process.env, NAME: undefined }.child_process.spawnleaves such variables out, and theenvoption ofspawnis typed{ [key: string]: string | undefined }. node-pty passes them to the child as the textundefined. For example,pty.spawn('git', ['rev-parse', '--is-inside-work-tree'], { env: { ...process.env, GIT_DIR: undefined } })printsfatal: not a git repository: 'undefined', wherechild_processprintstrue. This happens becauseTerminal._parseEnvcheckskeys[i] === undefined. A key is never undefined, so every pair gets added, andenv[key]becomes'undefined'when it is joined intoKEY=value. The check was added with the| undefinedtype in 0588def, which suggests it was meant to test the value.This PR checks the value instead of the key, so variables set to
undefinedare left out on Unix and Windows, like inchild_process.How to test
npx tsc -p src/tsconfig.json --types node,mocha && npx cross-env NODE_ENV=test mocha -R dot --exit lib/*.test.jsnpx eslint src/