Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): update local plugin resolution to use path utilities #9713

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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