@smoothbricks/nx-plugin 0.5.4 → 0.5.6

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.
Files changed (48) hide show
  1. package/README.md +81 -16
  2. package/dist/ensure-built-receipt.d.ts +2 -2
  3. package/dist/ensure-built-receipt.d.ts.map +1 -1
  4. package/dist/ensure-built-receipt.js +7 -2
  5. package/dist/ensure-built-witness.d.ts +12 -1
  6. package/dist/ensure-built-witness.d.ts.map +1 -1
  7. package/dist/ensure-built-witness.js +32 -0
  8. package/dist/ensure-built.d.ts +2 -2
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +27 -5
  11. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/executor.js +48 -11
  13. package/dist/executors/bounded-exec/progress.d.ts +1 -3
  14. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  15. package/dist/executors/bounded-exec/progress.js +1 -2
  16. package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
  17. package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
  18. package/dist/executors/bounded-exec/runner-hook.js +301 -0
  19. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
  20. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
  21. package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
  22. package/dist/producer-receipts.d.ts +24 -1
  23. package/dist/producer-receipts.d.ts.map +1 -1
  24. package/dist/producer-receipts.js +91 -60
  25. package/dist/runtime-input-policy.d.ts +60 -0
  26. package/dist/runtime-input-policy.d.ts.map +1 -0
  27. package/dist/runtime-input-policy.js +379 -0
  28. package/managed/raw/git-format-staged.yml +16 -3
  29. package/managed/raw/patches/nx@23.2.1.patch +397 -33
  30. package/package.json +7 -2
  31. package/src/ensure-built-receipt.ts +8 -2
  32. package/src/ensure-built-witness.ts +29 -1
  33. package/src/ensure-built.ts +39 -7
  34. package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
  35. package/src/executors/bounded-exec/executor.test.ts +89 -42
  36. package/src/executors/bounded-exec/executor.ts +45 -11
  37. package/src/executors/bounded-exec/progress.test.ts +1 -14
  38. package/src/executors/bounded-exec/progress.ts +2 -5
  39. package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
  40. package/src/executors/bounded-exec/runner-hook.ts +292 -0
  41. package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
  42. package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
  43. package/src/nx-daemon-start-never.test.ts +137 -0
  44. package/src/producer-receipts.test.ts +99 -0
  45. package/src/producer-receipts.ts +111 -58
  46. package/src/runtime-input-policy.ts +395 -0
  47. package/src/runtime-input.test.ts +160 -3
  48. package/test-timings.json +4 -0
@@ -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
+ });
@@ -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
- graphInstallationIdentity,
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
- let installationMiss = 'unproved installed module/resolution closure';
206
- const identity = graphInstallationIdentity(
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
- local ?? { plugins: [], packages: [], identity: '[]' },
212
- (detail) => {
213
- installationMiss = detail;
214
- },
303
+ census,
215
304
  );
216
- if (identity === null)
217
- return { ok: false, detail: `Producer installed module/resolution closure is unproved: ${installationMiss}` };
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