@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
@@ -1,8 +1,9 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { randomUUID } from 'node:crypto';
3
- import { existsSync } from 'node:fs';
3
+ import type { BigIntStats } from 'node:fs';
4
+ import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync } from 'node:fs';
4
5
  import { createRequire } from 'node:module';
5
- import { join, resolve } from 'node:path';
6
+ import { isAbsolute, join, resolve } from 'node:path';
6
7
 
7
8
  import type * as NxConfiguration from 'nx/src/config/nx-json';
8
9
  import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
@@ -19,6 +20,7 @@ import type * as NxCreateTaskGraph from 'nx/src/tasks-runner/create-task-graph';
19
20
  import type { TaskResults } from 'nx/src/tasks-runner/life-cycle';
20
21
  import type * as NxRunCommand from 'nx/src/tasks-runner/run-command';
21
22
  import type * as NxTaskEnv from 'nx/src/tasks-runner/task-env';
23
+ import type * as NxTasksRunnerUtils from 'nx/src/tasks-runner/utils';
22
24
  import type * as NxCacheDirectory from 'nx/src/utils/cache-directory';
23
25
  import type * as NxCommandLineUtils from 'nx/src/utils/command-line-utils';
24
26
  import type { NxArgs } from 'nx/src/utils/command-line-utils';
@@ -38,6 +40,7 @@ interface NxRuntimeModules {
38
40
  readonly 'nx/src/tasks-runner/create-task-graph': typeof NxCreateTaskGraph;
39
41
  readonly 'nx/src/tasks-runner/run-command': typeof NxRunCommand;
40
42
  readonly 'nx/src/tasks-runner/task-env': typeof NxTaskEnv;
43
+ readonly 'nx/src/tasks-runner/utils': typeof NxTasksRunnerUtils;
41
44
  readonly 'nx/src/utils/cache-directory': typeof NxCacheDirectory;
42
45
  readonly 'nx/src/utils/command-line-utils': typeof NxCommandLineUtils;
43
46
  readonly 'nx/src/utils/exit-codes': typeof NxExitCodes;
@@ -203,19 +206,15 @@ export function firstCacheMiss(
203
206
  }
204
207
 
205
208
  /**
206
- * First task whose recorded outputs are no longer the bytes on disk.
209
+ * The entries the daemon does not vouch for.
207
210
  *
208
- * `matches[index]` is the daemon's verdict for `tasks[index]`. A short or
209
- * ragged array counts as stale rather than as a hit: a missing verdict is not a
210
- * positive one, and being wrong here means exec'ing a binary that was deleted.
211
+ * `verdicts[index]` is the daemon's verdict for `entries[index]`. A short or
212
+ * ragged array leaves the remainder unvouched rather than cleared: a missing
213
+ * verdict is not a positive one, and being wrong here means exec'ing a binary
214
+ * that was deleted.
211
215
  */
212
- export function firstStaleOutputs(tasks: readonly Task[], matches: readonly boolean[]): MissReason | null {
213
- for (let index = 0; index < tasks.length; index += 1) {
214
- if (matches[index] !== true) {
215
- return { kind: 'stale-outputs', taskId: tasks[index].id };
216
- }
217
- }
218
- return null;
216
+ export function unvouched<Entry>(entries: readonly Entry[], verdicts: readonly boolean[]): Entry[] {
217
+ return entries.filter((_, index) => verdicts[index] !== true);
219
218
  }
220
219
 
221
220
  /**
@@ -366,7 +365,7 @@ async function ensureBuiltInWorkspace(
366
365
  } else if (inputsChanged) {
367
366
  reason = { kind: 'stale-inputs', taskId: selectorTaskId };
368
367
  } else {
369
- reason = await probe(nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx);
368
+ reason = await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx);
370
369
  }
371
370
  performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
372
371
  const outcome =
@@ -472,6 +471,7 @@ async function refreshWorkspaceContext(
472
471
 
473
472
  /** `null` when the target is already built; otherwise the first reason it is not. */
474
473
  async function probe(
474
+ workspaceRoot: string,
475
475
  nxJson: NxJsonConfiguration,
476
476
  nxArgs: NxArgs,
477
477
  projectGraph: ProjectGraph,
@@ -485,6 +485,7 @@ async function probe(
485
485
  const { getTaskSpecificEnv } = requireNx('nx/src/tasks-runner/task-env');
486
486
  const { getCache } = requireNx('nx/src/tasks-runner/cache');
487
487
  const { getRunnerOptions } = requireNx('nx/src/tasks-runner/run-command');
488
+ const { getExecutorNameForTask } = requireNx('nx/src/tasks-runner/utils');
488
489
 
489
490
  // `isCloudDefault: false` because these options are only read here by the
490
491
  // hasher, which looks at `selectivelyHashTsConfig`; the cloud credentials the
@@ -512,20 +513,29 @@ async function probe(
512
513
  await hashTasks(hasher, projectGraph, taskGraph, perTaskEnvs, getTaskDetails(), tasks);
513
514
  performance.measure('ensureBuilt:hash', 'ensureBuilt:hash:start');
514
515
 
516
+ // An `nx:noop` task has no command: Nx completes it without spawning
517
+ // anything, and normalizes a command-less target that only names dependencies
518
+ // to one. A cache record would vouch for no work — the tasks it aggregates are
519
+ // in this graph and answer for themselves — so none is required. Demanding one
520
+ // made every call through an uncacheable aggregate hand the whole graph to Nx,
521
+ // which replays each dependency's cached log. Outputs a noop declares are
522
+ // still verified below, with every other task's.
523
+ const recordedTasks = tasks.filter((task) => getExecutorNameForTask(task, projectGraph) !== 'nx:noop');
524
+
515
525
  // Nx's own factory, but deliberately never `init()`ed: that is what makes
516
526
  // this a local-only, side-effect-free question. `init()` attaches the remote
517
527
  // cache — a network round-trip, and a download is precisely the work this
518
528
  // probe exists to detect — and asserts that the cache directory matches the
519
- // database. Neither matters here: a hit is only declared when the outputs on
520
- // disk already match, so the cache directory is never read.
529
+ // database. Neither matters here: the directory is read only to compare
530
+ // outputs against, and an artifact missing from it is a miss.
521
531
  performance.mark('ensureBuilt:cache:start');
522
532
  const cache = getCache(runnerOptions);
523
- const cachedResults = await cache.getBatch(tasks.filter((task) => task.cache && task.hash));
533
+ const cachedResults = await cache.getBatch(recordedTasks.filter((task) => task.cache && task.hash));
524
534
  const cachedCodeByHash = new Map<string, number>();
525
535
  for (const [hash, result] of cachedResults) {
526
536
  cachedCodeByHash.set(hash, result.code);
527
537
  }
528
- const cacheMiss = firstCacheMiss(tasks, cachedCodeByHash);
538
+ const cacheMiss = firstCacheMiss(recordedTasks, cachedCodeByHash);
529
539
  performance.measure('ensureBuilt:cache', 'ensureBuilt:cache:start');
530
540
  if (cacheMiss) {
531
541
  return cacheMiss;
@@ -534,22 +544,257 @@ async function probe(
534
544
  // A cache record only proves the task once succeeded with these inputs. The
535
545
  // artifact this facility is about — the binary that is about to be exec'd —
536
546
  // lives in the working tree, where anything may have deleted or rewritten it
537
- // since. Only the daemon can settle that, because it holds the recorded
538
- // output hashes.
539
- const withOutputs: Task[] = [];
540
- const outputEntries: { outputs: string[]; hash: string }[] = [];
547
+ // since. The daemon answers that cheaply from the output hashes it recorded
548
+ // when Nx last wrote them.
549
+ const claims: OutputClaim[] = [];
541
550
  for (const task of tasks) {
542
- const hash = task.hash;
543
- if (task.outputs.length === 0 || hash === undefined) {
544
- continue;
551
+ if (task.outputs.length > 0 && task.hash !== undefined) {
552
+ claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
545
553
  }
546
- withOutputs.push(task);
547
- outputEntries.push({ outputs: task.outputs, hash });
548
554
  }
549
555
  performance.mark('ensureBuilt:outputs:start');
550
- const matches = await daemonClient.outputsHashesMatchBatch(outputEntries);
556
+ const verdicts = await daemonClient.outputsHashesMatchBatch(claims.map(({ outputs, hash }) => ({ outputs, hash })));
551
557
  performance.measure('ensureBuilt:outputs', 'ensureBuilt:outputs:start');
552
- return firstStaleOutputs(withOutputs, matches);
558
+ const doubted = unvouched(claims, verdicts);
559
+ if (doubted.length === 0) {
560
+ return null;
561
+ }
562
+
563
+ // Its records are lossy, though, and a lost record is not a changed output.
564
+ // They live in memory, so a restart drops every one. They are kept per
565
+ // collapsed directory (Nx tracks at most three paths per level), so a write
566
+ // anywhere under `packages/` voids a task whose outputs span four projects.
567
+ // And a write the daemon processes more than 2 s after a record erases it,
568
+ // which a daemon busy hashing does to a restore's own writes — so each
569
+ // restore guarantees the next one. What settles the question is what a hit
570
+ // would restore: Nx's own local artifact for this exact hash. A working tree
571
+ // that already holds every entry of it is what the restore would leave.
572
+ // Anything else — no artifact, a difference, an error while reading — stays
573
+ // a miss.
574
+ performance.mark('ensureBuilt:artifacts:start');
575
+ const { expandOutputs } = requireNx('nx/src/native');
576
+ const comparison: ArtifactComparison = {
577
+ expandOutputs,
578
+ left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
579
+ right: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
580
+ observed: [],
581
+ realDirectories: new Set(),
582
+ };
583
+ for (const claim of doubted) {
584
+ // `recordedTasks` excludes `nx:noop`, so a noop's declared outputs have no
585
+ // artifact here and stay a miss.
586
+ const artifact = cachedResults.get(claim.hash);
587
+ if (artifact === undefined || !outputsMatchArtifact(comparison, workspaceRoot, artifact.outputsPath, claim)) {
588
+ return { kind: 'stale-outputs', taskId: claim.taskId };
589
+ }
590
+ }
591
+ // A write that landed while the bytes were being compared would otherwise be
592
+ // recorded as current.
593
+ const movedBeforeRecord = firstMovedClaim(comparison.observed);
594
+ if (movedBeforeRecord !== null) {
595
+ return { kind: 'stale-outputs', taskId: movedBeforeRecord };
596
+ }
597
+ // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
598
+ // next call is back on the cheap path.
599
+ await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
600
+ const movedDuringRecord = firstMovedClaim(comparison.observed);
601
+ performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
602
+ return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
603
+ }
604
+
605
+ /** A task's declared outputs, resolved to workspace-relative paths, and the hash they were cached under. */
606
+ interface OutputClaim {
607
+ readonly taskId: string;
608
+ readonly outputs: string[];
609
+ readonly hash: string;
610
+ }
611
+
612
+ /** A working-tree node as it was when compared, so a later write to it can be noticed. */
613
+ interface ObservedNode {
614
+ readonly taskId: string;
615
+ readonly path: string;
616
+ readonly stats: BigIntStats;
617
+ }
618
+
619
+ interface ArtifactComparison {
620
+ readonly expandOutputs: typeof NxNative.expandOutputs;
621
+ readonly left: Buffer;
622
+ readonly right: Buffer;
623
+ readonly observed: ObservedNode[];
624
+ /** Workspace directories already proven real (not symlinks) on the way to an entry. */
625
+ readonly realDirectories: Set<string>;
626
+ }
627
+
628
+ /** Read size for byte comparison: large enough to stream a binary quickly, small enough never to load one whole. */
629
+ const COMPARE_CHUNK_BYTES = 1 << 20;
630
+
631
+ /**
632
+ * Whether restoring `claim` from the artifact would leave the working tree as
633
+ * it is.
634
+ *
635
+ * The entries are the ones Nx's restore copies: `expandOutputs` over the
636
+ * artifact root with the task's outputs, globs and negations included. That
637
+ * walk is bounded by the artifact, which holds nothing but outputs, and it
638
+ * needs no reading of the pattern here. A restore replaces each entry whole
639
+ * and touches nothing else, so a working-tree file that matches a glob but is
640
+ * absent from the artifact survives it and is no difference. An output path
641
+ * Nx would refuse to restore (absolute, or climbing out with `..`), an
642
+ * artifact holding no entry at all, and system errors while reading are all
643
+ * misses.
644
+ */
645
+ function outputsMatchArtifact(
646
+ comparison: ArtifactComparison,
647
+ workspaceRoot: string,
648
+ artifactRoot: string,
649
+ claim: OutputClaim,
650
+ ): boolean {
651
+ if (claim.outputs.some((output) => isAbsolute(output) || output.split('/').includes('..'))) {
652
+ return false;
653
+ }
654
+ try {
655
+ const entries = comparison.expandOutputs(artifactRoot, claim.outputs);
656
+ return (
657
+ entries.length > 0 &&
658
+ entries.every(
659
+ (entry) =>
660
+ throughRealDirectories(comparison.realDirectories, workspaceRoot, entry) &&
661
+ sameTree(comparison, claim.taskId, join(workspaceRoot, entry), join(artifactRoot, entry)),
662
+ )
663
+ );
664
+ } catch (error) {
665
+ if (isSystemError(error)) {
666
+ return false;
667
+ }
668
+ throw error;
669
+ }
670
+ }
671
+
672
+ /**
673
+ * Whether every directory between the workspace root and `entry` is a real
674
+ * directory. A restore realizes a symlinked parent as a directory, so reading
675
+ * an entry through one would compare bytes the restore does not leave there —
676
+ * and would read the filesystem behind a link.
677
+ */
678
+ function throughRealDirectories(realDirectories: Set<string>, workspaceRoot: string, entry: string): boolean {
679
+ let directory = workspaceRoot;
680
+ const segments = entry.split('/');
681
+ for (const segment of segments.slice(0, -1)) {
682
+ directory = join(directory, segment);
683
+ if (realDirectories.has(directory)) {
684
+ continue;
685
+ }
686
+ if (lstatSync(directory, { throwIfNoEntry: false })?.isDirectory() !== true) {
687
+ return false;
688
+ }
689
+ realDirectories.add(directory);
690
+ }
691
+ return true;
692
+ }
693
+
694
+ /**
695
+ * Whether `actual` is `expected` as a restore would recreate it: same node
696
+ * type; a symlink by its text, never followed, because the link is what Nx
697
+ * stores and restores; a file by permission bits and bytes; a directory by its
698
+ * entry names and each entry in turn. Every working-tree node compared is
699
+ * observed, so a write during the comparison can be caught afterwards.
700
+ */
701
+ function sameTree(comparison: ArtifactComparison, taskId: string, actual: string, expected: string): boolean {
702
+ const actualStats = lstatSync(actual, { bigint: true, throwIfNoEntry: false });
703
+ const expectedStats = lstatSync(expected, { bigint: true, throwIfNoEntry: false });
704
+ if (actualStats === undefined || expectedStats === undefined) {
705
+ return false;
706
+ }
707
+ comparison.observed.push({ taskId, path: actual, stats: actualStats });
708
+ if (expectedStats.isSymbolicLink()) {
709
+ return actualStats.isSymbolicLink() && readlinkSync(actual) === readlinkSync(expected);
710
+ }
711
+ if (expectedStats.isFile()) {
712
+ return (
713
+ actualStats.isFile() &&
714
+ actualStats.size === expectedStats.size &&
715
+ (actualStats.mode & 0o7777n) === (expectedStats.mode & 0o7777n) &&
716
+ sameBytes(comparison, actual, expected, Number(expectedStats.size))
717
+ );
718
+ }
719
+ if (expectedStats.isDirectory()) {
720
+ if (!actualStats.isDirectory()) {
721
+ return false;
722
+ }
723
+ const names = readdirSync(actual).sort();
724
+ const expectedNames = readdirSync(expected).sort();
725
+ if (names.length !== expectedNames.length) {
726
+ return false;
727
+ }
728
+ for (let index = 0; index < names.length; index += 1) {
729
+ if (names[index] !== expectedNames[index]) {
730
+ return false;
731
+ }
732
+ }
733
+ return names.every((name) => sameTree(comparison, taskId, join(actual, name), join(expected, name)));
734
+ }
735
+ return false;
736
+ }
737
+
738
+ /** Byte equality of two files already known to be `size` bytes, streamed through the comparison's buffers. */
739
+ function sameBytes(comparison: ArtifactComparison, actual: string, expected: string, size: number): boolean {
740
+ const actualFd = openSync(actual, 'r');
741
+ try {
742
+ const expectedFd = openSync(expected, 'r');
743
+ try {
744
+ for (let offset = 0; offset < size; offset += COMPARE_CHUNK_BYTES) {
745
+ const length = Math.min(COMPARE_CHUNK_BYTES, size - offset);
746
+ // A short read means the file changed length since it was sized.
747
+ if (
748
+ readSync(actualFd, comparison.left, 0, length, offset) !== length ||
749
+ readSync(expectedFd, comparison.right, 0, length, offset) !== length ||
750
+ comparison.left.compare(comparison.right, 0, length, 0, length) !== 0
751
+ ) {
752
+ return false;
753
+ }
754
+ }
755
+ return true;
756
+ } finally {
757
+ closeSync(expectedFd);
758
+ }
759
+ } finally {
760
+ closeSync(actualFd);
761
+ }
762
+ }
763
+
764
+ /**
765
+ * The task owning the first observed node that is no longer the node it was
766
+ * compared as: replaced (device, inode), rewritten (size, mtime) or otherwise
767
+ * touched (mode, ctime).
768
+ */
769
+ function firstMovedClaim(observed: readonly ObservedNode[]): string | null {
770
+ for (const { taskId, path, stats } of observed) {
771
+ let current: BigIntStats | undefined;
772
+ try {
773
+ current = lstatSync(path, { bigint: true, throwIfNoEntry: false });
774
+ } catch (error) {
775
+ if (isSystemError(error)) {
776
+ return taskId;
777
+ }
778
+ throw error;
779
+ }
780
+ if (
781
+ current === undefined ||
782
+ current.dev !== stats.dev ||
783
+ current.ino !== stats.ino ||
784
+ current.mode !== stats.mode ||
785
+ current.size !== stats.size ||
786
+ current.mtimeNs !== stats.mtimeNs ||
787
+ current.ctimeNs !== stats.ctimeNs
788
+ ) {
789
+ return taskId;
790
+ }
791
+ }
792
+ return null;
793
+ }
794
+
795
+ /** An operating-system or native-binding failure, as opposed to a programming error. */
796
+ function isSystemError(error: unknown): boolean {
797
+ return error instanceof Error && 'code' in error && typeof error.code === 'string';
553
798
  }
554
799
 
555
800
  interface CompletedRun {
package/src/index.test.ts CHANGED
@@ -93,6 +93,90 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
93
93
  }
94
94
  }, 120_000);
95
95
 
96
+ it('never restores a stale child artifact from the cached build aggregate', async () => {
97
+ const workspace = await createWorkspace();
98
+ const root = workspace.context.workspaceRoot;
99
+ const repositoryRoot = fileURLToPath(new URL('../../../', import.meta.url));
100
+ try {
101
+ await symlink(join(repositoryRoot, 'node_modules'), join(root, 'node_modules'), 'dir');
102
+ await workspace.write(
103
+ 'package.json',
104
+ '{"name":"aggregate-workspace","private":true,"workspaces":["packages/*"]}\n',
105
+ );
106
+ await workspace.write(
107
+ 'nx.json',
108
+ JSON.stringify({
109
+ plugins: [fileURLToPath(new URL('../dist/index.js', import.meta.url))],
110
+ namedInputs: { default: ['{projectRoot}/**/*'] },
111
+ }),
112
+ );
113
+ // Emitted artifacts are not sources, so the aggregate's own inputs never see them.
114
+ await workspace.write('.gitignore', 'dist\n.nx\nnode_modules\n');
115
+ await workspace.write('toolchain.txt', 'toolchain-1\n');
116
+ await workspace.write('packages/app/source.txt', 'source\n');
117
+ // The emitter keys on one input outside its project, as a transform or
118
+ // toolchain does. The inferred `build` aggregate hashes only the project,
119
+ // so a toolchain change moves the emitter's key and not the aggregate's.
120
+ await workspace.write(
121
+ 'packages/app/package.json',
122
+ JSON.stringify({
123
+ name: 'app',
124
+ private: true,
125
+ nx: {
126
+ targets: {
127
+ 'emit-js': {
128
+ executor: 'nx:run-commands',
129
+ cache: true,
130
+ inputs: ['{projectRoot}/source.txt', '{workspaceRoot}/toolchain.txt'],
131
+ outputs: ['{projectRoot}/dist'],
132
+ options: {
133
+ command: 'mkdir -p dist && cat source.txt ../../toolchain.txt > dist/app.js',
134
+ cwd: '{projectRoot}',
135
+ },
136
+ },
137
+ },
138
+ },
139
+ }),
140
+ );
141
+ const build = async () => {
142
+ const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
143
+ cwd: root,
144
+ env: {
145
+ ...process.env,
146
+ PATH: `${join(repositoryRoot, 'node_modules/.bin')}:${process.env.PATH ?? ''}`,
147
+ NX_DAEMON: 'false',
148
+ NX_ISOLATE_PLUGINS: 'false',
149
+ NX_WORKSPACE_DATA_DIRECTORY: join(root, '.nx/workspace-data'),
150
+ NX_CACHE_DIRECTORY: join(root, '.nx/cache'),
151
+ },
152
+ stdout: 'pipe',
153
+ stderr: 'pipe',
154
+ });
155
+ const [exitCode, stdout, stderr] = await Promise.all([
156
+ child.exited,
157
+ new Response(child.stdout).text(),
158
+ new Response(child.stderr).text(),
159
+ ]);
160
+ return { exitCode, output: stdout + stderr };
161
+ };
162
+ const artifact = () => readFile(join(root, 'packages/app/dist/app.js'), 'utf8');
163
+
164
+ const first = await build();
165
+ expect(first.exitCode, first.output).toBe(0);
166
+ expect(await artifact()).toBe('source\ntoolchain-1\n');
167
+
168
+ await workspace.write('toolchain.txt', 'toolchain-2\n');
169
+ const second = await build();
170
+ expect(second.exitCode, second.output).toBe(0);
171
+ // The emitter reran on its new key. The aggregate's key did not move, so
172
+ // it replays from cache — and a replay must not put back the dist it
173
+ // would have claimed on the first run.
174
+ expect(await artifact(), second.output).toBe('source\ntoolchain-2\n');
175
+ } finally {
176
+ await workspace.cleanup();
177
+ }
178
+ }, 120_000);
179
+
96
180
  it('names standalone package projects from package metadata', async () => {
97
181
  const workspace = await createWorkspace();
98
182
  try {
@@ -220,12 +304,6 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
220
304
  expect(targets.build?.executor).toBe('nx:noop');
221
305
  expect(targets.build?.cache).toBe(true);
222
306
  expect(targets.build?.dependsOn).toEqual(['^build', 'tsc-js']);
223
- // An `nx:noop` aggregate writes no file, so it must claim none: a
224
- // `{projectRoot}/dist` claim here would double-cache its children's bytes
225
- // and make `github-ci nx-run-many --collect-outputs` attribute every file
226
- // under dist to `build`, including the platform artifacts that collect
227
- // deliberately leaves to the platform step.
228
- expect(targets.build?.outputs).toBeUndefined();
229
307
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
230
308
  expect(targets.clean?.cache).toBe(false);
231
309
 
@@ -787,7 +865,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
787
865
  for (const name of ['cargo-test-compile', 'cargo-test-archive', 'cargo-lint', CARGO_CROSS_LINT_TARGET]) {
788
866
  const inputs = targets[name]?.inputs ?? [];
789
867
  expect(inputs).toContainEqual({ dependentTasksOutputFiles: '**/*', transitive: false });
790
- expect(inputs).toContain('{projectRoot}/crates/ferris-core/**/*');
868
+ expect(inputs).toContain('{workspaceRoot}/packages/ferris/crates/ferris-core/**/*');
791
869
  }
792
870
  expect(targets['cargo-test-compile']?.executor).toBe('nx:run-commands');
793
871
  expect(targets['cargo-test-compile']?.cache).toBe(false);
@@ -830,16 +908,20 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
830
908
  expect(targets['cargo-test-ferris-core']?.options?.command).toMatch(
831
909
  /^cargo --frozen nextest run --archive-file target\/nextest\/archive\.tar\.zst --workspace-remap \. -E 'package\(ferris-core\)' --no-tests=pass --user-config-file none --tool-config-file "smoo:\$PWD\/.*nextest\.toml"$/,
832
910
  );
833
- expect(targets['cargo-test-ferris-core']?.inputs).toContain('{projectRoot}/.config/nextest.toml');
911
+ expect(targets['cargo-test-ferris-core']?.inputs).toContain(
912
+ '{workspaceRoot}/packages/ferris/.config/nextest.toml',
913
+ );
834
914
  expect(targets['cargo-test-ferris-core']?.configurations?.production).toEqual({});
835
915
  expect(targets['cargo-test-ferris-core']?.inputs).toEqual(
836
916
  expect.arrayContaining([
837
- '{projectRoot}/Cargo.toml',
838
- '{projectRoot}/Cargo.lock',
839
- '{projectRoot}/crates/ferris-core/**/*',
917
+ '{workspaceRoot}/packages/ferris/Cargo.toml',
918
+ '{workspaceRoot}/packages/ferris/Cargo.lock',
919
+ '{workspaceRoot}/packages/ferris/crates/ferris-core/**/*',
840
920
  ]),
841
921
  );
842
- expect(targets['cargo-test-ferris-core']?.inputs).not.toContain('{projectRoot}/crates/ferris-wasm/**/*');
922
+ expect(targets['cargo-test-ferris-core']?.inputs).not.toContain(
923
+ '{workspaceRoot}/packages/ferris/crates/ferris-wasm/**/*',
924
+ );
843
925
  // One clippy for the workspace in one target dir: a per-crate closure
844
926
  // compiled every shared dependency once per crate.
845
927
  expect(targets['cargo-lint']?.options).toMatchObject({
@@ -852,7 +934,10 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
852
934
  expect(targets['cargo-lint']?.cache).toBe(true);
853
935
  expect(targets['cargo-lint']?.outputs).toEqual([]);
854
936
  expect(targets['cargo-lint']?.inputs).toEqual(
855
- expect.arrayContaining(['{projectRoot}/crates/ferris-core/**/*', '{projectRoot}/crates/ferris-wasm/**/*']),
937
+ expect.arrayContaining([
938
+ '{workspaceRoot}/packages/ferris/crates/ferris-core/**/*',
939
+ '{workspaceRoot}/packages/ferris/crates/ferris-wasm/**/*',
940
+ ]),
856
941
  );
857
942
  expect(targets.lint?.dependsOn).toEqual(['cargo-lint']);
858
943
  expect(targets[CARGO_CROSS_LINT_TARGET]?.options).toMatchObject({
@@ -1153,12 +1238,23 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
1153
1238
  'cargo --frozen clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings',
1154
1239
  ],
1155
1240
  });
1156
- expect(outer['cargo-lint']?.inputs).toEqual(
1157
- expect.arrayContaining([
1158
- '{projectRoot}/packages/group/crates/alpha/**/*',
1159
- '{projectRoot}/packages/group/crates/beta/**/*',
1160
- ]),
1161
- );
1241
+ // Workspace-anchored, because Nx hashes a `{projectRoot}` glob against
1242
+ // only the files its project owns, and the group project owns alpha and
1243
+ // beta: spelled from the root project, those globs hash nothing and the
1244
+ // archive replays stale binaries against edited crates.
1245
+ for (const name of ['cargo-lint', 'cargo-test-archive']) {
1246
+ expect(outer[name]?.inputs).toEqual(
1247
+ expect.arrayContaining([
1248
+ '{workspaceRoot}/packages/group/crates/alpha/**/*',
1249
+ '{workspaceRoot}/packages/group/crates/beta/**/*',
1250
+ ]),
1251
+ );
1252
+ expect(
1253
+ (outer[name]?.inputs ?? []).filter(
1254
+ (input) => typeof input === 'string' && input.startsWith('{projectRoot}/packages/'),
1255
+ ),
1256
+ ).toEqual([]);
1257
+ }
1162
1258
  expect(group['cargo-lint']?.dependsOn).toEqual([{ projects: ['outer-root'], target: 'cargo-lint' }]);
1163
1259
  expect(group.build?.dependsOn).toEqual([
1164
1260
  '^build',
@@ -1167,7 +1263,8 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
1167
1263
  ]);
1168
1264
  expect(outer['cargo-lint-host-core']).toBeUndefined();
1169
1265
  expect(inner['cargo-lint']?.options).toMatchObject({ cwd: 'nested' });
1170
- expect(inner['cargo-lint']?.inputs).toContain('{projectRoot}/**/*');
1266
+ expect(inner['cargo-lint']?.inputs).toContain('{workspaceRoot}/nested/**/*');
1267
+ expect(inner['cargo-lint']?.inputs).toContain('{workspaceRoot}/nested/modules/leaf/**/*');
1171
1268
  expect(inner['cargo-lint']?.inputs).not.toContain('{workspaceRoot}/packages/group/crates/alpha/**/*');
1172
1269
  expect(leaf['cargo-lint']?.dependsOn).toEqual([{ projects: ['inner-root'], target: 'cargo-lint' }]);
1173
1270
  expect(leaf['cargo-lint-inner-leaf']).toBeUndefined();
package/src/index.ts CHANGED
@@ -1627,20 +1627,26 @@ async function createProjectTargets(
1627
1627
  };
1628
1628
  }
1629
1629
 
1630
- // No `outputs`, deliberately: this aggregate runs no command, so every file
1631
- // under dist belongs to the concrete target that emitted it. Claiming
1632
- // `{projectRoot}/dist` here would cache the children's bytes a second time
1633
- // under the aggregate's hash, and would make `smoo github-ci nx-run-many
1634
- // --collect-outputs` attribute to `build` whatever sits under dist — including
1635
- // platform-target artifacts the collect excludes on purpose and stale
1636
- // artifacts of targets that never ran. Two collected trees then claim one
1637
- // binary and `apply-outputs` rejects the overlap. `lint` and `test` are
1630
+ // Empty `outputs`, deliberately and explicitly: this aggregate runs no
1631
+ // command, so every file under dist belongs to the concrete target that
1632
+ // emitted it. Claiming `{projectRoot}/dist` here would cache the children's
1633
+ // bytes a second time under the aggregate's hash, and would make `smoo
1634
+ // github-ci nx-run-many --collect-outputs` attribute to `build` whatever sits
1635
+ // under dist — including platform-target artifacts the collect excludes on
1636
+ // purpose and stale artifacts of targets that never ran. Two collected trees
1637
+ // then claim one binary and `apply-outputs` rejects the overlap. Omitting the
1638
+ // key is not the same thing: Nx gives a `build` or `prepare` target without
1639
+ // `outputs` its legacy defaults (`{projectRoot}/dist`, `/build`, `/public`,
1640
+ // `dist/{projectRoot}`), so a cache hit on the aggregate — whose key does
1641
+ // not move when only a child's own inputs do — restored the children's
1642
+ // previous dist over what they had just emitted. `lint` and `test` are
1638
1643
  // aggregates on the same terms. Same reason nx.json must not put `outputs` in
1639
1644
  // targetDefaults.build (see validateBuildTargetDefault).
1640
1645
  if (hasOrdinaryBuildOutputTarget) {
1641
1646
  targets.build = {
1642
1647
  executor: 'nx:noop',
1643
1648
  cache: true,
1649
+ outputs: [],
1644
1650
  // `cargo-napi` is both an output family and a serialized cargo writer, so
1645
1651
  // the two sources overlap; the Set keeps one edge per target while holding
1646
1652
  // the order the families are listed in.
@@ -2358,8 +2364,10 @@ async function addCargoTestTargets(
2358
2364
  if (plan.package.projectRoot !== projectRoot) {
2359
2365
  continue;
2360
2366
  }
2361
- const inputRoot =
2362
- projectRoot === workspace.projectRoot ? '{projectRoot}' : posix.join('{workspaceRoot}', workspace.projectRoot);
2367
+ // Workspace-anchored for the reason `cargoPackageTestInputs` gives: a
2368
+ // `{projectRoot}` glob hashes only files this project owns, and sibling
2369
+ // manifests belong to whichever nested project holds them.
2370
+ const inputRoot = posix.join('{workspaceRoot}', workspace.projectRoot);
2363
2371
  // A workspace nextest build unifies features across every selected member.
2364
2372
  // A sibling manifest can therefore change this crate's tests without being
2365
2373
  // a path dependency. Track those manifests, not unrelated sibling sources.
@@ -2373,7 +2381,6 @@ async function addCargoTestTargets(
2373
2381
  workspaceRoot,
2374
2382
  absoluteProjectRoot: join(workspaceRoot, workspace.projectRoot),
2375
2383
  memberDir: plan.package.dir,
2376
- inputRoot,
2377
2384
  cache: workspace.inputsCache,
2378
2385
  })),
2379
2386
  ...workspace.packages.map((member) => posix.join(inputRoot, member.package.dir, 'Cargo.toml')),