Skip to content

Commit 355d5c5

Browse files
authored
fix(core): move a few api points to return root maps directly (#22949)
1 parent f76b8c6 commit 355d5c5

17 files changed

+196
-98
lines changed

packages/devkit/src/utils/add-plugin.spec.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { addPlugin, generateCombinations } from './add-plugin';
1010

1111
describe('addPlugin', () => {
1212
let tree: Tree;
13-
let createNodes: CreateNodes;
13+
let createNodes: CreateNodes<{ targetName: string }>;
1414
let graph: ProjectGraph;
1515
let fs: TempFs;
1616

@@ -30,9 +30,27 @@ describe('addPlugin', () => {
3030
targets: {},
3131
},
3232
},
33+
app2: {
34+
name: 'app2',
35+
type: 'app',
36+
data: {
37+
root: 'app2',
38+
targets: {},
39+
},
40+
},
41+
app3: {
42+
name: 'app3',
43+
type: 'app',
44+
data: {
45+
root: 'app3',
46+
targets: {},
47+
},
48+
},
3349
},
3450
dependencies: {
3551
app1: [],
52+
app2: [],
53+
app3: [],
3654
},
3755
};
3856
createNodes = [
@@ -45,12 +63,19 @@ describe('addPlugin', () => {
4563
[targetName]: { command: 'next build' },
4664
},
4765
},
66+
app2: {
67+
name: 'app2',
68+
targets: {
69+
[targetName]: { command: 'next build' },
70+
},
71+
},
4872
},
4973
}),
5074
];
5175

5276
await fs.createFiles({
5377
'app1/next.config.js': '',
78+
'app2/next.config.js': '',
5479
});
5580
});
5681

@@ -61,6 +86,10 @@ describe('addPlugin', () => {
6186
describe('adding the plugin', () => {
6287
it('should not conflicting with the existing graph', async () => {
6388
graph.nodes.app1.data.targets.build = {};
89+
graph.nodes.app2.data.targets.build1 = {};
90+
// app 3 doesn't have a next config, so it having this
91+
// target should not affect the plugin options
92+
graph.nodes.app3.data.targets.build2 = {};
6493

6594
await addPlugin(
6695
tree,
@@ -69,15 +98,15 @@ describe('addPlugin', () => {
6998
createNodes,
7099

71100
{
72-
targetName: ['build', '_build'],
101+
targetName: ['build', 'build1', 'build2'],
73102
},
74103
true
75104
);
76105

77106
expect(readJson(tree, 'nx.json').plugins).toContainEqual({
78107
plugin: '@nx/next/plugin',
79108
options: {
80-
targetName: '_build',
109+
targetName: 'build2',
81110
},
82111
});
83112
});
@@ -372,6 +401,7 @@ describe('addPlugin', () => {
372401
});
373402
});
374403
});
404+
375405
describe('generateCombinations', () => {
376406
it('should return all combinations for a 2x2 array of strings', () => {
377407
const input = {

packages/devkit/src/utils/convert-nx-executor.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import type { Executor, ExecutorContext } from 'nx/src/config/misc-interfaces';
33
import type { ProjectsConfigurations } from 'nx/src/devkit-exports';
44

55
import { requireNx } from '../../nx';
6+
import { NX_VERSION } from './package-json';
7+
import { lt } from 'semver';
68

79
const {
810
Workspaces,
911
readNxJsonFromDisk,
1012
retrieveProjectConfigurationsWithAngularProjects,
13+
readProjectConfigurationsFromRootMap,
1114
} = requireNx();
1215

1316
/**
@@ -32,7 +35,24 @@ export function convertNxExecutor(executor: Executor) {
3235
projects: await retrieveProjectConfigurationsWithAngularProjects(
3336
builderContext.workspaceRoot,
3437
nxJsonConfiguration
35-
).then((p) => (p as any).projectNodes ?? p.projects),
38+
).then((p) => {
39+
if ((p as any).projectNodes) {
40+
return (p as any).projectNodes;
41+
}
42+
// v18.3.4 changed projects to be keyed by root
43+
// rather than project name
44+
if (lt(NX_VERSION, '18.3.4')) {
45+
return p.projects;
46+
}
47+
48+
if (readProjectConfigurationsFromRootMap) {
49+
return readProjectConfigurationsFromRootMap(p.projects);
50+
}
51+
52+
throw new Error(
53+
'Unable to successfully map Nx executor -> Angular Builder'
54+
);
55+
}),
3656
}
3757
: // TODO(v19): remove retrieveProjectConfigurations. This is to be backwards compatible with Nx 16.5 and below.
3858
(workspaces as any).readProjectsConfigurations({

packages/nx/src/config/workspaces.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('Workspaces', () => {
5252
}
5353
);
5454
console.log(projects);
55-
expect(projects['my-package']).toEqual({
55+
expect(projects['packages/my-package']).toEqual({
5656
name: 'my-package',
5757
root: 'packages/my-package',
5858
sourceRoot: 'packages/my-package',

packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,29 +234,29 @@ async function processFilesAndCreateAndSerializeProjectGraph(
234234
const nxJson = readNxJson(workspaceRoot);
235235
global.NX_GRAPH_CREATION = true;
236236

237-
let graphNodes: ConfigurationResult;
237+
let projectConfigurationsResult: ConfigurationResult;
238238
let projectConfigurationsError;
239239

240240
try {
241-
graphNodes = await retrieveProjectConfigurations(
241+
projectConfigurationsResult = await retrieveProjectConfigurations(
242242
plugins,
243243
workspaceRoot,
244244
nxJson
245245
);
246246
} catch (e) {
247247
if (e instanceof ProjectConfigurationsError) {
248-
graphNodes = e.partialProjectConfigurationsResult;
248+
projectConfigurationsResult = e.partialProjectConfigurationsResult;
249249
projectConfigurationsError = e;
250250
} else {
251251
throw e;
252252
}
253253
}
254254
await processCollectedUpdatedAndDeletedFiles(
255-
graphNodes,
255+
projectConfigurationsResult,
256256
updatedFileHashes,
257257
deletedFiles
258258
);
259-
const g = await createAndSerializeProjectGraph(graphNodes);
259+
const g = await createAndSerializeProjectGraph(projectConfigurationsResult);
260260

261261
delete global.NX_GRAPH_CREATION;
262262

@@ -284,7 +284,7 @@ async function processFilesAndCreateAndSerializeProjectGraph(
284284
error: new DaemonProjectGraphError(
285285
errors,
286286
g.projectGraph,
287-
graphNodes.sourceMaps
287+
projectConfigurationsResult.sourceMaps
288288
),
289289
projectGraph: null,
290290
projectFileMapCache: null,

packages/nx/src/devkit-internals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { getExecutorInformation } from './command-line/run/executor-utils';
88
export { readNxJson as readNxJsonFromDisk } from './config/nx-json';
99
export { calculateDefaultProjectName } from './config/calculate-default-project-name';
1010
export { retrieveProjectConfigurationsWithAngularProjects } from './project-graph/utils/retrieve-workspace-files';
11+
export { readProjectConfigurationsFromRootMap } from './project-graph/utils/project-configuration-utils';
1112
export { splitTarget } from './utils/split-target';
1213
export { combineOptionsForExecutor } from './utils/params';
1314
export { sortObjectByKeys } from './utils/object-sort';

packages/nx/src/executors/utils/convert-nx-executor.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Observable } from 'rxjs';
66
import { readNxJson } from '../../config/nx-json';
77
import { Executor, ExecutorContext } from '../../config/misc-interfaces';
88
import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files';
9+
import { readProjectConfigurationsFromRootMap } from '../../project-graph/utils/project-configuration-utils';
910
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
1011
import { loadNxPlugins } from '../../project-graph/plugins/internal-api';
1112

@@ -25,13 +26,15 @@ export function convertNxExecutor(executor: Executor) {
2526
);
2627
const projectsConfigurations: ProjectsConfigurations = {
2728
version: 2,
28-
projects: (
29-
await retrieveProjectConfigurations(
30-
plugins,
31-
builderContext.workspaceRoot,
32-
nxJsonConfiguration
33-
)
34-
).projects,
29+
projects: readProjectConfigurationsFromRootMap(
30+
(
31+
await retrieveProjectConfigurations(
32+
plugins,
33+
builderContext.workspaceRoot,
34+
nxJsonConfiguration
35+
)
36+
).projects
37+
),
3538
};
3639
cleanup();
3740
const context: ExecutorContext = {

packages/nx/src/generators/utils/project-configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
211211
(r) => deletedFiles.indexOf(r) === -1
212212
);
213213

214-
const rootMap: Map<string, ProjectConfiguration> = new Map();
214+
const rootMap: Record<string, ProjectConfiguration> = {};
215215
for (const projectFile of projectFiles) {
216216
if (basename(projectFile) === 'project.json') {
217217
const json = readJson(tree, projectFile);
@@ -230,7 +230,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
230230
projectFile,
231231
readNxJson(tree)
232232
);
233-
if (!rootMap.has(config.root)) {
233+
if (!rootMap[config.root]) {
234234
mergeProjectConfigurationIntoRootMap(
235235
rootMap,
236236
// Inferred targets, tags, etc don't show up when running generators

packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ describe('explicit package json dependencies', () => {
1818
projects: {
1919
proj: {
2020
root: 'libs/proj',
21+
name: 'proj',
2122
},
2223
proj2: {
2324
root: 'libs/proj2',
25+
name: 'proj2',
2426
},
2527
proj3: {
2628
root: 'libs/proj3',
29+
name: 'proj3',
2730
},
2831
proj4: {
2932
root: 'libs/proj4',
33+
name: 'proj4',
3034
},
3135
},
3236
};

packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,9 @@ async function createContext(
580580

581581
return {
582582
externalNodes: builder.getUpdatedProjectGraph().externalNodes,
583-
projects: projects,
583+
projects: Object.fromEntries(
584+
Object.entries(projects).map(([root, config]) => [config.name, config])
585+
),
584586
nxJsonConfiguration: nxJson,
585587
filesToProcess: fileMap,
586588
fileMap: fileMap,

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function getFileMap(): {
6060
}
6161

6262
export async function buildProjectGraphUsingProjectFileMap(
63-
projects: Record<string, ProjectConfiguration>,
63+
projectRootMap: Record<string, ProjectConfiguration>,
6464
externalNodes: Record<string, ProjectGraphExternalNode>,
6565
fileMap: FileMap,
6666
allWorkspaceFiles: FileData[],
@@ -75,6 +75,12 @@ export async function buildProjectGraphUsingProjectFileMap(
7575
storedAllWorkspaceFiles = allWorkspaceFiles;
7676
storedRustReferences = rustReferences;
7777

78+
const projects: Record<string, ProjectConfiguration> = {};
79+
for (const root in projectRootMap) {
80+
const project = projectRootMap[root];
81+
projects[project.name] = project;
82+
}
83+
7884
const nxJson = readNxJson();
7985
const projectGraphVersion = '6.0';
8086
assertWorkspaceValidity(projects, nxJson);
@@ -233,15 +239,9 @@ function createContext(
233239
fileMap: FileMap,
234240
filesToProcess: FileMap
235241
): CreateDependenciesContext {
236-
const clonedProjects = Object.keys(projects).reduce((map, projectName) => {
237-
map[projectName] = {
238-
...projects[projectName],
239-
};
240-
return map;
241-
}, {} as Record<string, ProjectConfiguration>);
242242
return {
243243
nxJsonConfiguration: nxJson,
244-
projects: clonedProjects,
244+
projects,
245245
externalNodes,
246246
workspaceRoot,
247247
fileMap,

packages/nx/src/project-graph/file-map-utils.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ describe('fileMapUtils', () => {
88
version: 2,
99
projects: {
1010
demo: {
11+
name: 'demo',
1112
root: 'apps/demo',
1213
sourceRoot: 'apps/demo/src',
1314
projectType: 'application' as ProjectType,
1415
},
1516
'demo-e2e': {
17+
name: 'demo-e2e',
1618
root: 'apps/demo-e2e',
1719
sourceRoot: 'apps/demo-e2e/src',
1820
projectType: 'application' as ProjectType,
1921
},
2022
ui: {
23+
name: 'ui',
2124
root: 'libs/ui',
2225
sourceRoot: 'libs/ui/src',
2326
projectType: 'library' as ProjectType,

packages/nx/src/project-graph/file-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function getProjectsSyncNoInference(root: string, nxJson: NxJsonConfiguration) {
192192
...getDefaultPluginsSync(root),
193193
];
194194

195-
const projectRootMap: Map<string, ProjectConfiguration> = new Map();
195+
const projectRootMap: Record<string, ProjectConfiguration> = {};
196196

197197
// We iterate over plugins first - this ensures that plugins specified first take precedence.
198198
for (const plugin of plugins) {

packages/nx/src/project-graph/plugins/loader.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
registerTsConfigPaths,
1818
} from '../../plugins/js/utils/register';
1919
import {
20+
ProjectRootMappings,
2021
createProjectRootMappingsFromProjectConfigurations,
2122
findProjectForPath,
2223
} from '../utils/find-project-for-path';
@@ -111,31 +112,35 @@ function lookupLocalPlugin(
111112
projects: Record<string, ProjectConfiguration>,
112113
root = workspaceRoot
113114
) {
114-
const plugin = findNxProjectForImportPath(importPath, projects, root);
115-
if (!plugin) {
115+
const projectConfig = findNxProjectForImportPath(importPath, projects, root);
116+
if (!projectConfig) {
116117
return null;
117118
}
118119

119-
const projectConfig: ProjectConfiguration = projects[plugin];
120120
return { path: path.join(root, projectConfig.root), projectConfig };
121121
}
122122

123123
function findNxProjectForImportPath(
124124
importPath: string,
125125
projects: Record<string, ProjectConfiguration>,
126126
root = workspaceRoot
127-
): string | null {
127+
): ProjectConfiguration | null {
128128
const tsConfigPaths: Record<string, string[]> = readTsConfigPaths(root);
129129
const possiblePaths = tsConfigPaths[importPath]?.map((p) =>
130130
normalizePath(path.relative(root, path.join(root, p)))
131131
);
132132
if (possiblePaths?.length) {
133-
const projectRootMappings =
134-
createProjectRootMappingsFromProjectConfigurations(projects);
133+
const projectRootMappings: ProjectRootMappings = new Map();
134+
const projectNameMap = new Map<string, ProjectConfiguration>();
135+
for (const projectRoot in projects) {
136+
const project = projects[projectRoot];
137+
projectRootMappings.set(project.root, project.name);
138+
projectNameMap.set(project.name, project);
139+
}
135140
for (const tsConfigPath of possiblePaths) {
136141
const nxProject = findProjectForPath(tsConfigPath, projectRootMappings);
137142
if (nxProject) {
138-
return nxProject;
143+
return projectNameMap.get(nxProject);
139144
}
140145
}
141146
logger.verbose(

0 commit comments

Comments
 (0)