Skip to content

Commit

Permalink
Fix #1943 (#1952)
Browse files Browse the repository at this point in the history
* Fix #1943

* add regression test
  • Loading branch information
cspotcode committed Feb 7, 2023
1 parent 7fbc75e commit a376570
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bin.ts
Expand Up @@ -630,7 +630,7 @@ function phase4(payload: BootstrapState) {
if (executeEntrypoint) {
if (
payload.isInChildProcess &&
versionGteLt(process.versions.node, '18.6.0')
versionGteLt(process.versions.node, '18.6.0', '18.7.0')
) {
// HACK workaround node regression
require('../dist-raw/runmain-hack.js').run(entryPointPath);
Expand Down
11 changes: 9 additions & 2 deletions src/esm.ts
Expand Up @@ -6,7 +6,7 @@ import {
fileURLToPath,
pathToFileURL,
} from 'url';
import { extname } from 'path';
import { extname, resolve as pathResolve } from 'path';
import * as assert from 'assert';
import { normalizeSlashes, versionGteLt } from './util';
import { createRequire } from 'module';
Expand Down Expand Up @@ -137,12 +137,19 @@ export function createEsmHooks(tsNodeService: Service) {
return protocol === null || protocol === 'file:';
}

const runMainHackUrl = pathToFileURL(
pathResolve(__dirname, '../dist-raw/runmain-hack.js')
).toString();

/**
* Named "probably" as a reminder that this is a guess.
* node does not explicitly tell us if we're resolving the entrypoint or not.
*/
function isProbablyEntrypoint(specifier: string, parentURL: string) {
return parentURL === undefined && specifier.startsWith('file://');
return (
(parentURL === undefined || parentURL === runMainHackUrl) &&
specifier.startsWith('file://')
);
}
// Side-channel between `resolve()` and `load()` hooks
const rememberIsProbablyEntrypoint = new Set();
Expand Down
8 changes: 8 additions & 0 deletions src/test/esm-loader.spec.ts
Expand Up @@ -336,6 +336,14 @@ test.suite('esm', (test) => {
});
}

test('extensionless entrypoint, regression test for #1943', async (t) => {
const { err, stdout } = await exec(
`${BIN_ESM_PATH} ./esm-loader-entrypoint-cjs-fallback/extensionless-entrypoint`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});

test.suite('parent passes signals to child', (test) => {
signalTest('SIGINT');
signalTest('SIGTERM');
Expand Down

0 comments on commit a376570

Please sign in to comment.