Skip to content

Commit ec533b6

Browse files
authored
fix: TsconfigPathsPlugin walks up parent directories when tsconfig is true (#585)
1 parent 9e1c1e1 commit ec533b6

6 files changed

Lines changed: 139 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enhanced-resolve": patch
3+
---
4+
5+
When `tsconfig: true` is used, walk up parent directories to find `tsconfig.json`, matching TypeScript's own `findConfigFile` behavior.

‎lib/TsconfigPathsPlugin.js‎

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,39 @@ module.exports = class TsconfigPathsPlugin {
337337
return callback(null, cached);
338338
}
339339

340+
if (this.isAutoConfigFile) {
341+
this._findTsconfigUpward(
342+
resolver,
343+
request.path || process.cwd(),
344+
(err, result) => {
345+
if (err) {
346+
request.tsconfigPathsMap = null;
347+
return callback(err);
348+
}
349+
if (!result) {
350+
request.tsconfigPathsMap = null;
351+
return callback(null, null);
352+
}
353+
const map = /** @type {TsconfigPathsMap} */ (result);
354+
request.tsconfigPathsMap = map;
355+
if (resolveContext.fileDependencies) {
356+
for (const fileDependency of map.fileDependencies) {
357+
resolveContext.fileDependencies.add(fileDependency);
358+
}
359+
}
360+
callback(null, map);
361+
},
362+
);
363+
return;
364+
}
365+
340366
const absTsconfigPath = resolver.join(
341367
request.path || process.cwd(),
342368
this.configFile,
343369
);
344370
this._loadTsconfigPathsMap(resolver, absTsconfigPath, (err, result) => {
345371
if (err) {
346372
request.tsconfigPathsMap = null;
347-
if (
348-
this.isAutoConfigFile &&
349-
/** @type {NodeJS.ErrnoException} */ (err).code === "ENOENT"
350-
) {
351-
return callback(null, null);
352-
}
353373
return callback(err);
354374
}
355375

@@ -364,6 +384,45 @@ module.exports = class TsconfigPathsPlugin {
364384
});
365385
}
366386

387+
/**
388+
* Walk up from startDir to the filesystem root looking for tsconfig.json.
389+
* Like TypeScript's own `findConfigFile` / `forEachAncestorDirectory`.
390+
* @param {Resolver} resolver the resolver
391+
* @param {string} startDir the directory to start searching from
392+
* @param {(err: Error | null, result?: TsconfigPathsMap | null) => void} callback the callback
393+
* @returns {void}
394+
*/
395+
_findTsconfigUpward(resolver, startDir, callback) {
396+
const { fileSystem } = resolver;
397+
const configFileName = this.configFile;
398+
399+
/**
400+
* @param {string} dir current directory
401+
*/
402+
const check = (dir) => {
403+
const candidate = resolver.join(dir, configFileName);
404+
fileSystem.stat(candidate, (statErr) => {
405+
if (!statErr) {
406+
// Found — load it
407+
this._loadTsconfigPathsMap(resolver, candidate, (loadErr, result) => {
408+
if (loadErr) return callback(loadErr);
409+
callback(null, result);
410+
});
411+
return;
412+
}
413+
// Not found — move to parent
414+
const parentDir = resolver.dirname(dir);
415+
if (parentDir === dir) {
416+
// Reached filesystem root, no tsconfig.json found
417+
return callback(null, null);
418+
}
419+
check(parentDir);
420+
});
421+
};
422+
423+
check(startDir);
424+
}
425+
367426
/**
368427
* Load tsconfig.json and build complete TsconfigPathsMap
369428
* Includes main project paths and all referenced projects
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { helper } from "@utils/helper";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const helper = "helper";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@utils/*": ["./src/utils/*"]
6+
}
7+
}
8+
}

‎test/tsconfig-paths.test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,65 @@ describe("TsconfigPathsPlugin", () => {
12681268
});
12691269
});
12701270

1271+
describe("tsconfig: true should walk up parent directories", () => {
1272+
const upwardDir = path.resolve(
1273+
__dirname,
1274+
"fixtures",
1275+
"tsconfig-paths",
1276+
"upward-traversal",
1277+
);
1278+
1279+
it("should find tsconfig.json in parent directory when resolving from subdirectory", (done) => {
1280+
const resolver = ResolverFactory.createResolver({
1281+
fileSystem,
1282+
extensions: [".ts", ".tsx"],
1283+
mainFields: ["browser", "main"],
1284+
mainFiles: ["index"],
1285+
tsconfig: true,
1286+
});
1287+
1288+
// resolve from src/app/ — tsconfig.json is in upward-traversal/ (parent's parent)
1289+
resolver.resolve(
1290+
{},
1291+
path.join(upwardDir, "src", "app"),
1292+
"@utils/helper",
1293+
{},
1294+
(err, result) => {
1295+
if (err) return done(err);
1296+
if (!result) return done(new Error("No result"));
1297+
expect(result).toEqual(
1298+
path.join(upwardDir, "src", "utils", "helper.ts"),
1299+
);
1300+
done();
1301+
},
1302+
);
1303+
});
1304+
1305+
it("should still fall through when no tsconfig.json exists anywhere up", (done) => {
1306+
const noTsconfigDir = path.resolve(
1307+
__dirname,
1308+
"fixtures",
1309+
"tsconfig-paths",
1310+
"no-tsconfig",
1311+
);
1312+
1313+
const resolver = ResolverFactory.createResolver({
1314+
fileSystem,
1315+
extensions: [".ts", ".tsx"],
1316+
mainFields: ["browser", "main"],
1317+
mainFiles: ["index"],
1318+
tsconfig: true,
1319+
});
1320+
1321+
resolver.resolve({}, noTsconfigDir, "./src/index", {}, (err, result) => {
1322+
if (err) return done(err);
1323+
if (!result) return done(new Error("No result"));
1324+
expect(result).toEqual(path.join(noTsconfigDir, "src", "index.ts"));
1325+
done();
1326+
});
1327+
});
1328+
});
1329+
12711330
describe("JSONC support (comments in tsconfig.json)", () => {
12721331
const jsoncExampleDir = path.resolve(
12731332
__dirname,

0 commit comments

Comments
 (0)