fix: don't add configDir to modules when tsconfig has no baseUrl#587
Conversation
When `tsconfig: true` walks up the directory tree and finds a tsconfig.json without `baseUrl`, `tsconfigPathsToResolveOptions` was unconditionally adding `configDir` (the tsconfig's directory) to the module search paths. This caused bare specifiers like `"package"` to incorrectly resolve to files at the tsconfig root (e.g. `package.json`) instead of the correct `node_modules` entry. Only add `absoluteBaseUrl` to modules when the tsconfig explicitly sets `baseUrl`.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #587 +/- ##
=======================================
Coverage 96.45% 96.45%
=======================================
Files 50 50
Lines 2964 2964
Branches 937 937
=======================================
Hits 2859 2859
Misses 89 89
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Merging this PR will improve performance by ×5.7
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Memory | prefer-absolute: absolute paths (warm) |
3.1 KB | 1.8 KB | +76.51% |
| ⚡ | Memory | deep-hierarchy: relative from 10-deep dir (warm) |
18.6 KB | 1 KB | ×18 |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing fix/tsconfig-no-baseurl-modules (9adb0af) with main (e6f2158)
Summary
Fixes https://github.com/webpack/webpack/actions/runs/26091453506/job/76736654246?pr=20966
When
tsconfig: trueand_findTsconfigUpwardwalks up the directory tree to find atsconfig.jsonwithoutbaseUrl,tsconfigPathsToResolveOptionswas unconditionally adding the tsconfig's directory (configDir) to the resolver'smoduleslist.This caused bare specifiers to incorrectly resolve against files in the tsconfig root directory. For example,
import value from "package"would resolve to<tsconfig_root>/package.json(a JSON module) instead ofnode_modules/package/index.js.Root cause
In
tsconfigPathsToResolveOptions:When
baseUrlisundefined,absoluteBaseUrlfalls back toconfigDir. The guard only checks truthiness, so it always adds it.Fix
Only add
absoluteBaseUrltomoduleswhenbaseUrlwas explicitly set in the tsconfig:Reproduction
A subdirectory with
node_modules/package/index.jsand a parenttsconfig.json(nobaseUrl):Resolving
"package"fromsubdir/withtsconfig: true:root/package.json(wrong)subdir/node_modules/package/index.js(correct)This bug was discovered through a webpack test failure (
ConfigTestCases › managedPaths › futureDefaults) whereexperiments.futureDefaultsenablesresolve.tsconfig: true.Use of AI: Claude Code was used to investigate and draft this fix under human review.