-
-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ESM node processes being unable to fork into other scripts (#1814)
* Fix ESM node processes being unable to fork into other scripts Currently, Node processes instantiated through the `--esm` flag result in a child process being created so that the ESM loader can be registered. This works fine and is reasonable. The child process approach to register ESM hooks currently prevents the NodeJS `fork` method from being used because the `execArgv` propagated into forked processes causes `ts-node` (which is also propagated as child exec script -- this is good because it allows nested type resolution to work) to always execute the original entry-point, causing potential infinite loops because the designated fork module script is not executed as expected. This commit fixes this by not encoding the entry-point information into the state that is captured as part of the `execArgv`. Instead the entry-point information is always retrieved from the parsed rest command line arguments in the final stage (`phase4`). Fixes #1812. * Fix `--cwd` to actually set the working directory and work with ESM child process Currently the `--esm` option does not necessarily do what the documentation suggests. i.e. the script does not run as if the working directory is the specified directory. This commit fixes this, so that the option is useful for TSConfig resolution, as well as for controlling the script working directory. Also fixes that the CWD encoded in the bootstrap brotli state for the ESM child process messes with the entry-point resolution, if e.g. the entry-point in `child_process.fork` is relative to a specified `cwd`. * changes based on review * lint-fix * enable transpileOnly in new tests for performance * Tweak basic working dir tests to verify that --cwd affects entrypoint resolution but not process.cwd() * update forking tests: disable non --esm test with comment about known bug and link to tickets make tests set cwd for fork() call, to be sure it is respected and not overridden by --cwd * use swc compiler to avoid issue with ancient TS versions not understanding import.meta.url syntax * Remove tests that I think are redundant (but I've asked for confirmation in code review) * fix another issue with old TS * final review updates Co-authored-by: Andrew Bradley <cspotcode@gmail.com>
- Loading branch information
1 parent
86b63bf
commit 32d07e2
Showing
25 changed files
with
390 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { brotliCompressSync, brotliDecompressSync, constants } from 'zlib'; | ||
|
||
/** @internal */ | ||
export const argPrefix = '--brotli-base64-config='; | ||
|
||
/** @internal */ | ||
export function compress(object: any) { | ||
return brotliCompressSync(Buffer.from(JSON.stringify(object), 'utf8'), { | ||
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MIN_QUALITY, | ||
}).toString('base64'); | ||
} | ||
|
||
/** @internal */ | ||
export function decompress(str: string) { | ||
return JSON.parse( | ||
brotliDecompressSync(Buffer.from(str, 'base64')).toString() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { BootstrapState, bootstrap } from '../bin'; | ||
import { brotliDecompressSync } from 'zlib'; | ||
import { argPrefix, compress, decompress } from './argv-payload'; | ||
|
||
const base64ConfigArg = process.argv[2]; | ||
const argPrefix = '--brotli-base64-config='; | ||
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; | ||
payload.parseArgvResult.restArgs = process.argv.slice(3); | ||
const state = decompress(base64Payload) as BootstrapState; | ||
|
||
bootstrap(payload); | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.