Skip to content

Commit 7025abf

Browse files
authored
Fix #1657: bug where node flags were not correctly preserved in execArgv (#1658)
* Fix bug * Fix for windows
1 parent 3426db1 commit 7025abf

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

src/bin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ Options:
376376
}
377377

378378
// Prepend `ts-node` arguments to CLI for child processes.
379-
process.execArgv.unshift(
379+
process.execArgv.push(
380380
__filename,
381381
...process.argv.slice(2, process.argv.length - args._.length)
382382
);

src/test/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const DIST_DIR = resolve(__dirname, '..');
2929
export const TEST_DIR = join(__dirname, '../../tests');
3030
export const PROJECT = join(TEST_DIR, 'tsconfig.json');
3131
export const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node');
32+
export const BIN_PATH_JS = join(TEST_DIR, 'node_modules/ts-node/dist/bin.js');
3233
export const BIN_SCRIPT_PATH = join(
3334
TEST_DIR,
3435
'node_modules/.bin/ts-node-script'

src/test/index.spec.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as expect from 'expect';
33
import { join, resolve, sep as pathSep } from 'path';
44
import { tmpdir } from 'os';
55
import semver = require('semver');
6-
import { nodeSupportsEsmHooks, ts } from './helpers';
6+
import { BIN_PATH_JS, nodeSupportsEsmHooks, ts } from './helpers';
77
import { lstatSync, mkdtempSync } from 'fs';
88
import { npath } from '@yarnpkg/fslib';
99
import type _createRequire from 'create-require';
@@ -1071,6 +1071,44 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async (
10711071
expect(stdout).toBe('foo\n');
10721072
});
10731073

1074+
test.suite('node environment', (test) => {
1075+
test.suite('Sets argv and execArgv correctly in forked processes', (test) => {
1076+
forkTest(`node --no-warnings ${BIN_PATH_JS}`, BIN_PATH_JS, '--no-warnings');
1077+
forkTest(
1078+
`${BIN_PATH}`,
1079+
process.platform === 'win32' ? BIN_PATH_JS : BIN_PATH
1080+
);
1081+
1082+
function forkTest(
1083+
command: string,
1084+
expectParentArgv0: string,
1085+
nodeFlag?: string
1086+
) {
1087+
test(command, async (t) => {
1088+
const { err, stderr, stdout } = await exec(
1089+
`${command} --skipIgnore ./recursive-fork/index.ts argv2`
1090+
);
1091+
expect(err).toBeNull();
1092+
expect(stderr).toBe('');
1093+
const generations = stdout.split('\n');
1094+
const expectation = {
1095+
execArgv: [nodeFlag, BIN_PATH_JS, '--skipIgnore'].filter((v) => v),
1096+
argv: [
1097+
// Note: argv[0] is *always* BIN_PATH_JS in child & grandchild
1098+
expectParentArgv0,
1099+
resolve(TEST_DIR, 'recursive-fork/index.ts'),
1100+
'argv2',
1101+
],
1102+
};
1103+
expect(JSON.parse(generations[0])).toMatchObject(expectation);
1104+
expectation.argv[0] = BIN_PATH_JS;
1105+
expect(JSON.parse(generations[1])).toMatchObject(expectation);
1106+
expect(JSON.parse(generations[2])).toMatchObject(expectation);
1107+
});
1108+
}
1109+
});
1110+
});
1111+
10741112
test('Detect when typescript adds new ModuleKind values; flag as a failure so we can update our code flagged [MUST_UPDATE_FOR_NEW_MODULEKIND]', async () => {
10751113
// We have marked a few places in our code with MUST_UPDATE_FOR_NEW_MODULEKIND to make it easier to update them when TS adds new ModuleKinds
10761114
const foundKeys: string[] = [];

tests/recursive-fork/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fork } from 'child_process';
2+
3+
console.log(JSON.stringify({ execArgv: process.execArgv, argv: process.argv }));
4+
if (process.env.generation !== 'grandchild') {
5+
const nextGeneration =
6+
process.env.generation === 'child' ? 'grandchild' : 'child';
7+
fork(__filename, process.argv.slice(2), {
8+
env: { ...process.env, generation: nextGeneration },
9+
stdio: 'inherit',
10+
});
11+
}

tests/recursive-fork/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

tests/recursive-fork/tsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node"
4+
}
5+
}

0 commit comments

Comments
 (0)