@smoothbricks/nx-plugin 0.4.10 → 0.4.12

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.
@@ -0,0 +1,343 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readdir, readFile } from 'node:fs/promises';
3
+ import { join, sep } from 'node:path';
4
+ import { isNonSourceDirectory } from './source-directories.js';
5
+ /**
6
+ * A release rewrites `version` in every manifest it publishes. Those manifests
7
+ * are inputs of the validation targets, so a publish run misses the cache for
8
+ * the whole gate set over a change that alters no code.
9
+ *
10
+ * Nx hashes JSON manifests with the version removed natively, through a `json`
11
+ * input with `excludeFields` — no process, and the file hashing stays in the
12
+ * native hasher. TOML has no such input, so crate manifests are the one
13
+ * remaining case the plugin computes itself, and this module is only that
14
+ * case. It runs inside `createNodes`, which already reads these manifests, and
15
+ * the digest it returns rides into the project's named inputs as a literal —
16
+ * Nx hashes a project's `namedInputs` definitions as part of the project
17
+ * configuration, so no process and no `runtime` carrier is needed to get the
18
+ * value into the task hash.
19
+ *
20
+ * This treatment belongs to checks that READ source, never to a target that
21
+ * produces a shipped artifact. A crate embeds its version at compile time
22
+ * through `env!("CARGO_PKG_VERSION")`, so a version-insensitive build hash
23
+ * would let a post-bump build hit a pre-bump artifact and ship a binary
24
+ * reporting the previous version — silent, and worse than the cache miss it
25
+ * saves.
26
+ *
27
+ * Every fallback here over-invalidates rather than under-invalidates. A
28
+ * manifest excluded from a fileset with no digest replacing it serves stale
29
+ * results in silence, so nothing in this module drops bytes it is unsure
30
+ * about: what it cannot read line by line, it hashes verbatim.
31
+ */
32
+ const DIGEST_TAG = 'versionless-crate-manifests-v2';
33
+ const CARGO_MANIFEST = 'Cargo.toml';
34
+ /** Frames the parts of the digest, so no path can spell out another part's content. */
35
+ const SEPARATOR = Buffer.of(0);
36
+ /** The ASCII the scanner steers by. Cargo manifests are UTF-8, whose non-ASCII bytes are all >= 0x80. */
37
+ const TAB = 0x09;
38
+ const LINE_FEED = 0x0a;
39
+ const CARRIAGE_RETURN = 0x0d;
40
+ const SPACE = 0x20;
41
+ const QUOTATION_MARK = 0x22;
42
+ const NUMBER_SIGN = 0x23;
43
+ const HYPHEN = 0x2d;
44
+ const FULL_STOP = 0x2e;
45
+ const DIGIT_ZERO = 0x30;
46
+ const DIGIT_NINE = 0x39;
47
+ const EQUALS_SIGN = 0x3d;
48
+ const UPPER_A = 0x41;
49
+ const UPPER_Z = 0x5a;
50
+ const LEFT_SQUARE_BRACKET = 0x5b;
51
+ const REVERSE_SOLIDUS = 0x5c;
52
+ const RIGHT_SQUARE_BRACKET = 0x5d;
53
+ const LOW_LINE = 0x5f;
54
+ const LOWER_A = 0x61;
55
+ const LOWER_Z = 0x7a;
56
+ const LEFT_CURLY_BRACKET = 0x7b;
57
+ const RIGHT_CURLY_BRACKET = 0x7d;
58
+ const APOSTROPHE = 0x27;
59
+ const PACKAGE = 'package';
60
+ const WORKSPACE = 'workspace';
61
+ const VERSION = 'version';
62
+ /**
63
+ * Feed one crate manifest to `hash` without the versions the crate declares
64
+ * for itself.
65
+ *
66
+ * A forward scan over the bytes that hands the runs it keeps straight to the
67
+ * hasher as views: the manifest is never parsed into objects, never
68
+ * re-serialised, and never copied. `[package].version` and the
69
+ * `[workspace.package].version` that members inherit are the crate's own and
70
+ * go whole — trailing comment and newline included. A `version` under a
71
+ * dependency table, a `[target.'cfg(…)'.dependencies]` table or an inline
72
+ * table names a DIFFERENT crate and stays: dropping it would let a genuinely
73
+ * different dependency serve a stale result.
74
+ *
75
+ * Some of TOML is out of reach of a line-oriented scan. A multi-line basic or
76
+ * literal string can hold anything at all, a `[package]` header and a
77
+ * `version = "…"` line included, and no scan that has not lexed the whole
78
+ * document can tell that text from the document. So from the first `"""` or
79
+ * `'''`, from an unterminated value, and from any line it cannot classify,
80
+ * this elides nothing more and hashes the rest of the manifest verbatim. That
81
+ * direction is the safe one: hashing too much costs a cache miss on release,
82
+ * hashing too little serves a wrong result.
83
+ */
84
+ export function updateVersionlessCrateManifest(hash, manifest) {
85
+ const end = manifest.length;
86
+ // Bytes scanned but not yet handed over. Only an elision advances this past
87
+ // a line, so a manifest with no version at all is fed as one whole view.
88
+ let kept = 0;
89
+ // Whether the keys being read belong to a table whose `version` is the
90
+ // crate's own rather than some dependency's.
91
+ let ownVersionTable = false;
92
+ // Brackets left open by a value that spans lines: `keywords = [` and the
93
+ // lines under it are value, not statements.
94
+ let depth = 0;
95
+ let line = 0;
96
+ while (line < end) {
97
+ const lineFeed = manifest.indexOf(LINE_FEED, line);
98
+ const lineEnd = lineFeed < 0 ? end : lineFeed;
99
+ const nextLine = lineFeed < 0 ? end : lineFeed + 1;
100
+ if (depth > 0) {
101
+ depth = readDepth(manifest, line, lineEnd, depth);
102
+ if (depth < 0)
103
+ break;
104
+ line = nextLine;
105
+ continue;
106
+ }
107
+ const at = skipBlanks(manifest, line, lineEnd);
108
+ if (at === lineEnd || manifest[at] === NUMBER_SIGN) {
109
+ line = nextLine;
110
+ continue;
111
+ }
112
+ if (manifest[at] === LEFT_SQUARE_BRACKET) {
113
+ // Only a comment may follow a header, so the rest of the line is not
114
+ // scanned: a `"""` inside that comment is not a multi-line string.
115
+ ownVersionTable = isOwnVersionTable(manifest, at + 1, lineEnd);
116
+ line = nextLine;
117
+ continue;
118
+ }
119
+ const equals = readKeyEnd(manifest, at, lineEnd);
120
+ if (equals < 0)
121
+ break;
122
+ const own = ownVersionTable && isOwnVersionKey(manifest, at, equals);
123
+ depth = readDepth(manifest, equals + 1, lineEnd, 0);
124
+ if (depth < 0)
125
+ break;
126
+ if (own && depth === 0) {
127
+ hash.update(manifest.subarray(kept, line));
128
+ kept = nextLine;
129
+ }
130
+ line = nextLine;
131
+ }
132
+ hash.update(manifest.subarray(kept, end));
133
+ }
134
+ /**
135
+ * The digest of every crate manifest under a project, with the versions the
136
+ * crates declare for themselves removed.
137
+ *
138
+ * This is exactly what the validation targets exclude from their filesets.
139
+ * Paths are part of the digest, so moving a crate is a change even when no
140
+ * manifest body differs. A project with no crate manifest never declares this
141
+ * input at all, so the empty digest is not a case that reaches Nx.
142
+ *
143
+ * A crate that pins a workspace sibling by `path` + `version` still carries
144
+ * that sibling's version, so a release does invalidate it. That is the honest
145
+ * answer: the dependency the digest describes really did change.
146
+ */
147
+ export async function hashVersionlessCrateManifests(projectRoot, workspaceRoot) {
148
+ const root = join(workspaceRoot, projectRoot);
149
+ const paths = [];
150
+ await collectCrateManifests(root, '', paths);
151
+ paths.sort();
152
+ const manifests = await Promise.all(paths.map((path) => readFile(join(root, path))));
153
+ const hash = createHash('sha256');
154
+ hash.update(DIGEST_TAG);
155
+ hash.update(SEPARATOR);
156
+ for (let index = 0; index < paths.length; index++) {
157
+ hash.update(paths[index]);
158
+ hash.update(SEPARATOR);
159
+ updateVersionlessCrateManifest(hash, manifests[index]);
160
+ hash.update(SEPARATOR);
161
+ }
162
+ return hash.digest('hex');
163
+ }
164
+ /** Collects manifest paths relative to the project root, `/`-separated so the digest is platform-independent. */
165
+ async function collectCrateManifests(directory, prefix, found) {
166
+ // No `catch`: an unreadable directory would otherwise hash as an empty
167
+ // manifest set, which is the silent-staleness failure this module exists to
168
+ // avoid. The caller degrades to the raw manifests instead. Symlinked
169
+ // directories are not entries `isDirectory` reports, so the walk cannot loop
170
+ // and matches what Nx keeps in its file map.
171
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
172
+ if (entry.isDirectory()) {
173
+ // The exclusions every source walk in this plugin uses, so the digest
174
+ // covers the same files Nx keeps in its file map.
175
+ if (!isNonSourceDirectory(entry.name)) {
176
+ await collectCrateManifests(`${directory}${sep}${entry.name}`, `${prefix}${entry.name}/`, found);
177
+ }
178
+ }
179
+ else if (entry.name === CARGO_MANIFEST) {
180
+ found.push(`${prefix}${entry.name}`);
181
+ }
182
+ }
183
+ }
184
+ /** `from` is just past the `[`. */
185
+ function isOwnVersionTable(manifest, from, to) {
186
+ // `[[bin]]` and every other array of tables: a crate's own version is never
187
+ // declared in one.
188
+ if (manifest[from] === LEFT_SQUARE_BRACKET)
189
+ return false;
190
+ let after = readWord(manifest, from, to, PACKAGE);
191
+ if (after < 0) {
192
+ const afterWorkspace = readWord(manifest, from, to, WORKSPACE);
193
+ if (afterWorkspace < 0)
194
+ return false;
195
+ const dot = skipBlanks(manifest, afterWorkspace, to);
196
+ if (dot >= to || manifest[dot] !== FULL_STOP)
197
+ return false;
198
+ after = readWord(manifest, dot + 1, to, PACKAGE);
199
+ if (after < 0)
200
+ return false;
201
+ }
202
+ // The path has to END here: `[package.metadata]` is a different table, and
203
+ // the version under it is not the crate's.
204
+ const close = skipBlanks(manifest, after, to);
205
+ return close < to && manifest[close] === RIGHT_SQUARE_BRACKET;
206
+ }
207
+ /** `to` is the offset of the `=`, so the key path must end exactly there. */
208
+ function isOwnVersionKey(manifest, from, to) {
209
+ const afterVersion = readWord(manifest, from, to, VERSION);
210
+ if (afterVersion < 0)
211
+ return false;
212
+ const dot = skipBlanks(manifest, afterVersion, to);
213
+ if (dot === to)
214
+ return true;
215
+ if (manifest[dot] !== FULL_STOP)
216
+ return false;
217
+ // `version.workspace = true` inherits the version, so it is the same field.
218
+ const afterWorkspace = readWord(manifest, dot + 1, to, WORKSPACE);
219
+ return afterWorkspace >= 0 && skipBlanks(manifest, afterWorkspace, to) === to;
220
+ }
221
+ /**
222
+ * Offset just past `word` written bare, `"quoted"` or `'quoted'`, or -1.
223
+ *
224
+ * A quoted key that spells the word with an escape (`"pack\u0061ge"`) does not
225
+ * match: decoding it would mean carrying a decoder, and not matching only
226
+ * keeps bytes the digest could have dropped.
227
+ */
228
+ function readWord(manifest, from, to, word) {
229
+ const at = skipBlanks(manifest, from, to);
230
+ const quote = manifest[at];
231
+ const quoted = quote === QUOTATION_MARK || quote === APOSTROPHE;
232
+ const start = quoted ? at + 1 : at;
233
+ const stop = start + word.length;
234
+ if (stop > to)
235
+ return -1;
236
+ for (let index = 0; index < word.length; index++) {
237
+ if (manifest[start + index] !== word.charCodeAt(index))
238
+ return -1;
239
+ }
240
+ if (quoted)
241
+ return manifest[stop] === quote ? stop + 1 : -1;
242
+ // A bare key runs on: `versioning` is not `version`.
243
+ return stop === to || !isBareKeyByte(manifest[stop]) ? stop : -1;
244
+ }
245
+ /** Offset of the `=` that closes this line's key path, or -1 when the line is not one. */
246
+ function readKeyEnd(manifest, from, to) {
247
+ let at = from;
248
+ for (;;) {
249
+ at = readKeySegment(manifest, at, to);
250
+ if (at < 0)
251
+ return -1;
252
+ at = skipBlanks(manifest, at, to);
253
+ if (at >= to)
254
+ return -1;
255
+ const byte = manifest[at];
256
+ if (byte === EQUALS_SIGN)
257
+ return at;
258
+ if (byte !== FULL_STOP)
259
+ return -1;
260
+ at++;
261
+ }
262
+ }
263
+ /** Offset just past one key segment — bare, `"quoted"` or `'quoted'` — or -1. */
264
+ function readKeySegment(manifest, from, to) {
265
+ const at = skipBlanks(manifest, from, to);
266
+ if (at >= to)
267
+ return -1;
268
+ const byte = manifest[at];
269
+ if (byte === QUOTATION_MARK || byte === APOSTROPHE)
270
+ return readStringEnd(manifest, at, to);
271
+ let stop = at;
272
+ while (stop < to && isBareKeyByte(manifest[stop]))
273
+ stop++;
274
+ return stop === at ? -1 : stop;
275
+ }
276
+ /**
277
+ * Bracket depth still open at the end of a value or of the line continuing
278
+ * one, or -1 when the scan must stop: a multi-line string opens, a string
279
+ * never closes, or a bracket closes one that never opened.
280
+ */
281
+ function readDepth(manifest, from, to, depth) {
282
+ let open = depth;
283
+ let at = from;
284
+ while (at < to) {
285
+ const byte = manifest[at];
286
+ // A comment runs to the end of the line and cannot open anything.
287
+ if (byte === NUMBER_SIGN)
288
+ return open;
289
+ if (byte === QUOTATION_MARK || byte === APOSTROPHE) {
290
+ if (manifest[at + 1] === byte && manifest[at + 2] === byte)
291
+ return -1;
292
+ at = readStringEnd(manifest, at, to);
293
+ if (at < 0)
294
+ return -1;
295
+ continue;
296
+ }
297
+ if (byte === LEFT_SQUARE_BRACKET || byte === LEFT_CURLY_BRACKET) {
298
+ open++;
299
+ }
300
+ else if (byte === RIGHT_SQUARE_BRACKET || byte === RIGHT_CURLY_BRACKET) {
301
+ open--;
302
+ if (open < 0)
303
+ return -1;
304
+ }
305
+ at++;
306
+ }
307
+ return open;
308
+ }
309
+ /** `from` is the opening quote. Offset just past the closing one, or -1 when the line ends first. */
310
+ function readStringEnd(manifest, from, to) {
311
+ const quote = manifest[from];
312
+ let at = from + 1;
313
+ while (at < to) {
314
+ const byte = manifest[at];
315
+ // A literal string has no escapes: `'a\'` closes at that quote.
316
+ if (byte === REVERSE_SOLIDUS && quote === QUOTATION_MARK) {
317
+ at += 2;
318
+ continue;
319
+ }
320
+ if (byte === quote)
321
+ return at + 1;
322
+ at++;
323
+ }
324
+ return -1;
325
+ }
326
+ /** Whitespace between tokens. A `\r` only ever precedes the `\n` a line was split on. */
327
+ function skipBlanks(manifest, from, to) {
328
+ let at = from;
329
+ while (at < to) {
330
+ const byte = manifest[at];
331
+ if (byte !== SPACE && byte !== TAB && byte !== CARRIAGE_RETURN)
332
+ break;
333
+ at++;
334
+ }
335
+ return at;
336
+ }
337
+ function isBareKeyByte(byte) {
338
+ return ((byte >= LOWER_A && byte <= LOWER_Z) ||
339
+ (byte >= UPPER_A && byte <= UPPER_Z) ||
340
+ (byte >= DIGIT_ZERO && byte <= DIGIT_NINE) ||
341
+ byte === LOW_LINE ||
342
+ byte === HYPHEN);
343
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"typecheck-test-policy.d.ts","sourceRoot":"","sources":["../src/typecheck-test-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,QAAQ,CAAC;AAI1C;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,cAAc,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,IAAI,EAAE,IAAI,GAAG,OAAO,CAyDhE;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,EAAE,CAiEtE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAmE9D"}
@@ -384,6 +384,14 @@ export function applyTypecheckTestPolicy(root) {
384
384
  referencePaths,
385
385
  }) || fileChanged;
386
386
  if (fileChanged) {
387
+ // A documented tsconfig cannot survive JSON.stringify, so the policy says
388
+ // what it wants instead of quietly deleting the reasoning. The check
389
+ // half (checkTypecheckTestPolicy) reports the same drift, so CI still
390
+ // fails until a human applies it.
391
+ if (existsSync(tsconfigTestPath) && hasJsonComments(readFileSync(tsconfigTestPath, 'utf8'))) {
392
+ console.error(`refused ${packagePath}/tsconfig.test.json carries comments a rewrite would delete; apply the policy by hand`);
393
+ continue;
394
+ }
387
395
  writeJsonObjectFs(tsconfigTestPath, tsconfigTest);
388
396
  changed = true;
389
397
  }
@@ -583,17 +591,83 @@ function collectTsconfigTestReferencePaths(root, packagePath, pkg, workspaceName
583
591
  }
584
592
  return paths;
585
593
  }
594
+ /**
595
+ * Read a tsconfig or package.json. tsconfig files are JSONC — TypeScript
596
+ * permits comments and trailing commas — so a plain `JSON.parse` fails on a
597
+ * documented one. Returning null there meant "file absent", and the caller
598
+ * regenerated it from scratch: a `lib` the test program declared, an
599
+ * `exclude`, an extra `include` glob and every comment explaining them
600
+ * disappeared on the next `smoo monorepo update`. Comments are stripped for
601
+ * parsing only; a caller that intends to write back must decide what to do
602
+ * about them (see writeJsonObjectFs's caller).
603
+ */
586
604
  function readJsonObject(path) {
587
605
  if (!existsSync(path)) {
588
606
  return null;
589
607
  }
608
+ const text = readFileSync(path, 'utf8');
590
609
  try {
591
- const parsed = JSON.parse(readFileSync(path, 'utf8'));
610
+ const parsed = JSON.parse(stripJsonComments(text));
592
611
  return isRecord(parsed) ? parsed : null;
593
612
  }
594
- catch {
595
- return null;
613
+ catch (error) {
614
+ // A file that exists but cannot be parsed is a configuration error, not an
615
+ // invitation to overwrite it.
616
+ throw new Error(`${path} is not valid JSON/JSONC: ${error instanceof Error ? error.message : String(error)}`);
617
+ }
618
+ }
619
+ /** Whether the file carries comments a rewrite would destroy. */
620
+ function hasJsonComments(text) {
621
+ return stripJsonComments(text) !== text;
622
+ }
623
+ /**
624
+ * Remove `//` and block comments outside string literals, and trailing commas.
625
+ * Small on purpose: the alternative is a JSONC dependency in a plugin whose
626
+ * only JSONC inputs are tsconfig files.
627
+ */
628
+ function stripJsonComments(text) {
629
+ let output = '';
630
+ let inString = false;
631
+ let escaped = false;
632
+ let index = 0;
633
+ while (index < text.length) {
634
+ const char = text[index];
635
+ if (inString) {
636
+ output += char;
637
+ if (escaped) {
638
+ escaped = false;
639
+ }
640
+ else if (char === '\\') {
641
+ escaped = true;
642
+ }
643
+ else if (char === '"') {
644
+ inString = false;
645
+ }
646
+ index += 1;
647
+ continue;
648
+ }
649
+ if (char === '"') {
650
+ inString = true;
651
+ output += char;
652
+ index += 1;
653
+ continue;
654
+ }
655
+ if (char === '/' && text[index + 1] === '/') {
656
+ while (index < text.length && text[index] !== '\n')
657
+ index += 1;
658
+ continue;
659
+ }
660
+ if (char === '/' && text[index + 1] === '*') {
661
+ index += 2;
662
+ while (index < text.length && !(text[index] === '*' && text[index + 1] === '/'))
663
+ index += 1;
664
+ index += 2;
665
+ continue;
666
+ }
667
+ output += char;
668
+ index += 1;
596
669
  }
670
+ return output.replace(/,(\s*[}\]])/g, '$1');
597
671
  }
598
672
  function writeJsonObjectFs(path, value) {
599
673
  writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -108,7 +108,7 @@
108
108
  "typia": "14.0.2"
109
109
  },
110
110
  "devDependencies": {
111
- "@smoothbricks/lmao": "0.3.11",
111
+ "@smoothbricks/lmao": "0.3.12",
112
112
  "@smoothbricks/validation": "0.1.8"
113
113
  }
114
114
  }
@@ -629,6 +629,89 @@ export const CARGO_TEST_ARCHIVE_TARGET = 'cargo-test-archive';
629
629
  */
630
630
  export const CARGO_TEST_ARCHIVE_FILE = 'target/nextest/archive.tar.zst';
631
631
 
632
+ /**
633
+ * One foreign target triple this cargo workspace also archives its tests for.
634
+ */
635
+ export interface CargoCrossTestTarget {
636
+ target: string;
637
+ /**
638
+ * The repository's own cargo driver for this triple, relative to the cargo
639
+ * workspace root — a program that accepts cargo's argv and links for the
640
+ * target. Exported to the archive command as `CARGO`, which is cargo's own
641
+ * driver variable and the only seam nextest offers: nextest shells out to
642
+ * `$CARGO metadata` and `$CARGO test --no-run --target <triple>` (measured),
643
+ * so a repository whose cross link needs zig, an SDK or a sysroot puts that
644
+ * knowledge in one script instead of in the generated command.
645
+ *
646
+ * Absent means the ambient toolchain already links the triple.
647
+ */
648
+ cargo?: string;
649
+ }
650
+
651
+ /**
652
+ * Foreign target triples this cargo workspace archives tests for, from
653
+ * `[workspace.metadata.smoothbricks.test] cross-targets`. An entry is either
654
+ * the triple as a string, or a table adding that triple's cargo driver:
655
+ *
656
+ * ```toml
657
+ * [workspace.metadata.smoothbricks.test]
658
+ * cross-targets = [{ target = "aarch64-apple-darwin", cargo = "scripts/cargo-for-nextest.sh" }]
659
+ * ```
660
+ *
661
+ * The DECLARATION lives in Cargo.toml rather than in a CI config because a
662
+ * Rust target triple is a cargo fact: the same list decides which archives the
663
+ * graph can produce whether or not anything is running CI, and it is read from
664
+ * the same manifest `shards` comes out of, beside it in the same
665
+ * `smoothbricks.test` table, with the parser this module already uses.
666
+ */
667
+ export function listCargoCrossTestTargets(absoluteProjectRoot: string): CargoCrossTestTarget[] {
668
+ const workspaceTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
669
+ if (!existsSync(workspaceTomlPath)) {
670
+ return [];
671
+ }
672
+ const parsed: unknown = parseToml(readFileSync(workspaceTomlPath, 'utf-8'));
673
+ if (!isRecord(parsed) || !isRecord(parsed.workspace)) {
674
+ return [];
675
+ }
676
+ const metadata = isRecord(parsed.workspace.metadata) ? parsed.workspace.metadata : null;
677
+ const smoothbricks = metadata && isRecord(metadata.smoothbricks) ? metadata.smoothbricks : null;
678
+ const test = smoothbricks && isRecord(smoothbricks.test) ? smoothbricks.test : null;
679
+ const declared = test?.['cross-targets'];
680
+ if (declared === undefined) {
681
+ return [];
682
+ }
683
+ if (!Array.isArray(declared)) {
684
+ throw new Error(`${workspaceTomlPath}: smoothbricks.test.cross-targets must be an array of target triples`);
685
+ }
686
+ const crossTargets: CargoCrossTestTarget[] = [];
687
+ for (const entry of declared) {
688
+ const target = typeof entry === 'string' ? entry : isRecord(entry) ? entry.target : undefined;
689
+ // A triple becomes an Nx target NAME and an archive FILE name, so anything
690
+ // a shell or Nx would have to quote is refused where it is declared rather
691
+ // than emitted into a command.
692
+ if (typeof target !== 'string' || !/^[a-z0-9][a-z0-9._-]*$/.test(target)) {
693
+ throw new Error(
694
+ `${workspaceTomlPath}: smoothbricks.test.cross-targets entries must be target triples like ` +
695
+ `"aarch64-apple-darwin", got ${JSON.stringify(entry)}`,
696
+ );
697
+ }
698
+ const cargo = isRecord(entry) ? entry.cargo : undefined;
699
+ // The driver is an input of every archive built through it, so it has to be
700
+ // a path inside this workspace rather than a command line: `{projectRoot}/`
701
+ // plus this string is what makes editing it invalidate the archive.
702
+ if (cargo !== undefined && (typeof cargo !== 'string' || cargo.length === 0 || !/^[\w./-]+$/.test(cargo))) {
703
+ throw new Error(
704
+ `${workspaceTomlPath}: smoothbricks.test.cross-targets ${target} cargo must be a path inside the ` +
705
+ `cargo workspace, got ${JSON.stringify(cargo)}`,
706
+ );
707
+ }
708
+ if (!crossTargets.some((existing) => existing.target === target)) {
709
+ crossTargets.push(cargo === undefined ? { target } : { target, cargo });
710
+ }
711
+ }
712
+ return crossTargets;
713
+ }
714
+
632
715
  /**
633
716
  * Tests that nextest.toml singles out are pinned to this suffix instead of
634
717
  * being sharded. Only a sharded crate has one: an unsharded crate runs its
@@ -126,7 +126,10 @@ describe('Linux cross-check policy', () => {
126
126
  // ...and runs Nx rather than cargo directly, so caching and project
127
127
  // discovery stay Nx's job.
128
128
  expect(CROSS_CHECK_SCRIPT_COMMAND).toContain(`nx run-many -t ${CARGO_CROSS_LINT_TARGET}`);
129
- expect(CROSS_CHECK_SCRIPT_COMMAND).toContain('--quiet');
129
+ // The probe-only pre-push gate never runs this, so a human does — and
130
+ // devenv's own progress has to reach them, or a 0.4 GiB closure and an
131
+ // unbounded clippy are indistinguishable from a hang.
132
+ expect(CROSS_CHECK_SCRIPT_COMMAND).not.toContain('--quiet');
130
133
  });
131
134
 
132
135
  it('uses the verb:qualifier root script convention', () => {
@@ -131,7 +131,70 @@ export const CROSS_CHECK_SCRIPT_NAME = 'check:linux';
131
131
  /**
132
132
  * The developer entry point: one command on macOS that reports what CI's Linux
133
133
  * job would. `tooling/devenv` is the managed wrapper, so this resolves the
134
- * repository's devenv config from any repo in the fleet without naming its path,
135
- * and `--quiet` keeps the output to Nx's own.
134
+ * repository's devenv config from any repo in the fleet without naming its path.
135
+ *
136
+ * Deliberately NOT `--quiet`. The pre-push gate is a probe: it consults the Nx
137
+ * cache and refuses a push that has no hit, never entering a profile or starting
138
+ * a compile of its own. That makes this script the only place the gate ever
139
+ * runs, and a human watching it needs devenv's progress too — the cross profile
140
+ * is a 0.4 GiB closure and the clippy behind it is unbounded, so a silent
141
+ * terminal is indistinguishable from a hang. Suppressing that output only paid
142
+ * while a hook ran this for you and nobody read it.
136
143
  */
137
- export const CROSS_CHECK_SCRIPT_COMMAND = `tooling/devenv --quiet -P ${DEVENV_CROSS_PROFILE} shell -- nx run-many -t ${CARGO_CROSS_LINT_TARGET}`;
144
+ export const CROSS_CHECK_SCRIPT_COMMAND = `tooling/devenv -P ${DEVENV_CROSS_PROFILE} shell -- nx run-many -t ${CARGO_CROSS_LINT_TARGET}`;
145
+
146
+ /**
147
+ * A cross-built test archive and the target that EXECUTES it, per foreign
148
+ * target triple the cargo workspace declares in
149
+ * `[workspace.metadata.smoothbricks.test] cross-targets`.
150
+ *
151
+ * TWO names because the halves run on two machines. `cargo-lint-cross` above
152
+ * type-checks foreign code and states outright that a cross TEST target could
153
+ * only build a harness it must then refuse to launch — that is still true of
154
+ * one machine. It stops being true across two: the triple's archive is built
155
+ * where the cross toolchain is, shipped, and executed where those binaries are
156
+ * native, which is the only arrangement in which a cross compile is proved by
157
+ * running rather than by linking.
158
+ *
159
+ * `cargo-cross-test-` and not `cargo-test-`: `packageNameFromCargoTestTarget`
160
+ * reads everything under that prefix as a crate, so a triple there would invent
161
+ * a workspace member named `aarch64-apple-darwin` and make the per-crate
162
+ * coverage policy unsatisfiable. The `-cross` qualifier is the same neutral one
163
+ * `CARGO_CROSS_LINT_TARGET` carries, and for the same reason: it stays outside
164
+ * every platform-suffix and build-output family, so no aggregate sweeps a
165
+ * foreign archive into `build`.
166
+ *
167
+ * These names live here rather than beside the Cargo.toml reader because the
168
+ * workflow generator renders jobs from them: the CLI already imports this
169
+ * module, and a second spelling of the convention on that side would drift
170
+ * silently the first time either half changed.
171
+ */
172
+ export function cargoCrossTestArchiveTargetName(triple: string): string {
173
+ return `cargo-cross-test-archive-${triple}`;
174
+ }
175
+
176
+ /** The bounded runner that executes one triple's archive. */
177
+ export function cargoCrossTestTargetName(triple: string): string {
178
+ return `cargo-cross-test-${triple}`;
179
+ }
180
+
181
+ /**
182
+ * One archive file per triple, beside the host-native
183
+ * `CARGO_TEST_ARCHIVE_FILE`, relative to the cargo workspace root. Distinct
184
+ * paths are what let both exist: each is a declared Nx output, so a single path
185
+ * for two triples would let a restored cache serve one machine's binaries under
186
+ * the other's name.
187
+ */
188
+ export function cargoCrossTestArchiveFile(triple: string): string {
189
+ return `target/nextest/archive-${triple}.tar.zst`;
190
+ }
191
+
192
+ const CARGO_CROSS_TEST_ARCHIVE_PREFIX = cargoCrossTestArchiveTargetName('');
193
+
194
+ /** Inverse of `cargoCrossTestArchiveTargetName`; null for every other target. */
195
+ export function cargoCrossTestTripleFromArchiveTarget(targetName: string): string | null {
196
+ return targetName.startsWith(CARGO_CROSS_TEST_ARCHIVE_PREFIX) &&
197
+ targetName.length > CARGO_CROSS_TEST_ARCHIVE_PREFIX.length
198
+ ? targetName.slice(CARGO_CROSS_TEST_ARCHIVE_PREFIX.length)
199
+ : null;
200
+ }