Skip to content

Commit e1b7b73

Browse files
yharaskrikFrozenPandaz
authored andcommitted
fix(core): use workspace root for path resolution when baseUrl is not set (#34453)
## Current Behavior When a project-level `tsconfig.json` (e.g., `apps/aurora/tsconfig.json`) inherits `paths` via `extends` from `tsconfig.base.json` at the workspace root and no explicit `baseUrl` is set, Nx incorrectly resolves `./`-prefixed path mappings relative to the project tsconfig directory instead of the workspace root where the paths were defined. This causes errors when loading TypeScript config files (e.g., `rspack.config.ts`) that import workspace libraries using path aliases: NX Cannot find module './libs/plugins/rspack/src' `@swc-node/register`'s `readDefaultTsConfig` auto-sets `baseUrl` to `dirname(tsConfigPath)` (the project directory) when not explicitly configured, causing SWC to rewrite imports to incorrect relative paths during transpilation. ## Expected Behavior Path aliases defined in `tsconfig.base.json` (e.g., `"@trellis/plugins/rspack": ["./libs/plugins/rspack/src/index.ts"]`) should resolve relative to the workspace root when no `baseUrl` is configured. This is needed so that when using `tsgo` and needing to prefix all paths with `./` (no more `baseUrl` allowed) the paths are still resolved from the right spot. I tested this fix against our codebase on the branch I was trying to switch to tsgo on and it seemed to work. ## Related Issue(s) Fixes https://discord.com/channels/1143497901675401286/1471627045694865581 (cherry picked from commit a0e3455)
1 parent 5e4bbf9 commit e1b7b73

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

packages/nx/src/plugins/js/utils/register.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { join, sep } from 'path';
22
import type { TsConfigOptions } from 'ts-node';
33
import type { CompilerOptions } from 'typescript';
44
import { logger, NX_PREFIX, stripIndent } from '../../../utils/logger';
5+
import { workspaceRoot } from '../../../utils/workspace-root';
56
import { readTsConfigWithoutFiles } from './typescript';
67

78
const swcNodeInstalled = packageIsInstalled('@swc-node/register');
@@ -422,6 +423,21 @@ function readCompilerOptionsWithSwc(tsConfigPath) {
422423
// @swc-node/register filters the files to transpile based on it, but it can be limiting when processing
423424
// files not part of the received tsconfig included files (e.g. shared helpers, or config files not in source, etc.).
424425
delete compilerOptions.files;
426+
427+
// @swc-node/register's readDefaultTsConfig auto-sets baseUrl to the
428+
// dirname of the tsconfig when not explicitly configured. This is incorrect
429+
// when paths are inherited via "extends" from a parent tsconfig at a
430+
// different directory level (e.g., tsconfig.base.json at workspace root),
431+
// because SWC will resolve "./"-prefixed paths relative to the wrong
432+
// directory. Use the workspace root as baseUrl in that case.
433+
// baseUrl will not be configured when using newer versions of TypeScript like `tsgo`.
434+
if (compilerOptions.paths) {
435+
const { options: tsOptions } = readTsConfigWithoutFiles(tsConfigPath);
436+
if (!tsOptions.baseUrl) {
437+
compilerOptions.baseUrl = workspaceRoot;
438+
}
439+
}
440+
425441
return compilerOptions;
426442
}
427443

0 commit comments

Comments
 (0)