Fix `extendEnv: false` inheriting `process.env` when `env` is not set
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 8017b27 · Browse files · 4 files·+21 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+21 added−5 removed
- tests:+17 added−2 removed
- other:+4 added−3 removed
Provenance
- Commit
67acf60- Tree
d91aa81- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
8017b27
4 files changed+21 added−5 removed
modified:docs/api.md+1 added-1 removed
876 unmodified lines877 878 ### options.env879 880 _Type:_ `object`\881-_Default:_ [`process.env`](https://nodejs.org/api/process.html#processenv)881+_Default:_ `{}`882 883 [Environment variables](https://en.wikipedia.org/wiki/Environment_variable).884 885 Unless the [`extendEnv`](#optionsextendenv) option is `false`, the subprocess also uses the current process' environment variables ([`process.env`](https://nodejs.org/api/process.html#processenv)).476 unmodified linesmodified:lib/arguments/options.js+2 added-1 removed
87 unmodified lines88899091929293949596979 unmodified lines87 unmodified lines serialization,});
const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, nodePath}) => { // Spread `envOption` even when it is `undefined`, so `extendEnv: false` results in an empty environment instead of inheriting `process.env` const env = extendEnv ? {...process.env, ...envOption} : envOption; const env = extendEnv ? {...process.env, ...envOption} : {...envOption};
if (preferLocal || node) { return npmRunPathEnv({ env,9 unmodified linesmodified:test/arguments/env.js+17 added-2 removed
1233445678913 unmodified lines232425262728293031323334353637383940414243443 unmodified linesimport path from 'node:path';import process from 'node:process';import test from 'ava';import {execa} from '../../index.js';import {setFixtureDirectory, PATH_KEY} from '../helpers/fixtures-directory.js';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory, PATH_KEY, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';
setFixtureDirectory();process.env.FOO = 'foo';
13 unmodified lines const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}, extendEnv: false}); t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);});
// `process.env` must not leak when the `env` option is not set, since `extendEnv: false` means only `env` is used.// The file is spawned with an absolute path since the environment has no `PATH`.const environmentFile = path.join(FIXTURES_DIRECTORY, 'environment.js');
const testNoEnvironment = async (t, execaMethod, options) => { const {stdout} = await execaMethod(process.execPath, [environmentFile], {extendEnv: false, ...options}); t.deepEqual(stdout.split('\n'), ['undefined', 'undefined']);};
test('do not use process.env with `extendEnv: false` and no `env`', testNoEnvironment, execa, {});test('do not use process.env with `extendEnv: false` and no `env`, sync', testNoEnvironment, execaSync, {});test('do not use process.env with `extendEnv: false` and `env: {}`', testNoEnvironment, execa, {env: {}});test('do not use process.env with `extendEnv: false`, no `env` and `preferLocal`', testNoEnvironment, execa, {preferLocal: true});
test('use extend environment with `extendEnv: true` and `shell: true`', async t => { process.env.TEST = 'test'; const command = isWindows ? 'echo %TEST%' : 'echo $TEST'; const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});3 unmodified linesmodified:types/arguments/options.d.ts+1 added-1 removed
77 unmodified lines78 [Environment variables](https://en.wikipedia.org/wiki/Environment_variable).79 80 Unless the `extendEnv` option is `false`, the subprocess also uses the current process' environment variables ([`process.env`](https://nodejs.org/api/process.html#processenv)).81 82- @default [process.env](https://nodejs.org/api/process.html#processenv)82+ @default {}83 */84 readonly env?: Readonly<Partial<Record<string, string>>>;85 86 /**339 unmodified lines