Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The options object can have the following properties:
- `nodeOptions` - any valid options to node's underlying `spawn` function
- `throwOnError` - if true, non-zero exit codes will throw an error
- `nodePath` - if `false`, `node_modules/.bin` directories and the current node executable's directory will not be prepended to `PATH` (defaults to `true`)
- `inheritEnv` - if `false`, `process.env` will not be inherited (defaults to `true`)

### Passing a string to stdin

Expand Down
6 changes: 4 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ function addNodeBinToPath(cwd: URL | string, path: EnvPathInfo): EnvPathInfo {
export function computeEnv(
cwd: URL | string,
env?: EnvLike,
nodePath: boolean = true
nodePath: boolean = true,
inheritEnv: boolean = true
): EnvLike {
const baseEnv = inheritEnv ? process.env : {};
const envWithDefault = {
...process.env,
...baseEnv,
...env
};

Expand Down
15 changes: 13 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface CommonOptions {
timeout: number;
throwOnError: boolean;
nodePath: boolean;
inheritEnv: boolean;
}

export interface Options extends CommonOptions {
Expand Down Expand Up @@ -316,7 +317,12 @@ export class ExecProcess implements Result {
nodeOptions.signal = combineSignals(signals);
}

nodeOptions.env = computeEnv(cwd, nodeOptions.env, options.nodePath);
nodeOptions.env = computeEnv(
cwd,
nodeOptions.env,
options.nodePath,
options.inheritEnv
);

const crossResult = normalizeSpawnCommand(
this._command,
Expand Down Expand Up @@ -394,7 +400,12 @@ export function xSync(
nodeOptions.timeout = opts.timeout;
}

nodeOptions.env = computeEnv(cwd, nodeOptions.env, opts.nodePath);
nodeOptions.env = computeEnv(
cwd,
nodeOptions.env,
opts.nodePath,
opts.inheritEnv
);

const crossResult = normalizeSpawnCommand(command, args ?? [], nodeOptions);

Expand Down
11 changes: 11 additions & 0 deletions src/test/env_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ describe('computeEnv', async () => {
expect(env[pathKey]).toBe(originalPath);
});

test('does not inherit process.env when inheritEnv is false', () => {
process.env.TINYEXEC_ISOLATE_TEST = 'foo';
try {
const env = computeEnv(process.cwd(), {BAR: 'bar'}, false, false);

expect(env).toEqual({BAR: 'bar'});
} finally {
delete process.env.TINYEXEC_ISOLATE_TEST;
}
});

test('prepends local node_modules/.bin and directory of node executable to PATH', () => {
/** The original variable is just `PATH=/usr/local/bin` */
const originalPath = path.join(pathSep, 'usr', 'local', 'bin');
Expand Down