Skip to content

Commit 257bbbb

Browse files
fix: avoid redundant filesystem probes during module resolution
Fixes #809 ## Problem Since v4.21.1, projects with many local imports start 2–4x slower. Resolving a TypeScript specifier generated doomed extension candidates (e.g. `./file.ts` → `file.ts.ts`) and probed each one; every miss triggered a full failed resolution plus Node's eager CommonJS-hint decoration, which re-enters tsx's patched CJS resolver. The sync-hooks path (v4.21.1+, Node ≥24.11.1) runs that cascade on the main thread. ## Changes - `mapTsExtensions()` no longer generates guaranteed-miss candidates: verbatim TS extensions (`.ts`/`.tsx`/`.mts`/`.cts`) resolve as-is, and swap-list extensions (`.js`/`.jsx`/`.cjs`/`.mjs`) drop appended-extension guesses. Extension-less specifiers still search. - ESM and CJS resolvers skip candidates that provably don't exist (one `existsSync`) instead of a full failed resolution + error decoration. Resolution results are unchanged except for pathological doubled-extension files (`x.js.ts`), aligning tsx with esbuild/tsc. Covered by `tests/specs/resolution-priority.ts`. esm-ts benchmark, 1000 modules (Node 24.15, M5 Pro): this PR 804ms vs 4.21.0 1412ms vs 4.21.1 3775ms.
1 parent c178197 commit 257bbbb

7 files changed

Lines changed: 253 additions & 6 deletions

File tree

‎src/cjs/api/module-resolve-filename/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const createResolveFilename = (
8787

8888
nextResolveSimple = createTsExtensionResolver(
8989
nextResolveSimple,
90+
parent?.path ?? undefined,
9091
Boolean(
9192
// If register.namespace is used (e.g. tsx.require())
9293
namespace

‎src/cjs/api/module-resolve-filename/resolve-ts-extensions.ts‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
1+
import path from 'node:path';
2+
import { existsSync } from 'node:fs';
13
import { mapTsExtensions } from '../../../utils/map-ts-extensions.js';
24
import type { NodeError } from '../../../types.js';
35
import {
46
isFilePath,
7+
isRelativePath,
58
isDirectoryPattern,
69
} from '../../../utils/path-utils.js';
710
import type { SimpleResolve } from '../types.js';
811
import { logCjs as log } from '../../../utils/debug.js';
912

13+
/**
14+
* Failed resolutions are expensive: Module._findPath stats several
15+
* implicit extension & index variants before constructing a
16+
* MODULE_NOT_FOUND error (https://github.com/privatenumber/tsx/issues/809)
17+
*
18+
* Skip candidates that can be cheaply confirmed to not exist
19+
*/
20+
const candidateDoesntExist = (
21+
candidate: string,
22+
parentPath: string | undefined,
23+
) => {
24+
let filePath: string | undefined;
25+
if (path.isAbsolute(candidate)) {
26+
filePath = candidate;
27+
} else if (isRelativePath(candidate) && parentPath) {
28+
filePath = path.resolve(parentPath, candidate);
29+
}
30+
return filePath !== undefined && !existsSync(filePath);
31+
};
32+
1033
/**
1134
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
1235
*/
1336
const resolveTsFilename = (
1437
resolve: SimpleResolve,
1538
request: string,
39+
parentPath: string | undefined,
1640
isTsParent: boolean,
1741
allowJs: boolean,
1842
) => {
@@ -35,6 +59,10 @@ const resolveTsFilename = (
3559
}
3660

3761
for (const tryTsPath of tsPath) {
62+
if (candidateDoesntExist(tryTsPath, parentPath)) {
63+
continue;
64+
}
65+
3866
try {
3967
return resolve(tryTsPath);
4068
} catch (error) {
@@ -51,6 +79,7 @@ const resolveTsFilename = (
5179

5280
export const createTsExtensionResolver = (
5381
nextResolve: SimpleResolve,
82+
parentPath: string | undefined,
5483
isTsParent: boolean,
5584
allowJs: boolean,
5685
): SimpleResolve => (
@@ -64,7 +93,13 @@ export const createTsExtensionResolver = (
6493

6594
// It should only try to resolve TS extensions first if it's a local file (non dependency)
6695
if (isFilePath(request)) {
67-
const resolvedTsFilename = resolveTsFilename(nextResolve, request, isTsParent, allowJs);
96+
const resolvedTsFilename = resolveTsFilename(
97+
nextResolve,
98+
request,
99+
parentPath,
100+
isTsParent,
101+
allowJs,
102+
);
68103
if (resolvedTsFilename) {
69104
return resolvedTsFilename;
70105
}
@@ -81,7 +116,13 @@ export const createTsExtensionResolver = (
81116
const isExportsPath = nodeError.message.match(/^Cannot find module '([^']+)'$/);
82117
if (isExportsPath) {
83118
const exportsPath = isExportsPath[1];
84-
const tsFilename = resolveTsFilename(nextResolve, exportsPath, isTsParent, allowJs);
119+
const tsFilename = resolveTsFilename(
120+
nextResolve,
121+
exportsPath,
122+
parentPath,
123+
isTsParent,
124+
allowJs,
125+
);
85126
if (tsFilename) {
86127
return tsFilename;
87128
}
@@ -90,14 +131,26 @@ export const createTsExtensionResolver = (
90131
const isMainPath = nodeError.message.match(/^Cannot find module '([^']+)'. Please verify that the package.json has a valid "main" entry$/);
91132
if (isMainPath) {
92133
const mainPath = isMainPath[1];
93-
const tsFilename = resolveTsFilename(nextResolve, mainPath, isTsParent, allowJs);
134+
const tsFilename = resolveTsFilename(
135+
nextResolve,
136+
mainPath,
137+
parentPath,
138+
isTsParent,
139+
allowJs,
140+
);
94141
if (tsFilename) {
95142
return tsFilename;
96143
}
97144
}
98145
}
99146

100-
const resolvedTsFilename = resolveTsFilename(nextResolve, request, isTsParent, allowJs);
147+
const resolvedTsFilename = resolveTsFilename(
148+
nextResolve,
149+
request,
150+
parentPath,
151+
isTsParent,
152+
allowJs,
153+
);
101154
if (resolvedTsFilename) {
102155
return resolvedTsFilename;
103156
}

‎src/esm/hook/resolve.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { existsSync } from 'node:fs';
23
import { fileURLToPath, pathToFileURL } from 'node:url';
34
import type {
45
ResolveHook,
@@ -103,6 +104,45 @@ const isCommonJsRequireContext = (
103104
&& !context.conditions.includes('import')
104105
);
105106

107+
/**
108+
* Maps a candidate specifier to a file path if it can be statted directly.
109+
* Returns undefined when existence can't be cheaply determined
110+
* (e.g. bare specifiers), in which case the candidate must be probed
111+
* via nextResolve()
112+
*/
113+
const getProbeFilePath = (
114+
candidate: string,
115+
parentURL: string | undefined,
116+
) => {
117+
const [pathname] = candidate.split('?');
118+
try {
119+
if (pathname.startsWith(fileUrlPrefix)) {
120+
return fileURLToPath(pathname);
121+
}
122+
if (path.isAbsolute(pathname)) {
123+
return pathname;
124+
}
125+
if (isRelativePath(pathname) && parentURL?.startsWith(fileUrlPrefix)) {
126+
return fileURLToPath(new URL(pathname, parentURL));
127+
}
128+
} catch {}
129+
};
130+
131+
/**
132+
* Failed resolutions are expensive: Node constructs an ERR_MODULE_NOT_FOUND
133+
* and decorates it with a CommonJS resolution hint, which re-enters the
134+
* (tsx-patched) CJS resolver (https://github.com/privatenumber/tsx/issues/809)
135+
*
136+
* Skip candidates that can be cheaply confirmed to not exist
137+
*/
138+
const candidateDoesntExist = (
139+
candidate: string,
140+
parentURL: string | undefined,
141+
) => {
142+
const filePath = getProbeFilePath(candidate, parentURL);
143+
return filePath !== undefined && !existsSync(filePath);
144+
};
145+
106146
const resolveExtensions = async (
107147
url: string,
108148
context: ResolveHookContext,
@@ -122,6 +162,10 @@ const resolveExtensions = async (
122162

123163
let caughtError: unknown;
124164
for (const tsPath of tryPaths) {
165+
if (candidateDoesntExist(tsPath, context.parentURL)) {
166+
continue;
167+
}
168+
125169
try {
126170
return await nextResolve(tsPath, context);
127171
} catch (error) {
@@ -138,6 +182,10 @@ const resolveExtensions = async (
138182
}
139183

140184
if (throwError) {
185+
if (caughtError === undefined) {
186+
// All candidates were skipped; resolve one to produce a real error
187+
return nextResolve(tryPaths[0]!, context);
188+
}
141189
throw caughtError;
142190
}
143191
};
@@ -161,6 +209,10 @@ const resolveExtensionsSync = (
161209

162210
let caughtError: unknown;
163211
for (const tsPath of tryPaths) {
212+
if (candidateDoesntExist(tsPath, context.parentURL)) {
213+
continue;
214+
}
215+
164216
try {
165217
return nextResolve(tsPath, context);
166218
} catch (error) {
@@ -177,6 +229,10 @@ const resolveExtensionsSync = (
177229
}
178230

179231
if (throwError) {
232+
if (caughtError === undefined) {
233+
// All candidates were skipped; resolve one to produce a real error
234+
return nextResolve(tryPaths[0]!, context);
235+
}
180236
throw caughtError;
181237
}
182238
};

‎src/utils/map-ts-extensions.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ tsExtensions['.jsx'] = ['.tsx', '.ts', '.jsx', '.js'];
2222
tsExtensions['.cjs'] = ['.cts'];
2323
tsExtensions['.mjs'] = ['.mts'];
2424

25+
/**
26+
* TypeScript resolves these extensions verbatim, so there are no
27+
* alternative paths to try. Guessing appended extensions for them
28+
* (e.g. `file.ts` -> `file.ts.ts`) only produces misses, and each
29+
* miss is expensive: Node decorates ERR_MODULE_NOT_FOUND with a
30+
* CommonJS resolution hint, which re-enters the (tsx-patched)
31+
* CJS resolver (https://github.com/privatenumber/tsx/issues/809)
32+
*
33+
* This mirrors esbuild's model, which separates "TypeScript's file
34+
* extension swapping" (`.js` -> `.ts`, only for known JS extensions)
35+
* from "node's implicit file extension searching" (appending to an
36+
* extension-less path). Neither appends onto an existing extension,
37+
* so neither produces `file.ts.ts`.
38+
* https://github.com/evanw/esbuild/issues/3201
39+
*/
40+
const verbatimExtensions = new Set(['.ts', '.tsx', '.mts', '.cts']);
41+
2542
export const mapTsExtensions = (
2643
filePath: string,
2744
) => {
@@ -30,6 +47,10 @@ export const mapTsExtensions = (
3047
const [pathname] = splitPath;
3148
const extension = path.extname(pathname);
3249

50+
if (verbatimExtensions.has(extension)) {
51+
return;
52+
}
53+
3354
const tryPaths: string[] = [];
3455

3556
const tryExtensions = tsExtensions[extension];
@@ -45,6 +66,8 @@ export const mapTsExtensions = (
4566
),
4667
),
4768
);
69+
70+
return tryPaths;
4871
}
4972

5073
const guessExtensions = (

‎tests/fixtures.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ export const files = {
220220
'index.js': 'throw new Error("should not be loaded")',
221221
},
222222

223-
// TODO: test resolution priority for files 'index.tsx` & 'index.tsx.ts` via 'index.tsx'
224-
225223
'jsx/index.jsx': sourcemap.tag`
226224
import assert from 'assert';
227225
export const cjsContext = ${cjsContextCheck};

‎tests/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { transformSpec } from './specs/transform';
1313
import { commonJsModeContracts } from './specs/commonjs-mode-contracts';
1414
import { nodeCapabilitiesSpec } from './specs/node-capabilities';
1515
import { versionSensitiveTests } from './specs/version-sensitive';
16+
import { resolutionPriority } from './specs/resolution-priority';
1617

1718
(async () => {
1819
// Prevent stuck CI runs
@@ -34,6 +35,7 @@ import { versionSensitiveTests } from './specs/version-sensitive';
3435
await versionSensitiveTests(primaryNode);
3536
await commonJsModeContracts(primaryNode);
3637
await smoke(primaryNode);
38+
await resolutionPriority(primaryNode);
3739
await api(primaryNode);
3840
await cli(primaryNode);
3941
await watch(primaryNode);

0 commit comments

Comments
 (0)