Skip to content

fix: don't add configDir to modules when tsconfig has no baseUrl#587

Merged
alexander-akait merged 1 commit into
mainfrom
fix/tsconfig-no-baseurl-modules
May 19, 2026
Merged

fix: don't add configDir to modules when tsconfig has no baseUrl#587
alexander-akait merged 1 commit into
mainfrom
fix/tsconfig-no-baseurl-modules

Conversation

@xiaoxiaojx

@xiaoxiaojx xiaoxiaojx commented May 19, 2026

Copy link
Copy Markdown
Member

Summary

Fixes https://github.com/webpack/webpack/actions/runs/26091453506/job/76736654246?pr=20966

When tsconfig: true and _findTsconfigUpward walks up the directory tree to find a tsconfig.json without baseUrl, tsconfigPathsToResolveOptions was unconditionally adding the tsconfig's directory (configDir) to the resolver's modules list.

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 of node_modules/package/index.js.

Root cause

In tsconfigPathsToResolveOptions:

const absoluteBaseUrl = !baseUrl ? configDir : resolver.join(configDir, baseUrl);
// ...
if (absoluteBaseUrl && !modules.includes(absoluteBaseUrl)) {
    modules.push(absoluteBaseUrl);  // ← bug: adds configDir even without baseUrl
}

When baseUrl is undefined, absoluteBaseUrl falls back to configDir. The guard only checks truthiness, so it always adds it.

Fix

Only add absoluteBaseUrl to modules when baseUrl was explicitly set in the tsconfig:

if (baseUrl && absoluteBaseUrl && !modules.includes(absoluteBaseUrl)) {

Reproduction

A subdirectory with node_modules/package/index.js and a parent tsconfig.json (no baseUrl):

root/
├── tsconfig.json          # { "compilerOptions": { "target": "ES2017" } }
├── package.json           # { "name": "myproject" }
└── subdir/
    └── node_modules/
        └── package/
            ├── index.js   # module.exports = 42;
            └── package.json

Resolving "package" from subdir/ with tsconfig: true:

  • Before: resolves to root/package.json (wrong)
  • After: resolves to subdir/node_modules/package/index.js (correct)

This bug was discovered through a webpack test failure (ConfigTestCases › managedPaths › futureDefaults) where experiments.futureDefaults enables resolve.tsconfig: true.

Use of AI: Claude Code was used to investigate and draft this fix under human review.

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`.
@changeset-bot

changeset-bot Bot commented May 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 9adb0af

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.45%. Comparing base (e6f2158) to head (9adb0af).

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           
Flag Coverage Δ
integration 96.45% <100.00%> (ø)

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.

@alexander-akait alexander-akait merged commit 9ceca83 into main May 19, 2026
34 checks passed
@alexander-akait alexander-akait deleted the fix/tsconfig-no-baseurl-modules branch May 19, 2026 12:24
@codspeed-hq

codspeed-hq Bot commented May 19, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by ×5.7

⚠️ 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

⚡ 2 improved benchmarks
✅ 136 untouched benchmarks

Performance Changes

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)

Open in CodSpeed

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