Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-tsconfig-paths-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"enhanced-resolve": patch
---

Fix TsconfigPathsPlugin circular project references causing stack overflow, add support for extending from unscoped npm packages, and use `stat` instead of `readFile` for existence checks in extends resolution.
26 changes: 25 additions & 1 deletion lib/TsconfigPathsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ function mergeTsconfigs(base, config) {
* @returns {string} the path with substituted template
*/
function substituteConfigDir(pathValue, configDir) {
// eslint-disable-next-line no-template-curly-in-string
if (!pathValue.includes("${configDir}")) return pathValue;
return pathValue.replace(/\$\{configDir\}/g, configDir);
}

Expand Down Expand Up @@ -492,7 +494,7 @@ module.exports = class TsconfigPathsPlugin {
extendedConfigValue,
);

fileSystem.readFile(initialExtendedConfigPath, (existsErr) => {
fileSystem.stat(initialExtendedConfigPath, (existsErr) => {
let extendedConfigPath = initialExtendedConfigPath;
if (existsErr) {
// Handle scoped package extends like "@scope/name" (no sub-path):
Expand All @@ -518,6 +520,18 @@ module.exports = class TsconfigPathsPlugin {
currentDir,
normalize(`node_modules/${extendedConfigValue}`),
);
} else if (
!originalExtendedConfigValue.startsWith(".") &&
!originalExtendedConfigValue.startsWith("/")
) {
// Handle unscoped package extends like "my-base-config" (no sub-path):
// "my-base-config" should resolve to node_modules/my-base-config/tsconfig.json
extendedConfigPath = resolver.join(
currentDir,
normalize(
`node_modules/${originalExtendedConfigValue}/${DEFAULT_CONFIG_FILE}`,
),
);
}
}

Expand Down Expand Up @@ -561,6 +575,7 @@ module.exports = class TsconfigPathsPlugin {
* @param {Set<string>} fileDependencies the file dependencies
* @param {{ [baseUrl: string]: TsconfigPathsData }} referenceMatchMap the map to populate
* @param {(err: Error | null) => void} callback callback
* @param {Set<string>=} visitedRefPaths visited reference config paths (for circular reference detection)
* @returns {void}
*/
_loadTsconfigReferences(
Expand All @@ -570,9 +585,11 @@ module.exports = class TsconfigPathsPlugin {
fileDependencies,
referenceMatchMap,
callback,
visitedRefPaths,
) {
if (references.length === 0) return callback(null);

const visited = visitedRefPaths || new Set();
let pending = references.length;
const finishOne = () => {
if (--pending === 0) callback(null);
Expand All @@ -585,6 +602,12 @@ module.exports = class TsconfigPathsPlugin {
DEFAULT_CONFIG_FILE,
);

if (visited.has(refConfigPath)) {
finishOne();
continue;
}
visited.add(refConfigPath);

this._loadTsconfig(
resolver,
refConfigPath,
Expand Down Expand Up @@ -615,6 +638,7 @@ module.exports = class TsconfigPathsPlugin {
fileDependencies,
referenceMatchMap,
finishOne,
visited,
);
} else {
finishOne();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "my-base-config"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = "a";
10 changes: 10 additions & 0 deletions test/fixtures/tsconfig-paths/references-circular/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@a/*": ["./src/*"]
},
"composite": true
},
"references": [{ "path": "../b" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = "b";
10 changes: 10 additions & 0 deletions test/fixtures/tsconfig-paths/references-circular/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@b/*": ["./src/*"]
},
"composite": true
},
"references": [{ "path": "../a" }]
}
92 changes: 92 additions & 0 deletions test/tsconfig-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const referencesProjectDir = path.resolve(
"tsconfig-paths",
"references-project",
);
const referencesCircularDir = path.resolve(
__dirname,
"fixtures",
"tsconfig-paths",
"references-circular",
);
const extendsUnscopedPkgDir = path.resolve(
__dirname,
"fixtures",
"tsconfig-paths",
"extends-unscoped-pkg",
);

describe("TsconfigPathsPlugin", () => {
it("resolves exact mapped path '@components/*' via tsconfig option (example)", (done) => {
Expand Down Expand Up @@ -1330,4 +1342,84 @@ describe("TsconfigPathsPlugin", () => {
});
});
});

describe("bug: circular project references should not cause infinite recursion", () => {
it("should handle circular references without hanging or crashing", (done) => {
const aDir = path.join(referencesCircularDir, "a");
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
mainFiles: ["index"],
tsconfig: {
configFile: path.join(aDir, "tsconfig.json"),
references: "auto",
},
});

// a references b, b references a — should not stack overflow
resolver.resolve({}, aDir, "@a/index", {}, (err, result) => {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(path.join(aDir, "src", "index.ts"));
done();
});
});

it("should resolve paths from a circular-referenced project", (done) => {
const aDir = path.join(referencesCircularDir, "a");
const bDir = path.join(referencesCircularDir, "b");
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
mainFiles: ["index"],
tsconfig: {
configFile: path.join(aDir, "tsconfig.json"),
references: "auto",
},
});

// From b's context, @b/* should resolve via b's own paths
resolver.resolve({}, bDir, "@b/index", {}, (err, result) => {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(path.join(bDir, "src", "index.ts"));
done();
});
});
});

describe("bug: unscoped npm package in extends field", () => {
it("should resolve paths inherited from an unscoped npm package tsconfig (extends 'my-base-config')", (done) => {
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
mainFiles: ["index"],
tsconfig: path.join(extendsUnscopedPkgDir, "tsconfig.json"),
});

resolver.resolve(
{},
extendsUnscopedPkgDir,
"@pkg/util",
{},
(err, result) => {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(
path.join(
extendsUnscopedPkgDir,
"node_modules",
"my-base-config",
"src",
"util.ts",
),
);
done();
},
);
});
});
});
Loading