Skip to content

Commit

Permalink
Fix #1657: bug where node flags were not correctly preserved in `exec…
Browse files Browse the repository at this point in the history
…Argv` (#1658)

* Fix bug

* Fix for windows
  • Loading branch information
cspotcode authored Feb 23, 2022
1 parent 3426db1 commit 7025abf
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ Options:
}

// Prepend `ts-node` arguments to CLI for child processes.
process.execArgv.unshift(
process.execArgv.push(
__filename,
...process.argv.slice(2, process.argv.length - args._.length)
);
Expand Down
1 change: 1 addition & 0 deletions src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const DIST_DIR = resolve(__dirname, '..');
export const TEST_DIR = join(__dirname, '../../tests');
export const PROJECT = join(TEST_DIR, 'tsconfig.json');
export const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node');
export const BIN_PATH_JS = join(TEST_DIR, 'node_modules/ts-node/dist/bin.js');
export const BIN_SCRIPT_PATH = join(
TEST_DIR,
'node_modules/.bin/ts-node-script'
Expand Down
40 changes: 39 additions & 1 deletion src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as expect from 'expect';
import { join, resolve, sep as pathSep } from 'path';
import { tmpdir } from 'os';
import semver = require('semver');
import { nodeSupportsEsmHooks, ts } from './helpers';
import { BIN_PATH_JS, nodeSupportsEsmHooks, ts } from './helpers';
import { lstatSync, mkdtempSync } from 'fs';
import { npath } from '@yarnpkg/fslib';
import type _createRequire from 'create-require';
Expand Down Expand Up @@ -1071,6 +1071,44 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async (
expect(stdout).toBe('foo\n');
});

test.suite('node environment', (test) => {
test.suite('Sets argv and execArgv correctly in forked processes', (test) => {
forkTest(`node --no-warnings ${BIN_PATH_JS}`, BIN_PATH_JS, '--no-warnings');
forkTest(
`${BIN_PATH}`,
process.platform === 'win32' ? BIN_PATH_JS : BIN_PATH
);

function forkTest(
command: string,
expectParentArgv0: string,
nodeFlag?: string
) {
test(command, async (t) => {
const { err, stderr, stdout } = await exec(
`${command} --skipIgnore ./recursive-fork/index.ts argv2`
);
expect(err).toBeNull();
expect(stderr).toBe('');
const generations = stdout.split('\n');
const expectation = {
execArgv: [nodeFlag, BIN_PATH_JS, '--skipIgnore'].filter((v) => v),
argv: [
// Note: argv[0] is *always* BIN_PATH_JS in child & grandchild
expectParentArgv0,
resolve(TEST_DIR, 'recursive-fork/index.ts'),
'argv2',
],
};
expect(JSON.parse(generations[0])).toMatchObject(expectation);
expectation.argv[0] = BIN_PATH_JS;
expect(JSON.parse(generations[1])).toMatchObject(expectation);
expect(JSON.parse(generations[2])).toMatchObject(expectation);
});
}
});
});

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 () => {
// 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
const foundKeys: string[] = [];
Expand Down
11 changes: 11 additions & 0 deletions tests/recursive-fork/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fork } from 'child_process';

console.log(JSON.stringify({ execArgv: process.execArgv, argv: process.argv }));
if (process.env.generation !== 'grandchild') {
const nextGeneration =
process.env.generation === 'child' ? 'grandchild' : 'child';
fork(__filename, process.argv.slice(2), {
env: { ...process.env, generation: nextGeneration },
stdio: 'inherit',
});
}
1 change: 1 addition & 0 deletions tests/recursive-fork/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions tests/recursive-fork/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"moduleResolution": "node"
}
}

0 comments on commit 7025abf

Please sign in to comment.