Skip to content

Commit 0b710a2

Browse files
committed
fix(core): convert filePath to an absolute path before typescript resolves the module
Convert the filePath to an absolute path inside findProjectFromImport before calling resolveImportWithTypescript, because TypeScript’s module resolution will not correctly traverse up the directory tree toward the workspace root when given a relative path. closed #33985
1 parent 768c580 commit 0b710a2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isBuiltin } from 'node:module';
2-
import { dirname, join, posix, relative } from 'node:path';
2+
import { dirname, join, posix, relative, isAbsolute } from 'node:path';
33
import { clean, satisfies } from 'semver';
44
import type {
55
ProjectGraphExternalNode,
@@ -146,11 +146,17 @@ export class TargetProjectLocator {
146146
}
147147

148148
if (this.tsConfig.config) {
149+
// Convert to an absolute file path because TypeScript's module resolution won't
150+
// properly walk up the directory tree (toward the workspace root) when given a relative path.
151+
const absolutePath = isAbsolute(filePath)
152+
? filePath
153+
: this.getAbsolutePath(filePath);
154+
149155
// TODO: this can be removed once we rework resolveImportWithRequire below
150156
// to properly handle ESM (exports, imports, conditions)
151157
const resolvedProject = this.resolveImportWithTypescript(
152158
importExpr,
153-
filePath
159+
absolutePath
154160
);
155161
if (resolvedProject) {
156162
return resolvedProject;

0 commit comments

Comments
 (0)