@smoothbricks/nx-plugin 0.5.5 → 0.5.7
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 +59 -11
- package/dist/ensure-built-receipt.d.ts +2 -2
- package/dist/ensure-built-receipt.d.ts.map +1 -1
- package/dist/ensure-built-receipt.js +7 -2
- package/dist/ensure-built-witness.d.ts +12 -1
- package/dist/ensure-built-witness.d.ts.map +1 -1
- package/dist/ensure-built-witness.js +32 -0
- package/dist/ensure-built.d.ts +2 -2
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +27 -5
- package/dist/executors/all-verdicts/executor.d.ts +35 -0
- package/dist/executors/all-verdicts/executor.d.ts.map +1 -0
- package/dist/executors/all-verdicts/executor.js +133 -0
- package/dist/executors/all-verdicts/schema.d.ts +10 -0
- package/dist/executors/all-verdicts/schema.d.ts.map +1 -0
- package/dist/executors/all-verdicts/schema.js +2 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +48 -11
- package/dist/executors/bounded-exec/progress.d.ts +1 -3
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
- package/dist/executors/bounded-exec/progress.js +1 -2
- package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
- package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
- package/dist/executors/bounded-exec/runner-hook.js +301 -0
- package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
- package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
- package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/producer-receipts.d.ts +24 -1
- package/dist/producer-receipts.d.ts.map +1 -1
- package/dist/producer-receipts.js +91 -60
- package/executors.json +5 -0
- package/managed/raw/git-format-staged.yml +16 -3
- package/managed/raw/patches/nx@23.2.1.patch +397 -33
- package/package.json +2 -2
- package/src/ensure-built-receipt.ts +8 -2
- package/src/ensure-built-witness.ts +29 -1
- package/src/ensure-built.ts +39 -7
- package/src/executors/all-verdicts/executor.test.ts +135 -0
- package/src/executors/all-verdicts/executor.ts +164 -0
- package/src/executors/all-verdicts/schema.json +22 -0
- package/src/executors/all-verdicts/schema.ts +10 -0
- package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
- package/src/executors/bounded-exec/executor.test.ts +89 -42
- package/src/executors/bounded-exec/executor.ts +45 -11
- package/src/executors/bounded-exec/progress.test.ts +1 -14
- package/src/executors/bounded-exec/progress.ts +2 -5
- package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
- package/src/executors/bounded-exec/runner-hook.ts +292 -0
- package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
- package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
- package/src/index.test.ts +7 -5
- package/src/index.ts +4 -1
- package/src/nx-daemon-start-never.test.ts +137 -0
- package/src/producer-receipts.test.ts +99 -0
- package/src/producer-receipts.ts +111 -58
- package/test-timings.json +101 -96
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { shellWord } from '../../runtime-input.js';
|
|
3
|
+
import { invocationSpan, withInvocationFlag } from './cpu-tokens.js';
|
|
4
|
+
import { GO_TEST } from './verdict.js';
|
|
5
|
+
|
|
6
|
+
const CARGO_TEST = /^cargo(?:\s+(?:-\S+|\+\S+))*\s+test(?=\s|$)/;
|
|
7
|
+
const MESSAGE_FORMAT = /(?<![\w-])--message-format(?:=|\s+)(?:"[^"]*"|'[^']*'|[^\s]+)/g;
|
|
8
|
+
export type RunnerPhase = 'build' | 'active' | 'terminal';
|
|
9
|
+
export interface RunnerPhaseEvent {
|
|
10
|
+
id: number;
|
|
11
|
+
phase: RunnerPhase;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Only complete, direct chains have a known lifecycle. Shell wrappers remain bounded from spawn. */
|
|
15
|
+
export function withRunnerLifecycle(command: string): { command: string; runners: number } | null {
|
|
16
|
+
// Redirections could replace the private descriptor; newlines/backticks/escapes need a real shell parser.
|
|
17
|
+
if (/[<>\n\r`\\]/.test(command)) return null;
|
|
18
|
+
let rest = command.trim();
|
|
19
|
+
const parts: string[] = [];
|
|
20
|
+
let runners = 0;
|
|
21
|
+
const hook = fileURLToPath(
|
|
22
|
+
new URL(import.meta.url.endsWith('.ts') ? './runner-hook.ts' : './runner-hook.js', import.meta.url),
|
|
23
|
+
);
|
|
24
|
+
while (rest !== '') {
|
|
25
|
+
const kind = rest.search(GO_TEST) === 0 ? 'go' : CARGO_TEST.test(rest) ? 'cargo' : null;
|
|
26
|
+
if (kind === null) return null;
|
|
27
|
+
const invocation = kind === 'go' ? GO_TEST : CARGO_TEST;
|
|
28
|
+
const { argsEnd } = invocationSpan(rest, invocation);
|
|
29
|
+
let runner = rest.slice(0, argsEnd).trimEnd();
|
|
30
|
+
if (kind === 'cargo') {
|
|
31
|
+
// Only Cargo's own arguments, never those after the libtest separator, select build reporting.
|
|
32
|
+
const separator = /\s--(?:\s|$)/.exec(runner);
|
|
33
|
+
const end = separator?.index ?? runner.length;
|
|
34
|
+
runner =
|
|
35
|
+
withInvocationFlag(
|
|
36
|
+
runner.slice(0, end),
|
|
37
|
+
invocation,
|
|
38
|
+
MESSAGE_FORMAT,
|
|
39
|
+
'--message-format=json-render-diagnostics',
|
|
40
|
+
true,
|
|
41
|
+
) + runner.slice(end);
|
|
42
|
+
}
|
|
43
|
+
// The hook owns an inherited duplex descriptor, which Bun's net.Socket({fd}) does not adopt.
|
|
44
|
+
const runtime = process.versions.bun === undefined ? process.execPath : 'node';
|
|
45
|
+
parts.push(`${shellWord(runtime)} ${shellWord(hook)} ${runners} ${kind} ${runner}`);
|
|
46
|
+
runners += 1;
|
|
47
|
+
rest = rest.slice(argsEnd).trimStart();
|
|
48
|
+
if (rest === '') break;
|
|
49
|
+
const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
|
|
50
|
+
if (separator === null) return null;
|
|
51
|
+
parts.push(separator[0].trim());
|
|
52
|
+
rest = rest.slice(separator[0].length);
|
|
53
|
+
if (rest === '') return null;
|
|
54
|
+
}
|
|
55
|
+
return runners === 0 ? null : { command: parts.join(' '), runners };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A private supervisor channel, not runner output, grants authority to pause the test clock. */
|
|
59
|
+
export function createRunnerLifecycle(runners: number) {
|
|
60
|
+
let current = 0;
|
|
61
|
+
let phase: RunnerPhase = 'active'; // Shell and supervisor startup are bounded until their build declaration.
|
|
62
|
+
let failure: string | null = null;
|
|
63
|
+
return {
|
|
64
|
+
isTesting: (): boolean => failure !== null || phase === 'active' || (phase === 'terminal' && current < runners - 1),
|
|
65
|
+
error: (): string | null => failure,
|
|
66
|
+
finished: (): boolean => phase === 'terminal' && failure === null,
|
|
67
|
+
accept(line: string): RunnerPhaseEvent | null {
|
|
68
|
+
if (failure !== null) return null;
|
|
69
|
+
let event: unknown;
|
|
70
|
+
try {
|
|
71
|
+
event = JSON.parse(line);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
74
|
+
failure = 'invalid JSON on the runner lifecycle channel';
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
typeof event !== 'object' ||
|
|
79
|
+
event === null ||
|
|
80
|
+
!('id' in event) ||
|
|
81
|
+
!('phase' in event) ||
|
|
82
|
+
typeof event.id !== 'number' ||
|
|
83
|
+
!Number.isSafeInteger(event.id) ||
|
|
84
|
+
event.id < current ||
|
|
85
|
+
event.id >= runners ||
|
|
86
|
+
(event.id === current && phase === 'terminal') ||
|
|
87
|
+
(event.phase !== 'build' && event.phase !== 'active' && event.phase !== 'terminal')
|
|
88
|
+
) {
|
|
89
|
+
failure = 'invalid or retired invocation on the runner lifecycle channel';
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
// && and || may skip invocations. A new supervisor can advance only after the previous one ended.
|
|
93
|
+
if (event.id !== current && phase !== 'terminal') {
|
|
94
|
+
failure = 'overlapping invocations on a serial runner lifecycle channel';
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
current = event.id;
|
|
98
|
+
phase = event.phase;
|
|
99
|
+
return { id: current, phase };
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
package/src/index.test.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { BOUNDED_TEST_IDLE_TIMEOUT_MS, BOUNDED_TEST_KILL_AFTER_MS } from './boun
|
|
|
24
24
|
import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
|
|
25
25
|
import { exceptionalTestFilter, packageNameFromCargoTestTarget } from './cargo-workspace.js';
|
|
26
26
|
import { CARGO_CROSS_LINT_COMMAND, CARGO_CROSS_LINT_TARGET, CARGO_LINT_CLIPPY_COMMAND } from './cross-check-policy.js';
|
|
27
|
+
import { ALL_VERDICTS_EXECUTOR } from './executors/all-verdicts/schema.js';
|
|
27
28
|
import { cargoCommandNpmPackages, createNodesV2, createNodesV2ForPlatform } from './index.js';
|
|
28
29
|
import { NEXTEST_EXTRACTION_DIRECTORY } from './nextest-extraction.js';
|
|
29
30
|
import { applyWorkspaceConfig, RELEASE_CONFIGURATION } from './workspace-config-policy.js';
|
|
@@ -188,9 +189,9 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
188
189
|
'patches/nx.patch',
|
|
189
190
|
]);
|
|
190
191
|
const before = context.hashFilesMatchingGlob(positive, negative);
|
|
191
|
-
await workspace.write('patches/README.md', name
|
|
192
|
-
await workspace.write('patches/compiler/notes.md', name
|
|
193
|
-
await workspace.write('packages/unrelated/source.ts',
|
|
192
|
+
await workspace.write('patches/README.md', `${name} docs\n`);
|
|
193
|
+
await workspace.write('patches/compiler/notes.md', `${name} nested docs\n`);
|
|
194
|
+
await workspace.write('packages/unrelated/source.ts', `export const value = ${name.length};\n`);
|
|
194
195
|
context.incrementalUpdate(
|
|
195
196
|
['patches/README.md', 'patches/compiler/notes.md', 'packages/unrelated/source.ts'],
|
|
196
197
|
[],
|
|
@@ -198,7 +199,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
198
199
|
expect(context.hashFilesMatchingGlob(positive, negative)).toBe(before);
|
|
199
200
|
let previous = before;
|
|
200
201
|
for (const file of ['patches/nx.patch', 'patches/compiler/PIN', 'patches/compiler/VERSION']) {
|
|
201
|
-
await workspace.write(file, name
|
|
202
|
+
await workspace.write(file, `${name} changed ${file}\n`);
|
|
202
203
|
context.incrementalUpdate([file], []);
|
|
203
204
|
const changed = context.hashFilesMatchingGlob(positive, negative);
|
|
204
205
|
expect(changed).not.toBe(previous);
|
|
@@ -1053,13 +1054,14 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
1053
1054
|
const biome = `biome check --files-ignore-unknown=true '${root}'`;
|
|
1054
1055
|
const rust = await inferProjectTargets(workspace, `${root}/package.json`);
|
|
1055
1056
|
const rustLint = resolveLint(rust);
|
|
1056
|
-
expect(rustLint.executor).toBe(
|
|
1057
|
+
expect(rustLint.executor).toBe(ALL_VERDICTS_EXECUTOR);
|
|
1057
1058
|
expect(rustLint.dependsOn).toEqual(['cargo-lint']);
|
|
1058
1059
|
expect(rustLint.options?.commands).toEqual([biome]);
|
|
1059
1060
|
|
|
1060
1061
|
// A JavaScript source joins ESLint's file list the moment it appears, with no tsconfig to announce it.
|
|
1061
1062
|
await workspace.write(`${root}/src/check.js`, 'debugger;\n');
|
|
1062
1063
|
const withJavaScript = resolveLint(await inferProjectTargets(workspace, `${root}/package.json`));
|
|
1064
|
+
expect(withJavaScript.executor).toBe(ALL_VERDICTS_EXECUTOR);
|
|
1063
1065
|
expect(withJavaScript.dependsOn).toEqual(['cargo-lint']);
|
|
1064
1066
|
expect(withJavaScript.options?.commands).toEqual([biome, `eslint '${root}/src/check.js'`]);
|
|
1065
1067
|
|
package/src/index.ts
CHANGED
|
@@ -66,6 +66,7 @@ import {
|
|
|
66
66
|
runsFrozenCargo,
|
|
67
67
|
} from './cross-check-policy.js';
|
|
68
68
|
import type { EslintFileSet } from './eslint-file-set.js';
|
|
69
|
+
import { ALL_VERDICTS_EXECUTOR } from './executors/all-verdicts/schema.js';
|
|
69
70
|
import { closeRunCpuPool, serveRunCpuPool } from './executors/bounded-exec/run-cpu-pool.js';
|
|
70
71
|
import { hashVersionlessCrateManifests } from './manifest-hash.js';
|
|
71
72
|
import {
|
|
@@ -1939,9 +1940,11 @@ async function createProjectTargets(
|
|
|
1939
1940
|
inputs: lintProgramInputs,
|
|
1940
1941
|
eslintFileSet,
|
|
1941
1942
|
} = projectLintCommands(projectRoot, workspaceRoot, eslintSources);
|
|
1943
|
+
// Every lint tool reaches its own verdict in one run: run-commands would end the
|
|
1944
|
+
// other tools once one fails, and their findings would surface a run later.
|
|
1942
1945
|
if (validationTargets.length > 0 || lintCommands.length > 0) {
|
|
1943
1946
|
targets.lint = {
|
|
1944
|
-
executor: lintCommands.length > 0 ?
|
|
1947
|
+
executor: lintCommands.length > 0 ? ALL_VERDICTS_EXECUTOR : 'nx:noop',
|
|
1945
1948
|
...(eslintFileSet === null ? {} : { metadata: { eslintFileSet } }),
|
|
1946
1949
|
cache: true,
|
|
1947
1950
|
dependsOn: validationTargets,
|
|
@@ -125,6 +125,143 @@ it('a client that may never start a daemon continues without one that stopped un
|
|
|
125
125
|
});
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
+
const taskOutput = (project: string) => `${project}/dist/${project}.txt`;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Two cacheable tasks: `p:build` runs `first` and writes its output, and `q:build` copies it. `q:build`'s inputs are
|
|
132
|
+
* `p:build`'s outputs, so it is hashed only once `p:build` has finished, and whatever `first` did to the daemon is
|
|
133
|
+
* behind every daemon request after it: that hash, recording outputs, the post-run hooks.
|
|
134
|
+
*/
|
|
135
|
+
async function twoTaskWorkspace(workspace: string, first: string): Promise<void> {
|
|
136
|
+
await fixtureWorkspace(workspace);
|
|
137
|
+
const build = (command: string) => ({ command, outputs: ['{projectRoot}/dist'], cache: true });
|
|
138
|
+
await writeFile(
|
|
139
|
+
join(workspace, 'p', 'project.json'),
|
|
140
|
+
JSON.stringify({
|
|
141
|
+
name: 'p',
|
|
142
|
+
targets: { build: build(`${first}; mkdir -p p/dist && echo p > ${taskOutput('p')}`) },
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
await mkdir(join(workspace, 'q'));
|
|
146
|
+
await writeFile(
|
|
147
|
+
join(workspace, 'q', 'project.json'),
|
|
148
|
+
JSON.stringify({
|
|
149
|
+
name: 'q',
|
|
150
|
+
implicitDependencies: ['p'],
|
|
151
|
+
targets: {
|
|
152
|
+
build: {
|
|
153
|
+
...build(`mkdir -p q/dist && cp ${taskOutput('p')} ${taskOutput('q')}`),
|
|
154
|
+
dependsOn: ['^build'],
|
|
155
|
+
inputs: ['default', { dependentTasksOutputFiles: '**/*.txt' }],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Start the fixture's daemon with a client allowed to, under `runtime`, and return the pid its record names. */
|
|
163
|
+
async function startDaemon(workspace: string, env: Record<string, string>, runtime: string): Promise<number> {
|
|
164
|
+
const ran = spawnSync(runtime, [nxEntry, 'show', 'projects', '--json'], { cwd: workspace, env, encoding: 'utf8' });
|
|
165
|
+
expect(ran.status, `${ran.stdout}${ran.stderr}`).toBe(0);
|
|
166
|
+
const daemon = await recordedPid(workspace);
|
|
167
|
+
if (daemon === null) throw new Error('a client allowed to start a daemon left no record');
|
|
168
|
+
return daemon;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** `nx run-many -t build` under the job rule; requires it to succeed with `q:build` built from `p:build`. */
|
|
172
|
+
async function runBothUnderNever(workspace: string, env: Record<string, string>, runtime: string): Promise<string> {
|
|
173
|
+
const run = spawnSync(runtime, [nxEntry, 'run-many', '-t', 'build', '--outputStyle=static'], {
|
|
174
|
+
cwd: workspace,
|
|
175
|
+
env: { ...env, NX_DAEMON_START: 'never' },
|
|
176
|
+
encoding: 'utf8',
|
|
177
|
+
});
|
|
178
|
+
const printed = `${run.stdout}${run.stderr}`;
|
|
179
|
+
expect(run.status, printed).toBe(0);
|
|
180
|
+
expect(await readFile(join(workspace, taskOutput('q')), 'utf8')).toBe('p\n');
|
|
181
|
+
// Not disabled for the checkout: the marker only `nx reset` clears would follow it into every terminal.
|
|
182
|
+
await expect(readFile(join(workspace, '.nx/workspace-data/d/disabled'), 'utf8')).rejects.toMatchObject({
|
|
183
|
+
code: 'ENOENT',
|
|
184
|
+
});
|
|
185
|
+
return printed;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The daemon stops while a run uses it, after the graph came from it: `p:build` kills it, and every daemon request
|
|
190
|
+
* after that goes to a daemon that is gone. Each continues in the client, the run finishes, and nothing is disabled
|
|
191
|
+
* or started.
|
|
192
|
+
*/
|
|
193
|
+
it('a run whose daemon stops under it continues without one, and disables nothing', async () => {
|
|
194
|
+
await withNxFixture('nx-daemon-start-midrun', async ({ workspace, socketDirectory }) => {
|
|
195
|
+
await twoTaskWorkspace(
|
|
196
|
+
workspace,
|
|
197
|
+
'kill -9 $STOP_DAEMON; while kill -0 $STOP_DAEMON 2>/dev/null; do sleep 0.05; done',
|
|
198
|
+
);
|
|
199
|
+
const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
|
|
200
|
+
const daemon = await startDaemon(workspace, env, 'bun');
|
|
201
|
+
|
|
202
|
+
const printed = await runBothUnderNever(workspace, { ...env, STOP_DAEMON: String(daemon) }, 'bun');
|
|
203
|
+
expect(isRunning(daemon)).toBe(false);
|
|
204
|
+
expect(printed).toContain(
|
|
205
|
+
'NX_DAEMON_START=never: this client never starts a daemon. Continuing without the daemon.',
|
|
206
|
+
);
|
|
207
|
+
// No successor: the record still names the daemon the task killed.
|
|
208
|
+
expect(await recordedPid(workspace)).toBe(daemon);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The daemon restarts itself while a run uses it: `p:build` writes a lockfile, so the next request finds the daemon
|
|
214
|
+
* outdated, and it starts its successor and ends the request's socket. The client waits for that successor and
|
|
215
|
+
* resumes on it, as the job rule allows: its starter is the restarting daemon, not the client. The run finishes.
|
|
216
|
+
*/
|
|
217
|
+
it('a run whose daemon restarts itself under it finishes', async () => {
|
|
218
|
+
await withNxFixture('nx-daemon-start-restart', async ({ workspace, socketDirectory }) => {
|
|
219
|
+
// A lockfile Nx parses: an unparsable one fails the successor's graph, which is a different failure.
|
|
220
|
+
const lockfile = JSON.stringify({
|
|
221
|
+
name: 'daemon-start-never',
|
|
222
|
+
lockfileVersion: 3,
|
|
223
|
+
requires: true,
|
|
224
|
+
packages: { '': { name: 'daemon-start-never' } },
|
|
225
|
+
});
|
|
226
|
+
await twoTaskWorkspace(workspace, `echo '${lockfile}' > package-lock.json`);
|
|
227
|
+
const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
|
|
228
|
+
await startDaemon(workspace, env, 'node');
|
|
229
|
+
|
|
230
|
+
await runBothUnderNever(workspace, env, 'node');
|
|
231
|
+
expect(await readFile(join(workspace, '.nx/workspace-data/d/daemon.log'), 'utf8')).toContain('Restarting daemon');
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A restarting daemon opens the log twice for its successor's stdout and stderr. Left to garbage collection, Node >=26
|
|
237
|
+
* turns each collected `FileHandle` into a fatal `ERR_INVALID_STATE`, which ended a restarting daemon in a job with
|
|
238
|
+
* the socket its clients were still writing to. Collected deliberately here, right after the restart's spawn, so the
|
|
239
|
+
* descriptors' lifetime decides the exit rather than when a collection happens to run.
|
|
240
|
+
*/
|
|
241
|
+
it('a restarting daemon closes the log descriptors it hands its successor', async () => {
|
|
242
|
+
await withNxFixture('nx-daemon-restart-descriptors', async ({ workspace, socketDirectory }) => {
|
|
243
|
+
await fixtureWorkspace(workspace);
|
|
244
|
+
const shutdownUtils = join(repositoryRoot, 'node_modules/nx/dist/src/daemon/server/shutdown-utils.js');
|
|
245
|
+
const restart = `const { setTimeout: sleep } = require('node:timers/promises');
|
|
246
|
+
require(${JSON.stringify(shutdownUtils)}).startNewDaemonInBackground().then(async () => {
|
|
247
|
+
for (let round = 0; round < 3; round++) { gc(); await sleep(50); }
|
|
248
|
+
})`;
|
|
249
|
+
const ran = spawnSync('node', ['--expose-gc', '-e', restart], {
|
|
250
|
+
cwd: workspace,
|
|
251
|
+
env: { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' },
|
|
252
|
+
encoding: 'utf8',
|
|
253
|
+
});
|
|
254
|
+
expect(ran.status, `${ran.stdout}${ran.stderr}`).toBe(0);
|
|
255
|
+
expect(ran.stderr).not.toContain('ERR_INVALID_STATE');
|
|
256
|
+
// The successor is the fixture's to stop: wait for the record it writes once it serves.
|
|
257
|
+
const deadline = Date.now() + 60_000;
|
|
258
|
+
while ((await recordedPid(workspace)) === null) {
|
|
259
|
+
if (Date.now() > deadline) throw new Error('the restarted daemon wrote no record within 60 s');
|
|
260
|
+
await Bun.sleep(100);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
128
265
|
/**
|
|
129
266
|
* An explicit `nx daemon --start` calls the daemon start directly, past the implicit-start check above, so under the
|
|
130
267
|
* job's rule it is refused as policy: a non-zero exit naming `NX_DAEMON_START=never` and the fixture escape, and no
|
|
@@ -5,10 +5,15 @@ import { tmpdir } from 'node:os';
|
|
|
5
5
|
import { dirname, join } from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
import type { Task, TaskGraph } from 'nx/src/config/task-graph';
|
|
8
|
+
import { expandOutputs } from 'nx/src/native';
|
|
8
9
|
import type { TaskResults } from 'nx/src/tasks-runner/life-cycle';
|
|
10
|
+
import { readModulePackageJson } from 'nx/src/utils/package-json';
|
|
11
|
+
import { pastTick } from './__tests__/past-tick.js';
|
|
9
12
|
import type { EnsureBuiltProducerReceipt } from './ensure-built.js';
|
|
13
|
+
import { censusStart, type GraphInstallationCensus, readInstallationCensus } from './ensure-built-witness.js';
|
|
10
14
|
import {
|
|
11
15
|
completedProducerTasks,
|
|
16
|
+
installedClosureTool,
|
|
12
17
|
PRODUCER_RECORDS,
|
|
13
18
|
postTasksExecution,
|
|
14
19
|
preTasksExecution,
|
|
@@ -184,3 +189,97 @@ describe('actual producer completion evidence', () => {
|
|
|
184
189
|
}
|
|
185
190
|
});
|
|
186
191
|
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A workspace whose Nx is installed and whose plugin is a developer link: its package is a checkout outside
|
|
195
|
+
* `node_modules`, whose `dist` a rebuild rewrites in place without touching any lockfile or `node_modules` entry.
|
|
196
|
+
*/
|
|
197
|
+
async function developerLinkedWorkspace(): Promise<{ readonly root: string; readonly dist: string }> {
|
|
198
|
+
const root = await realpath(await mkdtemp(join(tmpdir(), 'producer-census-')));
|
|
199
|
+
const checkout = join(root, 'checkout');
|
|
200
|
+
await mkdir(join(checkout, 'dist'), { recursive: true });
|
|
201
|
+
await writeFile(
|
|
202
|
+
join(checkout, 'package.json'),
|
|
203
|
+
JSON.stringify({ name: '@smoothbricks/nx-plugin', version: '0.0.0', files: ['dist'] }),
|
|
204
|
+
);
|
|
205
|
+
const dist = join(checkout, 'dist/index.js');
|
|
206
|
+
await writeFile(dist, 'export const value = 1;\n');
|
|
207
|
+
await mkdir(join(root, 'node_modules/nx'), { recursive: true });
|
|
208
|
+
await writeFile(join(root, 'node_modules/nx/package.json'), JSON.stringify({ name: 'nx', version: '0.0.0' }));
|
|
209
|
+
await writeFile(join(root, 'node_modules/nx/index.js'), 'module.exports = {};\n');
|
|
210
|
+
await mkdir(join(root, 'node_modules/@smoothbricks'));
|
|
211
|
+
await symlink(checkout, join(root, 'node_modules/@smoothbricks/nx-plugin'), 'dir');
|
|
212
|
+
return { root, dist };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
describe("the producer hook's installed-closure census", () => {
|
|
216
|
+
const sentinel = '0'.repeat(64);
|
|
217
|
+
/** One census of `root`'s closure, taking hashes from the census recorded at `record`. */
|
|
218
|
+
function closure(root: string, record: string) {
|
|
219
|
+
const tool = installedClosureTool(
|
|
220
|
+
root,
|
|
221
|
+
{ plugins: ['@smoothbricks/nx-plugin'] },
|
|
222
|
+
null,
|
|
223
|
+
record,
|
|
224
|
+
readModulePackageJson,
|
|
225
|
+
expandOutputs,
|
|
226
|
+
{ start: censusStart(), observed: [] },
|
|
227
|
+
);
|
|
228
|
+
if (!tool.ok) throw new Error(tool.detail);
|
|
229
|
+
return tool.value;
|
|
230
|
+
}
|
|
231
|
+
/** Records a census whose every file hash is `sentinel`: a census that reads a file reports its real hash instead. */
|
|
232
|
+
async function recordSentinels(record: string): Promise<GraphInstallationCensus> {
|
|
233
|
+
const recorded = readInstallationCensus(record);
|
|
234
|
+
if (recorded === undefined) throw new Error(`no census recorded at ${record}`);
|
|
235
|
+
await writeFile(
|
|
236
|
+
record,
|
|
237
|
+
JSON.stringify({ ...recorded, files: recorded.files.map(({ path }) => ({ path, sha256: sentinel })) }),
|
|
238
|
+
);
|
|
239
|
+
return recorded;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
it('reads no file bytes of a closure whose node stamps have not moved, and names the phase it spent', async () => {
|
|
243
|
+
const { root } = await developerLinkedWorkspace();
|
|
244
|
+
try {
|
|
245
|
+
const record = join(root, 'census.json');
|
|
246
|
+
await pastTick();
|
|
247
|
+
closure(root, record);
|
|
248
|
+
const recorded = await recordSentinels(record);
|
|
249
|
+
const census = performance.getEntriesByName('producerReceipts:census', 'measure').length;
|
|
250
|
+
const files = new Set(recorded.files.map(({ path }) => path));
|
|
251
|
+
const hashed = closure(root, record).nodes.filter(({ path }) => files.has(path));
|
|
252
|
+
expect(hashed).toHaveLength(files.size);
|
|
253
|
+
expect(files.size).toBeGreaterThan(2);
|
|
254
|
+
expect(hashed.map((node) => (node.type === 'file' ? node.sha256 : node.type))).toEqual(
|
|
255
|
+
hashed.map(() => sentinel),
|
|
256
|
+
);
|
|
257
|
+
expect(performance.getEntriesByName('producerReceipts:census', 'measure').length).toBe(census + 1);
|
|
258
|
+
} finally {
|
|
259
|
+
await rm(root, { recursive: true, force: true });
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('reads again a developer-linked dist rewritten in place, with no lockfile or node_modules change', async () => {
|
|
264
|
+
const { root, dist } = await developerLinkedWorkspace();
|
|
265
|
+
try {
|
|
266
|
+
const record = join(root, 'census.json');
|
|
267
|
+
await pastTick();
|
|
268
|
+
closure(root, record);
|
|
269
|
+
await recordSentinels(record);
|
|
270
|
+
const rebuilt = 'export const value = 2;\n';
|
|
271
|
+
await writeFile(dist, rebuilt);
|
|
272
|
+
const nodes = closure(root, record).nodes;
|
|
273
|
+
expect(nodes.find(({ path }) => path === dist)).toMatchObject({
|
|
274
|
+
type: 'file',
|
|
275
|
+
sha256: createHash('sha256').update(rebuilt).digest('hex'),
|
|
276
|
+
});
|
|
277
|
+
expect(nodes.find(({ path }) => path === join(root, 'node_modules/nx/index.js'))).toMatchObject({
|
|
278
|
+
type: 'file',
|
|
279
|
+
sha256: sentinel,
|
|
280
|
+
});
|
|
281
|
+
} finally {
|
|
282
|
+
await rm(root, { recursive: true, force: true });
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
package/src/producer-receipts.ts
CHANGED
|
@@ -15,7 +15,9 @@ import { delimiter, dirname, join, resolve } from 'node:path';
|
|
|
15
15
|
import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
|
|
16
16
|
import type { ProjectGraph } from 'nx/src/config/project-graph';
|
|
17
17
|
import type { Task, TaskGraph } from 'nx/src/config/task-graph';
|
|
18
|
+
import type { expandOutputs } from 'nx/src/native';
|
|
18
19
|
import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api';
|
|
20
|
+
import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
|
|
19
21
|
import typia from 'typia';
|
|
20
22
|
import {
|
|
21
23
|
createWorkspaceNxRequire,
|
|
@@ -35,13 +37,16 @@ import {
|
|
|
35
37
|
receiptNode,
|
|
36
38
|
} from './ensure-built-receipt.js';
|
|
37
39
|
import {
|
|
40
|
+
censusFileHashes,
|
|
38
41
|
censusStart,
|
|
39
|
-
|
|
42
|
+
graphInstallationCensus,
|
|
40
43
|
movedNodes,
|
|
41
44
|
type NodeWitness,
|
|
45
|
+
readInstallationCensus,
|
|
42
46
|
workspaceSnapshot,
|
|
47
|
+
writeWitness,
|
|
43
48
|
} from './ensure-built-witness.js';
|
|
44
|
-
import { graphInputContractIdentity } from './graph-input-contract.js';
|
|
49
|
+
import { type GraphInputContractProof, graphInputContractIdentity } from './graph-input-contract.js';
|
|
45
50
|
|
|
46
51
|
export interface ProducerReceiptsOptions {
|
|
47
52
|
readonly producerReceipts?: { readonly targets: readonly string[] };
|
|
@@ -56,16 +61,16 @@ export interface ProducerReceiptsOptions {
|
|
|
56
61
|
* is no source or input of the run that publishes it.
|
|
57
62
|
*/
|
|
58
63
|
export const PRODUCER_RECORDS = join('.cache', 'producer-receipts');
|
|
64
|
+
/**
|
|
65
|
+
* The installation census the pre-task hook took last, in Nx's workspace-data beside the graph witness: a hash source
|
|
66
|
+
* for the next census ({@link installedClosureTool}), never a proof and never part of a receipt.
|
|
67
|
+
*/
|
|
68
|
+
const PRODUCER_INSTALLATION_CENSUS = 'producer-installation-census.json';
|
|
59
69
|
interface ToolResolution {
|
|
60
70
|
readonly name: string;
|
|
61
71
|
readonly from: string;
|
|
62
72
|
readonly target: string | null;
|
|
63
73
|
}
|
|
64
|
-
interface Installation {
|
|
65
|
-
readonly nodes: readonly NodeWitness[];
|
|
66
|
-
readonly resolutions: readonly (readonly [string, string, string | null])[];
|
|
67
|
-
readonly localInputs: string | null;
|
|
68
|
-
}
|
|
69
74
|
interface TargetBasis {
|
|
70
75
|
readonly graph: TaskGraph;
|
|
71
76
|
readonly source: ProducerSourceWitness;
|
|
@@ -179,6 +184,90 @@ function targetGraph(root: string, target: string, projectGraph: ProjectGraph):
|
|
|
179
184
|
false,
|
|
180
185
|
);
|
|
181
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* The installed closure of Nx, this plugin, TypeScript and the local graph plugins as one producer tool: every node of
|
|
189
|
+
* the installation census, and the module namespace each of its resolutions searched. The census is not proved by a
|
|
190
|
+
* stat pass of its own: each of its nodes is stat-ed again as a tool node, a file whose stamp moved since the census
|
|
191
|
+
* read it is read again, and the capture's stability check refuses a tool node that moves before publication.
|
|
192
|
+
*
|
|
193
|
+
* Its value is a function of the closure's node stamps (device, inode, mode, size, mtime and ctime), not of the moment
|
|
194
|
+
* it is taken. So every node is stat-ed on every call, and that is what makes reuse safe: a file whose stamp is the one
|
|
195
|
+
* the census recorded at `record` read it at keeps that census's hash, so an unchanged closure reads no file bytes,
|
|
196
|
+
* while a file rewritten in place, as a developer-linked package's rebuilt `dist` is with no lockfile or
|
|
197
|
+
* `node_modules` change, moves its ctime and is read again. The tool's nodes take the hash this census read
|
|
198
|
+
* ({@link censusFileHashes}) instead of reading each file a second time: a write landing between two reads could make
|
|
199
|
+
* them disagree.
|
|
200
|
+
*/
|
|
201
|
+
export function installedClosureTool(
|
|
202
|
+
root: string,
|
|
203
|
+
nxJson: NxJsonConfiguration,
|
|
204
|
+
localInputs: GraphInputContractProof | null,
|
|
205
|
+
record: string,
|
|
206
|
+
resolvePackage: typeof readModulePackageJsonWithoutFallbacks,
|
|
207
|
+
expand: typeof expandOutputs,
|
|
208
|
+
census: { readonly start: bigint; readonly observed: NodeWitness[] },
|
|
209
|
+
): ReceiptEvidence<EnsureBuiltProducerTool> {
|
|
210
|
+
let installationMiss = 'unproved installed module/resolution closure';
|
|
211
|
+
performance.mark('producerReceipts:census:start');
|
|
212
|
+
const installation = graphInstallationCensus(
|
|
213
|
+
root,
|
|
214
|
+
nxJson,
|
|
215
|
+
resolvePackage,
|
|
216
|
+
expand,
|
|
217
|
+
localInputs ?? { plugins: [], packages: [], identity: '[]' },
|
|
218
|
+
(detail) => {
|
|
219
|
+
installationMiss = detail;
|
|
220
|
+
},
|
|
221
|
+
readInstallationCensus(record),
|
|
222
|
+
);
|
|
223
|
+
performance.measure('producerReceipts:census', 'producerReceipts:census:start');
|
|
224
|
+
if (installation === null)
|
|
225
|
+
return { ok: false, detail: `Producer installed module/resolution closure is unproved: ${installationMiss}` };
|
|
226
|
+
writeWitness(record, installation);
|
|
227
|
+
const paths = new Set(installation.nodes.map(({ path }) => path));
|
|
228
|
+
const absent = new Set<string>();
|
|
229
|
+
function namespace(path: string): void {
|
|
230
|
+
let stats: ReturnType<typeof lstatSync>;
|
|
231
|
+
try {
|
|
232
|
+
stats = lstatSync(path);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (error instanceof Error && 'code' in error && ['ENOENT', 'ENOTDIR'].includes(String(error.code))) {
|
|
235
|
+
absent.add(path);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
paths.add(path);
|
|
241
|
+
if (stats.isSymbolicLink()) paths.add(realpathSync(path));
|
|
242
|
+
if (stats.isDirectory()) for (const name of readdirSync(path)) paths.add(join(path, name));
|
|
243
|
+
}
|
|
244
|
+
const resolutions: ToolResolution[] = [];
|
|
245
|
+
for (const [from, name, target] of installation.resolutions) {
|
|
246
|
+
resolutions.push({ from, name, target });
|
|
247
|
+
for (const directory of createRequire(join(from, 'package.json')).resolve.paths(name) ?? []) {
|
|
248
|
+
namespace(directory);
|
|
249
|
+
const candidate = join(directory, name);
|
|
250
|
+
if (existsSync(candidate)) namespace(candidate);
|
|
251
|
+
else absent.add(candidate);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const recorded = censusFileHashes(installation);
|
|
255
|
+
const nodes = [...paths].sort().map((path) => receiptNode(path, path, census, recorded));
|
|
256
|
+
return {
|
|
257
|
+
ok: true,
|
|
258
|
+
value: {
|
|
259
|
+
kind: 'module',
|
|
260
|
+
name: 'installed-nx-plugin-closure',
|
|
261
|
+
from: root,
|
|
262
|
+
path: root,
|
|
263
|
+
physicalPath: realpathSync(root),
|
|
264
|
+
nodes,
|
|
265
|
+
missing: [...absent].sort(),
|
|
266
|
+
resolutions,
|
|
267
|
+
sha256: sha(nodes),
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|
|
182
271
|
/** Tool resolution is captured in producer scope; readers inspect its recorded filesystem, not their own environment. */
|
|
183
272
|
function toolsFor(
|
|
184
273
|
root: string,
|
|
@@ -202,62 +291,19 @@ function toolsFor(
|
|
|
202
291
|
if (proof.runtimeSource.kind === 'opaque') return { ok: false, detail: proof.runtimeSource.detail };
|
|
203
292
|
needsCargo ||= Object.values(proof.inputs).some((input) => input.runtime.length > 0);
|
|
204
293
|
}
|
|
205
|
-
|
|
206
|
-
const
|
|
294
|
+
const observed: NodeWitness[] = [];
|
|
295
|
+
const census = { start: censusStart(), observed };
|
|
296
|
+
const closure = installedClosureTool(
|
|
207
297
|
root,
|
|
208
298
|
nxJson,
|
|
299
|
+
local,
|
|
300
|
+
join(data, PRODUCER_INSTALLATION_CENSUS),
|
|
209
301
|
load('nx/src/utils/package-json').readModulePackageJson,
|
|
210
302
|
load('nx/src/native').expandOutputs,
|
|
211
|
-
|
|
212
|
-
(detail) => {
|
|
213
|
-
installationMiss = detail;
|
|
214
|
-
},
|
|
303
|
+
census,
|
|
215
304
|
);
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
const installation = typia.json.isParse<Installation>(identity);
|
|
219
|
-
if (installation === null) return { ok: false, detail: 'Invalid installed module closure evidence' };
|
|
220
|
-
const observed: NodeWitness[] = [];
|
|
221
|
-
const census = { start: censusStart(), observed };
|
|
222
|
-
const paths = new Set(installation.nodes.map(({ path }) => path));
|
|
223
|
-
const absent = new Set<string>();
|
|
224
|
-
function namespace(path: string): void {
|
|
225
|
-
let stats: ReturnType<typeof lstatSync>;
|
|
226
|
-
try {
|
|
227
|
-
stats = lstatSync(path);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
if (error instanceof Error && 'code' in error && ['ENOENT', 'ENOTDIR'].includes(String(error.code))) {
|
|
230
|
-
absent.add(path);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
throw error;
|
|
234
|
-
}
|
|
235
|
-
paths.add(path);
|
|
236
|
-
if (stats.isSymbolicLink()) paths.add(realpathSync(path));
|
|
237
|
-
if (stats.isDirectory()) for (const name of readdirSync(path)) paths.add(join(path, name));
|
|
238
|
-
}
|
|
239
|
-
const resolutions: ToolResolution[] = [];
|
|
240
|
-
for (const [from, name, target] of installation.resolutions) {
|
|
241
|
-
resolutions.push({ from, name, target });
|
|
242
|
-
for (const directory of createRequire(join(from, 'package.json')).resolve.paths(name) ?? []) {
|
|
243
|
-
namespace(directory);
|
|
244
|
-
const candidate = join(directory, name);
|
|
245
|
-
if (existsSync(candidate)) namespace(candidate);
|
|
246
|
-
else absent.add(candidate);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
const nodes = [...paths].sort().map((path) => receiptNode(path, path, census));
|
|
250
|
-
const module: EnsureBuiltProducerTool = {
|
|
251
|
-
kind: 'module',
|
|
252
|
-
name: 'installed-nx-plugin-closure',
|
|
253
|
-
from: root,
|
|
254
|
-
path: root,
|
|
255
|
-
physicalPath: realpathSync(root),
|
|
256
|
-
nodes,
|
|
257
|
-
missing: [...absent].sort(),
|
|
258
|
-
resolutions,
|
|
259
|
-
sha256: sha(nodes),
|
|
260
|
-
};
|
|
305
|
+
if (!closure.ok) return closure;
|
|
306
|
+
const module = closure.value;
|
|
261
307
|
const tools: EnsureBuiltProducerTool[] = [module];
|
|
262
308
|
for (const name of ['nx-runtime', 'bun', 'sh', 'git', ...(needsCargo ? ['rustc', 'cargo'] : [])]) {
|
|
263
309
|
const candidates =
|
|
@@ -365,9 +411,13 @@ export async function preTasksExecution(
|
|
|
365
411
|
if (targets.length === 0) return;
|
|
366
412
|
try {
|
|
367
413
|
const load = createWorkspaceNxRequire(context.workspaceRoot);
|
|
414
|
+
// A hook that can hold a run for minutes names its phases: Nx reports each measure under NX_PERF_LOGGING.
|
|
415
|
+
performance.mark('producerReceipts:graph:start');
|
|
368
416
|
const projectGraph = await load('nx/src/project-graph/project-graph').createProjectGraphAsync();
|
|
417
|
+
performance.measure('producerReceipts:graph', 'producerReceipts:graph:start');
|
|
369
418
|
const bases = new Map<string, TargetBasis>();
|
|
370
419
|
for (const target of targets) {
|
|
420
|
+
performance.mark('producerReceipts:source:start');
|
|
371
421
|
const graph = targetGraph(context.workspaceRoot, target, projectGraph);
|
|
372
422
|
const outputs = producerOutputPaths(
|
|
373
423
|
context.workspaceRoot,
|
|
@@ -375,13 +425,16 @@ export async function preTasksExecution(
|
|
|
375
425
|
load('nx/src/native').expandOutputs,
|
|
376
426
|
);
|
|
377
427
|
const source = captureProducerSource(context.workspaceRoot, outputs);
|
|
428
|
+
performance.measure('producerReceipts:source', 'producerReceipts:source:start');
|
|
378
429
|
if (!source.ok) {
|
|
379
430
|
runs.set(context.id, source);
|
|
380
431
|
return;
|
|
381
432
|
}
|
|
382
433
|
bases.set(target, { graph, source: source.value });
|
|
383
434
|
}
|
|
435
|
+
performance.mark('producerReceipts:tools:start');
|
|
384
436
|
const tools = toolsFor(context.workspaceRoot, context.nxJsonConfiguration, projectGraph, [...bases.values()]);
|
|
437
|
+
performance.measure('producerReceipts:tools', 'producerReceipts:tools:start');
|
|
385
438
|
runs.set(
|
|
386
439
|
context.id,
|
|
387
440
|
tools.ok
|