From aa01a94455e502dfb50d10edf3c6844f53125e71 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Tue, 17 Jan 2023 10:02:10 +0100 Subject: [PATCH] fix(core): project locator should use nodes when tsconfig is missing --- .../src/utils/target-project-locator.spec.ts | 25 +++++++++++++++++-- .../nx/src/utils/target-project-locator.ts | 24 ++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/utils/target-project-locator.spec.ts b/packages/nx/src/utils/target-project-locator.spec.ts index 8ea60158e5d0f..e8f22c66d5c46 100644 --- a/packages/nx/src/utils/target-project-locator.spec.ts +++ b/packages/nx/src/utils/target-project-locator.spec.ts @@ -12,7 +12,6 @@ jest.mock('nx/src/utils/workspace-root', () => ({ jest.mock('fs', () => require('memfs').fs); describe('findTargetProjectWithImport', () => { - let ctx: ProjectGraphProcessorContext; let projects: Record; let npmProjects: Record; let fsJson; @@ -69,7 +68,7 @@ describe('findTargetProjectWithImport', () => { }; vol.fromJSON(fsJson, '/root'); - ctx = { + const ctx = { workspace: { ...workspaceJson, ...nxJson, @@ -594,6 +593,14 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => { } as any; projects = { + '@org/proj1': { + name: '@org/proj1', + type: 'lib', + data: { + root: 'libs/proj1', + files: [], + }, + }, proj3a: { name: 'proj3a', type: 'lib', @@ -773,6 +780,20 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => { expect(res4).toEqual('proj'); }); + it('should be able to resolve local project', () => { + const result1 = targetProjectLocator.findProjectWithImport( + '@org/proj1', + 'libs/proj1/index.ts' + ); + const result2 = targetProjectLocator.findProjectWithImport( + '@org/proj1/some/nested/path', + 'libs/proj1/index.ts' + ); + + expect(result1).toEqual('@org/proj1'); + expect(result2).toEqual('@org/proj1'); + }); + it('should be able to npm dependencies', () => { const result1 = targetProjectLocator.findProjectWithImport( '@ng/core', diff --git a/packages/nx/src/utils/target-project-locator.ts b/packages/nx/src/utils/target-project-locator.ts index 42b8dffcdff71..f7834f80baadb 100644 --- a/packages/nx/src/utils/target-project-locator.ts +++ b/packages/nx/src/utils/target-project-locator.ts @@ -17,6 +17,7 @@ export class TargetProjectLocator { private tsConfig = this.getRootTsConfig(); private paths = this.tsConfig.config?.compilerOptions?.paths; private typescriptResolutionCache = new Map(); + private projectResolutionCache = new Map(); private npmResolutionCache = new Map(); constructor( @@ -40,6 +41,7 @@ export class TargetProjectLocator { return this.findProjectOfResolvedModule(resolvedModule); } + // find project using tsconfig paths const paths = this.findPaths(normalizedImportExpr); if (paths) { for (let p of paths) { @@ -50,6 +52,12 @@ export class TargetProjectLocator { } } + // check if it exists in projects + const project = this.findProject(normalizedImportExpr); + if (project) { + return project; + } + // try to find npm package before using expensive typescript resolution const npmProject = this.findNpmPackage(normalizedImportExpr); if (npmProject) { @@ -127,6 +135,22 @@ export class TargetProjectLocator { return; } + private findProject(importExpr: string): string | undefined { + if (this.projectResolutionCache.has(importExpr)) { + return this.projectResolutionCache.get(importExpr); + } else { + const project = Object.values(this.nodes).find( + (project) => + importExpr === project.name || + importExpr.startsWith(`${project.name}/`) + ); + if (project) { + this.npmResolutionCache.set(importExpr, project.name); + return project.name; + } + } + } + private findNpmPackage(npmImport: string): string | undefined { if (this.npmResolutionCache.has(npmImport)) { return this.npmResolutionCache.get(npmImport);