diff --git a/src/bin.ts b/src/bin.ts index f470a2c83..580f6256a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -28,8 +28,9 @@ import { } from './index'; import type { TSInternal } from './ts-compiler-types'; import { addBuiltinLibsToObject } from '../dist-raw/node-internal-modules-cjs-helpers'; -import { callInChild } from './child/spawn-child'; +import { callInChildWithEsm } from './child/spawn-child-with-esm'; import { findAndReadConfig } from './configuration'; +import { getChildProcessArguments } from './child/child-exec-args'; /** * Main `bin` functionality. @@ -46,10 +47,6 @@ export function main( ) { const args = parseArgv(argv, entrypointArgs); const state: BootstrapState = { - shouldUseChildProcess: false, - isInChildProcess: false, - isCli: true, - tsNodeScript: __filename, parseArgvResult: args, }; return bootstrap(state); @@ -61,37 +58,78 @@ export function main( * Can be marshalled when necessary to resume bootstrapping in a child process. */ export interface BootstrapState { - isInChildProcess: boolean; - shouldUseChildProcess: boolean; - /** - * True if bootstrapping the ts-node CLI process or the direct child necessitated by `--esm`. - * false if bootstrapping a subsequently `fork()`ed child. - */ - isCli: boolean; - tsNodeScript: string; parseArgvResult: ReturnType; phase2Result?: ReturnType; phase3Result?: ReturnType; } +/** + * Bootstrap state that is passed to the child process used to execute + * the final bootstrap phase. + * + * This state may be encoded in process command line arguments and should + * only capture information that should be persisted to e.g. forked child processes. + */ +export interface BootstrapStateForForkedProcesses { + // For the final bootstrap we are only interested in the user arguments + // that should be passed to the entry-point script (or eval script). + // We don't want to encode any options that would break child forking. e.g. + // persisting the `--eval` option would break `child_process.fork` in scripts. + parseArgvResult: Pick, 'restArgs'>; + phase3Result: Pick< + ReturnType, + 'enableEsmLoader' | 'preloadedConfig' + >; +} + +export interface BootstrapStateInitialProcessChild + extends Omit { + initialProcessOptions: { resolutionCwd: string } & Pick< + ReturnType, + // These are options which should not persist into forked child processes, + // but can be passed-through in the initial child process creation -- but should + // not be encoded in the Brotli state for child process forks (through `execArgv`.) + 'version' | 'showConfig' | 'code' | 'print' | 'interactive' + >; +} + +export type BootstrapStateForChild = Omit< + BootstrapStateForForkedProcesses, + 'initialProcessOptions' +> & + Partial; + /** @internal */ export function bootstrap(state: BootstrapState) { - if (!state.phase2Result) { - state.phase2Result = phase2(state); - if (state.shouldUseChildProcess && !state.isInChildProcess) { - // Note: When transitioning into the child-process after `phase2`, - // the updated working directory needs to be preserved. - return callInChild(state); - } - } - if (!state.phase3Result) { - state.phase3Result = phase3(state); - if (state.shouldUseChildProcess && !state.isInChildProcess) { - // Note: When transitioning into the child-process after `phase2`, - // the updated working directory needs to be preserved. - return callInChild(state); - } + state.phase2Result = phase2(state); + state.phase3Result = phase3(state); + + const initialChildState: BootstrapStateInitialProcessChild = { + ...createBootstrapStateForChildProcess(state as Required), + // Aside with the default child process state, we attach the initial process + // options since this `callInChild` invocation is from the initial process. + // Later when forking, the initial process options are omitted / not persisted. + initialProcessOptions: { + code: state.parseArgvResult.code, + interactive: state.parseArgvResult.interactive, + print: state.parseArgvResult.print, + showConfig: state.parseArgvResult.showConfig, + version: state.parseArgvResult.version, + resolutionCwd: state.phase2Result.resolutionCwd, + }, + }; + + // For ESM, we need to spawn a new Node process to be able to register our hooks. + if (state.phase3Result.enableEsmLoader) { + // Note: When transitioning into the child process for the final phase, + // we want to preserve the initial user working directory. + callInChildWithEsm(initialChildState, process.cwd()); + } else { + completeBootstrap(initialChildState); } +} +/** Final phase of the bootstrap. */ +export function completeBootstrap(state: BootstrapStateForChild) { return phase4(state); } @@ -274,7 +312,7 @@ function parseArgv(argv: string[], entrypointArgs: Record) { } function phase2(payload: BootstrapState) { - const { help, version, cwdArg, esm } = payload.parseArgvResult; + const { help, version, cwdArg } = payload.parseArgvResult; if (help) { console.log(` @@ -328,14 +366,15 @@ Options: process.exit(0); } - const cwd = cwdArg ? resolve(cwdArg) : process.cwd(); - - // If ESM is explicitly enabled through the flag, stage3 should be run in a child process - // with the ESM loaders configured. - if (esm) payload.shouldUseChildProcess = true; + let resolutionCwd: string; + if (cwdArg !== undefined) { + resolutionCwd = resolve(cwdArg); + } else { + resolutionCwd = process.cwd(); + } return { - cwd, + resolutionCwd, }; } @@ -367,7 +406,7 @@ function phase3(payload: BootstrapState) { esm, experimentalSpecifierResolution, } = payload.parseArgvResult; - const { cwd } = payload.phase2Result!; + const { resolutionCwd } = payload.phase2Result!; // NOTE: When we transition to a child process for ESM, the entry-point script determined // here might not be the one used later in `phase4`. This can happen when we execute the @@ -375,10 +414,13 @@ function phase3(payload: BootstrapState) { // We will always use the original TS project in forked processes anyway, so it is // expected and acceptable to retrieve the entry-point information here in `phase2`. // See: https://github.com/TypeStrong/ts-node/issues/1812. - const { entryPointPath } = getEntryPointInfo(payload); + const { entryPointPath } = getEntryPointInfo( + resolutionCwd, + payload.parseArgvResult! + ); const preloadedConfig = findAndReadConfig({ - cwd, + cwd: resolutionCwd, emit, files, pretty, @@ -391,7 +433,7 @@ function phase3(payload: BootstrapState) { ignore, logError, projectSearchDir: getProjectSearchDir( - cwd, + resolutionCwd, scriptMode, cwdMode, entryPointPath @@ -411,11 +453,10 @@ function phase3(payload: BootstrapState) { experimentalSpecifierResolution as ExperimentalSpecifierResolution, }); - // If ESM is enabled through the parsed tsconfig, stage4 should be run in a child - // process with the ESM loaders configured. - if (preloadedConfig.options.esm) payload.shouldUseChildProcess = true; - - return { preloadedConfig }; + return { + preloadedConfig, + enableEsmLoader: !!(preloadedConfig.options.esm || esm), + }; } /** @@ -433,10 +474,15 @@ function phase3(payload: BootstrapState) { * configuration and entry-point information is only reliable in the final phase. More * details can be found in here: https://github.com/TypeStrong/ts-node/issues/1812. */ -function getEntryPointInfo(state: BootstrapState) { - const { code, interactive, restArgs } = state.parseArgvResult!; - const { cwd } = state.phase2Result!; - const { isCli } = state; +function getEntryPointInfo( + resolutionCwd: string, + argvResult: { + code: string | undefined; + interactive: boolean | undefined; + restArgs: string[]; + } +) { + const { code, interactive, restArgs } = argvResult; // Figure out which we are executing: piped stdin, --eval, REPL, and/or entrypoint // This is complicated because node's behavior is complicated @@ -448,14 +494,9 @@ function getEntryPointInfo(state: BootstrapState) { (interactive || (process.stdin.isTTY && !executeEval)); const executeStdin = !executeEval && !executeRepl && !executeEntrypoint; - /** - * Unresolved. May point to a symlink, not realpath. May be missing file extension - * NOTE: resolution relative to cwd option (not `process.cwd()`) is legacy backwards-compat; should be changed in next major: https://github.com/TypeStrong/ts-node/issues/1834 - */ + /** Unresolved. May point to a symlink, not realpath. May be missing file extension */ const entryPointPath = executeEntrypoint - ? isCli - ? resolve(cwd, restArgs[0]) - : resolve(restArgs[0]) + ? resolve(resolutionCwd, restArgs[0]) : undefined; return { @@ -467,12 +508,11 @@ function getEntryPointInfo(state: BootstrapState) { }; } -function phase4(payload: BootstrapState) { - const { isInChildProcess, tsNodeScript } = payload; - const { version, showConfig, restArgs, code, print, argv } = - payload.parseArgvResult; - const { cwd } = payload.phase2Result!; - const { preloadedConfig } = payload.phase3Result!; +function phase4(payload: BootstrapStateForChild) { + const { restArgs } = payload.parseArgvResult; + const { preloadedConfig } = payload.phase3Result; + const resolutionCwd = + payload.initialProcessOptions?.resolutionCwd ?? process.cwd(); const { entryPointPath, @@ -480,7 +520,11 @@ function phase4(payload: BootstrapState) { executeEval, executeRepl, executeStdin, - } = getEntryPointInfo(payload); + } = getEntryPointInfo(resolutionCwd, { + code: payload.initialProcessOptions?.code, + interactive: payload.initialProcessOptions?.interactive, + restArgs: payload.parseArgvResult.restArgs, + }); /** * , [stdin], and [eval] are all essentially virtual files that do not exist on disc and are backed by a REPL @@ -496,7 +540,7 @@ function phase4(payload: BootstrapState) { let stdinStuff: VirtualFileState | undefined; let evalAwarePartialHost: EvalAwarePartialHost | undefined = undefined; if (executeEval) { - const state = new EvalState(join(cwd, EVAL_FILENAME)); + const state = new EvalState(join(resolutionCwd, EVAL_FILENAME)); evalStuff = { state, repl: createRepl({ @@ -509,10 +553,10 @@ function phase4(payload: BootstrapState) { // Create a local module instance based on `cwd`. const module = (evalStuff.module = new Module(EVAL_NAME)); module.filename = evalStuff.state.path; - module.paths = (Module as any)._nodeModulePaths(cwd); + module.paths = (Module as any)._nodeModulePaths(resolutionCwd); } if (executeStdin) { - const state = new EvalState(join(cwd, STDIN_FILENAME)); + const state = new EvalState(join(resolutionCwd, STDIN_FILENAME)); stdinStuff = { state, repl: createRepl({ @@ -525,10 +569,10 @@ function phase4(payload: BootstrapState) { // Create a local module instance based on `cwd`. const module = (stdinStuff.module = new Module(STDIN_NAME)); module.filename = stdinStuff.state.path; - module.paths = (Module as any)._nodeModulePaths(cwd); + module.paths = (Module as any)._nodeModulePaths(resolutionCwd); } if (executeRepl) { - const state = new EvalState(join(cwd, REPL_FILENAME)); + const state = new EvalState(join(resolutionCwd, REPL_FILENAME)); replStuff = { state, repl: createRepl({ @@ -552,7 +596,8 @@ function phase4(payload: BootstrapState) { }, }); register(service); - if (isInChildProcess) + + if (payload.phase3Result.enableEsmLoader) ( require('./child/child-loader') as typeof import('./child/child-loader') ).lateBindHooks(createEsmHooks(service)); @@ -563,13 +608,13 @@ function phase4(payload: BootstrapState) { stdinStuff?.repl.setService(service); // Output project information. - if (version === 2) { + if (payload.initialProcessOptions?.version === 2) { console.log(`ts-node v${VERSION}`); console.log(`node ${process.version}`); console.log(`compiler v${service.ts.version}`); process.exit(0); } - if (version >= 3) { + if ((payload.initialProcessOptions?.version ?? 0) >= 3) { console.log(`ts-node v${VERSION} ${dirname(__dirname)}`); console.log(`node ${process.version}`); console.log( @@ -578,7 +623,7 @@ function phase4(payload: BootstrapState) { process.exit(0); } - if (showConfig) { + if (payload.initialProcessOptions?.showConfig) { const ts = service.ts as any as TSInternal; if (typeof ts.convertToTSConfig !== 'function') { console.error( @@ -613,7 +658,8 @@ function phase4(payload: BootstrapState) { }, ...ts.convertToTSConfig( service.config, - service.configFilePath ?? join(cwd, 'ts-node-implicit-tsconfig.json'), + service.configFilePath ?? + join(resolutionCwd, 'ts-node-implicit-tsconfig.json'), service.ts.sys ), }; @@ -626,12 +672,21 @@ function phase4(payload: BootstrapState) { process.exit(0); } - // Prepend `ts-node` arguments to CLI for child processes. - process.execArgv.push( - tsNodeScript, - ...argv.slice(2, argv.length - restArgs.length) + const forkPersistentBootstrapState: BootstrapStateForForkedProcesses = + createBootstrapStateForChildProcess(payload); + + const { childScriptPath, childScriptArgs } = getChildProcessArguments( + payload.phase3Result.enableEsmLoader, + forkPersistentBootstrapState ); + // Append the child script path and arguments to the process `execArgv`. + // The final phase is always invoked with Node directly, but subsequent + // forked instances (of the user entry-point) should directly jump into + // the final phase by landing directly in the child script with the Brotli + // encoded bootstrap state (as computed above with `forkPersistentBootstrapState`). + process.execArgv.push(childScriptPath, ...childScriptArgs); + // TODO this comes from BootstrapState process.argv = [process.argv[1]] .concat(executeEntrypoint ? ([entryPointPath] as string[]) : []) @@ -640,7 +695,7 @@ function phase4(payload: BootstrapState) { // Execute the main contents (either eval, script or piped). if (executeEntrypoint) { if ( - payload.isInChildProcess && + payload.phase3Result.enableEsmLoader && versionGteLt(process.versions.node, '18.6.0') ) { // HACK workaround node regression @@ -656,8 +711,8 @@ function phase4(payload: BootstrapState) { evalAndExitOnTsError( evalStuff!.repl, evalStuff!.module!, - code!, - print, + payload.initialProcessOptions!.code!, + payload.initialProcessOptions!.print, 'eval' ); } @@ -667,7 +722,7 @@ function phase4(payload: BootstrapState) { } if (executeStdin) { - let buffer = code || ''; + let buffer = payload.initialProcessOptions?.code ?? ''; process.stdin.on('data', (chunk: Buffer) => (buffer += chunk)); process.stdin.on('end', () => { evalAndExitOnTsError( @@ -675,7 +730,7 @@ function phase4(payload: BootstrapState) { stdinStuff!.module!, buffer, // `echo 123 | node -p` still prints 123 - print, + payload.initialProcessOptions?.print ?? false, 'stdin' ); }); @@ -683,6 +738,22 @@ function phase4(payload: BootstrapState) { } } +function createBootstrapStateForChildProcess( + state: BootstrapStateInitialProcessChild | BootstrapStateForForkedProcesses +): BootstrapStateForForkedProcesses { + // NOTE: Build up the child process fork bootstrap state manually so that we do + // not encode unnecessary properties into the bootstrap state that is persisted + return { + parseArgvResult: { + restArgs: state.parseArgvResult.restArgs, + }, + phase3Result: { + enableEsmLoader: state.phase3Result!.enableEsmLoader, + preloadedConfig: state.phase3Result!.preloadedConfig, + }, + }; +} + /** * Get project search path from args. */ diff --git a/src/child/child-entrypoint.ts b/src/child/child-entrypoint.ts index 0550170bf..1e5e874e9 100644 --- a/src/child/child-entrypoint.ts +++ b/src/child/child-entrypoint.ts @@ -1,24 +1,11 @@ -import { BootstrapState, bootstrap } from '../bin'; +import { completeBootstrap, BootstrapStateForChild } from '../bin'; import { argPrefix, compress, decompress } from './argv-payload'; const base64ConfigArg = process.argv[2]; if (!base64ConfigArg.startsWith(argPrefix)) throw new Error('unexpected argv'); const base64Payload = base64ConfigArg.slice(argPrefix.length); -const state = decompress(base64Payload) as BootstrapState; +const state = decompress(base64Payload) as BootstrapStateForChild; -state.isInChildProcess = true; -state.tsNodeScript = __filename; -state.parseArgvResult.argv = process.argv; state.parseArgvResult.restArgs = process.argv.slice(3); -// Modify and re-compress the payload delivered to subsequent child processes. -// This logic may be refactored into bin.ts by https://github.com/TypeStrong/ts-node/issues/1831 -if (state.isCli) { - const stateForChildren: BootstrapState = { - ...state, - isCli: false, - }; - state.parseArgvResult.argv[2] = `${argPrefix}${compress(stateForChildren)}`; -} - -bootstrap(state); +completeBootstrap(state); diff --git a/src/child/child-exec-args.ts b/src/child/child-exec-args.ts new file mode 100644 index 000000000..b96caeb8e --- /dev/null +++ b/src/child/child-exec-args.ts @@ -0,0 +1,40 @@ +import { pathToFileURL } from 'url'; +import { brotliCompressSync } from 'zlib'; +import type { BootstrapStateForChild } from '../bin'; +import { versionGteLt } from '../util'; +import { argPrefix } from './argv-payload'; + +export function getChildProcessArguments( + enableEsmLoader: boolean, + state: BootstrapStateForChild +) { + if (enableEsmLoader && !versionGteLt(process.versions.node, '12.17.0')) { + throw new Error( + '`ts-node-esm` and `ts-node --esm` require node version 12.17.0 or newer.' + ); + } + + const nodeExecArgs = []; + + if (enableEsmLoader) { + nodeExecArgs.push( + '--require', + require.resolve('./child-require.js'), + '--loader', + // Node on Windows doesn't like `c:\` absolute paths here; must be `file:///c:/` + pathToFileURL(require.resolve('../../child-loader.mjs')).toString() + ); + } + + const childScriptArgs = [ + `${argPrefix}${brotliCompressSync( + Buffer.from(JSON.stringify(state), 'utf8') + ).toString('base64')}`, + ]; + + return { + nodeExecArgs, + childScriptArgs, + childScriptPath: require.resolve('./child-entrypoint.js'), + }; +} diff --git a/src/child/spawn-child-with-esm.ts b/src/child/spawn-child-with-esm.ts new file mode 100644 index 000000000..ca8892923 --- /dev/null +++ b/src/child/spawn-child-with-esm.ts @@ -0,0 +1,43 @@ +import { fork } from 'child_process'; +import type { BootstrapStateForChild } from '../bin'; +import { getChildProcessArguments } from './child-exec-args'; + +/** + * @internal + * @param state Bootstrap state to be transferred into the child process. + * @param enableEsmLoader Whether to enable the ESM loader or not. This option may + * be removed in the future when `--esm` is no longer a choice. + * @param targetCwd Working directory to be preserved when transitioning to + * the child process. + */ +export function callInChildWithEsm( + state: BootstrapStateForChild, + targetCwd: string +) { + const { childScriptArgs, childScriptPath, nodeExecArgs } = + getChildProcessArguments(/* enableEsmLoader */ true, state); + + childScriptArgs.push(...state.parseArgvResult.restArgs); + + const child = fork(childScriptPath, childScriptArgs, { + stdio: 'inherit', + execArgv: [...process.execArgv, ...nodeExecArgs], + cwd: targetCwd, + }); + child.on('error', (error) => { + console.error(error); + process.exit(1); + }); + child.on('exit', (code) => { + child.removeAllListeners(); + process.off('SIGINT', sendSignalToChild); + process.off('SIGTERM', sendSignalToChild); + process.exitCode = code === null ? 1 : code; + }); + // Ignore sigint and sigterm in parent; pass them to child + process.on('SIGINT', sendSignalToChild); + process.on('SIGTERM', sendSignalToChild); + function sendSignalToChild(signal: string) { + process.kill(child.pid, signal); + } +} diff --git a/src/child/spawn-child.ts b/src/child/spawn-child.ts deleted file mode 100644 index 618b8190a..000000000 --- a/src/child/spawn-child.ts +++ /dev/null @@ -1,52 +0,0 @@ -import type { BootstrapState } from '../bin'; -import { spawn } from 'child_process'; -import { pathToFileURL } from 'url'; -import { versionGteLt } from '../util'; -import { argPrefix, compress } from './argv-payload'; - -/** - * @internal - * @param state Bootstrap state to be transferred into the child process. - * @param targetCwd Working directory to be preserved when transitioning to - * the child process. - */ -export function callInChild(state: BootstrapState) { - if (!versionGteLt(process.versions.node, '12.17.0')) { - throw new Error( - '`ts-node-esm` and `ts-node --esm` require node version 12.17.0 or newer.' - ); - } - const child = spawn( - process.execPath, - [ - '--require', - require.resolve('./child-require.js'), - '--loader', - // Node on Windows doesn't like `c:\` absolute paths here; must be `file:///c:/` - pathToFileURL(require.resolve('../../child-loader.mjs')).toString(), - require.resolve('./child-entrypoint.js'), - `${argPrefix}${compress(state)}`, - ...state.parseArgvResult.restArgs, - ], - { - stdio: 'inherit', - argv0: process.argv0, - } - ); - child.on('error', (error) => { - console.error(error); - process.exit(1); - }); - child.on('exit', (code) => { - child.removeAllListeners(); - process.off('SIGINT', sendSignalToChild); - process.off('SIGTERM', sendSignalToChild); - process.exitCode = code === null ? 1 : code; - }); - // Ignore sigint and sigterm in parent; pass them to child - process.on('SIGINT', sendSignalToChild); - process.on('SIGTERM', sendSignalToChild); - function sendSignalToChild(signal: string) { - process.kill(child.pid, signal); - } -} diff --git a/src/test/esm-loader.spec.ts b/src/test/esm-loader.spec.ts index 375012a76..d6172a8c1 100644 --- a/src/test/esm-loader.spec.ts +++ b/src/test/esm-loader.spec.ts @@ -362,10 +362,8 @@ test.suite('esm', (test) => { test.suite('esm child process working directory', (test) => { test('should have the correct working directory in the user entry-point', async () => { const { err, stdout, stderr } = await exec( - `${BIN_PATH} --esm --cwd ./esm/ index.ts`, - { - cwd: resolve(TEST_DIR, 'working-dir'), - } + `${BIN_PATH} --esm index.ts`, + { cwd: './working-dir/esm/' } ); expect(err).toBe(null); @@ -377,7 +375,8 @@ test.suite('esm', (test) => { test.suite('esm child process and forking', (test) => { test('should be able to fork vanilla NodeJS script', async () => { const { err, stdout, stderr } = await exec( - `${BIN_PATH} --esm --cwd ./esm-child-process/ ./process-forking-js/index.ts` + `${BIN_PATH} --esm index.ts`, + { cwd: './esm-child-process/process-forking-js-worker/' } ); expect(err).toBe(null); @@ -385,9 +384,10 @@ test.suite('esm', (test) => { expect(stderr).toBe(''); }); - test('should be able to fork TypeScript script', async () => { + test('should be able to fork into a nested TypeScript ESM script', async () => { const { err, stdout, stderr } = await exec( - `${BIN_PATH} --esm --cwd ./esm-child-process/ ./process-forking-ts/index.ts` + `${BIN_PATH} --esm index.ts`, + { cwd: './esm-child-process/process-forking-esm-worker/' } ); expect(err).toBe(null); @@ -395,15 +395,37 @@ test.suite('esm', (test) => { expect(stderr).toBe(''); }); - test('should be able to fork TypeScript script by absolute path', async () => { - const { err, stdout, stderr } = await exec( - `${BIN_PATH} --esm --cwd ./esm-child-process/ ./process-forking-ts-abs/index.ts` - ); + test( + 'should be possible to fork into a nested TypeScript script with respect to ' + + 'the working directory', + async () => { + const { err, stdout, stderr } = await exec( + `${BIN_PATH} --esm index.ts`, + { cwd: './esm-child-process/process-forking-subdir-relative/' } + ); - expect(err).toBe(null); - expect(stdout.trim()).toBe('Passing: from main'); - expect(stderr).toBe(''); - }); + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing: from main'); + expect(stderr).toBe(''); + } + ); + + test.suite( + 'with NodeNext TypeScript resolution and `.mts` extension', + (test) => { + test.runIf(tsSupportsStableNodeNextNode16); + + test('should be able to fork into a nested TypeScript ESM script', async () => { + const { err, stdout, stderr } = await exec( + `${BIN_PATH} --esm ./esm-child-process/process-forking-esm-worker-next/index.mts` + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing: from main'); + expect(stderr).toBe(''); + }); + } + ); }); test.suite('parent passes signals to child', (test) => { diff --git a/src/test/helpers.ts b/src/test/helpers.ts index da86bddc2..20916a9f7 100644 --- a/src/test/helpers.ts +++ b/src/test/helpers.ts @@ -33,6 +33,10 @@ export const BIN_SCRIPT_PATH = join( TEST_DIR, 'node_modules/.bin/ts-node-script' ); +export const CHILD_ENTRY_POINT_SCRIPT = join( + TEST_DIR, + 'node_modules/ts-node/dist/child/child-entrypoint.js' +); export const BIN_CWD_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-cwd'); export const BIN_ESM_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-esm'); diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index f085a3639..fef5037f6 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -5,6 +5,7 @@ import { tmpdir } from 'os'; import semver = require('semver'); import { BIN_PATH_JS, + CHILD_ENTRY_POINT_SCRIPT, CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG, nodeSupportsEsmHooks, nodeSupportsSpawningChildProcess, @@ -619,10 +620,7 @@ test.suite('ts-node', (test) => { test('should have the correct working directory in the user entry-point', async () => { const { err, stdout, stderr } = await exec( - `${BIN_PATH} --cwd ./cjs index.ts`, - { - cwd: resolve(TEST_DIR, 'working-dir'), - } + `${BIN_PATH} --cwd ./working-dir/cjs/ index.ts` ); expect(err).toBe(null); @@ -630,19 +628,19 @@ test.suite('ts-node', (test) => { expect(stderr).toBe(''); }); - // Disabled due to bug: - // --cwd is passed to forked children when not using --esm, erroneously affects their entrypoint resolution. - // tracked/fixed by either https://github.com/TypeStrong/ts-node/issues/1834 - // or https://github.com/TypeStrong/ts-node/issues/1831 - test.skip('should be able to fork into a nested TypeScript script with a modified working directory', async () => { - const { err, stdout, stderr } = await exec( - `${BIN_PATH} --cwd ./working-dir/forking/ index.ts` - ); + test( + 'should be able to fork into a nested TypeScript script with a modified ' + + 'working directory', + async () => { + const { err, stdout, stderr } = await exec( + `${BIN_PATH} --cwd ./working-dir/forking/ index.ts` + ); - expect(err).toBe(null); - expect(stdout.trim()).toBe('Passing: from main'); - expect(stderr).toBe(''); - }); + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing: from main'); + expect(stderr).toBe(''); + } + ); test.suite('should read ts-node options from tsconfig.json', (test) => { const BIN_EXEC = `"${BIN_PATH}" --project tsconfig-options/tsconfig.json`; @@ -1132,35 +1130,61 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async ( 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(`node --no-warnings ${BIN_PATH_JS}`, BIN_PATH_JS, [ + '--no-warnings', + ]); forkTest( `${BIN_PATH}`, process.platform === 'win32' ? BIN_PATH_JS : BIN_PATH ); + test.suite('with esm enabled', (test) => { + test.runIf(nodeSupportsSpawningChildProcess); + + forkTest( + `node --no-warnings ${BIN_PATH_JS} --esm`, + CHILD_ENTRY_POINT_SCRIPT, + [ + '--no-warnings', + // Forked child processes should directly receive the necessary ESM loader + // Node flags through `execArgv`, avoiding doubled child process spawns. + '--require', + expect.stringMatching(/child-require.js$/), + '--loader', + expect.stringMatching(/child-loader.mjs$/), + ], + './recursive-fork-esm/index.ts' + ); + }); + function forkTest( command: string, expectParentArgv0: string, - nodeFlag?: string + nodeFlags?: (string | ReturnType)[], + testFixturePath = './recursive-fork/index.ts' ) { test(command, async (t) => { const { err, stderr, stdout } = await exec( - `${command} --skipIgnore ./recursive-fork/index.ts argv2` + `${command} --skipIgnore ${testFixturePath} argv2` ); expect(err).toBeNull(); expect(stderr).toBe(''); const generations = stdout.split('\n'); const expectation = { - execArgv: [nodeFlag, BIN_PATH_JS, '--skipIgnore'].filter((v) => v), + execArgv: [ + ...(nodeFlags || []), + CHILD_ENTRY_POINT_SCRIPT, + expect.stringMatching(/^--brotli-base64-config=.*/), + ], argv: [ - // Note: argv[0] is *always* BIN_PATH_JS in child & grandchild + // Note: argv[0] is *always* CHILD_ENTRY_POINT_SCRIPT in child & grandchild expectParentArgv0, - resolve(TEST_DIR, 'recursive-fork/index.ts'), + resolve(TEST_DIR, testFixturePath), 'argv2', ], }; expect(JSON.parse(generations[0])).toMatchObject(expectation); - expectation.argv[0] = BIN_PATH_JS; + expectation.argv[0] = CHILD_ENTRY_POINT_SCRIPT; expect(JSON.parse(generations[1])).toMatchObject(expectation); expect(JSON.parse(generations[2])).toMatchObject(expectation); }); diff --git a/tests/esm-child-process/process-forking-ts-abs/index.ts b/tests/esm-child-process/process-forking-esm-worker-next/index.mts similarity index 80% rename from tests/esm-child-process/process-forking-ts-abs/index.ts rename to tests/esm-child-process/process-forking-esm-worker-next/index.mts index ec94e846d..e3c56655d 100644 --- a/tests/esm-child-process/process-forking-ts-abs/index.ts +++ b/tests/esm-child-process/process-forking-esm-worker-next/index.mts @@ -6,13 +6,10 @@ import { fileURLToPath } from 'url'; // worker process finishes properly with the expected stdout message. process.exitCode = 1; -const workerProcess = fork( - join(dirname(fileURLToPath(import.meta.url)), 'subfolder/worker.ts'), - [], - { - stdio: 'pipe', - } -); +const containingDir = dirname(fileURLToPath(import.meta.url)); +const workerProcess = fork(join(containingDir, 'worker.mts'), [], { + stdio: 'pipe', +}); let stdout = ''; diff --git a/tests/esm-child-process/process-forking-esm-worker-next/tsconfig.json b/tests/esm-child-process/process-forking-esm-worker-next/tsconfig.json new file mode 100644 index 000000000..0b0d8971d --- /dev/null +++ b/tests/esm-child-process/process-forking-esm-worker-next/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "module": "NodeNext" + }, + "ts-node": { + "transpileOnly": true + } +} diff --git a/tests/esm-child-process/process-forking-ts-abs/subfolder/worker.ts b/tests/esm-child-process/process-forking-esm-worker-next/worker.mts similarity index 100% rename from tests/esm-child-process/process-forking-ts-abs/subfolder/worker.ts rename to tests/esm-child-process/process-forking-esm-worker-next/worker.mts diff --git a/tests/esm-child-process/process-forking-js/index.ts b/tests/esm-child-process/process-forking-esm-worker/index.ts similarity index 76% rename from tests/esm-child-process/process-forking-js/index.ts rename to tests/esm-child-process/process-forking-esm-worker/index.ts index 88a3bd61a..ff0f2e61b 100644 --- a/tests/esm-child-process/process-forking-js/index.ts +++ b/tests/esm-child-process/process-forking-esm-worker/index.ts @@ -1,14 +1,10 @@ import { fork } from 'child_process'; -import { dirname } from 'path'; -import { fileURLToPath } from 'url'; // Initially set the exit code to non-zero. We only set it to `0` when the // worker process finishes properly with the expected stdout message. process.exitCode = 1; -process.chdir(dirname(fileURLToPath(import.meta.url))); - -const workerProcess = fork('./worker.js', [], { +const workerProcess = fork('./worker.ts', [], { stdio: 'pipe', }); diff --git a/tests/esm-child-process/process-forking-js/package.json b/tests/esm-child-process/process-forking-esm-worker/package.json similarity index 100% rename from tests/esm-child-process/process-forking-js/package.json rename to tests/esm-child-process/process-forking-esm-worker/package.json diff --git a/tests/esm-child-process/process-forking-ts-abs/tsconfig.json b/tests/esm-child-process/process-forking-esm-worker/tsconfig.json similarity index 74% rename from tests/esm-child-process/process-forking-ts-abs/tsconfig.json rename to tests/esm-child-process/process-forking-esm-worker/tsconfig.json index 04e93e5c7..f16e691d2 100644 --- a/tests/esm-child-process/process-forking-ts-abs/tsconfig.json +++ b/tests/esm-child-process/process-forking-esm-worker/tsconfig.json @@ -3,6 +3,6 @@ "module": "ESNext" }, "ts-node": { - "swc": true + "transpileOnly": true } } diff --git a/tests/esm-child-process/process-forking-ts/subfolder/worker.ts b/tests/esm-child-process/process-forking-esm-worker/worker.ts similarity index 100% rename from tests/esm-child-process/process-forking-ts/subfolder/worker.ts rename to tests/esm-child-process/process-forking-esm-worker/worker.ts diff --git a/tests/esm-child-process/process-forking-ts/index.ts b/tests/esm-child-process/process-forking-js-worker/index.ts similarity index 83% rename from tests/esm-child-process/process-forking-ts/index.ts rename to tests/esm-child-process/process-forking-js-worker/index.ts index 2d59e0aab..22963dced 100644 --- a/tests/esm-child-process/process-forking-ts/index.ts +++ b/tests/esm-child-process/process-forking-js-worker/index.ts @@ -6,9 +6,8 @@ import { fileURLToPath } from 'url'; // worker process finishes properly with the expected stdout message. process.exitCode = 1; -process.chdir(join(dirname(fileURLToPath(import.meta.url)), 'subfolder')); - -const workerProcess = fork('./worker.ts', [], { +const workerPath = join(dirname(fileURLToPath(import.meta.url)), './worker.js'); +const workerProcess = fork(workerPath, [], { stdio: 'pipe', }); diff --git a/tests/esm-child-process/process-forking-ts-abs/package.json b/tests/esm-child-process/process-forking-js-worker/package.json similarity index 100% rename from tests/esm-child-process/process-forking-ts-abs/package.json rename to tests/esm-child-process/process-forking-js-worker/package.json diff --git a/tests/esm-child-process/process-forking-ts/tsconfig.json b/tests/esm-child-process/process-forking-js-worker/tsconfig.json similarity index 74% rename from tests/esm-child-process/process-forking-ts/tsconfig.json rename to tests/esm-child-process/process-forking-js-worker/tsconfig.json index 04e93e5c7..f16e691d2 100644 --- a/tests/esm-child-process/process-forking-ts/tsconfig.json +++ b/tests/esm-child-process/process-forking-js-worker/tsconfig.json @@ -3,6 +3,6 @@ "module": "ESNext" }, "ts-node": { - "swc": true + "transpileOnly": true } } diff --git a/tests/esm-child-process/process-forking-js/worker.js b/tests/esm-child-process/process-forking-js-worker/worker.js similarity index 100% rename from tests/esm-child-process/process-forking-js/worker.js rename to tests/esm-child-process/process-forking-js-worker/worker.js diff --git a/tests/esm-child-process/process-forking-subdir-relative/index.ts b/tests/esm-child-process/process-forking-subdir-relative/index.ts new file mode 100644 index 000000000..e0b27f3cf --- /dev/null +++ b/tests/esm-child-process/process-forking-subdir-relative/index.ts @@ -0,0 +1,22 @@ +import { fork } from 'child_process'; +import { join } from 'path'; + +// Initially set the exit code to non-zero. We only set it to `0` when the +// worker process finishes properly with the expected stdout message. +process.exitCode = 1; + +const workerProcess = fork('./worker.ts', [], { + stdio: 'pipe', + cwd: join(process.cwd(), 'subfolder/'), +}); + +let stdout = ''; + +workerProcess.stdout.on('data', (chunk) => (stdout += chunk.toString('utf8'))); +workerProcess.on('error', () => (process.exitCode = 1)); +workerProcess.on('close', (status, signal) => { + if (status === 0 && signal === null && stdout.trim() === 'Works') { + console.log('Passing: from main'); + process.exitCode = 0; + } +}); diff --git a/tests/esm-child-process/process-forking-ts/package.json b/tests/esm-child-process/process-forking-subdir-relative/package.json similarity index 100% rename from tests/esm-child-process/process-forking-ts/package.json rename to tests/esm-child-process/process-forking-subdir-relative/package.json diff --git a/tests/esm-child-process/process-forking-subdir-relative/subfolder/worker.ts b/tests/esm-child-process/process-forking-subdir-relative/subfolder/worker.ts new file mode 100644 index 000000000..4114d5ab0 --- /dev/null +++ b/tests/esm-child-process/process-forking-subdir-relative/subfolder/worker.ts @@ -0,0 +1,3 @@ +const message: string = 'Works'; + +console.log(message); diff --git a/tests/esm-child-process/process-forking-js/tsconfig.json b/tests/esm-child-process/process-forking-subdir-relative/tsconfig.json similarity index 74% rename from tests/esm-child-process/process-forking-js/tsconfig.json rename to tests/esm-child-process/process-forking-subdir-relative/tsconfig.json index 04e93e5c7..f16e691d2 100644 --- a/tests/esm-child-process/process-forking-js/tsconfig.json +++ b/tests/esm-child-process/process-forking-subdir-relative/tsconfig.json @@ -3,6 +3,6 @@ "module": "ESNext" }, "ts-node": { - "swc": true + "transpileOnly": true } } diff --git a/tests/project-resolution/a/index.ts b/tests/project-resolution/a/index.ts new file mode 100644 index 000000000..230f5ea09 --- /dev/null +++ b/tests/project-resolution/a/index.ts @@ -0,0 +1,9 @@ +export {}; +// Type assertion to please TS 2.7 +const register = process[(Symbol as any).for('ts-node.register.instance')]; +console.log( + JSON.stringify({ + options: register.options, + config: register.config, + }) +); diff --git a/tests/project-resolution/a/tsconfig.json b/tests/project-resolution/a/tsconfig.json new file mode 100644 index 000000000..377a86016 --- /dev/null +++ b/tests/project-resolution/a/tsconfig.json @@ -0,0 +1,12 @@ +{ + "ts-node": { + "transpileOnly": true + }, + "compilerOptions": { + "plugins": [ + { + "name": "plugin-a" + } + ] + } +} diff --git a/tests/project-resolution/b/index.ts b/tests/project-resolution/b/index.ts new file mode 100644 index 000000000..230f5ea09 --- /dev/null +++ b/tests/project-resolution/b/index.ts @@ -0,0 +1,9 @@ +export {}; +// Type assertion to please TS 2.7 +const register = process[(Symbol as any).for('ts-node.register.instance')]; +console.log( + JSON.stringify({ + options: register.options, + config: register.config, + }) +); diff --git a/tests/project-resolution/b/tsconfig.json b/tests/project-resolution/b/tsconfig.json new file mode 100644 index 000000000..dd98865c1 --- /dev/null +++ b/tests/project-resolution/b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "ts-node": { + "transpileOnly": true + }, + "compilerOptions": { + "plugins": [ + { + "name": "plugin-b" + } + ] + } +} diff --git a/tests/recursive-fork-esm/index.ts b/tests/recursive-fork-esm/index.ts new file mode 100644 index 000000000..380d6a644 --- /dev/null +++ b/tests/recursive-fork-esm/index.ts @@ -0,0 +1,17 @@ +import { fork } from 'child_process'; +import { fileURLToPath } from 'url'; + +// Type syntax to prove its compiled, though the import above should also +// prove the same +const a = null as any; +const currentScript = fileURLToPath(import.meta.url); + +console.log(JSON.stringify({ execArgv: process.execArgv, argv: process.argv })); +if (process.env.generation !== 'grandchild') { + const nextGeneration = + process.env.generation === 'child' ? 'grandchild' : 'child'; + fork(currentScript, process.argv.slice(2), { + env: { ...process.env, generation: nextGeneration }, + stdio: 'inherit', + }); +} diff --git a/tests/recursive-fork-esm/package.json b/tests/recursive-fork-esm/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/recursive-fork-esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/recursive-fork-esm/tsconfig.json b/tests/recursive-fork-esm/tsconfig.json new file mode 100644 index 000000000..e005d7499 --- /dev/null +++ b/tests/recursive-fork-esm/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "module": "NodeNext" + }, + "ts-node": { + "swc": true + } +} diff --git a/tests/working-dir/cjs/index.ts b/tests/working-dir/cjs/index.ts index f3ba1b30a..597b779e6 100644 --- a/tests/working-dir/cjs/index.ts +++ b/tests/working-dir/cjs/index.ts @@ -1,7 +1,7 @@ import { strictEqual } from 'assert'; -import { normalize, dirname } from 'path'; +import { join, normalize } from 'path'; -// Expect the working directory to be the parent directory. -strictEqual(normalize(process.cwd()), normalize(dirname(__dirname))); +// Expect the working directory to be the current directory. +strictEqual(normalize(process.cwd()), normalize(join(__dirname, '../..'))); console.log('Passing'); diff --git a/tests/working-dir/esm-node-next/index.ts b/tests/working-dir/esm-node-next/index.ts new file mode 100644 index 000000000..f290171a9 --- /dev/null +++ b/tests/working-dir/esm-node-next/index.ts @@ -0,0 +1,11 @@ +import { strictEqual } from 'assert'; +import { normalize, dirname } from 'path'; +import { fileURLToPath } from 'url'; + +// Expect the working directory to be the current directory. +strictEqual( + normalize(process.cwd()), + normalize(dirname(fileURLToPath(import.meta.url))) +); + +console.log('Passing'); diff --git a/tests/working-dir/esm-node-next/package.json b/tests/working-dir/esm-node-next/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/working-dir/esm-node-next/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/working-dir/esm-node-next/tsconfig.json b/tests/working-dir/esm-node-next/tsconfig.json new file mode 100644 index 000000000..3998b5074 --- /dev/null +++ b/tests/working-dir/esm-node-next/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "NodeNext" + } +} diff --git a/tests/working-dir/esm/index.ts b/tests/working-dir/esm/index.ts index 21230f9d8..0f6220303 100644 --- a/tests/working-dir/esm/index.ts +++ b/tests/working-dir/esm/index.ts @@ -1,11 +1,8 @@ -import { strictEqual } from 'assert'; -import { normalize, dirname } from 'path'; -import { fileURLToPath } from 'url'; +import { ok } from 'assert'; -// Expect the working directory to be the parent directory. -strictEqual( - normalize(process.cwd()), - normalize(dirname(dirname(fileURLToPath(import.meta.url)))) -); +// Expect the working directory to be the current directory. +// Note: Cannot use `import.meta.url` in this variant of the test +// because older TypeScript versions do not know about this syntax. +ok(/working-dir[\/\\]esm[\/\\]?/.test(process.cwd())); console.log('Passing'); diff --git a/tests/working-dir/esm/tsconfig.json b/tests/working-dir/esm/tsconfig.json index 04e93e5c7..f16e691d2 100644 --- a/tests/working-dir/esm/tsconfig.json +++ b/tests/working-dir/esm/tsconfig.json @@ -3,6 +3,6 @@ "module": "ESNext" }, "ts-node": { - "swc": true + "transpileOnly": true } }