Skip to content

fix(TsconfigPathsPlugin): resolve inherited paths like tsc - #710

Closed
bardrop wants to merge 2 commits into
webpack:mainfrom
bardrop:fix/tsconfig-extends-paths-base
Closed

bardrop wants to merge 2 commits into
webpack:mainfrom
bardrop:fix/tsconfig-extends-paths-base

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #705.

Two problems with configs loaded through extends in TsconfigPathsPlugin:

  1. paths without baseUrl were resolved relative to the root tsconfig. tsc resolves them relative to the config file that declares them (docs), so paths from ./configs/base.json or from a package config pointed to the wrong place.
    Now _loadTsconfig records the directory of the config that declares paths (pathsBasePath, the same name tsc uses internally). It travels with paths through mergeTsconfigs: a config that redefines paths also sets its own pathsBasePath. tsconfigPathsToResolveOptions uses it when there is no baseUrl. When there is a baseUrl (from any config), nothing changes: paths are still relative to baseUrl.
  2. ${configDir} in baseUrl was never substituted. In an extended config it was joined to that config's directory as a literal string. Now it is left as is when the extended config is loaded and substituted with the root config directory when the paths are built (the same as ${configDir} in paths).

I compared the result with ts.resolveModuleName (TypeScript 6.0.3, the version in devDependencies) on 12 setups: the 3 from the issue, root baseUrl + inherited paths, root paths overriding inherited paths, baseUrl + paths both in the base, a 2-level extends chain with paths in the middle config, ${configDir} in inherited paths, ${configDir} as an inherited baseUrl with relative paths, and an empty baseUrl ("") in the root config, in the base config, and in the base config next to paths. All 12 now match tsc. On main, 7 of them differ.

  1. Empty baseUrl: "baseUrl": "" is treated as set (the directory of its config), as tsc does. An inherited "" is rebased to its own config directory like any other inherited baseUrl (on main it was left empty and so meant the root directory).

Tests: new fixtures in test/fixtures/tsconfig-paths/extends-paths-base/ and 6 tests. The 3 cases from the issue and the inherited empty baseUrl fail on main. The root baseUrl and root empty baseUrl tests are guards and pass on both. test/tsconfig-paths.test.js passes (65), and npm run lint (eslint, tsc, prettier, cspell) is clean. Added a patch changeset.

Note: in my local run of the whole suite, the 18 "should resolve itself" tests in test/simple.test.js fail, but they fail the same way on main. They resolve ./enhanced-resolve/lib/index from the parent directory, and my clone's folder has a different name.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected inherited TypeScript path mapping resolution: mappings without a baseUrl resolve relative to the configuration that declares them.
    • Improved handling of ${configDir} and empty baseUrl values in extended configurations to align with TypeScript behavior.
  • Tests
    • Added coverage for inherited mappings and base URL resolution across extended configurations.

@changeset-bot

changeset-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: af9abbd

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The plugin now records the directory that declares inherited paths. When baseUrl is absent, it resolves path mappings from that directory. It also keeps ${configDir} templates until later substitution and treats empty baseUrl values as explicit values. The test suite adds synchronous regression coverage for extended configs, package configs, and empty or inherited baseUrl cases.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to af9ab

This change makes inherited tsconfig paths and ${configDir} resolve the way tsc does. No actionable merge-blocking risk remains in the reviewed material.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fixed component (TsconfigPathsPlugin) and the main behavior (resolving inherited paths like tsc). The changeset adds inherited-paths resolution relative to the declaring config, al…
Linked Issues check Passed [#705] Requirement 1: paths without baseUrl resolve relative to the config that declares them, including package configs. The summary of lib/TsconfigPathsPlugin.js shows that _loadTsconfig records pat…
Out of Scope Changes check Passed The pathsBasePath change, the deferred ${configDir} substitution in an extended baseUrl, and the tsconfigPathsToResolveOptions logic implement #705 directly. The fixtures and tests under test/fixtures…
Full details: Title check

Explanation

The title names the fixed component (TsconfigPathsPlugin) and the main behavior (resolving inherited paths like tsc). The changeset adds inherited-paths resolution relative to the declaring config, along with baseUrl handling, and the linked issue #705 matches this scope. The title is specific and matches the primary change.

Full details: Linked Issues check

Explanation

[#705] Requirement 1: paths without baseUrl resolve relative to the config that declares them, including package configs. The summary of lib/TsconfigPathsPlugin.js shows that _loadTsconfig records pathsBasePath for the declaring config, mergeTsconfigs carries it with paths, and tsconfigPathsToResolveOptions uses it when baseUrl is absent. The PR tests cover the file-relative case (test/fixtures/tsconfig-paths/extends-paths-base/relative). The package case uses the same extends path logic; its fixture under node_modules was excluded from review, so only the code path and the author's reported comparison support it. [#705] Requirement 2: ${configDir} in an inherited baseUrl is left unexpanded on load and substituted with the root config directory when options are built. Fixture config-dir-base-url covers this. [#705] Paths with a root baseUrl still resolve against that baseUrl, as tsc does. The incremental change since the previous review (empty baseUrl treated as set, with fixtures empty-base-url-root and empty-base-url-inherited) does not weaken either requirement. Limits: the whole-PR and incremental diffs could not be read in full. Test results (65 passing, lint clean) were reported by the author and were not re-run.

Full details: Out of Scope Changes check

Explanation

The pathsBasePath change, the deferred ${configDir} substitution in an extended baseUrl, and the tsconfigPathsToResolveOptions logic implement #705 directly. The fixtures and tests under test/fixtures/tsconfig-paths/extends-paths-base/ and test/tsconfig-paths.test.js support those behaviors. The empty baseUrl handling is not listed in #705, but the current PR description now documents it as item 3, with the tsc comparison. It uses the same baseUrl versus pathsBasePath decision that #705 requires, so it is a closely connected change and not unrelated work. The changeset is a patch note for the same behavior. The excluded node_modules package fixture was not inspected. The whole-PR diff was not fully readable, so the complete file set was checked only through the summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 05c15260-0164-43e4-a7d4-41e371170291
Commits

Reviewing files that changed from the base of the PR and between 9c66bd0 and af9abbd.

Files ignored due to path filters: 2

  • test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/base.json is excluded by !**/node_modules/**
  • test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/package.json is excluded by !**/node_modules/**
Files selected for processing: 2

  • lib/TsconfigPathsPlugin.js
  • test/tsconfig-paths.test.js

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9903a831-498c-4691-bb15-9388eaa6f750
Commits

Reviewing files that changed from the base of the PR and between ce269b9 and cf8073d.

Files ignored due to path filters: 2

  • test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/base.json is excluded by !**/node_modules/**
  • test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/package.json is excluded by !**/node_modules/**
Files selected for processing: 14

  • .changeset/tsconfig-extends-paths-base.md
  • lib/TsconfigPathsPlugin.js
  • test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/configs/base.json
  • test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/src/foo.ts
  • test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/tsconfig.json
  • test/fixtures/tsconfig-paths/extends-paths-base/package/src/foo.ts
  • test/fixtures/tsconfig-paths/extends-paths-base/package/tsconfig.json
  • test/fixtures/tsconfig-paths/extends-paths-base/relative/configs/base.json
  • test/fixtures/tsconfig-paths/extends-paths-base/relative/src/foo.ts
  • test/fixtures/tsconfig-paths/extends-paths-base/relative/tsconfig.json
  • test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/configs/base.json
  • test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/lib/foo.ts
  • test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/tsconfig.json
  • test/tsconfig-paths.test.js

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Comment thread lib/TsconfigPathsPlugin.js Resolved
paths declared in an extended config with no baseUrl were resolved
relative to the root tsconfig, so they missed when the extended config
lives in another directory or in a package. tsc resolves them relative
to the config that declares them, so remember that directory
(pathsBasePath, as tsc calls it) and use it when there is no baseUrl.

Also substitute ${configDir} in baseUrl. In an extended config it was
joined to the extended config's directory as a literal string.

Fixes #705
An empty baseUrl is the directory of the config that declares it, not
"no baseUrl". Keep using it in the root config (paths inherited from a
base config were resolved from the base config directory), and rebase
an inherited empty baseUrl to its own config directory.
@bardrop

bardrop commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main after #709 landed. The only conflict was both PRs adding a describe block at the end of test/tsconfig-paths.test.js, so I kept both. Tests and lint pass locally (same 18 "should resolve itself" failures as on main, because my clone folder isn't named enhanced-resolve).

@bardrop
bardrop force-pushed the fix/tsconfig-extends-paths-base branch from 9c66bd0 to af9abbd Compare November 12, 2026 10:03
alexander-akait added a commit that referenced this pull request Nov 12, 2026
#711)

**Summary**

Fixes #705.

#709 fixed `extends` lookup (#706), but configs loaded through `extends`
still resolve `paths` and `baseUrl` differently from tsc. This PR builds
on #710 by @bardrop. It applies its two commits onto current `main`,
since #710 now conflicts with #709 in `test/tsconfig-paths.test.js`. It
also adds two fixes:

- **#710 (commits `f70894d`, `5520785`)**: `paths` without `baseUrl`
resolve relative to the config that declares them (`pathsBasePath`).
`${configDir}` in an inherited `baseUrl` becomes the root config
directory. An inherited empty `baseUrl` becomes the directory of the
config that declares it.
- **Empty root `baseUrl` (`99da949`)**: `"baseUrl": ""` was used for
`paths` but never added to `modules`, so bare requests did not resolve
from it. This bug also exists on `main`.

  ```diff
- if (baseUrl && absoluteBaseUrl && !modules.includes(absoluteBaseUrl))
{
  + if (baseUrl !== undefined && !modules.includes(absoluteBaseUrl)) {
  ```

- **Generated types (`9ee297e`)**: regenerates
`types/TsconfigPathsPlugin.d.ts` for the new `pathsBasePath` JSDoc
property. Without it, CI's `build:types` check fails.

I compared enhanced-resolve with `ts.resolveModuleName` (TypeScript
6.0.3, `moduleResolution: bundler`) on 34 setups. They cover the #705
and #706 repros, multi-level and array `extends`, packages (`exports`,
the `tsconfig` field, scoped), `${configDir}`, empty `baseUrl`,
backslashes, and project references.

| Code                   | Setups that differ from tsc |
| ---------------------- | --------------------------- |
| `main`                 | 16 / 34                     |
| `main` + #710          | 1 / 34 (empty root `baseUrl`) |
| this PR                | 0 / 34                      |

The #705 repro, before and after:

```text
before: one @/foo -> Can't resolve '@/foo'   two @/foo -> Can't resolve '@/foo'   three foo -> Can't resolve 'foo'
after:  one @/foo -> src/foo.ts              two @/foo -> src/foo.ts              three foo -> src/foo.ts
```

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

fix

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

Yes. #710 adds 6 tests with fixtures in
`test/fixtures/tsconfig-paths/extends-paths-base/`. I added one test
that resolves `src/foo` through an empty root `baseUrl`; it fails
without `99da949` and passes with it. `npm run lint` is clean and `npm
run test:only` passes (1664 tests).

**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?**

n/a. The changeset from #710 is included.

**Use of AI**

Yes. I used Claude Code to apply #710 onto `main`, write a differential
script against tsc, find and fix the remaining empty-`baseUrl` case, add
its test, and regenerate the types. Lint and the full test suite were
run locally.

---
_Generated by [Claude
Code](https://claude.ai/code/session_013FhutR7wqtULZXURwFF5ER)_

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

## Summary by CodeRabbit

* **Bug Fixes**
* Corrected resolution of inherited TypeScript path aliases when
`baseUrl` is missing or empty.
* Improved handling of `${configDir}` in inherited `baseUrl` settings
and path resolution across extended configurations.
* Prevented one extended configuration from affecting subsequent loads
that share a filesystem cache.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Mradul Singh <msingh@dealmeridian.com>
Co-authored-by: Claude <noreply@anthropic.com>
@alexander-akait

Copy link
Copy Markdown
Member

Thanks for the PR, fixed in the main with more tests and small fixes, let’s close, feel free to feedback (better to open a new issue if you will find more bugs)

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.

TsconfigPathsPlugin: inherited paths and ${configDir} in baseUrl resolve against the wrong directory

2 participants