Skip to content

Commit 21d65cb

Browse files
authored
fix(core): use require.resolve instead of randomly matching from our … (#14523)
1 parent 51b1f48 commit 21d65cb

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

packages/nx/src/utils/target-project-locator.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,16 +781,23 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
781781
});
782782

783783
it('should be able to resolve local project', () => {
784+
jest
785+
.spyOn(targetProjectLocator as any, 'resolveImportWithRequire')
786+
.mockReturnValue('libs/proj1/index.ts');
787+
784788
const result1 = targetProjectLocator.findProjectWithImport(
785789
'@org/proj1',
786790
'libs/proj1/index.ts'
787791
);
792+
expect(result1).toEqual('@org/proj1');
793+
794+
jest
795+
.spyOn(targetProjectLocator as any, 'resolveImportWithRequire')
796+
.mockReturnValue('libs/proj1/some/nested/file.ts');
788797
const result2 = targetProjectLocator.findProjectWithImport(
789798
'@org/proj1/some/nested/path',
790799
'libs/proj1/index.ts'
791800
);
792-
793-
expect(result1).toEqual('@org/proj1');
794801
expect(result2).toEqual('@org/proj1');
795802
});
796803

packages/nx/src/utils/target-project-locator.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class TargetProjectLocator {
1717
private tsConfig = this.getRootTsConfig();
1818
private paths = this.tsConfig.config?.compilerOptions?.paths;
1919
private typescriptResolutionCache = new Map<string, string | null>();
20-
private projectResolutionCache = new Map<string, string | null>();
2120
private npmResolutionCache = new Map<string, string | null>();
2221

2322
constructor(
@@ -52,12 +51,6 @@ export class TargetProjectLocator {
5251
}
5352
}
5453

55-
// check if it exists in projects
56-
const project = this.findProject(normalizedImportExpr);
57-
if (project) {
58-
return project;
59-
}
60-
6154
// try to find npm package before using expensive typescript resolution
6255
const npmProject = this.findNpmPackage(normalizedImportExpr);
6356
if (npmProject) {
@@ -77,6 +70,15 @@ export class TargetProjectLocator {
7770
}
7871
}
7972

73+
try {
74+
const resolvedModule = this.resolveImportWithRequire(
75+
normalizedImportExpr,
76+
filePath
77+
);
78+
79+
return this.findProjectOfResolvedModule(resolvedModule);
80+
} catch {}
81+
8082
// nothing found, cache for later
8183
this.npmResolutionCache.set(normalizedImportExpr, undefined);
8284
return null;
@@ -135,22 +137,17 @@ export class TargetProjectLocator {
135137
return;
136138
}
137139

138-
private findProject(importExpr: string): string | undefined {
139-
if (this.projectResolutionCache.has(importExpr)) {
140-
return this.projectResolutionCache.get(importExpr);
141-
} else {
142-
const project = Object.values(this.nodes).find(
143-
(project) =>
144-
importExpr === project.name ||
145-
importExpr.startsWith(`${project.name}/`)
146-
);
147-
if (project) {
148-
this.projectResolutionCache.set(importExpr, project.name);
149-
return project.name;
150-
}
151-
}
140+
private resolveImportWithRequire(
141+
normalizedImportExpr: string,
142+
filePath: string
143+
) {
144+
return posix.relative(
145+
workspaceRoot,
146+
require.resolve(normalizedImportExpr, {
147+
paths: [dirname(filePath)],
148+
})
149+
);
152150
}
153-
154151
private findNpmPackage(npmImport: string): string | undefined {
155152
if (this.npmResolutionCache.has(npmImport)) {
156153
return this.npmResolutionCache.get(npmImport);

0 commit comments

Comments
 (0)