forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project-graph.ts
208 lines (194 loc) · 7.09 KB
/
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
import { ProjectGraphCache, readCache } from './nx-deps-cache';
import { buildProjectGraph } from './build-project-graph';
import { readNxJson, workspaceFileName } from './file-utils';
import { output } from '../utils/output';
import { isCI } from '../utils/is-ci';
import { defaultFileHasher } from '../hasher/file-hasher';
import {
isDaemonDisabled,
markDaemonAsDisabled,
writeDaemonLogs,
} from '../daemon/tmp-dir';
import { statSync } from 'fs';
import { ProjectGraph, ProjectGraphV4 } from '../config/project-graph';
import { stripIndents } from '../utils/strip-indents';
/**
* Synchronously reads the latest cached copy of the workspace's ProjectGraph.
* @throws {Error} if there is no cached ProjectGraph to read from
*/
export function readCachedProjectGraph(): ProjectGraph {
const projectGraphCache: ProjectGraphCache | false = readCache();
const angularSpecificError =
workspaceFileName() === 'angular.json'
? stripIndents`
Make sure invoke 'node ./decorate-angular-cli.js' in your postinstall script.
The decorated CLI will compute the project graph.
'ng --help' should say 'Smart, Fast and Extensible Build System'.
`
: '';
if (!projectGraphCache) {
throw new Error(stripIndents`
[readCachedProjectGraph] ERROR: No cached ProjectGraph is available.
If you are leveraging \`readCachedProjectGraph()\` directly then you will need to refactor your usage to first ensure that
the ProjectGraph is created by calling \`await createProjectGraphAsync()\` somewhere before attempting to read the data.
If you encounter this error as part of running standard \`nx\` commands then please open an issue on https://github.com/nrwl/nx
${angularSpecificError}
`);
}
const projectGraph = {
version: projectGraphCache.version,
nodes: projectGraphCache.nodes,
externalNodes: projectGraphCache.externalNodes,
dependencies: projectGraphCache.dependencies,
} as ProjectGraph;
return projectGraphAdapter(
projectGraph.version,
'5.0',
projectGraph
) as ProjectGraph;
}
async function buildProjectGraphWithoutDaemon() {
await defaultFileHasher.ensureInitialized();
return await buildProjectGraph();
}
/**
* Computes and returns a ProjectGraph.
*
* Nx will compute the graph either in a daemon process or in the current process.
*
* Nx will compute it in the current process if:
* * The process is running in CI (CI env variable is to true or other common variables used by CI providers are set).
* * It is running in the docker container.
* * The daemon process is disabled because of the previous error when starting the daemon.
* * `NX_DAEMON` is set to `false`.
* * `useDaemon` is set to false in `nx.json`
*
* `NX_DAEMON` env variable takes precedence:
* * If it is set to true, the daemon will always be used.
* * If it is set to false, the graph will always be computed in the current process.
*
* Tip: If you want to debug project graph creation, run your command with NX_DAEMON=false.
*
* Nx uses two layers of caching: the information about explicit dependencies stored on the disk and the information
* stored in the daemon process. To reset both run: `nx reset`.
*/
export async function createProjectGraphAsync(): Promise<ProjectGraph> {
const nxJson = readNxJson();
const useDaemonProcessOption =
nxJson.tasksRunnerOptions?.['default']?.options?.useDaemonProcess;
const env = process.env.NX_DAEMON;
// env takes precedence
// option=true,env=false => no daemon
// option=false,env=undefined => no daemon
// option=false,env=false => no daemon
// option=undefined,env=undefined => daemon
// option=true,env=true => daemon
// option=false,env=true => daemon
if (
isCI() ||
isDocker() ||
isDaemonDisabled() ||
(useDaemonProcessOption === undefined && env === 'false') ||
(useDaemonProcessOption === true && env === 'false') ||
(useDaemonProcessOption === false && env === undefined) ||
(useDaemonProcessOption === false && env === 'false')
) {
return await buildProjectGraphWithoutDaemon();
} else {
try {
const daemonClient = require('../daemon/client/client');
if (!(await daemonClient.isServerAvailable())) {
await daemonClient.startInBackground();
}
return daemonClient.getProjectGraphFromServer();
} catch (e) {
if (e.message.indexOf('inotify_add_watch') > -1) {
// common errors with the daemon due to OS settings (cannot watch all the files available)
output.note({
title: `Unable to start Nx Daemon due to the limited amount of inotify watches, continuing without the daemon.`,
bodyLines: [
'For more information read: https://askubuntu.com/questions/1088272/inotify-add-watch-failed-no-space-left-on-device',
'Nx Daemon is going to be disabled until you run "nx reset".',
],
});
} else {
const errorLogFile = writeDaemonLogs(e.message);
output.warn({
title: `Nx Daemon was not able to compute the project graph.`,
bodyLines: [
`Log file with the error: ${errorLogFile}`,
`Please file an issue at https://github.com/nrwl/nx`,
'Nx Daemon is going to be disabled until you run "nx reset".',
],
});
}
markDaemonAsDisabled();
return buildProjectGraphWithoutDaemon();
}
}
}
function isDocker() {
try {
statSync('/.dockerenv');
return true;
} catch {
return false;
}
}
function printErrorMessage(e: any) {
const lines = e.message.split('\n');
output.error({
title: lines[0],
bodyLines: lines.slice(1),
});
}
/**
* Backwards compatibility adapter for project graph
* @param {string} sourceVersion
* @param {string} targetVersion
* @param projectGraph
* @param {ProjectGraph} projectGraph
* @returns {ProjectGraph}
*/
export function projectGraphAdapter(
sourceVersion: string,
targetVersion: string,
projectGraph: ProjectGraph
): ProjectGraph | ProjectGraphV4 {
if (sourceVersion === targetVersion) {
return projectGraph;
}
if (sourceVersion === '5.0' && targetVersion === '4.0') {
return projectGraphCompat5to4(projectGraph as ProjectGraph);
}
throw new Error(
`Invalid source or target versions. Source: ${sourceVersion}, Target: ${targetVersion}.
Only backwards compatibility between "5.0" and "4.0" is supported.
This error can be caused by "@nrwl/..." packages getting out of sync or outdated project graph cache.
Check the versions running "nx report" and/or remove your "nxdeps.json" file (in node_modules/.cache/nx folder).
`
);
}
/**
* Backwards compatibility adapter for project Nodes v4 to v5
* @param {ProjectGraph} projectGraph
* @returns {ProjectGraph}
*/
function projectGraphCompat5to4(projectGraph: ProjectGraph): ProjectGraphV4 {
const { externalNodes, ...rest } = projectGraph;
return {
...rest,
nodes: {
...projectGraph.nodes,
...externalNodes,
},
dependencies: {
...projectGraph.dependencies,
...Object.keys(externalNodes).reduce(
(acc, key) => ({ ...acc, [`npm:${key}`]: [] }),
{}
),
},
version: '4.0',
};
}