Skip to content

Skip environment variables whose value is undefined - #1022

Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/undefined-env-values
Draft

Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/undefined-env-values

Conversation

@federicobrancasi

Copy link
Copy Markdown
Member

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.

This PR checks the value instead of the key, so variables set to undefined are left out on Unix and Windows, like in child_process.

How to test

Command Result
npx tsc -p src/tsconfig.json --types node,mocha && npx cross-env NODE_ENV=test mocha -R dot --exit lib/*.test.js 25 passing; the new test "Terminal parseEnv should skip variables whose value is undefined" fails without the change
npx eslint src/ passes

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants