Skip to content

fix: TsconfigPathsPlugin circular references, unscoped pkg extends, and perf - #575

Merged
alexander-akait merged 1 commit into
mainfrom
fix/tsconfig-paths-plugin-bugs
May 8, 2026
Merged

alexander-akait merged 1 commit into
mainfrom
fix/tsconfig-paths-plugin-bugs

Conversation

@xiaoxiaojx

Copy link
Copy Markdown
Member

Summary

Fix multiple bugs and performance issues in TsconfigPathsPlugin:

  • Circular project references → stack overflow: When project references form a cycle (A references B, B references A) with references: "auto", _loadTsconfigReferences recurses infinitely. Fixed by tracking visited reference config paths in a Set and skipping already-visited ones.
  • Unscoped npm package extends not resolved: extends: "my-base-config" (unscoped package, no sub-path) failed to resolve to node_modules/my-base-config/tsconfig.json. The existing fallback logic only handled scoped packages (@scope/name) and sub-path packages (pkg/tsconfig). Added an else branch for unscoped packages.
  • readFile used for existence check: _loadTsconfigFromExtends called readFile just to check if a path exists, discarding the buffer. Changed to stat which avoids reading the entire file content unnecessarily.
  • substituteConfigDir regex on every call: The regex replacement ran even when paths contained no ${configDir}. Added a fast includes check to skip the regex when not needed.

What kind of change does this PR introduce?

fix, perf

Did you add tests for your changes?

Yes. Added 3 new test cases:

  • test/tsconfig-paths.test.js: "should handle circular references without hanging or crashing"
  • test/tsconfig-paths.test.js: "should resolve paths from a circular-referenced project"
  • test/tsconfig-paths.test.js: "should resolve paths inherited from an unscoped npm package tsconfig"

Test fixtures added:

  • test/fixtures/tsconfig-paths/references-circular/ — A↔B circular reference project
  • test/fixtures/tsconfig-paths/extends-unscoped-pkg/ — unscoped npm package extends

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

No documentation changes needed.

Use of AI

This PR was authored with the assistance of Claude Code (Claude Opus 4.6). The bugs were identified through code review, test cases were written first to reproduce, then fixes were applied and verified.

…nd perf improvements

- Fix infinite recursion when project references form a cycle (A→B→A)
  by tracking visited reference paths
- Fix `extends: "unscoped-pkg"` failing to resolve to
  node_modules/unscoped-pkg/tsconfig.json
- Use `stat` instead of `readFile` for existence checks in extends
  resolution to avoid reading entire file contents unnecessarily
- Skip regex in `substituteConfigDir` when path has no `${configDir}`
@changeset-bot

changeset-bot Bot commented May 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 510978b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
enhanced-resolve Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov

codecov Bot commented May 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.56%. Comparing base (292aee5) to head (510978b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #575      +/-   ##
==========================================
+ Coverage   96.54%   96.56%   +0.01%     
==========================================
  Files          50       50              
  Lines        2927     2937      +10     
  Branches      922      928       +6     
==========================================
+ Hits         2826     2836      +10     
  Misses         85       85              
  Partials       16       16              
Flag Coverage Δ
integration 96.56% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented May 8, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 94.56%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 134 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Memory resolve-to-context: directory resolve (warm) 96.6 KB 1.8 KB ×54
⚡ Memory tsconfig-paths: 5 path prefixes (warm) 101.9 KB 1.8 KB ×58
❌ Memory deep-hierarchy: relative from 10-deep dir (warm) 1 KB 18.6 KB -94.56%
⚡ Simulation cache-predicate: mixed cached/uncached requests (warm) 4 ms 2 ms ×2

Comparing fix/tsconfig-paths-plugin-bugs (510978b) with main (292aee5)

Open in CodSpeed

@alexander-akait
alexander-akait merged commit f28d8b9 into main May 8, 2026
34 of 35 checks passed
@alexander-akait
alexander-akait deleted the fix/tsconfig-paths-plugin-bugs branch May 8, 2026 08:24
alexander-akait pushed a commit that referenced this pull request Nov 12, 2026
<!-- Thanks for submitting a pull request! Please provide enough
information so that others can review your pull request. -->

**Summary**

<!-- Explain the **motivation** for making this change. What existing
problem does the pull request solve? -->
<!-- Try to link to an open issue for more information. -->
<!-- Any other information related to changes. -->

Fixes #706.

`TsconfigPathsPlugin` builds `extends` paths by hand. It appends `.json`
to any value without `.json` in it, and maps package names to
`node_modules` paths itself. tsc treats `./`, `../` and absolute values
as files (`.json` optional) and resolves everything else like a
`require`. So four forms that tsc accepts fail the whole tsconfig,
including the project's own `paths`.

This keeps the file branch and resolves everything else with a child
resolver set up like tsc's config lookup: `exports` with the
`require`/`types`/`node` conditions, the `tsconfig` field, then
`tsconfig.json`. That replaces the package special cases added in #483,
#575 and #631. Like tsc, backslashes are normalized first. When the
lookup fails, a bare file name such as `"tsconfig.base.json"` still
loads from the config's directory, as it always has here, though tsc
rejects it.

Resolving `@/foo` with the #706 repro, where tsc gives `src/foo.ts` for
all four:

| `extends` | Before | After |
| ---------------------------------- |
------------------------------------------ | ------------ |
| `"cfgpkg/strict"` via `exports` | `ENOENT …
node_modules/cfgpkg/strict.json` | `src/foo.ts` |
| `"cfgpkg"` with a `tsconfig` field | `ENOENT …
node_modules/cfgpkg/tsconfig.json` | `src/foo.ts` |
| `"./configs/base.tsconfig"` | `ENOENT … configs/base.tsconfig.json` |
`src/foo.ts` |
| `".."` | `ENOENT … dotdot/pkg/...json` | `src/foo.ts` |

<!-- In addition to that please answer these questions: -->

**What kind of change does this PR introduce?**

<!-- E.g. a fix, feat, refactor, perf, test, chore, ci, build, style,
revert, docs or describe it if you did not find a suitable kind of
change. -->

fix

**Did you add tests for your changes?**

<!-- Please note: in most cases, if you change the code, we will not
merge your changes unless you add tests. -->

Yes: five cases in `test/tsconfig-paths.test.js`, with fixtures in
`test/fixtures/tsconfig-paths/extends-resolution/`. The four #706 forms
fail on `main`. The `..` case also checks that a sibling `<dir>.json`
doesn't win. The bare file name case passes on `main` and guards the
kept fallback.

**Does this PR introduce a breaking change?**

<!-- If this PR introduces a breaking change, please describe the impact
and a migration path for existing applications. -->

No.

**If relevant, what needs to be documented once your changes are merged
or what have you already documented?**

<!-- List all the information that needs to be added to the
documentation after merge that has already been documented in this PR.
-->

n/a

**Use of AI**

<!-- If you have used AI, please state so here. Explain how you used it.
Make sure to read our AI policy
(https://github.com/webpack/governance/blob/main/AI_POLICY.md) or your
Pull Request may be closed due to irresponsible use of AI. -->

Yes. I used Claude Code to write the change and the tests. I reviewed
every line, checked each case against tsc's `getExtendsConfigPath`, and
ran `npm run lint` and the full test suite locally.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* TypeScript configuration inheritance now follows TypeScript-style path
resolution, including package exports, package configuration fields,
extensionless config files, and parent-directory references.
* Unsupported bare filenames are no longer treated as valid
configuration paths.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants