From 7025abf879e6155bd78ffb75b08b09ad6f314314 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Wed, 23 Feb 2022 01:16:16 -0500 Subject: [PATCH] Fix #1657: bug where node flags were not correctly preserved in `execArgv` (#1658) * Fix bug * Fix for windows --- src/bin.ts | 2 +- src/test/helpers.ts | 1 + src/test/index.spec.ts | 40 +++++++++++++++++++++++++++++- tests/recursive-fork/index.ts | 11 ++++++++ tests/recursive-fork/package.json | 1 + tests/recursive-fork/tsconfig.json | 5 ++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/recursive-fork/index.ts create mode 100644 tests/recursive-fork/package.json create mode 100644 tests/recursive-fork/tsconfig.json diff --git a/src/bin.ts b/src/bin.ts index 0194f3a3c..13907f39e 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -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) ); diff --git a/src/test/helpers.ts b/src/test/helpers.ts index 2ff36f157..5327459be 100644 --- a/src/test/helpers.ts +++ b/src/test/helpers.ts @@ -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' diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index a709e4e8a..5487a7b64 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -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'; @@ -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[] = []; diff --git a/tests/recursive-fork/index.ts b/tests/recursive-fork/index.ts new file mode 100644 index 000000000..ed68a2080 --- /dev/null +++ b/tests/recursive-fork/index.ts @@ -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', + }); +} diff --git a/tests/recursive-fork/package.json b/tests/recursive-fork/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/tests/recursive-fork/package.json @@ -0,0 +1 @@ +{} diff --git a/tests/recursive-fork/tsconfig.json b/tests/recursive-fork/tsconfig.json new file mode 100644 index 000000000..8e881cf9c --- /dev/null +++ b/tests/recursive-fork/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "moduleResolution": "node" + } +}