@smoothbricks/nx-plugin 0.4.20 → 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 -1
  4. package/dist/cargo-source-hash.d.ts.map +1 -1
  5. package/dist/cargo-source-hash.js +16 -12
  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 +196 -1
  32. package/src/cargo-source-hash.ts +22 -14
  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
@@ -73,19 +73,26 @@ runs:
73
73
  # and that DB lives in workspace-data. Since CI and Publish sit in
74
74
  # different concurrency groups they run simultaneously on one host, so a
75
75
  # repository-wide tree let one workflow rewrite the other's graph state
76
- # mid-read. CI concurrency is per workflow/ref, not workflow alone:
77
- # separate PRs and jobs must never share mutable graph or eval state.
76
+ # mid-read. Per workflow alone is not enough either: CI's concurrency
77
+ # group is per pull request (by number), or per ref for pushes, so
78
+ # separate PRs and jobs run at once and must never share mutable graph
79
+ # or eval state. The scope hashes that same identity, not github.ref: a
80
+ # merged close carries the base branch's ref, so two pull requests'
81
+ # cleanups merged a minute apart would share one scope while running in
82
+ # different concurrency groups.
78
83
  # Hash the exact identity (not a lossy path slug); keep cache artifacts
79
- # and their metadata DB together, warm across runs of the same ref/job.
84
+ # and their metadata DB together, warm across runs of the same pull
85
+ # request or ref and job.
80
86
  - name: 📂 Locate the Nx cache on the shared bind
81
87
  if: steps.runner-kind.outputs.host == 'true'
82
88
  shell: bash
83
89
  env:
84
90
  NX_CACHE_REPO: ${{ github.repository }}
85
91
  NX_CACHE_LANE: ${{ github.workflow }}
92
+ SCOPE_REF: ${{ github.event.pull_request.number || github.ref }}
86
93
  run: |
87
94
  lane=$(printf '%s' "$NX_CACHE_LANE" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9._-' '-')
88
- scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$GITHUB_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
95
+ scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$SCOPE_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
89
96
  NX_CACHE_ROOT="/var/cache/ci/nx/$NX_CACHE_REPO/${lane:-default}/$scope"
90
97
  mkdir -p "$NX_CACHE_ROOT/cache" "$NX_CACHE_ROOT/workspace-data"
91
98
  {
@@ -102,8 +109,12 @@ runs:
102
109
  # .devenv (eval SQLite, gc roots, profile) and .direnv survive the job, so
103
110
  # the next job on this host evaluates nothing and downloads nothing. The
104
111
  # Actions cache segment below is the ephemeral runners' substitute for this.
105
- # Scoped per workflow/ref/job/platform like Nx: separate PR runs and
106
- # jobs can overlap, and one eval-cache.db must have only one writer.
112
+ # Scoped per workflow, pull request (by number) or ref for pushes, job and
113
+ # platform, like Nx: separate PR runs and jobs can overlap, and one
114
+ # eval-cache.db must have only one writer. Not per github.ref: a merged
115
+ # close carries the base branch's ref, so two pull requests' cleanups
116
+ # would share this state and the second's bind mount below would shadow
117
+ # the first's workspace.
107
118
  #
108
119
  # The eval cache keys on canonical absolute paths, and act's host executor
109
120
  # checks out under <cache>/<random 8 chars>/hostexecutor - a new path every
@@ -124,9 +135,10 @@ runs:
124
135
  DEVENV_STATE_REPO: ${{ github.repository }}
125
136
  DEVENV_STATE_LANE: ${{ github.workflow }}
126
137
  DEVENV_STATE_JOB: ${{ github.job }}
138
+ SCOPE_REF: ${{ github.event.pull_request.number || github.ref }}
127
139
  run: |
128
140
  lane=$(printf '%s' "$DEVENV_STATE_LANE" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9._-' '-')
129
- scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$GITHUB_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
141
+ scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$SCOPE_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
130
142
  root="/var/cache/ci/devenv/$DEVENV_STATE_REPO/${lane:-default}/$DEVENV_STATE_JOB/$scope"
131
143
  mkdir -p "$root/.devenv" "$root/.direnv"
132
144
  rm -rf tooling/direnv/.devenv tooling/direnv/.direnv
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.20",
3
+ "version": "0.4.21",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -3,12 +3,16 @@ import { resolve } from 'node:path';
3
3
  import { hashCargoPathInputs } from '../cargo-source-hash.js';
4
4
 
5
5
  const args = process.argv.slice(2);
6
- if (args.length > 1) {
7
- process.stderr.write('usage: smoo-nx-cargo-hash [Cargo.toml]\n');
6
+ const includeWorkspace = args[0] === '--include-workspace';
7
+ const manifest = args[includeWorkspace ? 1 : 0];
8
+ if (args.length > (includeWorkspace ? 2 : 1) || manifest?.startsWith('-')) {
9
+ process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [Cargo.toml]\n');
8
10
  process.exit(2);
9
11
  }
10
12
  try {
11
- process.stdout.write(`${await hashCargoPathInputs(resolve(args[0] ?? 'Cargo.toml'), process.cwd())}\n`);
13
+ process.stdout.write(
14
+ `${await hashCargoPathInputs(resolve(manifest ?? 'Cargo.toml'), process.cwd(), { includeWorkspace })}\n`,
15
+ );
12
16
  } catch (error) {
13
17
  process.stderr.write(`smoo-nx-cargo-hash: ${error instanceof Error ? error.message : String(error)}\n`);
14
18
  process.stderr.write(
@@ -1,6 +1,6 @@
1
1
  import { expect, it } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
- import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3
+ import { mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join } from 'node:path';
6
6
  import { hashCargoPathInputs } from './cargo-source-hash.js';
@@ -90,3 +90,198 @@ it('the runtime input is byte-identical under cargo lock contention: nothing on
90
90
  await rm(root, { recursive: true, force: true });
91
91
  }
92
92
  });
93
+
94
+ async function workspaceCargoFixture(root: string) {
95
+ const workspace = join(root, 'workspace');
96
+ const manifest = join(workspace, 'Cargo.toml');
97
+ const sources = {
98
+ 'workspace/Cargo.toml': '[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf"]\nresolver="2"\n',
99
+ 'workspace/packages/app/Cargo.toml':
100
+ '[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
101
+ 'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
102
+ 'workspace/crates/bridge/Cargo.toml':
103
+ '[package]\nname="bridge"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nleaf={path="../leaf"}\nexternal={path="../../../external"}\n',
104
+ 'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
105
+ 'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
106
+ 'workspace/crates/leaf/src/lib.rs': 'pub fn answer() -> u8 { 1 }\n',
107
+ 'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
108
+ 'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
109
+ };
110
+ for (const [path, text] of Object.entries(sources)) {
111
+ const file = join(root, path);
112
+ await mkdir(dirname(file), { recursive: true });
113
+ await writeFile(file, text);
114
+ }
115
+ execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', manifest], { stdio: 'pipe' });
116
+ return { workspace, manifest, leafSource: join(workspace, 'crates/leaf/src/lib.rs') };
117
+ }
118
+
119
+ it('include-workspace hashes in-workspace transitive source edits without changing the external-only contract', async () => {
120
+ const root = await mkdtemp(join(tmpdir(), 'cargo-hash-workspace-'));
121
+ try {
122
+ const { workspace, manifest, leafSource } = await workspaceCargoFixture(root);
123
+ const external = await hashCargoPathInputs(manifest, workspace);
124
+ const included = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
125
+ expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).toBe(included);
126
+
127
+ await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
128
+ expect(await hashCargoPathInputs(manifest, workspace)).toBe(external);
129
+ const changed = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
130
+ expect(changed).not.toBe(included);
131
+ expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).toBe(changed);
132
+
133
+ await writeFile(join(root, 'external/src/lib.rs'), 'pub fn answer() -> u8 { 20 }\n');
134
+ expect(await hashCargoPathInputs(manifest, workspace)).not.toBe(external);
135
+ expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).not.toBe(changed);
136
+ } finally {
137
+ await rm(root, { recursive: true, force: true });
138
+ }
139
+ });
140
+
141
+ it('include-workspace CLI accepts an optional manifest and refuses unsupported arguments', async () => {
142
+ const root = await mkdtemp(join(tmpdir(), 'cargo-hash-cli-'));
143
+ try {
144
+ const { workspace, leafSource } = await workspaceCargoFixture(root);
145
+ const bin = join(import.meta.dir, '../dist/bin/smoo-nx-cargo-hash.js');
146
+ const run = async (args: string[]) => {
147
+ const child = Bun.spawn(['bun', bin, ...args], { cwd: workspace, stdout: 'pipe', stderr: 'pipe' });
148
+ const [exitCode, stdout, stderr] = await Promise.all([
149
+ child.exited,
150
+ new Response(child.stdout).text(),
151
+ new Response(child.stderr).text(),
152
+ ]);
153
+ return { exitCode, stdout, stderr };
154
+ };
155
+ const external = await run(['Cargo.toml']);
156
+ expect(external.exitCode, external.stderr).toBe(0);
157
+ const included = await run(['--include-workspace']);
158
+ expect(included.exitCode, included.stderr).toBe(0);
159
+ expect(included.stderr).toBe('');
160
+ expect(await run(['--include-workspace', 'Cargo.toml'])).toEqual(included);
161
+
162
+ await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
163
+ expect(await run([])).toEqual(external);
164
+ const changed = await run(['--include-workspace', 'Cargo.toml']);
165
+ expect(changed.exitCode, changed.stderr).toBe(0);
166
+ expect(changed.stderr).toBe('');
167
+ expect(changed.stdout).not.toBe(included.stdout);
168
+ expect(await run(['--include-workspace'])).toEqual(changed);
169
+
170
+ for (const args of [
171
+ ['--unknown'],
172
+ ['--include-workspace', '--unknown'],
173
+ ['Cargo.toml', '--include-workspace'],
174
+ ['--include-workspace', 'Cargo.toml', 'extra'],
175
+ ]) {
176
+ const refused = await run(args);
177
+ expect(refused.exitCode, refused.stderr).toBe(2);
178
+ expect(refused.stdout).toBe('');
179
+ }
180
+ } finally {
181
+ await rm(root, { recursive: true, force: true });
182
+ }
183
+ });
184
+
185
+ it.each(['helper', 'cli'])(
186
+ 'include-workspace keeps a custom Nx cached Cargo consumer fresh via %s',
187
+ async (mode) => {
188
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-hash-nx-')));
189
+ const repositoryRoot = join(import.meta.dir, '../../..');
190
+ const dist = join(import.meta.dir, '../dist');
191
+ try {
192
+ const { workspace, leafSource } = await workspaceCargoFixture(root);
193
+ execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
194
+ await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
195
+ await writeFile(join(workspace, '.gitignore'), 'node_modules/\ntarget/\ndist/\n.nx/\n');
196
+ await writeFile(join(workspace, 'package.json'), '{"name":"cargo-consumer-fixture","private":true}\n');
197
+ const hashProgram =
198
+ `import { hashCargoPathInputs } from ${JSON.stringify(join(dist, 'cargo-source-hash.js'))}; ` +
199
+ 'console.log(await hashCargoPathInputs("Cargo.toml", process.cwd(), { includeWorkspace: true }));';
200
+ await writeFile(
201
+ join(workspace, 'nx.json'),
202
+ JSON.stringify({
203
+ namedInputs: {
204
+ cargoSources: [
205
+ '{workspaceRoot}/Cargo.lock',
206
+ {
207
+ runtime:
208
+ mode === 'helper'
209
+ ? `bun -e '${hashProgram}'`
210
+ : `bun ${JSON.stringify(join(dist, 'bin/smoo-nx-cargo-hash.js'))} --include-workspace`,
211
+ },
212
+ ],
213
+ },
214
+ }),
215
+ );
216
+ await writeFile(
217
+ join(workspace, 'packages/app/project.json'),
218
+ JSON.stringify({
219
+ name: 'app',
220
+ targets: {
221
+ build: {
222
+ executor: 'nx:run-commands',
223
+ cache: true,
224
+ inputs: ['{projectRoot}/**/*', '!{projectRoot}/dist/**/*', 'cargoSources'],
225
+ outputs: ['{projectRoot}/dist'],
226
+ options: { command: 'bun packages/app/build.ts' },
227
+ },
228
+ },
229
+ }),
230
+ );
231
+ const executions = join(root, 'executions.log');
232
+ const outputDirectory = join(workspace, 'packages/app/dist');
233
+ const output = join(outputDirectory, 'result.txt');
234
+ await writeFile(
235
+ join(workspace, 'packages/app/build.ts'),
236
+ `import { execFileSync } from 'node:child_process';
237
+ import { appendFile, mkdir, writeFile } from 'node:fs/promises';
238
+ const result = execFileSync('cargo', ['run', '--quiet', '--locked', '--offline', '--manifest-path', 'packages/app/Cargo.toml'], { encoding: 'utf8' });
239
+ await mkdir(${JSON.stringify(outputDirectory)}, { recursive: true });
240
+ await writeFile(${JSON.stringify(output)}, result);
241
+ await appendFile(${JSON.stringify(executions)}, result);
242
+ `,
243
+ );
244
+ const run = async () => {
245
+ const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
246
+ cwd: workspace,
247
+ env: {
248
+ ...process.env,
249
+ NX_WORKSPACE_ROOT_PATH: workspace,
250
+ NX_DAEMON: 'false',
251
+ NX_ISOLATE_PLUGINS: 'false',
252
+ NX_WORKSPACE_DATA_DIRECTORY: join(workspace, '.nx/workspace-data'),
253
+ NX_CACHE_DIRECTORY: join(workspace, '.nx/cache'),
254
+ },
255
+ stdout: 'pipe',
256
+ stderr: 'pipe',
257
+ });
258
+ const [exitCode, stdout, stderr] = await Promise.all([
259
+ child.exited,
260
+ new Response(child.stdout).text(),
261
+ new Response(child.stderr).text(),
262
+ ]);
263
+ expect(exitCode, stdout + stderr).toBe(0);
264
+ };
265
+
266
+ await run();
267
+ expect(await readFile(output, 'utf8')).toBe('11\n');
268
+ expect(await readFile(executions, 'utf8')).toBe('11\n');
269
+ await rm(outputDirectory, { recursive: true });
270
+ await run();
271
+ expect(await readFile(output, 'utf8')).toBe('11\n');
272
+ expect(await readFile(executions, 'utf8')).toBe('11\n');
273
+
274
+ await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
275
+ await run();
276
+ expect(await readFile(output, 'utf8')).toBe('12\n');
277
+ expect(await readFile(executions, 'utf8')).toBe('11\n12\n');
278
+ await rm(outputDirectory, { recursive: true });
279
+ await run();
280
+ expect(await readFile(output, 'utf8')).toBe('12\n');
281
+ expect(await readFile(executions, 'utf8')).toBe('11\n12\n');
282
+ } finally {
283
+ await rm(root, { recursive: true, force: true });
284
+ }
285
+ },
286
+ 120_000,
287
+ );
@@ -34,6 +34,8 @@ 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
@@ -47,7 +49,11 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
47
49
  // the thrown error.
48
50
  const CARGO_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
49
51
 
50
- export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: string): Promise<string> {
52
+ export async function hashCargoPathInputs(
53
+ manifestPath: string,
54
+ workspaceRoot: string,
55
+ options?: { includeWorkspace?: boolean },
56
+ ): Promise<string> {
51
57
  const root = await realpath(workspaceRoot);
52
58
  const metadata = parseMetadata(
53
59
  execFileSync(
@@ -65,18 +71,20 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
65
71
  if (pkg.source !== null) continue;
66
72
  const manifest = await realpath(pkg.manifest_path);
67
73
  const directory = dirname(manifest);
68
- const fromRoot = relative(root, directory);
69
- const fromCargo = relative(cargoRoot, directory);
70
- if (
71
- fromRoot !== '..' &&
72
- !fromRoot.startsWith(`..${sep}`) &&
73
- !isAbsolute(fromRoot) &&
74
- fromCargo !== '..' &&
75
- !fromCargo.startsWith(`..${sep}`) &&
76
- !isAbsolute(fromCargo) &&
77
- !fromRoot.split(sep).includes('node_modules')
78
- )
79
- 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
+ }
80
88
  directories.add(directory);
81
89
  files.add(manifest);
82
90
  // Cargo resolves explicit package.workspace ownership as well as ancestor
@@ -88,7 +96,7 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
88
96
  ).trim();
89
97
  files.add(await realpath(governingManifest));
90
98
  for (const target of pkg.targets) files.add(await realpath(target.src_path));
91
- // An external member can inherit edition, lint policy, dependencies and
99
+ // A member can inherit edition, lint policy, dependencies and
92
100
  // profiles from a workspace above its package directory. Those manifests
93
101
  // are not descendants of the source roots returned by Cargo metadata.
94
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 () => {