diff --git a/src/bin.ts b/src/bin.ts index 8b5f91767..d8006af8a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -30,6 +30,7 @@ import type { TSInternal } from './ts-compiler-types'; import { addBuiltinLibsToObject } from '../dist-raw/node-internal-modules-cjs-helpers'; import { callInChild } from './child/spawn-child'; import { findAndReadConfig } from './configuration'; +import { getChildProcessArguments } from './child/child-exec-args'; /** * Main `bin` functionality. @@ -46,9 +47,6 @@ export function main( ) { const args = parseArgv(argv, entrypointArgs); const state: BootstrapState = { - shouldUseChildProcess: false, - isInChildProcess: false, - entrypoint: __filename, parseArgvResult: args, }; return bootstrap(state); @@ -60,28 +58,76 @@ export function main( * Can be marshalled when necessary to resume bootstrapping in a child process. */ export interface BootstrapState { - isInChildProcess: boolean; - shouldUseChildProcess: boolean; - entrypoint: 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) { - return callInChild(state); - } - } - if (!state.phase3Result) { - state.phase3Result = phase3(state); - if (state.shouldUseChildProcess && !state.isInChildProcess) { - 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, + }, + }; + + // Note: When transitioning into the child process for the final phase, + // we want to preserve the initial user working directory. + callInChild( + initialChildState, + state.phase3Result.enableEsmLoader, + process.cwd() + ); +} +/** Final phase of the bootstrap. */ +export function completeBootstrap(state: BootstrapStateForChild) { return phase4(state); } @@ -264,8 +310,7 @@ function parseArgv(argv: string[], entrypointArgs: Record) { } function phase2(payload: BootstrapState) { - const { help, version, code, interactive, cwdArg, restArgs, esm } = - payload.parseArgvResult; + const { help, version, cwdArg } = payload.parseArgvResult; if (help) { console.log(` @@ -319,28 +364,15 @@ Options: process.exit(0); } - // Figure out which we are executing: piped stdin, --eval, REPL, and/or entrypoint - // This is complicated because node's behavior is complicated - // `node -e code -i ./script.js` ignores -e - const executeEval = code != null && !(interactive && restArgs.length); - const executeEntrypoint = !executeEval && restArgs.length > 0; - const executeRepl = - !executeEntrypoint && - (interactive || (process.stdin.isTTY && !executeEval)); - const executeStdin = !executeEval && !executeRepl && !executeEntrypoint; - - const cwd = cwdArg || process.cwd(); - /** Unresolved. May point to a symlink, not realpath. May be missing file extension */ - const scriptPath = executeEntrypoint ? resolve(cwd, restArgs[0]) : undefined; + let resolutionCwd: string; + if (cwdArg !== undefined) { + resolutionCwd = resolve(cwdArg); + } else { + resolutionCwd = process.cwd(); + } - if (esm) payload.shouldUseChildProcess = true; return { - executeEval, - executeEntrypoint, - executeRepl, - executeStdin, - cwd, - scriptPath, + resolutionCwd, }; } @@ -372,10 +404,21 @@ function phase3(payload: BootstrapState) { esm, experimentalSpecifierResolution, } = payload.parseArgvResult; - const { cwd, scriptPath } = 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 + // original entry-point but then the process forks itself using e.g. `child_process.fork`. + // 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( + resolutionCwd, + payload.parseArgvResult! + ); const preloadedConfig = findAndReadConfig({ - cwd, + cwd: resolutionCwd, emit, files, pretty, @@ -387,7 +430,12 @@ function phase3(payload: BootstrapState) { compilerHost, ignore, logError, - projectSearchDir: getProjectSearchDir(cwd, scriptMode, cwdMode, scriptPath), + projectSearchDir: getProjectSearchDir( + resolutionCwd, + scriptMode, + cwdMode, + entryPointPath + ), project, skipProject, skipIgnore, @@ -403,23 +451,79 @@ function phase3(payload: BootstrapState) { experimentalSpecifierResolution as ExperimentalSpecifierResolution, }); - if (preloadedConfig.options.esm) payload.shouldUseChildProcess = true; - return { preloadedConfig }; + return { + preloadedConfig, + enableEsmLoader: !!(preloadedConfig.options.esm || esm), + }; } -function phase4(payload: BootstrapState) { - const { isInChildProcess, entrypoint } = payload; - const { version, showConfig, restArgs, code, print, argv } = - payload.parseArgvResult; - const { +/** + * Determines the entry-point information from the argv and phase2 result. This + * method will be invoked in two places: + * + * 1. In phase 3 to be able to find a project from the potential entry-point script. + * 2. In phase 4 to determine the actual entry-point script. + * + * Note that we need to explicitly re-resolve the entry-point information in the final + * stage because the previous stage information could be modified when the bootstrap + * invocation transitioned into a child process for ESM. + * + * Stages before (phase 4) can and will be cached by the child process through the Brotli + * 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( + 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 + // `node -e code -i ./script.js` ignores -e + const executeEval = code != null && !(interactive && restArgs.length); + const executeEntrypoint = !executeEval && restArgs.length > 0; + const executeRepl = + !executeEntrypoint && + (interactive || (process.stdin.isTTY && !executeEval)); + const executeStdin = !executeEval && !executeRepl && !executeEntrypoint; + + /** Unresolved. May point to a symlink, not realpath. May be missing file extension */ + const entryPointPath = executeEntrypoint + ? resolve(resolutionCwd, restArgs[0]) + : undefined; + + return { executeEval, - cwd, - executeStdin, + executeEntrypoint, executeRepl, + executeStdin, + entryPointPath, + }; +} + +function phase4(payload: BootstrapStateForChild) { + const { restArgs } = payload.parseArgvResult; + const { preloadedConfig } = payload.phase3Result; + const resolutionCwd = + payload.initialProcessOptions?.resolutionCwd ?? process.cwd(); + + const { + entryPointPath, executeEntrypoint, - scriptPath, - } = payload.phase2Result!; - const { preloadedConfig } = payload.phase3Result!; + executeEval, + executeRepl, + executeStdin, + } = 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 * service to handle eval-ing of code. @@ -434,7 +538,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({ @@ -447,10 +551,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({ @@ -463,10 +567,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({ @@ -490,7 +594,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)); @@ -501,13 +606,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( @@ -516,7 +621,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( @@ -551,7 +656,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 ), }; @@ -564,14 +670,24 @@ function phase4(payload: BootstrapState) { process.exit(0); } - // Prepend `ts-node` arguments to CLI for child processes. - process.execArgv.push( - entrypoint, - ...argv.slice(2, argv.length - restArgs.length) + const forkPersistentBootstrapState: BootstrapStateForForkedProcesses = + createBootstrapStateForChildProcess(payload); + + const { childScriptPath, childScriptArgs } = getChildProcessArguments( + payload.phase3Result.enableEsmLoader, + forkPersistentBootstrapState ); - // TODO this comes from BoostrapState + + // 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 ? ([scriptPath] as string[]) : []) + .concat(executeEntrypoint ? ([entryPointPath] as string[]) : []) .concat(restArgs.slice(executeEntrypoint ? 1 : 0)); // Execute the main contents (either eval, script or piped). @@ -585,8 +701,8 @@ function phase4(payload: BootstrapState) { evalAndExitOnTsError( evalStuff!.repl, evalStuff!.module!, - code!, - print, + payload.initialProcessOptions!.code!, + payload.initialProcessOptions!.print, 'eval' ); } @@ -596,7 +712,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( @@ -604,7 +720,7 @@ function phase4(payload: BootstrapState) { stdinStuff!.module!, buffer, // `echo 123 | node -p` still prints 123 - print, + payload.initialProcessOptions?.print ?? false, 'stdin' ); }); @@ -612,6 +728,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 03a02d2e9..2bf07342d 100644 --- a/src/child/child-entrypoint.ts +++ b/src/child/child-entrypoint.ts @@ -1,4 +1,4 @@ -import { BootstrapState, bootstrap } from '../bin'; +import { completeBootstrap, BootstrapStateForChild } from '../bin'; import { brotliDecompressSync } from 'zlib'; const base64ConfigArg = process.argv[2]; @@ -7,10 +7,8 @@ if (!base64ConfigArg.startsWith(argPrefix)) throw new Error('unexpected argv'); const base64Payload = base64ConfigArg.slice(argPrefix.length); const payload = JSON.parse( brotliDecompressSync(Buffer.from(base64Payload, 'base64')).toString() -) as BootstrapState; -payload.isInChildProcess = true; -payload.entrypoint = __filename; -payload.parseArgvResult.argv = process.argv; +) as BootstrapStateForChild; + payload.parseArgvResult.restArgs = process.argv.slice(3); -bootstrap(payload); +completeBootstrap(payload); diff --git a/src/child/child-exec-args.ts b/src/child/child-exec-args.ts new file mode 100644 index 000000000..3a25aa505 --- /dev/null +++ b/src/child/child-exec-args.ts @@ -0,0 +1,41 @@ +import { pathToFileURL } from 'url'; +import { brotliCompressSync } from 'zlib'; +import type { BootstrapStateForChild } from '../bin'; +import { versionGteLt } from '../util'; + +const argPrefix = '--brotli-base64-config='; + +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.ts b/src/child/spawn-child.ts index 12368fcef..446be06b0 100644 --- a/src/child/spawn-child.ts +++ b/src/child/spawn-child.ts @@ -1,37 +1,30 @@ -import type { BootstrapState } from '../bin'; -import { spawn } from 'child_process'; -import { brotliCompressSync } from 'zlib'; -import { pathToFileURL } from 'url'; -import { versionGteLt } from '../util'; +import { fork } from 'child_process'; +import type { BootstrapStateForChild } from '../bin'; +import { getChildProcessArguments } from './child-exec-args'; -const argPrefix = '--brotli-base64-config='; +/** + * @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 callInChild( + state: BootstrapStateForChild, + enableEsmLoader: boolean, + targetCwd: string +) { + const { childScriptArgs, childScriptPath, nodeExecArgs } = + getChildProcessArguments(enableEsmLoader, state); -/** @internal */ -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}${brotliCompressSync( - Buffer.from(JSON.stringify(state), 'utf8') - ).toString('base64')}`, - ...state.parseArgvResult.restArgs, - ], - { - stdio: 'inherit', - argv0: process.argv0, - } - ); + 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); diff --git a/src/test/esm-loader.spec.ts b/src/test/esm-loader.spec.ts index 41c421fd6..5d9f889a2 100644 --- a/src/test/esm-loader.spec.ts +++ b/src/test/esm-loader.spec.ts @@ -22,6 +22,7 @@ import { TEST_DIR, tsSupportsImportAssertions, tsSupportsResolveJsonModule, + tsSupportsStableNodeNextNode16, } from './helpers'; import { createExec, createSpawn, ExecReturn } from './exec-helpers'; import { join, resolve } from 'path'; @@ -358,6 +359,93 @@ 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 index.ts`, + { cwd: './working-dir/esm/' } + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing'); + expect(stderr).toBe(''); + }); + + test.suite( + 'with NodeNext TypeScript resolution and `.mts` extension', + (test) => { + test.runIf(tsSupportsStableNodeNextNode16); + + test('should have the correct working directory in the user entry-point', async () => { + const { err, stdout, stderr } = await exec( + `${BIN_PATH} --esm index.ts`, + { cwd: './working-dir/esm-node-next/' } + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing'); + expect(stderr).toBe(''); + }); + } + ); + }); + + 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 index.ts`, + { cwd: './esm-child-process/process-forking/' } + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing: from main'); + expect(stderr).toBe(''); + }); + + test('should be able to fork into a nested TypeScript ESM script', async () => { + const { err, stdout, stderr } = await exec( + `${BIN_PATH} --esm index.ts`, + { cwd: './esm-child-process/process-forking-nested-esm/' } + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing: from main'); + expect(stderr).toBe(''); + }); + + 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-nested-relative/' } + ); + + 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-nested-esm-node-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) => { test.runSerially(); 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 ca4c2cf85..5c4a865c7 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, @@ -617,6 +618,30 @@ 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 ./working-dir/cjs/ index.ts` + ); + + expect(err).toBe(null); + expect(stdout.trim()).toBe('Passing'); + expect(stderr).toBe(''); + }); + + 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(''); + } + ); + test.suite('should read ts-node options from tsconfig.json', (test) => { const BIN_EXEC = `"${BIN_PATH}" --project tsconfig-options/tsconfig.json`; @@ -1105,17 +1130,10 @@ 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( - `${BIN_PATH}`, - process.platform === 'win32' ? BIN_PATH_JS : BIN_PATH - ); + forkTest(`node --no-warnings ${BIN_PATH_JS}`, '--no-warnings'); + forkTest(`${BIN_PATH}`); - function forkTest( - command: string, - expectParentArgv0: string, - nodeFlag?: string - ) { + function forkTest(command: string, nodeFlag?: string) { test(command, async (t) => { const { err, stderr, stdout } = await exec( `${command} --skipIgnore ./recursive-fork/index.ts argv2` @@ -1124,16 +1142,18 @@ test.suite('node environment', (test) => { expect(stderr).toBe(''); const generations = stdout.split('\n'); const expectation = { - execArgv: [nodeFlag, BIN_PATH_JS, '--skipIgnore'].filter((v) => v), + execArgv: [ + nodeFlag, + CHILD_ENTRY_POINT_SCRIPT, + expect.stringMatching(/^--brotli-base64-config=.*/), + ].filter((v) => v), argv: [ - // Note: argv[0] is *always* BIN_PATH_JS in child & grandchild - expectParentArgv0, + CHILD_ENTRY_POINT_SCRIPT, 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); }); diff --git a/src/test/repl/repl-environment.spec.ts b/src/test/repl/repl-environment.spec.ts index d02341f1e..981e05567 100644 --- a/src/test/repl/repl-environment.spec.ts +++ b/src/test/repl/repl-environment.spec.ts @@ -6,6 +6,7 @@ import { context, expect } from '../testlib'; import * as getStream from 'get-stream'; import { + CHILD_ENTRY_POINT_SCRIPT, CMD_TS_NODE_WITH_PROJECT_FLAG, ctxTsNode, delay, @@ -145,8 +146,7 @@ test.suite( return modulePaths; } - // Executable is `ts-node` on Posix, `bin.js` on Windows due to Windows shimming limitations (this is determined by package manager) - const tsNodeExe = expect.stringMatching(/\b(ts-node|bin.js)$/); + const tsNodeExe = CHILD_ENTRY_POINT_SCRIPT; test('stdin', async (t) => { const { stdout } = await execTester({ diff --git a/tests/esm-child-process/process-forking-nested-esm-node-next/index.mts b/tests/esm-child-process/process-forking-nested-esm-node-next/index.mts new file mode 100644 index 000000000..e76286d8e --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm-node-next/index.mts @@ -0,0 +1,23 @@ +import { fork } from 'child_process'; +import { dirname, join } 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; + +const projectDir = dirname(fileURLToPath(import.meta.url)); +const workerProcess = fork(join(projectDir, 'worker.mts'), [], { + stdio: 'pipe', +}); + +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-nested-esm-node-next/tsconfig.json b/tests/esm-child-process/process-forking-nested-esm-node-next/tsconfig.json new file mode 100644 index 000000000..3998b5074 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm-node-next/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "NodeNext" + } +} diff --git a/tests/esm-child-process/process-forking-nested-esm-node-next/worker.mts b/tests/esm-child-process/process-forking-nested-esm-node-next/worker.mts new file mode 100644 index 000000000..4114d5ab0 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm-node-next/worker.mts @@ -0,0 +1,3 @@ +const message: string = 'Works'; + +console.log(message); diff --git a/tests/esm-child-process/process-forking-nested-esm/index.ts b/tests/esm-child-process/process-forking-nested-esm/index.ts new file mode 100644 index 000000000..ff0f2e61b --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm/index.ts @@ -0,0 +1,20 @@ +import { fork } from 'child_process'; + +// 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', +}); + +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-nested-esm/package.json b/tests/esm-child-process/process-forking-nested-esm/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/esm-child-process/process-forking-nested-esm/tsconfig.json b/tests/esm-child-process/process-forking-nested-esm/tsconfig.json new file mode 100644 index 000000000..1ac61592b --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "ESNext" + } +} diff --git a/tests/esm-child-process/process-forking-nested-esm/worker.ts b/tests/esm-child-process/process-forking-nested-esm/worker.ts new file mode 100644 index 000000000..4114d5ab0 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-esm/worker.ts @@ -0,0 +1,3 @@ +const message: string = 'Works'; + +console.log(message); diff --git a/tests/esm-child-process/process-forking-nested-relative/index.ts b/tests/esm-child-process/process-forking-nested-relative/index.ts new file mode 100644 index 000000000..e0b27f3cf --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-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-nested-relative/package.json b/tests/esm-child-process/process-forking-nested-relative/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-relative/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/esm-child-process/process-forking-nested-relative/subfolder/worker.ts b/tests/esm-child-process/process-forking-nested-relative/subfolder/worker.ts new file mode 100644 index 000000000..4114d5ab0 --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-relative/subfolder/worker.ts @@ -0,0 +1,3 @@ +const message: string = 'Works'; + +console.log(message); diff --git a/tests/esm-child-process/process-forking-nested-relative/tsconfig.json b/tests/esm-child-process/process-forking-nested-relative/tsconfig.json new file mode 100644 index 000000000..1ac61592b --- /dev/null +++ b/tests/esm-child-process/process-forking-nested-relative/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "ESNext" + } +} diff --git a/tests/esm-child-process/process-forking/index.ts b/tests/esm-child-process/process-forking/index.ts new file mode 100644 index 000000000..2e3e05ec8 --- /dev/null +++ b/tests/esm-child-process/process-forking/index.ts @@ -0,0 +1,20 @@ +import { fork } from 'child_process'; + +// 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.js', [], { + stdio: 'pipe', +}); + +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/package.json b/tests/esm-child-process/process-forking/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/esm-child-process/process-forking/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/esm-child-process/process-forking/tsconfig.json b/tests/esm-child-process/process-forking/tsconfig.json new file mode 100644 index 000000000..1ac61592b --- /dev/null +++ b/tests/esm-child-process/process-forking/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "ESNext" + } +} diff --git a/tests/esm-child-process/process-forking/worker.js b/tests/esm-child-process/process-forking/worker.js new file mode 100644 index 000000000..820d10b2e --- /dev/null +++ b/tests/esm-child-process/process-forking/worker.js @@ -0,0 +1 @@ +console.log('Works'); 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/working-dir/cjs/index.ts b/tests/working-dir/cjs/index.ts new file mode 100644 index 000000000..597b779e6 --- /dev/null +++ b/tests/working-dir/cjs/index.ts @@ -0,0 +1,7 @@ +import { strictEqual } from 'assert'; +import { join, normalize } from 'path'; + +// 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 new file mode 100644 index 000000000..0f6220303 --- /dev/null +++ b/tests/working-dir/esm/index.ts @@ -0,0 +1,8 @@ +import { ok } from 'assert'; + +// 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/package.json b/tests/working-dir/esm/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/working-dir/esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/working-dir/esm/tsconfig.json b/tests/working-dir/esm/tsconfig.json new file mode 100644 index 000000000..1ac61592b --- /dev/null +++ b/tests/working-dir/esm/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "ESNext" + } +} diff --git a/tests/working-dir/forking/index.ts b/tests/working-dir/forking/index.ts new file mode 100644 index 000000000..45ff8afd7 --- /dev/null +++ b/tests/working-dir/forking/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(__dirname, '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/working-dir/forking/subfolder/worker.ts b/tests/working-dir/forking/subfolder/worker.ts new file mode 100644 index 000000000..4114d5ab0 --- /dev/null +++ b/tests/working-dir/forking/subfolder/worker.ts @@ -0,0 +1,3 @@ +const message: string = 'Works'; + +console.log(message);