@smoothbricks/nx-plugin 0.3.13 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -10
- package/dist/bin/smoo-nx-exec.d.ts +3 -0
- package/dist/bin/smoo-nx-exec.d.ts.map +1 -0
- package/dist/bin/smoo-nx-exec.js +117 -0
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +5 -2
- package/dist/cargo-workspace.d.ts +15 -1
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +113 -8
- package/dist/ensure-built.d.ts +136 -0
- package/dist/ensure-built.d.ts.map +1 -0
- package/dist/ensure-built.js +419 -0
- package/dist/executors/typescript-emit/executor.d.ts.map +1 -1
- package/dist/executors/typescript-emit/executor.js +20 -2
- package/dist/executors/typescript-emit/schema.d.ts +1 -0
- package/dist/executors/typescript-emit/schema.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +335 -87
- package/package.json +11 -3
- package/src/bin/smoo-nx-exec.ts +127 -0
- package/src/bounded-test-policy.test.ts +21 -0
- package/src/bounded-test-policy.ts +6 -2
- package/src/cargo-workspace.test.ts +142 -0
- package/src/cargo-workspace.ts +137 -8
- package/src/ensure-built.test.ts +543 -0
- package/src/ensure-built.ts +602 -0
- package/src/executors/typescript-emit/executor.test.ts +37 -1
- package/src/executors/typescript-emit/executor.ts +24 -2
- package/src/executors/typescript-emit/schema.json +8 -0
- package/src/executors/typescript-emit/schema.ts +1 -0
- package/src/index.test.ts +173 -6
- package/src/index.ts +426 -89
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { chmod, mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
import type { Task } from 'nx/src/config/task-graph';
|
|
10
|
+
|
|
11
|
+
import { signalToCode } from 'nx/src/utils/exit-codes';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
cliExitOutcome,
|
|
15
|
+
describeMiss,
|
|
16
|
+
firstCacheMiss,
|
|
17
|
+
firstStaleOutputs,
|
|
18
|
+
parseTargetSelector,
|
|
19
|
+
} from './ensure-built.js';
|
|
20
|
+
|
|
21
|
+
function task(overrides: Partial<Task> & Pick<Task, 'id'>): Task {
|
|
22
|
+
const [project, target] = overrides.id.split(':');
|
|
23
|
+
return {
|
|
24
|
+
target: { project, target },
|
|
25
|
+
overrides: {},
|
|
26
|
+
outputs: [],
|
|
27
|
+
cache: true,
|
|
28
|
+
hash: `hash-of-${overrides.id}`,
|
|
29
|
+
...overrides,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('parseTargetSelector', () => {
|
|
34
|
+
it('accepts project:target and project:target:configuration', () => {
|
|
35
|
+
expect(parseTargetSelector('app:build')).toEqual({
|
|
36
|
+
project: 'app',
|
|
37
|
+
target: 'build',
|
|
38
|
+
configuration: undefined,
|
|
39
|
+
});
|
|
40
|
+
expect(parseTargetSelector('app:build:production')).toEqual({
|
|
41
|
+
project: 'app',
|
|
42
|
+
target: 'build',
|
|
43
|
+
configuration: 'production',
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('rejects anything that is not a two or three part selector', () => {
|
|
48
|
+
expect(parseTargetSelector('build')).toBeNull();
|
|
49
|
+
expect(parseTargetSelector('app:')).toBeNull();
|
|
50
|
+
expect(parseTargetSelector(':build')).toBeNull();
|
|
51
|
+
expect(parseTargetSelector('app:build:production:extra')).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('firstCacheMiss', () => {
|
|
56
|
+
const cached = new Map([
|
|
57
|
+
['hash-of-app:build', 0],
|
|
58
|
+
['hash-of-lib:build', 0],
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
it('clears a graph whose every task is a recorded success', () => {
|
|
62
|
+
expect(firstCacheMiss([task({ id: 'lib:build' }), task({ id: 'app:build' })], cached)).toBeNull();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('reports the uncacheable task rather than looking it up', () => {
|
|
66
|
+
expect(firstCacheMiss([task({ id: 'app:build', cache: false })], cached)).toEqual({
|
|
67
|
+
kind: 'uncacheable',
|
|
68
|
+
taskId: 'app:build',
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('reports a task that could not be hashed', () => {
|
|
73
|
+
expect(firstCacheMiss([task({ id: 'app:build', hash: undefined })], cached)).toEqual({
|
|
74
|
+
kind: 'unhashable',
|
|
75
|
+
taskId: 'app:build',
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('reports a task whose current inputs are not in the cache', () => {
|
|
80
|
+
expect(firstCacheMiss([task({ id: 'other:build' })], cached)).toEqual({
|
|
81
|
+
kind: 'not-cached',
|
|
82
|
+
taskId: 'other:build',
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('refuses to replay a cached failure', () => {
|
|
87
|
+
const failures = new Map([['hash-of-app:build', 3]]);
|
|
88
|
+
expect(firstCacheMiss([task({ id: 'app:build' })], failures)).toEqual({
|
|
89
|
+
kind: 'cached-failure',
|
|
90
|
+
taskId: 'app:build',
|
|
91
|
+
code: 3,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('names the first failing task in graph order', () => {
|
|
96
|
+
const tasks = [task({ id: 'lib:build' }), task({ id: 'app:build', cache: false }), task({ id: 'other:build' })];
|
|
97
|
+
expect(firstCacheMiss(tasks, cached)).toEqual({ kind: 'uncacheable', taskId: 'app:build' });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('firstStaleOutputs', () => {
|
|
102
|
+
const tasks = [task({ id: 'lib:build', outputs: ['lib/dist'] }), task({ id: 'app:build', outputs: ['app/dist'] })];
|
|
103
|
+
|
|
104
|
+
it('clears outputs the daemon vouches for', () => {
|
|
105
|
+
expect(firstStaleOutputs(tasks, [true, true])).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('reports the task whose outputs no longer match', () => {
|
|
109
|
+
expect(firstStaleOutputs(tasks, [true, false])).toEqual({ kind: 'stale-outputs', taskId: 'app:build' });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('treats a missing verdict as stale rather than as a hit', () => {
|
|
113
|
+
expect(firstStaleOutputs(tasks, [true])).toEqual({ kind: 'stale-outputs', taskId: 'app:build' });
|
|
114
|
+
expect(firstStaleOutputs(tasks, [])).toEqual({ kind: 'stale-outputs', taskId: 'lib:build' });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('describeMiss', () => {
|
|
119
|
+
it('names the task in every reason', () => {
|
|
120
|
+
const reasons = [
|
|
121
|
+
describeMiss({ kind: 'uncacheable', taskId: 'app:build' }),
|
|
122
|
+
describeMiss({ kind: 'unhashable', taskId: 'app:build' }),
|
|
123
|
+
describeMiss({ kind: 'not-cached', taskId: 'app:build' }),
|
|
124
|
+
describeMiss({ kind: 'cached-failure', taskId: 'app:build', code: 7 }),
|
|
125
|
+
describeMiss({ kind: 'stale-outputs', taskId: 'app:build' }),
|
|
126
|
+
describeMiss({ kind: 'cache-disabled', taskId: 'app:build' }),
|
|
127
|
+
describeMiss({ kind: 'no-daemon', taskId: 'app:build' }),
|
|
128
|
+
];
|
|
129
|
+
for (const reason of reasons) {
|
|
130
|
+
expect(reason).toContain('app:build');
|
|
131
|
+
}
|
|
132
|
+
expect(reasons[3]).toContain('7');
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('cliExitOutcome', () => {
|
|
137
|
+
const reason = { kind: 'no-daemon', taskId: 'app:build' } as const;
|
|
138
|
+
|
|
139
|
+
it('treats a clean exit as a completed build', () => {
|
|
140
|
+
expect(cliExitOutcome(reason, { code: 0, signal: null }, signalToCode)).toEqual({
|
|
141
|
+
disposition: 'built',
|
|
142
|
+
reason,
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('forwards a nonzero exit code verbatim', () => {
|
|
147
|
+
expect(cliExitOutcome(reason, { code: 3, signal: null }, signalToCode)).toEqual({
|
|
148
|
+
disposition: 'failed',
|
|
149
|
+
reason,
|
|
150
|
+
exitCode: 3,
|
|
151
|
+
signal: null,
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('reports a signal as a signal, not as a plain failure', () => {
|
|
156
|
+
expect(cliExitOutcome(reason, { code: null, signal: 'SIGTERM' }, signalToCode)).toEqual({
|
|
157
|
+
disposition: 'failed',
|
|
158
|
+
reason,
|
|
159
|
+
exitCode: signalToCode('SIGTERM'),
|
|
160
|
+
signal: 'SIGTERM',
|
|
161
|
+
});
|
|
162
|
+
expect(cliExitOutcome(reason, { code: null, signal: 'SIGINT' }, signalToCode).disposition).toBe('failed');
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
167
|
+
const repoRoot = dirname(dirname(packageRoot));
|
|
168
|
+
const binEntry = join(packageRoot, 'src', 'bin', 'smoo-nx-exec.ts');
|
|
169
|
+
const builtBinEntry = join(packageRoot, 'dist', 'bin', 'smoo-nx-exec.js');
|
|
170
|
+
const MARKER = 'EXEC_OK';
|
|
171
|
+
/** Nx keys controlled and reported by the fixture rather than inherited from its parent test task. */
|
|
172
|
+
const NX_ENV_KEYS = [
|
|
173
|
+
'NX_WORKSPACE_ROOT_PATH',
|
|
174
|
+
'NX_STREAM_OUTPUT',
|
|
175
|
+
'NX_PREFIX_OUTPUT',
|
|
176
|
+
'NX_LOAD_DOT_ENV_FILES',
|
|
177
|
+
'NX_SKIP_NX_CACHE',
|
|
178
|
+
'NX_DISABLE_NX_CACHE',
|
|
179
|
+
'NX_CACHE_DIRECTORY',
|
|
180
|
+
'NX_WORKSPACE_DATA_DIRECTORY',
|
|
181
|
+
];
|
|
182
|
+
const SHARED_NX_DIRECTORY_KEYS = ['NX_CACHE_DIRECTORY', 'NX_WORKSPACE_DATA_DIRECTORY'] as const;
|
|
183
|
+
|
|
184
|
+
interface BinRun {
|
|
185
|
+
readonly code: number | null;
|
|
186
|
+
readonly signal: NodeJS.Signals | null;
|
|
187
|
+
readonly stdout: string;
|
|
188
|
+
readonly stderr: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function fixtureNxEnvironment(env: Readonly<NodeJS.ProcessEnv> = {}): NodeJS.ProcessEnv {
|
|
192
|
+
const childEnv = { ...process.env };
|
|
193
|
+
for (const key of NX_ENV_KEYS) {
|
|
194
|
+
delete childEnv[key];
|
|
195
|
+
}
|
|
196
|
+
// The parent Nx process may set FORCE_COLOR while the shell exports
|
|
197
|
+
// NO_COLOR. Passing both to Bun emits a warning, which would make a genuine
|
|
198
|
+
// full hit look noisy.
|
|
199
|
+
delete childEnv.NO_COLOR;
|
|
200
|
+
childEnv.CI = '';
|
|
201
|
+
childEnv.NX_DAEMON = 'true';
|
|
202
|
+
for (const [key, value] of Object.entries(env)) {
|
|
203
|
+
if (value === undefined) {
|
|
204
|
+
delete childEnv[key];
|
|
205
|
+
} else {
|
|
206
|
+
childEnv[key] = value;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// CI shares these directories among the outer Nx tasks. A fixture Nx using
|
|
210
|
+
// either one can replace the orchestrator's project graph while sibling
|
|
211
|
+
// tasks are still running.
|
|
212
|
+
for (const key of SHARED_NX_DIRECTORY_KEYS) {
|
|
213
|
+
delete childEnv[key];
|
|
214
|
+
}
|
|
215
|
+
return childEnv;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The bin is exercised in a child process rather than by calling `ensureBuilt`
|
|
220
|
+
* directly, for two reasons that are not incidental: Nx binds its workspace
|
|
221
|
+
* root once per process, so a single test process cannot probe a fixture
|
|
222
|
+
* workspace and then anything else; and `execve` replaces the process, which is
|
|
223
|
+
* the behaviour under test.
|
|
224
|
+
*/
|
|
225
|
+
function runBinWith(
|
|
226
|
+
runtime: 'bun' | 'node',
|
|
227
|
+
entry: string,
|
|
228
|
+
workspace: string,
|
|
229
|
+
args: readonly string[],
|
|
230
|
+
env: Readonly<NodeJS.ProcessEnv>,
|
|
231
|
+
): Promise<BinRun> {
|
|
232
|
+
const childEnv = fixtureNxEnvironment(env);
|
|
233
|
+
const child = spawn(runtime, [entry, ...args], {
|
|
234
|
+
cwd: workspace,
|
|
235
|
+
env: childEnv,
|
|
236
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
237
|
+
});
|
|
238
|
+
let stdout = '';
|
|
239
|
+
let stderr = '';
|
|
240
|
+
child.stdout.on('data', (chunk: Buffer) => {
|
|
241
|
+
stdout += chunk.toString();
|
|
242
|
+
});
|
|
243
|
+
child.stderr.on('data', (chunk: Buffer) => {
|
|
244
|
+
stderr += chunk.toString();
|
|
245
|
+
});
|
|
246
|
+
// `Promise.withResolvers` would read better but needs lib es2024; this
|
|
247
|
+
// package inherits lib es2022 from tsconfig.base.json.
|
|
248
|
+
return new Promise((settle, reject) => {
|
|
249
|
+
child.once('error', reject);
|
|
250
|
+
child.once('close', (code, signal) => settle({ code, signal, stdout, stderr }));
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function runBin(workspace: string, args: readonly string[], env: Readonly<NodeJS.ProcessEnv> = {}): Promise<BinRun> {
|
|
255
|
+
return runBinWith('bun', binEntry, workspace, args, env);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function runBuiltBin(workspace: string, args: readonly string[]): Promise<BinRun> {
|
|
259
|
+
return runBinWith('node', builtBinEntry, workspace, args, {});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function nx(workspace: string, args: readonly string[]): Promise<number | null> {
|
|
263
|
+
const child = spawn(join(repoRoot, 'node_modules', '.bin', 'nx'), [...args], {
|
|
264
|
+
cwd: workspace,
|
|
265
|
+
env: fixtureNxEnvironment({ NX_WORKSPACE_ROOT_PATH: workspace }),
|
|
266
|
+
stdio: 'ignore',
|
|
267
|
+
});
|
|
268
|
+
return new Promise((settle, reject) => {
|
|
269
|
+
child.once('error', reject);
|
|
270
|
+
child.once('close', (code) => settle(code));
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
describe('smoo-nx-exec', () => {
|
|
275
|
+
let workspace = '';
|
|
276
|
+
const report = () => join(workspace, 'report');
|
|
277
|
+
const marker = () => join(workspace, 'packages', 'app', 'dist', 'marker.txt');
|
|
278
|
+
|
|
279
|
+
beforeAll(async () => {
|
|
280
|
+
// Realpath because macOS puts the temp directory behind a /private
|
|
281
|
+
// symlink, and a process's reported cwd is the resolved one.
|
|
282
|
+
workspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-')));
|
|
283
|
+
await mkdir(join(workspace, 'packages', 'app'), { recursive: true });
|
|
284
|
+
await mkdir(join(workspace, 'packages', 'lib'), { recursive: true });
|
|
285
|
+
// The repository's own node_modules, so the fixture resolves the same Nx
|
|
286
|
+
// this package is written against, including node_modules/.bin/nx for the
|
|
287
|
+
// daemon-disabled path.
|
|
288
|
+
await symlink(join(repoRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
|
|
289
|
+
await writeFile(join(workspace, 'nx.json'), JSON.stringify({ useDaemonProcess: true }));
|
|
290
|
+
await writeFile(
|
|
291
|
+
join(workspace, 'packages', 'lib', 'project.json'),
|
|
292
|
+
JSON.stringify({
|
|
293
|
+
name: 'lib',
|
|
294
|
+
targets: {
|
|
295
|
+
build: {
|
|
296
|
+
executor: 'nx:run-commands',
|
|
297
|
+
cache: true,
|
|
298
|
+
inputs: ['{projectRoot}/source.txt'],
|
|
299
|
+
outputs: ['{projectRoot}/dist'],
|
|
300
|
+
options: { command: 'mkdir -p dist && cat source.txt > dist/lib.txt', cwd: '{projectRoot}' },
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
await writeFile(join(workspace, 'packages', 'lib', 'source.txt'), 'lib\n');
|
|
306
|
+
await writeFile(
|
|
307
|
+
join(workspace, 'packages', 'app', 'project.json'),
|
|
308
|
+
JSON.stringify({
|
|
309
|
+
name: 'app',
|
|
310
|
+
targets: {
|
|
311
|
+
build: {
|
|
312
|
+
executor: 'nx:run-commands',
|
|
313
|
+
cache: true,
|
|
314
|
+
// `dependentTasksOutputFiles` is the discriminating part of this
|
|
315
|
+
// fixture: Nx's runner-warmup hasher deliberately leaves such a
|
|
316
|
+
// task unhashed, so a probe that used it instead of hashing every
|
|
317
|
+
// task would classify this graph 'unhashable' and never hit.
|
|
318
|
+
inputs: ['{projectRoot}/source.txt', { dependentTasksOutputFiles: '**/*' }],
|
|
319
|
+
outputs: ['{projectRoot}/dist'],
|
|
320
|
+
dependsOn: ['^build'],
|
|
321
|
+
options: { command: 'mkdir -p dist && cat source.txt > dist/marker.txt', cwd: '{projectRoot}' },
|
|
322
|
+
},
|
|
323
|
+
broken: {
|
|
324
|
+
executor: 'nx:run-commands',
|
|
325
|
+
cache: true,
|
|
326
|
+
options: { command: 'exit 3', cwd: '{projectRoot}' },
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
implicitDependencies: ['lib'],
|
|
330
|
+
}),
|
|
331
|
+
);
|
|
332
|
+
await writeFile(join(workspace, 'packages', 'app', 'source.txt'), 'built\n');
|
|
333
|
+
// Reports what the exec'd process actually inherited: the marker proves
|
|
334
|
+
// execve happened, the cwd proves it kept the caller's directory, and the
|
|
335
|
+
// NX_ lines prove temporary Nx configuration was removed or restored.
|
|
336
|
+
await writeFile(
|
|
337
|
+
report(),
|
|
338
|
+
`#!/bin/sh\necho ${MARKER} "$@"\necho "cwd=$PWD"\n${NX_ENV_KEYS.map((key) => `echo "${key}=\${${key}:-unset}"`).join('\n')}\n`,
|
|
339
|
+
);
|
|
340
|
+
await chmod(report(), 0o755);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
afterAll(async () => {
|
|
344
|
+
if (workspace) {
|
|
345
|
+
await nx(workspace, ['daemon', '--stop']);
|
|
346
|
+
await rm(workspace, { recursive: true, force: true });
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('builds the whole dependency graph on the first run, then execs the binary', async () => {
|
|
351
|
+
const run = await runBin(workspace, ['app:build', '--', './report', 'one']);
|
|
352
|
+
expect(run.code).toBe(0);
|
|
353
|
+
expect(run.stdout).toContain(`${MARKER} one`);
|
|
354
|
+
expect(existsSync(marker())).toBe(true);
|
|
355
|
+
expect(existsSync(join(workspace, 'packages', 'lib', 'dist', 'lib.txt'))).toBe(true);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('stays completely silent on a full cache hit', async () => {
|
|
359
|
+
const run = await runBin(workspace, ['app:build', '--', './report']);
|
|
360
|
+
expect(run.code).toBe(0);
|
|
361
|
+
expect(run.stderr).toBe('');
|
|
362
|
+
// Nothing but the exec'd binary's own output: no Nx banner, no task log.
|
|
363
|
+
expect(run.stdout.split('\n')[0]).toBe(MARKER);
|
|
364
|
+
expect(run.stdout).not.toContain('nx run');
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('runs the emitted wrapper under Node without losing the hot path', async () => {
|
|
368
|
+
const run = await runBuiltBin(workspace, ['app:build', '--', './report']);
|
|
369
|
+
expect(run.code).toBe(0);
|
|
370
|
+
expect(run.stderr).toBe('');
|
|
371
|
+
expect(run.stdout.split('\n')[0]).toBe(MARKER);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it('leaves the exec\u0027d binary the caller\u0027s cwd and none of Nx\u0027s environment', async () => {
|
|
375
|
+
// Invoked from a subdirectory, with the binary named relative to it: the
|
|
376
|
+
// resolution the wrapper's users actually perform.
|
|
377
|
+
const run = await runBin(join(workspace, 'packages', 'app'), [
|
|
378
|
+
'app:build',
|
|
379
|
+
'--workspace-root',
|
|
380
|
+
workspace,
|
|
381
|
+
'--',
|
|
382
|
+
'../../report',
|
|
383
|
+
]);
|
|
384
|
+
expect(run.code).toBe(0);
|
|
385
|
+
expect(run.stdout).toContain(`cwd=${join(workspace, 'packages', 'app')}`);
|
|
386
|
+
for (const key of NX_ENV_KEYS) {
|
|
387
|
+
expect(run.stdout).toContain(`${key}=unset`);
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('keeps fixture Nx out of CI shared state directories', async () => {
|
|
392
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], {
|
|
393
|
+
NX_CACHE_DIRECTORY: join(workspace, 'shared-cache'),
|
|
394
|
+
NX_WORKSPACE_DATA_DIRECTORY: join(workspace, 'shared-workspace-data'),
|
|
395
|
+
});
|
|
396
|
+
expect(run.code).toBe(0);
|
|
397
|
+
expect(run.stdout).toContain('NX_CACHE_DIRECTORY=unset');
|
|
398
|
+
expect(run.stdout).toContain('NX_WORKSPACE_DATA_DIRECTORY=unset');
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('restores the caller cache controls before exec', async () => {
|
|
402
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], {
|
|
403
|
+
NX_SKIP_NX_CACHE: 'caller-skip',
|
|
404
|
+
NX_DISABLE_NX_CACHE: 'caller-disable',
|
|
405
|
+
});
|
|
406
|
+
expect(run.code).toBe(0);
|
|
407
|
+
expect(run.stderr).toBe('');
|
|
408
|
+
expect(run.stdout).toContain('NX_SKIP_NX_CACHE=caller-skip');
|
|
409
|
+
expect(run.stdout).toContain('NX_DISABLE_NX_CACHE=caller-disable');
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it('honors a user cache bypass outside an Nx task child', async () => {
|
|
413
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], {
|
|
414
|
+
NX_TASK_TARGET_PROJECT: undefined,
|
|
415
|
+
NX_TASK_TARGET_TARGET: undefined,
|
|
416
|
+
NX_SKIP_NX_CACHE: 'true',
|
|
417
|
+
NX_VERBOSE_LOGGING: 'true',
|
|
418
|
+
});
|
|
419
|
+
expect(run.code).toBe(0);
|
|
420
|
+
expect(run.stderr).toContain('the Nx cache is disabled, so app:build must run');
|
|
421
|
+
expect(run.stdout).toContain(MARKER);
|
|
422
|
+
expect(run.stdout).toContain('NX_SKIP_NX_CACHE=true');
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('runs again once a recorded output is gone from the working tree', async () => {
|
|
426
|
+
await rm(marker());
|
|
427
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], { NX_VERBOSE_LOGGING: 'true' });
|
|
428
|
+
expect(run.code).toBe(0);
|
|
429
|
+
expect(run.stderr).toContain('outputs are missing or modified on disk');
|
|
430
|
+
expect(run.stdout).toContain(MARKER);
|
|
431
|
+
expect(existsSync(marker())).toBe(true);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it('runs again when a dependency\u0027s own inputs change', async () => {
|
|
435
|
+
await writeFile(join(workspace, 'packages', 'lib', 'source.txt'), 'lib changed\n');
|
|
436
|
+
const changed = await runBin(workspace, ['app:build', '--', './report'], { NX_VERBOSE_LOGGING: 'true' });
|
|
437
|
+
expect(changed.code).toBe(0);
|
|
438
|
+
expect(changed.stderr).toContain('lib:build has no cached result');
|
|
439
|
+
|
|
440
|
+
// And the graph settles back to silence, which is only reachable if the
|
|
441
|
+
// dependent-outputs task was hashed too.
|
|
442
|
+
const settled = await runBin(workspace, ['app:build', '--', './report']);
|
|
443
|
+
expect(settled.code).toBe(0);
|
|
444
|
+
expect(settled.stderr).toBe('');
|
|
445
|
+
expect(settled.stdout.split('\n')[0]).toBe(MARKER);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('forwards a failing target\u0027s exit code and never execs', async () => {
|
|
449
|
+
const run = await runBin(workspace, ['app:broken', '--', './report']);
|
|
450
|
+
expect(run.code).toBe(1);
|
|
451
|
+
expect(run.stdout).not.toContain(MARKER);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it('falls back to the workspace nx CLI when the daemon is disabled', async () => {
|
|
455
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], {
|
|
456
|
+
NX_DAEMON: 'false',
|
|
457
|
+
NX_VERBOSE_LOGGING: 'true',
|
|
458
|
+
});
|
|
459
|
+
expect(run.code).toBe(0);
|
|
460
|
+
expect(run.stderr).toContain('daemon is disabled');
|
|
461
|
+
expect(run.stdout).toContain(MARKER);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('prints the message from a plain-object Nx rejection', async () => {
|
|
465
|
+
const rejectingWorkspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-rejection-')));
|
|
466
|
+
try {
|
|
467
|
+
await mkdir(join(rejectingWorkspace, 'node_modules', 'nx', 'src', 'daemon', 'client'), { recursive: true });
|
|
468
|
+
await mkdir(join(rejectingWorkspace, 'node_modules', 'nx', 'src', 'utils'), { recursive: true });
|
|
469
|
+
await writeFile(join(rejectingWorkspace, 'package.json'), '{}');
|
|
470
|
+
await writeFile(join(rejectingWorkspace, 'nx.json'), '{}');
|
|
471
|
+
await writeFile(
|
|
472
|
+
join(rejectingWorkspace, 'node_modules', 'nx', 'package.json'),
|
|
473
|
+
JSON.stringify({ name: 'nx', type: 'commonjs' }),
|
|
474
|
+
);
|
|
475
|
+
await writeFile(
|
|
476
|
+
join(rejectingWorkspace, 'node_modules', 'nx', 'src', 'utils', 'workspace-root.js'),
|
|
477
|
+
'exports.workspaceRoot = process.env.NX_WORKSPACE_ROOT_PATH;\n',
|
|
478
|
+
);
|
|
479
|
+
await writeFile(
|
|
480
|
+
join(rejectingWorkspace, 'node_modules', 'nx', 'src', 'daemon', 'client', 'client.js'),
|
|
481
|
+
[
|
|
482
|
+
'exports.daemonClient = {',
|
|
483
|
+
' enabled() {',
|
|
484
|
+
" throw { stack: 'synthetic stack', message: 'daemon belongs to a different workspace' };",
|
|
485
|
+
' },',
|
|
486
|
+
'};',
|
|
487
|
+
'',
|
|
488
|
+
].join('\n'),
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
const run = await runBin(rejectingWorkspace, ['app:build', '--', './never-runs']);
|
|
492
|
+
expect(run.code).toBe(1);
|
|
493
|
+
expect(run.stderr).toContain('daemon belongs to a different workspace');
|
|
494
|
+
expect(run.stderr).not.toContain('[object Object]');
|
|
495
|
+
} finally {
|
|
496
|
+
await rm(rejectingWorkspace, { recursive: true, force: true });
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it('rejects a malformed invocation with a usage error', async () => {
|
|
501
|
+
const missingSeparator = await runBin(workspace, ['app:build']);
|
|
502
|
+
expect(missingSeparator.code).toBe(2);
|
|
503
|
+
expect(missingSeparator.stderr).toContain('missing `--`');
|
|
504
|
+
|
|
505
|
+
const badTarget = await runBin(workspace, ['build', '--', './report']);
|
|
506
|
+
expect(badTarget.code).toBe(2);
|
|
507
|
+
expect(badTarget.stderr).toContain('not a project:target');
|
|
508
|
+
|
|
509
|
+
const pathLookup = await runBin(workspace, ['app:build', '--', 'report']);
|
|
510
|
+
expect(pathLookup.code).toBe(2);
|
|
511
|
+
expect(pathLookup.stderr).toContain('not a name to look up on PATH');
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
describe('smoo-nx-exec signal forwarding', () => {
|
|
516
|
+
let workspace = '';
|
|
517
|
+
|
|
518
|
+
// A workspace whose `nx` is a script that kills itself. The daemon-disabled
|
|
519
|
+
// path spawns exactly that binary, so this is the whole signal path end to
|
|
520
|
+
// end — and deterministic, unlike racing a real build with a kill.
|
|
521
|
+
beforeAll(async () => {
|
|
522
|
+
workspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-signal-')));
|
|
523
|
+
await mkdir(join(workspace, 'node_modules', '.bin'), { recursive: true });
|
|
524
|
+
await writeFile(join(workspace, 'nx.json'), '{}');
|
|
525
|
+
const fakeNx = join(workspace, 'node_modules', '.bin', 'nx');
|
|
526
|
+
await writeFile(fakeNx, '#!/bin/sh\nkill -TERM $$\n');
|
|
527
|
+
await chmod(fakeNx, 0o755);
|
|
528
|
+
await writeFile(join(workspace, 'report'), `#!/bin/sh\necho ${MARKER}\n`);
|
|
529
|
+
await chmod(join(workspace, 'report'), 0o755);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
afterAll(async () => {
|
|
533
|
+
if (workspace) {
|
|
534
|
+
await rm(workspace, { recursive: true, force: true });
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it('re-raises the signal that killed nx instead of flattening it to a code', async () => {
|
|
539
|
+
const run = await runBin(workspace, ['app:build', '--', './report'], { NX_DAEMON: 'false' });
|
|
540
|
+
expect(run.signal).toBe('SIGTERM');
|
|
541
|
+
expect(run.stdout).not.toContain(MARKER);
|
|
542
|
+
});
|
|
543
|
+
});
|