From e17c98b71abd30a3fe01008fd43df22f99d69945 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Mon, 13 Jan 2025 15:23:52 -0500 Subject: [PATCH] fix(core): add cargo and gradle to external graph node --- .../devkit/ProjectGraphExternalNode.md | 8 +- .../src/rules/enforce-module-boundaries.ts | 6 ++ packages/js/src/utils/buildable-libs-utils.ts | 94 +++++++++---------- packages/nx/src/config/project-graph.ts | 16 +++- 4 files changed, 70 insertions(+), 54 deletions(-) diff --git a/docs/generated/devkit/ProjectGraphExternalNode.md b/docs/generated/devkit/ProjectGraphExternalNode.md index df774d2ba0dc27..3efa0d7b51a7ce 100644 --- a/docs/generated/devkit/ProjectGraphExternalNode.md +++ b/docs/generated/devkit/ProjectGraphExternalNode.md @@ -14,8 +14,8 @@ while allowing tracking of the full tree of different nested versions ### Properties - [data](../../devkit/documents/ProjectGraphExternalNode#data): Object -- [name](../../devkit/documents/ProjectGraphExternalNode#name): `npm:${string}` -- [type](../../devkit/documents/ProjectGraphExternalNode#type): "npm" +- [name](../../devkit/documents/ProjectGraphExternalNode#name): `npm:${string}` | `cargo:${string}` | `gradle:${string}` +- [type](../../devkit/documents/ProjectGraphExternalNode#type): "npm" | "cargo" | "gradle" ## Properties @@ -35,10 +35,10 @@ while allowing tracking of the full tree of different nested versions ### name -• **name**: \`npm:$\{string}\` +• **name**: \`npm:$\{string}\` \| \`cargo:$\{string}\` \| \`gradle:$\{string}\` --- ### type -• **type**: `"npm"` +• **type**: `"npm"` \| `"cargo"` \| `"gradle"` diff --git a/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts index 8615989bff468a..e430b21a47bdf9 100644 --- a/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts @@ -50,6 +50,7 @@ import { matchImportWithWildcard, stringifyTags, } from '../utils/runtime-lint-utils'; +import { isProjectGraphProjectNode } from 'nx/src/config/project-graph'; type Options = [ { @@ -525,6 +526,11 @@ export default ESLintUtils.RuleCreator( return; } + if (!isProjectGraphProjectNode(targetProject)) { + return; + } + targetProject = targetProject as ProjectGraphProjectNode; + // check constraints between libs and apps // check for circular dependency const circularPath = checkCircularPath( diff --git a/packages/js/src/utils/buildable-libs-utils.ts b/packages/js/src/utils/buildable-libs-utils.ts index 454b0c38133577..ac1896de2a929f 100644 --- a/packages/js/src/utils/buildable-libs-utils.ts +++ b/packages/js/src/utils/buildable-libs-utils.ts @@ -9,7 +9,6 @@ import { parseTargetString, readJsonFile, stripIndents, - workspaceRoot, writeJsonFile, } from '@nx/devkit'; import { unlinkSync } from 'fs'; @@ -19,6 +18,7 @@ import { output } from 'nx/src/utils/output'; import { dirname, join, relative, extname, resolve } from 'path'; import type * as ts from 'typescript'; import { readTsConfigPaths } from './typescript/ts-config'; +import { isProjectGraphExternalNode } from 'nx/src/config/project-graph'; function isBuildable(target: string, node: ProjectGraphProjectNode): boolean { return ( @@ -137,7 +137,7 @@ export function calculateProjectDependencies( } else { nonBuildableDependencies.push(dep); } - } else if (depNode.type === 'npm') { + } else if (isProjectGraphExternalNode(depNode)) { project = { name: depNode.data.packageName, outputs: [], @@ -529,54 +529,52 @@ export function updatePaths( ) { const pathsKeys = Object.keys(paths); // For each registered dependency - dependencies.forEach((dep) => { - if (dep.node.type === 'npm') { - return; - } - - // If there are outputs - if (dep.outputs && dep.outputs.length > 0) { - // Directly map the dependency name to the output paths (dist/packages/..., etc.) - paths[dep.name] = dep.outputs; - - // check for secondary entrypoints - // For each registered path - for (const path of pathsKeys) { - const nestedName = `${dep.name}/`; - - // If the path points to the current dependency and is nested (/) - if (path.startsWith(nestedName)) { - const nestedPart = path.slice(nestedName.length); - - // Bind potential secondary endpoints for ng-packagr projects - let mappedPaths = dep.outputs.map( - (output) => `${output}/${nestedPart}` - ); - - const { root } = dep.node.data; - // Update nested mappings to point to the dependency's output paths - mappedPaths = mappedPaths.concat( - paths[path].flatMap((p) => - dep.outputs.flatMap((output) => { - const basePath = p.replace(root, output); - return [ - // extension-less path to support compiled output - basePath.replace( - new RegExp(`${extname(basePath)}$`, 'gi'), - '' - ), - // original path with the root re-mapped to the output path - basePath, - ]; - }) - ) - ); - - paths[path] = mappedPaths; + dependencies + .filter((dep) => !isProjectGraphExternalNode(dep.node)) + .forEach((dep) => { + // If there are outputs + if (dep.outputs && dep.outputs.length > 0) { + // Directly map the dependency name to the output paths (dist/packages/..., etc.) + paths[dep.name] = dep.outputs; + + // check for secondary entrypoints + // For each registered path + for (const path of pathsKeys) { + const nestedName = `${dep.name}/`; + + // If the path points to the current dependency and is nested (/) + if (path.startsWith(nestedName)) { + const nestedPart = path.slice(nestedName.length); + + // Bind potential secondary endpoints for ng-packagr projects + let mappedPaths = dep.outputs.map( + (output) => `${output}/${nestedPart}` + ); + + const { root } = (dep.node as ProjectGraphProjectNode).data; + // Update nested mappings to point to the dependency's output paths + mappedPaths = mappedPaths.concat( + paths[path].flatMap((p) => + dep.outputs.flatMap((output) => { + const basePath = p.replace(root, output); + return [ + // extension-less path to support compiled output + basePath.replace( + new RegExp(`${extname(basePath)}$`, 'gi'), + '' + ), + // original path with the root re-mapped to the output path + basePath, + ]; + }) + ) + ); + + paths[path] = mappedPaths; + } } } - } - }); + }); } /** diff --git a/packages/nx/src/config/project-graph.ts b/packages/nx/src/config/project-graph.ts index 8de2e25d1e6fcc..c5bb73c510f5ed 100644 --- a/packages/nx/src/config/project-graph.ts +++ b/packages/nx/src/config/project-graph.ts @@ -103,6 +103,12 @@ export interface ProjectGraphProjectNode { }; } +export function isProjectGraphProjectNode( + node: ProjectGraphProjectNode | ProjectGraphExternalNode +): node is ProjectGraphProjectNode { + return node.type === 'app' || node.type === 'e2e' || node.type === 'lib'; +} + /** * A node describing an external dependency * `name` has as form of: @@ -114,8 +120,8 @@ export interface ProjectGraphProjectNode { * */ export interface ProjectGraphExternalNode { - type: 'npm'; - name: `npm:${string}`; + type: 'npm' | 'cargo' | 'gradle'; + name: `npm:${string}` | `cargo:${string}` | `gradle:${string}`; data: { version: string; packageName: string; @@ -123,6 +129,12 @@ export interface ProjectGraphExternalNode { }; } +export function isProjectGraphExternalNode( + node: ProjectGraphProjectNode | ProjectGraphExternalNode +): node is ProjectGraphExternalNode { + return node.type === 'npm' || node.type === 'cargo' || node.type === 'gradle'; +} + /** * A dependency between two projects */