Skip to content

Commit

Permalink
fix(core): update local plugin resolution to use path utilities (#9713)
Browse files Browse the repository at this point in the history
* fix(core): update local plugin resolution to use path utilities

* chore(core): update nx-plugin resolution to reuse target project locator logic
  • Loading branch information
AgentEnder committed Apr 6, 2022
1 parent 4b3486e commit 8af847c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
31 changes: 20 additions & 11 deletions packages/nx/src/utils/nx-plugin.ts
Expand Up @@ -13,6 +13,7 @@ import {
TargetConfiguration,
WorkspaceJsonConfiguration,
} from '../config/workspace-json-project-json';
import { findMatchingProjectForPath } from './target-project-locator';

export type ProjectTargetConfigurator = (
file: string
Expand Down Expand Up @@ -181,17 +182,15 @@ function findNxProjectForImportPath(
const possiblePaths = tsConfigPaths[importPath]?.map((p) =>
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) => p.startsWith(root))) {
return projectRootMappings[root];
if (possiblePaths?.length) {
const projectRootMappings = buildProjectRootMap(workspace.projects, root);
for (const tsConfigPath of possiblePaths) {
const nxProject = findMatchingProjectForPath(
tsConfigPath,
projectRootMappings
);
if (nxProject) {
return nxProject;
}
}
if (process.env.NX_VERBOSE_LOGGING) {
Expand All @@ -207,6 +206,16 @@ function findNxProjectForImportPath(
}
}

function buildProjectRootMap(
projects: Record<string, ProjectConfiguration>,
root: string
) {
return Object.entries(projects).reduce((m, [project, config]) => {
m.set(path.resolve(root, config.root), project);
return m;
}, new Map<string, string>());
}

let tsconfigPaths: Record<string, string[]>;
function readTsConfigPaths(root: string = workspaceRoot) {
if (!tsconfigPaths) {
Expand Down
39 changes: 26 additions & 13 deletions packages/nx/src/utils/target-project-locator.ts
Expand Up @@ -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<string, ProjectGraphProjectNode>
) {
const projectRootMappings = new Map();
const projectRootMappings = new Map<string, string>();
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<projectRoot, projectName>
*/
export function findMatchingProjectForPath(
filePath: string,
projectRootMap: Map<string, string>
) {
for (
let currentPath = filePath;
currentPath != dirname(currentPath);
currentPath = dirname(currentPath)
) {
const p = projectRootMap.get(currentPath);
if (p) {
return p;
}
}
return null;
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Expand Up @@ -78,6 +78,7 @@
"@nrwl/nx-dev/ui-home": ["./nx-dev/ui-home/src/index.ts"],
"@nrwl/nx-dev/ui-member-card": ["./nx-dev/ui-member-card/src/index.ts"],
"@nrwl/nx-dev/ui-sponsor-card": ["./nx-dev/ui-sponsor-card/src/index.ts"],
"@nrwl/nx-plugin": ["./packages/nx-plugin"],
"@nrwl/react": ["./packages/react"],
"@nrwl/react-native": ["./packages/react-native"],
"@nrwl/react/*": ["./packages/react/*"],
Expand Down

0 comments on commit 8af847c

Please sign in to comment.