/
build-project-graph.ts
409 lines (382 loc) · 11 KB
/
build-project-graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { workspaceRoot } from '../utils/app-root';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { assertWorkspaceValidity } from '../utils/assert-workspace-validity';
import { FileData, readNxJson, readWorkspaceJson } from './file-utils';
import { normalizeNxJson } from './normalize-nx-json';
import {
createCache,
extractCachedFileData,
ProjectGraphCache,
readCache,
shouldRecomputeWholeGraph,
writeCache,
} from './nx-deps-cache';
import { buildImplicitProjectDependencies } from './build-dependencies';
import {
buildNpmPackageNodes,
buildWorkspaceProjectNodes,
} from './build-nodes';
import * as os from 'os';
import { buildExplicitTypescriptAndPackageJsonDependencies } from './build-dependencies/build-explicit-typescript-and-package-json-dependencies';
import { loadNxPlugins } from '../utils/nx-plugin';
import { defaultFileHasher } from '../hasher/file-hasher';
import { createProjectFileMap } from './file-map-utils';
import { getRootTsConfigPath } from '../utils/typescript';
import {
ProjectFileMap,
ProjectGraph,
ProjectGraphProcessorContext,
} from '../config/project-graph';
import { readJsonFile } from '../utils/fileutils';
import {
NxJsonConfiguration,
NxJsonProjectConfiguration,
} from '../config/nx-json';
import { logger } from '../utils/logger';
import { ProjectGraphBuilder } from './project-graph-builder';
import {
ProjectConfiguration,
WorkspaceJsonConfiguration,
} from '../config/workspace-json-project-json';
export async function buildProjectGraph() {
const workspaceJson = readWorkspaceJson();
const { projectFileMap, allWorkspaceFiles } = createProjectFileMap(
workspaceJson,
defaultFileHasher.allFileData()
);
const cacheEnabled = process.env.NX_CACHE_PROJECT_GRAPH !== 'false';
let cache = cacheEnabled ? readCache() : null;
return (
await buildProjectGraphUsingProjectFileMap(
workspaceJson,
projectFileMap,
allWorkspaceFiles,
cache,
cacheEnabled
)
).projectGraph;
}
export async function buildProjectGraphUsingProjectFileMap(
workspaceJson: WorkspaceJsonConfiguration,
projectFileMap: ProjectFileMap,
allWorkspaceFiles: FileData[],
cache: ProjectGraphCache | null,
shouldWriteCache: boolean
): Promise<{
projectGraph: ProjectGraph;
projectGraphCache: ProjectGraphCache;
}> {
const nxJson = readNxJson();
const projectGraphVersion = '5.0';
assertWorkspaceValidity(workspaceJson, nxJson);
const normalizedNxJson = normalizeNxJson(
nxJson,
Object.keys(workspaceJson.projects)
);
const packageJsonDeps = readCombinedDeps();
const rootTsConfig = readRootTsConfig();
let filesToProcess;
let cachedFileData;
if (
cache &&
!shouldRecomputeWholeGraph(
cache,
packageJsonDeps,
workspaceJson,
normalizedNxJson,
rootTsConfig
)
) {
const fromCache = extractCachedFileData(projectFileMap, cache);
filesToProcess = fromCache.filesToProcess;
cachedFileData = fromCache.cachedFileData;
} else {
filesToProcess = projectFileMap;
cachedFileData = {};
}
const context = createContext(
workspaceJson,
normalizedNxJson,
projectFileMap,
filesToProcess
);
let projectGraph = await buildProjectGraphUsingContext(
nxJson,
context,
cachedFileData,
projectGraphVersion
);
const projectGraphCache = createCache(
nxJson,
packageJsonDeps,
projectGraph,
rootTsConfig
);
if (shouldWriteCache) {
writeCache(projectGraphCache);
}
projectGraph.allWorkspaceFiles = allWorkspaceFiles;
return {
projectGraph,
projectGraphCache,
};
}
function readCombinedDeps() {
const json = readJsonFile(join(workspaceRoot, 'package.json'));
return { ...json.dependencies, ...json.devDependencies };
}
async function buildProjectGraphUsingContext(
nxJson: NxJsonConfiguration,
ctx: ProjectGraphProcessorContext,
cachedFileData: { [project: string]: { [file: string]: FileData } },
projectGraphVersion: string
) {
performance.mark('build project graph:start');
const builder = new ProjectGraphBuilder();
buildWorkspaceProjectNodes(ctx, builder);
buildNpmPackageNodes(builder);
for (const proj of Object.keys(cachedFileData)) {
for (const f of builder.graph.nodes[proj].data.files) {
const cached = cachedFileData[proj][f.file];
if (cached && cached.deps) {
f.deps = [...cached.deps];
}
}
}
await buildExplicitDependencies(jsPluginConfig(nxJson), ctx, builder);
buildImplicitProjectDependencies(ctx, builder);
builder.setVersion(projectGraphVersion);
const initProjectGraph = builder.getUpdatedProjectGraph();
const r = updateProjectGraphWithPlugins(ctx, initProjectGraph);
performance.mark('build project graph:end');
performance.measure(
'build project graph',
'build project graph:start',
'build project graph:end'
);
return r;
}
interface NrwlJsPluginConfig {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
}
function jsPluginConfig(nxJson: NxJsonConfiguration): NrwlJsPluginConfig {
return nxJson?.pluginsConfig?.['@nrwl/js'] ?? {};
}
function buildExplicitDependencies(
jsPluginConfig: {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
},
ctx: ProjectGraphProcessorContext,
builder: ProjectGraphBuilder
) {
let totalNumOfFilesToProcess = totalNumberOfFilesToProcess(ctx);
// using workers has an overhead, so we only do it when the number of
// files we need to process is >= 100 and there are more than 2 CPUs
// to be able to use at least 2 workers (1 worker per CPU and
// 1 CPU for the main thread)
if (totalNumOfFilesToProcess < 100 || getNumberOfWorkers() <= 2) {
return buildExplicitDependenciesWithoutWorkers(
jsPluginConfig,
ctx,
builder
);
} else {
return buildExplicitDependenciesUsingWorkers(
jsPluginConfig,
ctx,
totalNumOfFilesToProcess,
builder
);
}
}
function totalNumberOfFilesToProcess(ctx: ProjectGraphProcessorContext) {
let totalNumOfFilesToProcess = 0;
Object.values(ctx.filesToProcess).forEach(
(t) => (totalNumOfFilesToProcess += t.length)
);
return totalNumOfFilesToProcess;
}
function splitFilesIntoBins(
ctx: ProjectGraphProcessorContext,
totalNumOfFilesToProcess: number,
numberOfWorkers: number
) {
// we want to have numberOfWorkers * 5 bins
const filesPerBin =
Math.round(totalNumOfFilesToProcess / numberOfWorkers / 5) + 1;
const bins: ProjectFileMap[] = [];
let currentProjectFileMap = {};
let currentNumberOfFiles = 0;
for (const source of Object.keys(ctx.filesToProcess)) {
for (const f of Object.values(ctx.filesToProcess[source])) {
if (!currentProjectFileMap[source]) currentProjectFileMap[source] = [];
currentProjectFileMap[source].push(f);
currentNumberOfFiles++;
if (currentNumberOfFiles >= filesPerBin) {
bins.push(currentProjectFileMap);
currentProjectFileMap = {};
currentNumberOfFiles = 0;
}
}
}
bins.push(currentProjectFileMap);
return bins;
}
function createWorkerPool(numberOfWorkers: number) {
const res = [];
for (let i = 0; i < numberOfWorkers; ++i) {
res.push(
new (require('worker_threads').Worker)(
join(__dirname, './project-graph-worker.js'),
{
env: process.env,
}
)
);
}
return res;
}
function buildExplicitDependenciesWithoutWorkers(
jsPluginConfig: {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
},
ctx: ProjectGraphProcessorContext,
builder: ProjectGraphBuilder
) {
buildExplicitTypescriptAndPackageJsonDependencies(
jsPluginConfig,
ctx.workspace,
builder.graph,
ctx.filesToProcess
).forEach((r) => {
builder.addExplicitDependency(
r.sourceProjectName,
r.sourceProjectFile,
r.targetProjectName
);
});
}
function buildExplicitDependenciesUsingWorkers(
jsPluginConfig: {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
},
ctx: ProjectGraphProcessorContext,
totalNumOfFilesToProcess: number,
builder: ProjectGraphBuilder
) {
const numberOfWorkers = Math.min(
totalNumOfFilesToProcess,
getNumberOfWorkers()
);
const bins = splitFilesIntoBins(
ctx,
totalNumOfFilesToProcess,
numberOfWorkers
);
const workers = createWorkerPool(numberOfWorkers);
let numberOfExpectedResponses = bins.length;
return new Promise((res, reject) => {
for (let w of workers) {
w.on('message', (explicitDependencies) => {
explicitDependencies.forEach((r) => {
builder.addExplicitDependency(
r.sourceProjectName,
r.sourceProjectFile,
r.targetProjectName
);
});
if (bins.length > 0) {
w.postMessage({ filesToProcess: bins.shift() });
}
// we processed all the bins
if (--numberOfExpectedResponses === 0) {
for (let w of workers) {
w.terminate();
}
res(null);
}
});
w.on('error', reject);
w.on('exit', (code) => {
if (code !== 0) {
reject(
new Error(
`Unable to complete project graph creation. Worker stopped with exit code: ${code}`
)
);
}
});
w.postMessage({
workspace: ctx.workspace,
projectGraph: builder.graph,
jsPluginConfig,
});
w.postMessage({ filesToProcess: bins.shift() });
}
});
}
function getNumberOfWorkers(): number {
return process.env.NX_PROJECT_GRAPH_MAX_WORKERS
? +process.env.NX_PROJECT_GRAPH_MAX_WORKERS
: os.cpus().length - 1;
}
function createContext(
workspaceJson: WorkspaceJsonConfiguration,
nxJson: NxJsonConfiguration,
fileMap: ProjectFileMap,
filesToProcess: ProjectFileMap
): ProjectGraphProcessorContext {
const projects: Record<
string,
ProjectConfiguration & NxJsonProjectConfiguration
> = Object.keys(workspaceJson.projects).reduce((map, projectName) => {
map[projectName] = {
...workspaceJson.projects[projectName],
};
return map;
}, {});
return {
workspace: {
...workspaceJson,
...nxJson,
projects,
},
fileMap,
filesToProcess,
};
}
function updateProjectGraphWithPlugins(
context: ProjectGraphProcessorContext,
initProjectGraph: ProjectGraph
) {
const plugins = loadNxPlugins(context.workspace.plugins).filter(
(x) => !!x.processProjectGraph
);
return (plugins || []).reduce((graph, plugin) => {
try {
return plugin.processProjectGraph(graph, context);
} catch (e) {
const message = `Failed to process the project graph with "${plugin.name}". This will error in the future!`;
if (process.env.NX_VERBOSE_LOGGING === 'true') {
console.error(e);
logger.error(message);
return graph;
} else {
logger.warn(message);
logger.warn(`Run with NX_VERBOSE_LOGGING=true to see the error.`);
}
return graph;
}
}, initProjectGraph);
}
function readRootTsConfig() {
const tsConfigPath = getRootTsConfigPath();
if (tsConfigPath) {
return readJsonFile(tsConfigPath);
}
}