Skip to content

Commit 04cf098

Browse files
authored
fix(core): change graph node type and name to string (#29610)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 437bad4 commit 04cf098

File tree

5 files changed

+79
-57
lines changed

5 files changed

+79
-57
lines changed

docs/generated/devkit/ProjectGraphExternalNode.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ while allowing tracking of the full tree of different nested versions
1414
### Properties
1515

1616
- [data](../../devkit/documents/ProjectGraphExternalNode#data): Object
17-
- [name](../../devkit/documents/ProjectGraphExternalNode#name): `npm:${string}`
18-
- [type](../../devkit/documents/ProjectGraphExternalNode#type): "npm"
17+
- [name](../../devkit/documents/ProjectGraphExternalNode#name): string
18+
- [type](../../devkit/documents/ProjectGraphExternalNode#type): string
1919

2020
## Properties
2121

@@ -35,10 +35,10 @@ while allowing tracking of the full tree of different nested versions
3535

3636
### name
3737

38-
**name**: \`npm:$\{string}\`
38+
**name**: `string`
3939

4040
---
4141

4242
### type
4343

44-
**type**: `"npm"`
44+
**type**: `string`

packages/eslint-plugin/src/rules/enforce-module-boundaries.ts

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
matchImportWithWildcard,
5151
stringifyTags,
5252
} from '../utils/runtime-lint-utils';
53+
import { isProjectGraphProjectNode } from 'nx/src/config/project-graph';
5354

5455
export type Options = [
5556
{
@@ -525,6 +526,11 @@ export default ESLintUtils.RuleCreator(
525526
return;
526527
}
527528

529+
if (!isProjectGraphProjectNode(targetProject)) {
530+
return;
531+
}
532+
targetProject = targetProject as ProjectGraphProjectNode;
533+
528534
// check constraints between libs and apps
529535
// check for circular dependency
530536
const circularPath = checkCircularPath(

packages/js/src/utils/buildable-libs-utils.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('updatePaths', () => {
1414
const deps: DependentBuildableProjectNode[] = [
1515
{
1616
name: '@proj/lib',
17-
node: { data: { root: 'libs/lib' } } as any,
17+
node: { type: 'lib', data: { root: 'libs/lib' } } as any,
1818
outputs: ['dist/libs/lib'],
1919
},
2020
];

packages/js/src/utils/buildable-libs-utils.ts

+54-50
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
parseTargetString,
1010
readJsonFile,
1111
stripIndents,
12-
workspaceRoot,
1312
writeJsonFile,
1413
} from '@nx/devkit';
1514
import { unlinkSync } from 'fs';
@@ -20,6 +19,10 @@ import { dirname, join, relative, extname, resolve } from 'path';
2019
import type * as ts from 'typescript';
2120
import { readTsConfigPaths } from './typescript/ts-config';
2221
import { randomUUID } from 'crypto';
22+
import {
23+
isProjectGraphExternalNode,
24+
isProjectGraphProjectNode,
25+
} from 'nx/src/config/project-graph';
2326

2427
function isBuildable(target: string, node: ProjectGraphProjectNode): boolean {
2528
return (
@@ -112,7 +115,7 @@ export function calculateProjectDependencies(
112115
.map(({ name: dep, isTopLevel }) => {
113116
let project: DependentBuildableProjectNode = null;
114117
const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
115-
if (depNode.type === 'lib') {
118+
if (isProjectGraphProjectNode(depNode) && depNode.type === 'lib') {
116119
if (isBuildable(targetName, depNode)) {
117120
const libPackageJsonPath = join(
118121
root,
@@ -138,7 +141,7 @@ export function calculateProjectDependencies(
138141
} else {
139142
nonBuildableDependencies.push(dep);
140143
}
141-
} else if (depNode.type === 'npm') {
144+
} else if (isProjectGraphExternalNode(depNode)) {
142145
project = {
143146
name: depNode.data.packageName,
144147
outputs: [],
@@ -530,54 +533,52 @@ export function updatePaths(
530533
) {
531534
const pathsKeys = Object.keys(paths);
532535
// For each registered dependency
533-
dependencies.forEach((dep) => {
534-
if (dep.node.type === 'npm') {
535-
return;
536-
}
537-
538-
// If there are outputs
539-
if (dep.outputs && dep.outputs.length > 0) {
540-
// Directly map the dependency name to the output paths (dist/packages/..., etc.)
541-
paths[dep.name] = dep.outputs;
542-
543-
// check for secondary entrypoints
544-
// For each registered path
545-
for (const path of pathsKeys) {
546-
const nestedName = `${dep.name}/`;
547-
548-
// If the path points to the current dependency and is nested (/)
549-
if (path.startsWith(nestedName)) {
550-
const nestedPart = path.slice(nestedName.length);
551-
552-
// Bind potential secondary endpoints for ng-packagr projects
553-
let mappedPaths = dep.outputs.map(
554-
(output) => `${output}/${nestedPart}`
555-
);
556-
557-
const { root } = dep.node.data;
558-
// Update nested mappings to point to the dependency's output paths
559-
mappedPaths = mappedPaths.concat(
560-
paths[path].flatMap((p) =>
561-
dep.outputs.flatMap((output) => {
562-
const basePath = p.replace(root, output);
563-
return [
564-
// extension-less path to support compiled output
565-
basePath.replace(
566-
new RegExp(`${extname(basePath)}$`, 'gi'),
567-
''
568-
),
569-
// original path with the root re-mapped to the output path
570-
basePath,
571-
];
572-
})
573-
)
574-
);
575-
576-
paths[path] = mappedPaths;
536+
dependencies
537+
.filter((dep) => isProjectGraphProjectNode(dep.node))
538+
.forEach((dep) => {
539+
// If there are outputs
540+
if (dep.outputs && dep.outputs.length > 0) {
541+
// Directly map the dependency name to the output paths (dist/packages/..., etc.)
542+
paths[dep.name] = dep.outputs;
543+
544+
// check for secondary entrypoints
545+
// For each registered path
546+
for (const path of pathsKeys) {
547+
const nestedName = `${dep.name}/`;
548+
549+
// If the path points to the current dependency and is nested (/)
550+
if (path.startsWith(nestedName)) {
551+
const nestedPart = path.slice(nestedName.length);
552+
553+
// Bind potential secondary endpoints for ng-packagr projects
554+
let mappedPaths = dep.outputs.map(
555+
(output) => `${output}/${nestedPart}`
556+
);
557+
558+
const { root } = (dep.node as ProjectGraphProjectNode).data;
559+
// Update nested mappings to point to the dependency's output paths
560+
mappedPaths = mappedPaths.concat(
561+
paths[path].flatMap((p) =>
562+
dep.outputs.flatMap((output) => {
563+
const basePath = p.replace(root, output);
564+
return [
565+
// extension-less path to support compiled output
566+
basePath.replace(
567+
new RegExp(`${extname(basePath)}$`, 'gi'),
568+
''
569+
),
570+
// original path with the root re-mapped to the output path
571+
basePath,
572+
];
573+
})
574+
)
575+
);
576+
577+
paths[path] = mappedPaths;
578+
}
577579
}
578580
}
579-
}
580-
});
581+
});
581582
}
582583

583584
/**
@@ -630,7 +631,10 @@ export function updateBuildableProjectPackageJsonDependencies(
630631
) {
631632
try {
632633
let depVersion;
633-
if (entry.node.type === 'lib') {
634+
if (
635+
isProjectGraphProjectNode(entry.node) &&
636+
entry.node.type === 'lib'
637+
) {
634638
const outputs = getOutputsForTargetAndConfiguration(
635639
{
636640
project: projectName,

packages/nx/src/config/project-graph.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ export interface ProjectGraphProjectNode {
103103
};
104104
}
105105

106+
export function isProjectGraphProjectNode(
107+
node: ProjectGraphProjectNode | ProjectGraphExternalNode
108+
): node is ProjectGraphProjectNode {
109+
return node.type === 'app' || node.type === 'e2e' || node.type === 'lib';
110+
}
111+
106112
/**
107113
* A node describing an external dependency
108114
* `name` has as form of:
@@ -114,15 +120,21 @@ export interface ProjectGraphProjectNode {
114120
*
115121
*/
116122
export interface ProjectGraphExternalNode {
117-
type: 'npm';
118-
name: `npm:${string}`;
123+
type: string; // not app, e2e, or lib
124+
name: string;
119125
data: {
120126
version: string;
121127
packageName: string;
122128
hash?: string;
123129
};
124130
}
125131

132+
export function isProjectGraphExternalNode(
133+
node: ProjectGraphProjectNode | ProjectGraphExternalNode
134+
): node is ProjectGraphExternalNode {
135+
return isProjectGraphProjectNode(node) === false;
136+
}
137+
126138
/**
127139
* A dependency between two projects
128140
*/

0 commit comments

Comments
 (0)