From 767f2c09ace9ccb8b4d2b886cf4b66c0a41f4ed4 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Wed, 6 Apr 2022 13:22:36 -0400 Subject: [PATCH] chore(core): update nx-plugin resolution to reuse target project locator logic --- packages/nx/src/utils/nx-plugin.ts | 29 +++++++++----- .../nx/src/utils/target-project-locator.ts | 39 ++++++++++++------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/nx/src/utils/nx-plugin.ts b/packages/nx/src/utils/nx-plugin.ts index 5ee643be95807a..737e9ab255b5c4 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -13,6 +13,7 @@ import { TargetConfiguration, WorkspaceJsonConfiguration, } from '../config/workspace-json-project-json'; +import { findMatchingProjectForPath } from './target-project-locator'; export type ProjectTargetConfigurator = ( file: string @@ -182,16 +183,14 @@ function findNxProjectForImportPath( path.resolve(root, p) ); if (tsConfigPaths[importPath]) { - const projectRootMappings = Object.entries(workspace.projects).reduce( - (m, [project, config]) => { - m[path.resolve(root, config.root)] = project; - return m; - }, - {} - ); - for (const root of Object.keys(projectRootMappings)) { - if (possiblePaths.some((p) => !path.relative(root, p).startsWith('..'))) { - return projectRootMappings[root]; + const projectRootMappings = buildProjectRootMap(workspace.projects, root); + for (const tsConfigPath of tsconfigPaths[importPath]) { + const nxProject = findMatchingProjectForPath( + tsConfigPath, + projectRootMappings + ); + if (nxProject) { + return nxProject; } } if (process.env.NX_VERBOSE_LOGGING) { @@ -207,6 +206,16 @@ function findNxProjectForImportPath( } } +function buildProjectRootMap( + projects: Record, + root: string +) { + return Object.entries(projects).reduce((m, [project, config]) => { + m.set(path.resolve(root, config.root), project); + return m; + }, new Map()); +} + let tsconfigPaths: Record; function readTsConfigPaths(root: string = workspaceRoot) { if (!tsconfigPaths) { diff --git a/packages/nx/src/utils/target-project-locator.ts b/packages/nx/src/utils/target-project-locator.ts index 3d9b5cafd658b6..cd3345c914816a 100644 --- a/packages/nx/src/utils/target-project-locator.ts +++ b/packages/nx/src/utils/target-project-locator.ts @@ -178,30 +178,43 @@ export class TargetProjectLocator { } private findMatchingProjectFiles(file: string) { - for ( - let currentPath = file; - currentPath != dirname(currentPath); - currentPath = dirname(currentPath) - ) { - const p = this.projectRootMappings.get(currentPath); - if (p) { - return p; - } - } - return null; + const project = findMatchingProjectForPath(file, this.projectRootMappings); + return this.nodes[project]; } } function createProjectRootMappings( nodes: Record ) { - const projectRootMappings = new Map(); + const projectRootMappings = new Map(); for (const projectName of Object.keys(nodes)) { const root = nodes[projectName].data.root; projectRootMappings.set( root && root.endsWith('/') ? root.substring(0, root.length - 1) : root, - nodes[projectName] + projectName ); } return projectRootMappings; } + +/** + * Locates a project in projectRootMap based on a file within it + * @param filePath path that is inside of projectName + * @param projectRootMap Map + */ +export function findMatchingProjectForPath( + filePath: string, + projectRootMap: Map +) { + for ( + let currentPath = filePath; + currentPath != dirname(currentPath); + currentPath = dirname(currentPath) + ) { + const p = projectRootMap.get(currentPath); + if (p) { + return p; + } + } + return null; +}