@smoothbricks/nx-plugin 0.4.19 → 0.4.21

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 (44) hide show
  1. package/README.md +68 -18
  2. package/dist/bin/smoo-nx-cargo-hash.js +5 -3
  3. package/dist/cargo-source-hash.d.ts +3 -11
  4. package/dist/cargo-source-hash.d.ts.map +1 -1
  5. package/dist/cargo-source-hash.js +25 -14
  6. package/dist/cargo-workspace.d.ts +0 -1
  7. package/dist/cargo-workspace.d.ts.map +1 -1
  8. package/dist/cargo-workspace.js +9 -2
  9. package/dist/ensure-built.d.ts +6 -5
  10. package/dist/ensure-built.d.ts.map +1 -1
  11. package/dist/ensure-built.js +227 -30
  12. package/dist/index.js +18 -10
  13. package/dist/managed-files/ci-workflow.d.ts +20 -0
  14. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  15. package/dist/managed-files/ci-workflow.js +42 -13
  16. package/dist/managed-files/files.d.ts +1 -1
  17. package/dist/managed-files/files.d.ts.map +1 -1
  18. package/dist/managed-files/files.js +8 -1
  19. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +37 -0
  20. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  21. package/dist/managed-files/pr-preview-cleanup-workflow.js +34 -6
  22. package/dist/workspace-manifest.d.ts +3 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/tooling/direnv/setup-environment.ts +23 -91
  25. package/managed/raw/tooling/git-hooks/commit-msg.sh +6 -0
  26. package/managed/raw/tooling/git-hooks/pre-commit.sh +6 -0
  27. package/managed/raw/tooling/git-hooks/pre-push.sh +6 -0
  28. package/managed/templates/github/actions/setup-devenv/action.yml +19 -7
  29. package/package.json +1 -1
  30. package/src/bin/smoo-nx-cargo-hash.ts +7 -3
  31. package/src/cargo-source-hash.test.ts +237 -1
  32. package/src/cargo-source-hash.ts +32 -16
  33. package/src/cargo-workspace.test.ts +17 -10
  34. package/src/cargo-workspace.ts +9 -3
  35. package/src/ensure-built.test.ts +87 -29
  36. package/src/ensure-built.ts +274 -29
  37. package/src/index.test.ts +117 -20
  38. package/src/index.ts +18 -11
  39. package/src/managed-files/ci-workflow.ts +45 -14
  40. package/src/managed-files/files.ts +9 -2
  41. package/src/managed-files/generator.test.ts +5 -1
  42. package/src/managed-files/github-host-cache-isolation.test.ts +32 -6
  43. package/src/managed-files/pr-preview-cleanup-workflow.ts +87 -6
  44. package/src/workspace-manifest.ts +3 -1
@@ -34,18 +34,32 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
34
34
  * patches, and transitive path dependencies. A locked offline query observes
35
35
  * that graph without fetching dependencies or changing the lockfile. Git and
36
36
  * registry packages are immutable inputs already identified by Cargo.lock.
37
+ * By default, Nx's in-workspace inputs own workspace packages; includeWorkspace
38
+ * also covers those packages for custom targets without inferred Cargo inputs.
37
39
  *
38
40
  * This covers Rust sources and Cargo configuration, matching the plugin's
39
41
  * in-workspace Rust inputs. Build scripts' other data/environment inputs remain
40
42
  * explicit Nx inputs, just as they are for in-workspace crates.
41
43
  */
42
- export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: string): Promise<string> {
44
+ // Nx hashes a runtime input's stdout AND stderr. Cargo reports lock contention
45
+ // on stderr ("Blocking waiting for file lock on package cache") a
46
+ // timing-dependent number of times when Nx hashes many tasks at once, which
47
+ // made one unchanged tree produce four different task hashes per run. Cargo's
48
+ // stderr is kept for the failure path only, where execFileSync attaches it to
49
+ // the thrown error.
50
+ const CARGO_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
51
+
52
+ export async function hashCargoPathInputs(
53
+ manifestPath: string,
54
+ workspaceRoot: string,
55
+ options?: { includeWorkspace?: boolean },
56
+ ): Promise<string> {
43
57
  const root = await realpath(workspaceRoot);
44
58
  const metadata = parseMetadata(
45
59
  execFileSync(
46
60
  'cargo',
47
61
  ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
48
- { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'inherit'] },
62
+ { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CARGO_STDIO },
49
63
  ),
50
64
  );
51
65
  const cargoRoot = await realpath(metadata.workspace_root);
@@ -57,18 +71,20 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
57
71
  if (pkg.source !== null) continue;
58
72
  const manifest = await realpath(pkg.manifest_path);
59
73
  const directory = dirname(manifest);
60
- const fromRoot = relative(root, directory);
61
- const fromCargo = relative(cargoRoot, directory);
62
- if (
63
- fromRoot !== '..' &&
64
- !fromRoot.startsWith(`..${sep}`) &&
65
- !isAbsolute(fromRoot) &&
66
- fromCargo !== '..' &&
67
- !fromCargo.startsWith(`..${sep}`) &&
68
- !isAbsolute(fromCargo) &&
69
- !fromRoot.split(sep).includes('node_modules')
70
- )
71
- continue;
74
+ if (!options?.includeWorkspace) {
75
+ const fromRoot = relative(root, directory);
76
+ const fromCargo = relative(cargoRoot, directory);
77
+ if (
78
+ fromRoot !== '..' &&
79
+ !fromRoot.startsWith(`..${sep}`) &&
80
+ !isAbsolute(fromRoot) &&
81
+ fromCargo !== '..' &&
82
+ !fromCargo.startsWith(`..${sep}`) &&
83
+ !isAbsolute(fromCargo) &&
84
+ !fromRoot.split(sep).includes('node_modules')
85
+ )
86
+ continue;
87
+ }
72
88
  directories.add(directory);
73
89
  files.add(manifest);
74
90
  // Cargo resolves explicit package.workspace ownership as well as ancestor
@@ -76,11 +92,11 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
76
92
  const governingManifest = execFileSync(
77
93
  'cargo',
78
94
  ['locate-project', '--workspace', '--manifest-path', manifest, '--message-format', 'plain'],
79
- { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] },
95
+ { cwd: root, encoding: 'utf8', stdio: CARGO_STDIO },
80
96
  ).trim();
81
97
  files.add(await realpath(governingManifest));
82
98
  for (const target of pkg.targets) files.add(await realpath(target.src_path));
83
- // An external member can inherit edition, lint policy, dependencies and
99
+ // A member can inherit edition, lint policy, dependencies and
84
100
  // profiles from a workspace above its package directory. Those manifests
85
101
  // are not descendants of the source roots returned by Cargo metadata.
86
102
  for (let ancestor = dirname(directory); !ancestors.has(ancestor); ancestor = dirname(ancestor)) {
@@ -136,10 +136,11 @@ describe('Cargo workspace layouts', () => {
136
136
  absoluteProjectRoot: join(root, 'packages/app'),
137
137
  memberDir: 'crates/app',
138
138
  });
139
- expect(matches(inputs, 'packages/base/src/lib.rs', 'packages/app')).toBe(true);
140
- expect(matches(inputs, '.cargo/config.toml', 'packages/app')).toBe(true);
141
- expect(matches(inputs, 'packages/rust-toolchain.toml', 'packages/app')).toBe(true);
142
- expect(matches(inputs, 'packages/base/target/debug/output', 'packages/app')).toBe(false);
139
+ expect(matches(inputs, 'packages/app/crates/app/src/lib.rs')).toBe(true);
140
+ expect(matches(inputs, 'packages/base/src/lib.rs')).toBe(true);
141
+ expect(matches(inputs, '.cargo/config.toml')).toBe(true);
142
+ expect(matches(inputs, 'packages/rust-toolchain.toml')).toBe(true);
143
+ expect(matches(inputs, 'packages/base/target/debug/output')).toBe(false);
143
144
  expect(inputs).not.toContain('externalRustCrates');
144
145
  expect(inputs.some((input) => input.includes('../'))).toBe(false);
145
146
  } finally {
@@ -269,12 +270,18 @@ describe('Cargo workspace layouts', () => {
269
270
  });
270
271
  });
271
272
 
272
- function matches(inputs: readonly string[], path: string, projectRoot = ''): boolean {
273
- const filesets = inputs.filter((input) => input.includes('{projectRoot}') || input.includes('{workspaceRoot}'));
274
- const match = (input: string): boolean =>
275
- new Glob(
276
- input.replace('{projectRoot}/', projectRoot ? `${projectRoot}/` : '').replace('{workspaceRoot}/', ''),
277
- ).match(path);
273
+ /**
274
+ * Whether Nx would hash `path` (workspace-relative) under `inputs`.
275
+ *
276
+ * Only `{workspaceRoot}` filesets resolve here. Nx resolves a `{projectRoot}`
277
+ * fileset against the files its project owns, which a directory prefix cannot
278
+ * model: resolving it as one would pass the very inputs that hash nothing once
279
+ * a nested project owns the crate, so this harness refuses them instead.
280
+ */
281
+ function matches(inputs: readonly string[], path: string): boolean {
282
+ expect(inputs.filter((input) => input.includes('{projectRoot}'))).toEqual([]);
283
+ const filesets = inputs.filter((input) => input.includes('{workspaceRoot}'));
284
+ const match = (input: string): boolean => new Glob(input.replace('{workspaceRoot}/', '')).match(path);
278
285
  return (
279
286
  filesets.some((input) => !input.startsWith('!') && match(input)) &&
280
287
  !filesets.some((input) => input.startsWith('!') && match(input.slice(1)))
@@ -221,7 +221,6 @@ export interface CargoPackageTestInputsOptions {
221
221
  /** The cargo workspace root holding the top-level `Cargo.toml`. */
222
222
  absoluteProjectRoot: string;
223
223
  memberDir: string;
224
- inputRoot?: string;
225
224
  /** Shared across every call of one graph computation; see {@link createCargoInputsCache}. */
226
225
  cache?: CargoInputsCache;
227
226
  }
@@ -247,7 +246,7 @@ export function createCargoInputsCache(): CargoInputsCache {
247
246
 
248
247
  export function cargoPackageTestInputs(options: CargoPackageTestInputsOptions): Promise<string[]> {
249
248
  const cache = options.cache ?? createCargoInputsCache();
250
- const key = `${options.absoluteProjectRoot}\0${options.memberDir}\0${options.inputRoot ?? '{projectRoot}'}`;
249
+ const key = `${options.absoluteProjectRoot}\0${options.memberDir}`;
251
250
  const cached = cache.results.get(key);
252
251
  if (cached !== undefined) return cached;
253
252
  const derived = deriveCargoPackageTestInputs(options, cache);
@@ -256,9 +255,16 @@ export function cargoPackageTestInputs(options: CargoPackageTestInputsOptions):
256
255
  }
257
256
 
258
257
  async function deriveCargoPackageTestInputs(
259
- { workspaceRoot, absoluteProjectRoot, memberDir, inputRoot = '{projectRoot}' }: CargoPackageTestInputsOptions,
258
+ { workspaceRoot, absoluteProjectRoot, memberDir }: CargoPackageTestInputsOptions,
260
259
  cache: CargoInputsCache,
261
260
  ): Promise<string[]> {
261
+ // Every fileset is anchored at `{workspaceRoot}`, never `{projectRoot}`: Nx
262
+ // hashes a `{projectRoot}` glob against only the files its project OWNS, and
263
+ // a nested Nx project owns the crates under it. A repository-root cargo
264
+ // workspace whose members live in `packages/*` would then hash none of its
265
+ // members' sources, and its archive would cache-hit against edited crates
266
+ // while every per-crate runner reran on the stale binaries.
267
+ const inputRoot = posix.join('{workspaceRoot}', relative(workspaceRoot, absoluteProjectRoot).split(sep).join('/'));
262
268
  const loadManifest = (path: string): Promise<TomlTable> => {
263
269
  const normalized = resolve(path);
264
270
  const cached = cache.manifests.get(normalized);
@@ -1,6 +1,5 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
2
2
  import { spawn } from 'node:child_process';
3
- import { existsSync } from 'node:fs';
4
3
  import { chmod, mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
5
4
  import { tmpdir } from 'node:os';
6
5
  import { dirname, join } from 'node:path';
@@ -10,13 +9,7 @@ import type { Task } from 'nx/src/config/task-graph';
10
9
 
11
10
  import { signalToCode } from 'nx/src/utils/exit-codes';
12
11
 
13
- import {
14
- cliExitOutcome,
15
- describeMiss,
16
- firstCacheMiss,
17
- firstStaleOutputs,
18
- parseTargetSelector,
19
- } from './ensure-built.js';
12
+ import { cliExitOutcome, describeMiss, firstCacheMiss, parseTargetSelector, unvouched } from './ensure-built.js';
20
13
 
21
14
  function task(overrides: Partial<Task> & Pick<Task, 'id'>): Task {
22
15
  const [project, target] = overrides.id.split(':');
@@ -98,20 +91,20 @@ describe('firstCacheMiss', () => {
98
91
  });
99
92
  });
100
93
 
101
- describe('firstStaleOutputs', () => {
94
+ describe('unvouched', () => {
102
95
  const tasks = [task({ id: 'lib:build', outputs: ['lib/dist'] }), task({ id: 'app:build', outputs: ['app/dist'] })];
103
96
 
104
97
  it('clears outputs the daemon vouches for', () => {
105
- expect(firstStaleOutputs(tasks, [true, true])).toBeNull();
98
+ expect(unvouched(tasks, [true, true])).toEqual([]);
106
99
  });
107
100
 
108
- it('reports the task whose outputs no longer match', () => {
109
- expect(firstStaleOutputs(tasks, [true, false])).toEqual({ kind: 'stale-outputs', taskId: 'app:build' });
101
+ it('keeps the tasks whose outputs the daemon does not vouch for', () => {
102
+ expect(unvouched(tasks, [true, false]).map(({ id }) => id)).toEqual(['app:build']);
110
103
  });
111
104
 
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' });
105
+ it('treats a missing verdict as unvouched rather than as a hit', () => {
106
+ expect(unvouched(tasks, [true]).map(({ id }) => id)).toEqual(['app:build']);
107
+ expect(unvouched(tasks, []).map(({ id }) => id)).toEqual(['lib:build', 'app:build']);
115
108
  });
116
109
  });
117
110
 
@@ -260,15 +253,9 @@ function runBuiltBin(workspace: string, args: readonly string[]): Promise<BinRun
260
253
  return runBinWith('node', builtBinEntry, workspace, args, {});
261
254
  }
262
255
 
263
- function nx(workspace: string, args: readonly string[]): Promise<number | null> {
264
- const child = spawn(join(repoRoot, 'node_modules', '.bin', 'nx'), [...args], {
265
- cwd: workspace,
266
- env: fixtureNxEnvironment({ NX_WORKSPACE_ROOT_PATH: workspace }),
267
- stdio: 'ignore',
268
- });
269
- return new Promise((settle, reject) => {
270
- child.once('error', reject);
271
- child.once('close', (code) => settle(code));
256
+ function nx(workspace: string, args: readonly string[]): Promise<BinRun> {
257
+ return runBinWith('node', join(repoRoot, 'node_modules', '.bin', 'nx'), workspace, args, {
258
+ NX_WORKSPACE_ROOT_PATH: workspace,
272
259
  });
273
260
  }
274
261
 
@@ -279,6 +266,7 @@ describe('smoo-nx-exec', () => {
279
266
  // Outside the workspace: a log inside it would be an undeclared output the
280
267
  // snapshot diff rightly reports as a changed input.
281
268
  const builds = () => `${workspace}-lib-builds.log`;
269
+ const ships = () => `${workspace}-app-ships.log`;
282
270
 
283
271
  beforeAll(async () => {
284
272
  // Realpath because macOS puts the temp directory behind a /private
@@ -343,6 +331,20 @@ describe('smoo-nx-exec', () => {
343
331
  cache: true,
344
332
  options: { command: 'exit 3', cwd: '{projectRoot}' },
345
333
  },
334
+ // A command-less aggregate, as Nx normalizes any target that only
335
+ // names dependencies. It declares no `cache`, so it is uncacheable.
336
+ stage: {
337
+ executor: 'nx:noop',
338
+ dependsOn: ['build'],
339
+ },
340
+ // A cacheable leaf that reaches its build only through the aggregate.
341
+ ship: {
342
+ executor: 'nx:run-commands',
343
+ cache: true,
344
+ inputs: ['{projectRoot}/source.txt'],
345
+ dependsOn: ['stage'],
346
+ options: { command: `cat dist/marker.txt >> ${ships()}`, cwd: '{projectRoot}' },
347
+ },
346
348
  },
347
349
  implicitDependencies: ['lib'],
348
350
  }),
@@ -363,15 +365,22 @@ describe('smoo-nx-exec', () => {
363
365
  await nx(workspace, ['daemon', '--stop']);
364
366
  await rm(workspace, { recursive: true, force: true });
365
367
  await rm(builds(), { force: true });
368
+ await rm(ships(), { force: true });
366
369
  }
367
370
  });
368
371
 
369
- it('builds the whole dependency graph on the first run, then execs the binary', async () => {
372
+ it('reuses the normal Nx CLI cache before executing the binary', async () => {
373
+ const built = await nx(workspace, ['run-many', '-t', 'build', '-p', 'app', '--outputStyle=static-failures-only']);
374
+ expect(built.code, built.stdout + built.stderr).toBe(0);
375
+ expect(await readFile(marker(), 'utf-8')).toBe('built\nlib\n');
376
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
377
+
370
378
  const run = await runBin(workspace, ['app:build', '--', './report', 'one']);
371
379
  expect(run.code).toBe(0);
372
- expect(run.stdout).toContain(`${MARKER} one`);
373
- expect(existsSync(marker())).toBe(true);
374
- expect(existsSync(join(workspace, 'packages', 'lib', 'dist', 'lib.txt'))).toBe(true);
380
+ expect(run.stderr).toBe('');
381
+ expect(run.stdout.split('\n')[0]).toBe(`${MARKER} one`);
382
+ expect(await readFile(marker(), 'utf-8')).toBe('built\nlib\n');
383
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
375
384
  });
376
385
 
377
386
  it('stays completely silent on a full cache hit', async () => {
@@ -381,6 +390,53 @@ describe('smoo-nx-exec', () => {
381
390
  // Nothing but the exec'd binary's own output: no Nx banner, no task log.
382
391
  expect(run.stdout.split('\n')[0]).toBe(MARKER);
383
392
  expect(run.stdout).not.toContain('nx run');
393
+ expect(await readFile(marker(), 'utf-8')).toBe('built\nlib\n');
394
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
395
+ });
396
+
397
+ it('stays silent through an uncacheable nx:noop aggregate, which has no command to run', async () => {
398
+ const first = await runBin(workspace, ['app:ship', '--', './report']);
399
+ expect(first.code, first.stdout + first.stderr).toBe(0);
400
+ expect(await readFile(ships(), 'utf-8')).toBe('built\nlib\n');
401
+
402
+ // Every task but the aggregate is now a recorded success. Treating the
403
+ // aggregate like an uncacheable command hands the whole graph back to Nx,
404
+ // which replays each dependency's cached log on every invocation.
405
+ const again = await runBin(workspace, ['app:ship', '--', './report']);
406
+ expect(again.code).toBe(0);
407
+ expect(again.stderr).toBe('');
408
+ expect(again.stdout.split('\n')[0], again.stdout).toBe(MARKER);
409
+ expect(again.stdout).not.toContain('nx run');
410
+ expect(await readFile(ships(), 'utf-8')).toBe('built\nlib\n');
411
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
412
+ });
413
+
414
+ it('stays silent when the daemon has lost its record of outputs that still match the cache', async () => {
415
+ // The daemon's output records live in memory and are lossy: a restart
416
+ // drops every one, and in a busy workspace a restore's own write events can
417
+ // be processed after Nx's 2 s grace and erase the record just made. Either
418
+ // way the working tree still holds exactly the cached bytes.
419
+ const stopped = await nx(workspace, ['daemon', '--stop']);
420
+ expect(stopped.code, stopped.stdout + stopped.stderr).toBe(0);
421
+
422
+ const run = await runBin(workspace, ['app:build', '--', './report']);
423
+ expect(run.code).toBe(0);
424
+ expect(run.stderr).toBe('');
425
+ expect(run.stdout.split('\n')[0], run.stdout).toBe(MARKER);
426
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
427
+ });
428
+
429
+ it('never vouches for rewritten output bytes the daemon holds no record of', async () => {
430
+ const stopped = await nx(workspace, ['daemon', '--stop']);
431
+ expect(stopped.code, stopped.stdout + stopped.stderr).toBe(0);
432
+ // Same length, different bytes: only a content comparison can tell.
433
+ await writeFile(marker(), 'BUILT\nLIB\n');
434
+
435
+ const run = await runBin(workspace, ['app:build', '--', './report'], { NX_VERBOSE_LOGGING: 'true' });
436
+ expect(run.code).toBe(0);
437
+ expect(run.stderr).toContain('app:build outputs are missing or modified on disk');
438
+ expect(await readFile(marker(), 'utf-8')).toBe('built\nlib\n');
439
+ expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
384
440
  });
385
441
 
386
442
  it('runs the emitted wrapper under Node without losing the hot path', async () => {
@@ -442,12 +498,14 @@ describe('smoo-nx-exec', () => {
442
498
  });
443
499
 
444
500
  it('runs again once a recorded output is gone from the working tree', async () => {
501
+ const before = await readFile(builds(), 'utf-8');
445
502
  await rm(marker());
446
503
  const run = await runBin(workspace, ['app:build', '--', './report'], { NX_VERBOSE_LOGGING: 'true' });
447
504
  expect(run.code).toBe(0);
448
505
  expect(run.stderr).toContain('outputs are missing or modified on disk');
449
506
  expect(run.stdout).toContain(MARKER);
450
- expect(existsSync(marker())).toBe(true);
507
+ expect(await readFile(marker(), 'utf-8')).toBe('built\nlib\n');
508
+ expect(await readFile(builds(), 'utf-8')).toBe(before);
451
509
  });
452
510
 
453
511
  it('runs again when a dependency\u0027s own inputs change', async () => {