Skip to content

fix: Resolve tsconfig path aliases like TypeScript does in monorepos - #213

Merged
bennycode merged 2 commits into
mainfrom
fix/monorepo-paths
Sep 29, 2026
Merged

bennycode merged 2 commits into
mainfrom
fix/monorepo-paths

Conversation

@bennycode

Copy link
Copy Markdown
Owner

Fixes #127

Path aliases were resolved relative to the folder of the converted project's tsconfig.json. TypeScript resolves them relative to baseUrl or, if that's missing, relative to the tsconfig.json that declares paths. In the Nx layout from the issue, an app's tsconfig extends the root config that holds the aliases. So @mylib/utils/helper was looked up under apps/myapp/libs/..., not found, and left without an extension.

The base folder now comes from baseUrl, or from TypeScript's pathsBasePath when there's no baseUrl. pathsBasePath is set by TypeScript's config parser but isn't in its public types, so it's read with an in check instead of a cast.

Two related bugs in the same code path showed up while fixing this:

  • An alias with several targets, like "@gen/*": ["./a/*", "./b/*"], was turned into one comma-joined path. Each target is now tried in order.
  • Wildcard patterns matched too much. @mylib/* matched the plain @mylib import, and @app/* would match @application/x. With the correct base folder this turned @mylib into @mylib/index.js. A wildcard pattern now has to match everything before the *, including the slash.

The getNormalizedPath unit test also ran its expect directly inside describe, which vitest reports as an empty suite. It now uses it blocks and covers multiple targets.

Path aliases were resolved relative to the directory of the converted
project's tsconfig.json. TypeScript resolves them relative to "baseUrl"
or, without it, relative to the tsconfig.json that declares "paths". In
a monorepo where an app's tsconfig extends a root config, aliased
imports like "@mylib/utils/helper" were looked up inside the app folder,
not found, and left without a file extension.

Two related issues in the same code path are fixed as well:

- An alias with several targets was joined into one comma-separated
  path. Each target is now tried in order.
- Wildcard patterns matched too much: "@mylib/*" matched "@MyLib" and
  "@app/*" matched "@application/x". The resolved base path exposed
  this, because "@MyLib" was turned into "@mylib/index.js".
# Conflicts:
#	src/util/replaceModulePath.ts
@bennycode
bennycode merged commit c1a5523 into main Sep 29, 2026
2 checks passed
@bennycode
bennycode deleted the fix/monorepo-paths branch September 29, 2026 14:19
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.

Support for tsconfig paths in monorepos

1 participant