@smoothbricks/nx-plugin 0.5.1 → 0.5.3

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 (60) hide show
  1. package/README.md +74 -13
  2. package/dist/bin/smoo-nx-test-clocks.js +2 -2
  3. package/dist/bin/smoo-nx-test-walks.js +6 -9
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +25 -15
  6. package/dist/cargo-source-hash.d.ts +2 -2
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +106 -46
  9. package/dist/ensure-built-witness.d.ts +33 -5
  10. package/dist/ensure-built-witness.d.ts.map +1 -1
  11. package/dist/ensure-built-witness.js +84 -18
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +19 -5
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +4 -0
  16. package/dist/owned-installation.d.ts +6 -5
  17. package/dist/owned-installation.d.ts.map +1 -1
  18. package/dist/owned-installation.js +324 -201
  19. package/dist/test-walk-policy.d.ts +8 -0
  20. package/dist/test-walk-policy.d.ts.map +1 -1
  21. package/dist/test-walk-policy.js +34 -0
  22. package/dist/workspace-manifest.d.ts +5 -5
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/patches/nx@23.2.1.patch +196 -48
  25. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  26. package/managed/raw/tooling/direnv/linked-snapshots.ts +31 -4
  27. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  28. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  29. package/package.json +3 -3
  30. package/runtime-input.sh +21 -9
  31. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  32. package/src/__tests__/owned-installation.ts +5 -6
  33. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  34. package/src/bin/smoo-nx-test-walks.ts +6 -9
  35. package/src/cargo-closure-filesets.test.ts +63 -0
  36. package/src/cargo-closure-input.test.ts +1 -1
  37. package/src/cargo-closure-input.ts +23 -15
  38. package/src/cargo-closure-key.test.ts +1 -1
  39. package/src/cargo-resolve-cache.test.ts +1 -1
  40. package/src/cargo-source-hash.test.ts +143 -2
  41. package/src/cargo-source-hash.ts +108 -44
  42. package/src/cross-check-policy.test.ts +53 -12
  43. package/src/ensure-built-witness.test.ts +53 -2
  44. package/src/ensure-built-witness.ts +103 -18
  45. package/src/ensure-built.test.ts +40 -5
  46. package/src/ensure-built.ts +28 -6
  47. package/src/index.ts +4 -0
  48. package/src/linked-snapshots.nx.test.ts +1 -1
  49. package/src/nextest-partitions.test.ts +1 -1
  50. package/src/nx-daemon-start-never.test.ts +126 -0
  51. package/src/nx-daemon-validity.test.ts +51 -0
  52. package/src/nx-task-output-lease.test.ts +136 -0
  53. package/src/owned-installation.test.ts +105 -0
  54. package/src/owned-installation.ts +335 -93
  55. package/src/runtime-input.test.ts +22 -1
  56. package/src/task-results.test.ts +1 -1
  57. package/src/test-walk-policy.test.ts +35 -0
  58. package/src/test-walk-policy.ts +32 -0
  59. package/src/workspace-manifest.ts +5 -5
  60. package/test-timings.json +95 -92
@@ -1,11 +1,25 @@
1
1
  import { expect, it } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
- import { mkdir, mkdtemp, readFile, realpath, rename, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
3
+ import { createHash } from 'node:crypto';
4
+ import { statSync } from 'node:fs';
5
+ import {
6
+ chmod,
7
+ mkdir,
8
+ mkdtemp,
9
+ readFile,
10
+ realpath,
11
+ rename,
12
+ rm,
13
+ stat,
14
+ symlink,
15
+ utimes,
16
+ writeFile,
17
+ } from 'node:fs/promises';
4
18
  import { homedir, tmpdir } from 'node:os';
5
19
  import { dirname, join, relative } from 'node:path';
6
20
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
7
21
  import { runtimeInputValue } from './__tests__/runtime-input.js';
8
- import { hashCargoPathInputs, readCargoResolve } from './cargo-source-hash.js';
22
+ import { CARGO_ANCESTOR_INPUTS, compiledInFiles, hashCargoPathInputs, readCargoResolve } from './cargo-source-hash.js';
9
23
 
10
24
  useFixtureCargoHome();
11
25
 
@@ -564,3 +578,130 @@ it.each(['helper', 'cli'])(
564
578
  }
565
579
  },
566
580
  );
581
+
582
+ it('keeps the v2 transcript across forward source includes and an ignored source included through an alias', async () => {
583
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-hash-fused-')));
584
+ const manifest = join(root, 'Cargo.toml');
585
+ const contents = new Map([
586
+ ['Cargo.toml', '[workspace]\nmembers=["a","z"]\nresolver="2"\n'],
587
+ ['a/Cargo.toml', '[package]\nname="a"\nversion="0.1.0"\nedition="2021"\n'],
588
+ ['z/Cargo.toml', '[package]\nname="z"\nversion="0.1.0"\nedition="2021"\n'],
589
+ ['a/src/lib.rs', 'const Z: &str = include_str!("../../z/src/lib.rs");\n'],
590
+ ['z/src/lib.rs', 'const H: &str = include_str!("../hidden.txt");\nconst P: &str = include_str!("payload.bin");\n'],
591
+ ['z/src/hidden.rs', 'const S: &str = include_str!("secret.bin");\n'],
592
+ ['z/src/payload.bin', 'payload'],
593
+ ['z/src/secret.bin', 'secret'],
594
+ ['.gitignore', 'z/src/hidden.rs\n'],
595
+ ]);
596
+ try {
597
+ for (const [path, text] of contents) {
598
+ await mkdir(dirname(join(root, path)), { recursive: true });
599
+ await writeFile(join(root, path), text);
600
+ }
601
+ await symlink('src/hidden.rs', join(root, 'z/hidden.txt'));
602
+ execFileSync('git', ['init', '--quiet', root], { stdio: 'pipe' });
603
+ execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', manifest], { stdio: 'pipe' });
604
+ // Independent explicit v2 transcript. In particular, z's source is named by a,
605
+ // while hidden.rs is discovery-only until the unignored alias names its bytes.
606
+ const inputs: { file: string; key: string }[] = [
607
+ { file: manifest, key: JSON.stringify(['a', '0.1.0', 'ancestor', '1/Cargo.toml']) },
608
+ { file: join(root, 'a/Cargo.toml'), key: JSON.stringify(['a', '0.1.0', 'file', 'Cargo.toml']) },
609
+ { file: join(root, 'a/src/lib.rs'), key: JSON.stringify(['a', '0.1.0', 'file', 'src/lib.rs']) },
610
+ {
611
+ file: join(root, 'z/src/lib.rs'),
612
+ key: JSON.stringify(['a', '0.1.0', 'include', 'src/lib.rs\0../../z/src/lib.rs']),
613
+ },
614
+ { file: join(root, 'z/Cargo.toml'), key: JSON.stringify(['z', '0.1.0', 'file', 'Cargo.toml']) },
615
+ {
616
+ file: join(root, 'z/src/secret.bin'),
617
+ key: JSON.stringify(['z', '0.1.0', 'include', 'src/hidden.rs\0secret.bin']),
618
+ },
619
+ {
620
+ file: join(root, 'z/src/hidden.rs'),
621
+ key: JSON.stringify(['z', '0.1.0', 'include', 'src/lib.rs\0../hidden.txt']),
622
+ },
623
+ {
624
+ file: join(root, 'z/src/payload.bin'),
625
+ key: JSON.stringify(['z', '0.1.0', 'include', 'src/lib.rs\0payload.bin']),
626
+ },
627
+ ];
628
+ for (let ancestor = dirname(root), depth = 2; ; ancestor = dirname(ancestor), depth += 1) {
629
+ for (const name of CARGO_ANCESTOR_INPUTS) {
630
+ const file = join(ancestor, name);
631
+ if (statSync(file, { throwIfNoEntry: false })?.isFile()) {
632
+ inputs.push({ file, key: JSON.stringify(['a', '0.1.0', 'ancestor', depth + '/' + name]) });
633
+ }
634
+ }
635
+ if (dirname(ancestor) === ancestor) break;
636
+ }
637
+ const expected = createHash('sha256');
638
+ expected.update('cargo-path-inputs-v2\0');
639
+ expected.update(JSON.stringify(['z', '0.1.0', 'link', 'hidden.txt']));
640
+ expected.update('\0symlink\0src/hidden.rs\0');
641
+ for (const { file, key } of inputs.sort((left, right) =>
642
+ left.key < right.key ? -1 : left.key > right.key ? 1 : 0,
643
+ )) {
644
+ const bytes = await readFile(file);
645
+ expected.update(key);
646
+ expected.update('\0' + bytes.length + '\0');
647
+ expected.update(bytes);
648
+ }
649
+ const before = await hashCargoPathInputs(manifest, root, { includeWorkspace: true });
650
+ expect(before).toBe(expected.digest('hex'));
651
+ await writeFile(join(root, 'z/src/secret.bin'), 'changed');
652
+ expect(await hashCargoPathInputs(manifest, root, { includeWorkspace: true })).not.toBe(before);
653
+ } finally {
654
+ await rm(root, { recursive: true, force: true });
655
+ }
656
+ });
657
+
658
+ it('keeps literal discovery exact and refuses unreadable sources', async () => {
659
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-hash-literals-')));
660
+ const source = join(root, 'src/lib.rs');
661
+ const unicode = 'spelling-e\u0301-\u{10000}-payload';
662
+ try {
663
+ await mkdir(dirname(source), { recursive: true });
664
+ await writeFile(
665
+ join(root, 'Cargo.toml'),
666
+ '[package]\nname="literals"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
667
+ );
668
+ await writeFile(join(root, 'src/data'), 'data');
669
+ await writeFile(join(root, 'src', unicode), 'unicode spelling');
670
+ await writeFile(
671
+ source,
672
+ [
673
+ 'const A: &str = include_str!(r#"data"#,);',
674
+ 'const B: &[u8] = include_bytes!["data"];',
675
+ 'const C: &str = include_str!{"data"};',
676
+ `const U: &str = include_str!(${JSON.stringify(unicode)});`,
677
+ '// include_str!("missing")',
678
+ '/* outer /* include_bytes!("missing") */ comment */',
679
+ 'const D: &str = r###"include_str!("missing")"###;',
680
+ 'include!("missing");',
681
+ 'include_str!("");',
682
+ 'include_str!("da\\\\ta");',
683
+ 'include_str!(concat!("da", "ta"));',
684
+ 'include_str!("missing");',
685
+ 'include_str!("data"];',
686
+ 'include_str!("unterminated',
687
+ ].join('\n'),
688
+ );
689
+ expect(compiledInFiles(root, new Map())).toEqual([
690
+ { file: join(root, 'src/data'), source, literal: 'data' },
691
+ { file: join(root, 'src', unicode), source, literal: unicode },
692
+ ]);
693
+ execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', join(root, 'Cargo.toml')], {
694
+ stdio: 'pipe',
695
+ });
696
+ await chmod(source, 0);
697
+ expect(() => compiledInFiles(root, new Map())).toThrow();
698
+ // A cold native Cargo census can report the denial before the source read;
699
+ // both paths must preserve the operational refusal rather than skip the file.
700
+ await expect(hashCargoPathInputs(join(root, 'Cargo.toml'), root, { includeWorkspace: true })).rejects.toThrow(
701
+ 'EACCES',
702
+ );
703
+ } finally {
704
+ await chmod(source, 0o600);
705
+ await rm(root, { recursive: true, force: true });
706
+ }
707
+ });
@@ -812,15 +812,26 @@ export async function hashCargoPathInputs(
812
812
  const inputKey = (pkg: LocalCargoPackage, role: InputRole, path: string) =>
813
813
  JSON.stringify([pkg.name, pkg.version, role, path.split(sep).join('/')]);
814
814
  const directories = new Map<string, LocalCargoPackage>();
815
- const files = new Map<string, string>();
815
+ interface FileInput {
816
+ readonly file: string;
817
+ readonly key: string;
818
+ readonly order: number;
819
+ }
820
+ const files = new Map<string, FileInput>();
821
+ const compareInputs = (left: FileInput, right: FileInput) =>
822
+ left.key < right.key ? -1 : left.key > right.key ? 1 : left.order - right.order;
816
823
  const ancestors = new Set<string>();
817
824
  const links = new Map<string, { key: string; target: string }>();
818
825
  const packageSources: { file: string; key: string }[] = [];
819
826
  // Read shared bytes once. The smallest declared identity makes multiple owners
820
827
  // deterministic without using a consuming checkout's mount path as an input.
821
- const includeFile = (file: string, key: string) => {
828
+ const includeFile = (file: string, key: string): FileInput | undefined => {
822
829
  const prior = files.get(file);
823
- if (prior === undefined || key < prior) files.set(file, key);
830
+ if (prior !== undefined && key >= prior.key) return undefined;
831
+ // Rekeying must retain the old Map insertion order for equal v2 identities.
832
+ const input = { file, key, order: prior?.order ?? files.size };
833
+ files.set(file, input);
834
+ return input;
824
835
  };
825
836
  const packages = [...cargo.local].sort((left, right) =>
826
837
  left.name < right.name
@@ -899,17 +910,24 @@ export async function hashCargoPathInputs(
899
910
  }
900
911
  }
901
912
  for (const [directory, owner] of directories) await walk(directory, owner, '');
902
- const compiledCache = new Map<string, CompiledCargoInput[]>();
913
+ interface SourceScan {
914
+ readonly directory: string;
915
+ readonly owner: LocalCargoPackage;
916
+ readonly sources: readonly string[];
917
+ readonly found: Map<string, CompiledCargoInput>;
918
+ }
919
+ const scans = new Map<string, SourceScan>();
920
+ const groups = new Map<string, SourceScan[]>();
903
921
  for (const [directory, owner] of directories) {
904
- for (const input of compiledInFiles(directory, compiledCache)) {
905
- if (!ignored(input.file)) {
906
- includeFile(
907
- await realpath(input.file),
908
- inputKey(owner, 'include', relative(directory, input.source) + '\0' + input.literal),
909
- );
910
- }
911
- }
922
+ const scan: SourceScan = { directory, owner, sources: [...compiledSourceFiles(directory)], found: new Map() };
923
+ for (const source of scan.sources) scans.set(source, scan);
924
+ // Equal package identities can interleave, so their discovery shares one boundary.
925
+ const prefix = JSON.stringify([owner.name, owner.version]).slice(0, -1) + ',';
926
+ const group = groups.get(prefix);
927
+ if (group === undefined) groups.set(prefix, [scan]);
928
+ else group.push(scan);
912
929
  }
930
+ const scheduled = [...files.values()].sort(compareInputs);
913
931
  const hash = createHash('sha256');
914
932
  hash.update('cargo-path-inputs-v2\0');
915
933
  for (const { key, target } of [...links.values()].sort((left, right) =>
@@ -920,14 +938,51 @@ export async function hashCargoPathInputs(
920
938
  hash.update(target);
921
939
  hash.update('\0');
922
940
  }
923
- for (const [file, key] of [...files].sort((left, right) => (left[1] < right[1] ? -1 : left[1] > right[1] ? 1 : 0))) {
924
- const bytes = await readFile(file);
925
- hash.update(key);
941
+ async function consume(input: FileInput): Promise<void> {
942
+ if (files.get(input.file) !== input) return;
943
+ const bytes = await readFile(input.file);
944
+ const scan = scans.get(input.file);
945
+ if (scan !== undefined) {
946
+ scans.delete(input.file);
947
+ collectCompiledInputs(input.file, bytes.toString('utf8'), scan.found);
948
+ }
949
+ hash.update(input.key);
926
950
  hash.update('\0');
927
951
  hash.update(String(bytes.length));
928
952
  hash.update('\0');
929
953
  hash.update(bytes);
930
954
  }
955
+ let next = 0;
956
+ for (const [prefix, group] of [...groups].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))) {
957
+ // All ordinary sources have a file identity before this package's include identities.
958
+ // Earlier includes may already have consumed a later package's source; only its
959
+ // discovered references survive that boundary, never the source buffer.
960
+ const boundary = prefix + '"include",';
961
+ while (next < scheduled.length && scheduled[next].key < boundary) await consume(scheduled[next++]);
962
+ const included: FileInput[] = [];
963
+ for (const scan of group) {
964
+ for (const source of scan.sources) {
965
+ if (!scans.delete(source)) continue;
966
+ // Discovery also visits ignored sources. They have no scheduled identity;
967
+ // if an include alias later names one, preserve the old read-then-hash path.
968
+ collectCompiledInputs(source, readFileSync(source, 'utf8'), scan.found);
969
+ }
970
+ for (const input of [...scan.found.values()].sort((left, right) =>
971
+ left.file < right.file ? -1 : left.file > right.file ? 1 : 0,
972
+ )) {
973
+ if (ignored(input.file)) continue;
974
+ const accepted = includeFile(
975
+ await realpath(input.file),
976
+ inputKey(scan.owner, 'include', relative(scan.directory, input.source) + '\0' + input.literal),
977
+ );
978
+ if (accepted !== undefined) included.push(accepted);
979
+ }
980
+ scan.found.clear();
981
+ }
982
+ for (const input of included.sort(compareInputs)) await consume(input);
983
+ while (next < scheduled.length && scheduled[next].key.startsWith(prefix)) await consume(scheduled[next++]);
984
+ }
985
+ while (next < scheduled.length) await consume(scheduled[next++]);
931
986
  return hash.digest('hex');
932
987
  }
933
988
 
@@ -1062,15 +1117,7 @@ const INCLUDE_CLOSERS: Readonly<Record<string, string>> = { '(': ')', '[': ']',
1062
1117
  const INCLUDE_STRING = /^"([^"\\\r\n]*)"$/u;
1063
1118
  const INCLUDE_RAW_STRING = /^r(#*)"([\s\S]*)"\1$/u;
1064
1119
 
1065
- /**
1066
- * Existing literal include_str!/include_bytes! files read by this package's Rust sources.
1067
- * One graph owns the memo; runtime hashes use a fresh memo. Nested packages own their
1068
- * sources separately. Computed paths and arbitrary build-script reads remain explicit inputs.
1069
- */
1070
- export function compiledInFiles(root: string, cache: Map<string, CompiledCargoInput[]>): CompiledCargoInput[] {
1071
- const cached = cache.get(root);
1072
- if (cached !== undefined) return cached;
1073
- const found = new Map<string, CompiledCargoInput>();
1120
+ function* compiledSourceFiles(root: string): Generator<string> {
1074
1121
  const skipped: readonly string[] = HASH_SKIPPED_DIRECTORIES;
1075
1122
  const scan = [root];
1076
1123
  while (scan.length > 0) {
@@ -1081,29 +1128,46 @@ export function compiledInFiles(root: string, cache: Map<string, CompiledCargoIn
1081
1128
  if (entry.isDirectory()) {
1082
1129
  if (!skipped.includes(entry.name) && !existsSync(join(child, 'Cargo.toml'))) scan.push(child);
1083
1130
  } else if (entry.isFile() && entry.name.endsWith('.rs')) {
1084
- const text = readFileSync(child, 'utf8');
1085
- if (!text.includes('include_')) continue;
1086
- const tokens = rustTokens(text);
1087
- for (let index = 0; index < tokens.length; index += 1) {
1088
- if (tokens[index]?.text !== 'include_str' && tokens[index]?.text !== 'include_bytes') continue;
1089
- if (tokens[index + 1]?.text !== '!') continue;
1090
- const closing = INCLUDE_CLOSERS[tokens[index + 2]?.text ?? ''];
1091
- const end = tokens[index + 4]?.text === ',' ? index + 5 : index + 4;
1092
- if (closing === undefined || tokens[end]?.text !== closing) continue;
1093
- const literal = tokens[index + 3]?.text ?? '';
1094
- const path = INCLUDE_STRING.exec(literal)?.[1] ?? INCLUDE_RAW_STRING.exec(literal)?.[2];
1095
- if (path === undefined || path === '') continue;
1096
- const file = resolve(current, path);
1097
- if (statSync(file, { throwIfNoEntry: false })?.isFile()) {
1098
- const prior = found.get(file);
1099
- if (prior === undefined || child < prior.source || (child === prior.source && path < prior.literal)) {
1100
- found.set(file, { file, source: child, literal: path });
1101
- }
1102
- }
1103
- }
1131
+ yield child;
1132
+ }
1133
+ }
1134
+ }
1135
+ }
1136
+
1137
+ function collectCompiledInputs(source: string, text: string, found: Map<string, CompiledCargoInput>): void {
1138
+ if (!text.includes('include_')) return;
1139
+ const tokens = rustTokens(text);
1140
+ for (let index = 0; index < tokens.length; index += 1) {
1141
+ if (tokens[index]?.text !== 'include_str' && tokens[index]?.text !== 'include_bytes') continue;
1142
+ if (tokens[index + 1]?.text !== '!') continue;
1143
+ const closing = INCLUDE_CLOSERS[tokens[index + 2]?.text ?? ''];
1144
+ const end = tokens[index + 4]?.text === ',' ? index + 5 : index + 4;
1145
+ if (closing === undefined || tokens[end]?.text !== closing) continue;
1146
+ // Own the spelling before matching: captures (and RegExp's last-match state)
1147
+ // must reference path-sized storage, not a view into the decoded source.
1148
+ const literal = Buffer.from(tokens[index + 3]?.text ?? '', 'utf8').toString('utf8');
1149
+ const path = INCLUDE_STRING.exec(literal)?.[1] ?? INCLUDE_RAW_STRING.exec(literal)?.[2];
1150
+ if (path === undefined || path === '') continue;
1151
+ const file = resolve(dirname(source), path);
1152
+ if (statSync(file, { throwIfNoEntry: false })?.isFile()) {
1153
+ const prior = found.get(file);
1154
+ if (prior === undefined || source < prior.source || (source === prior.source && path < prior.literal)) {
1155
+ found.set(file, { file, source, literal: path });
1104
1156
  }
1105
1157
  }
1106
1158
  }
1159
+ }
1160
+
1161
+ /**
1162
+ * Existing literal include_str!/include_bytes! files read by this package's Rust sources.
1163
+ * One graph owns the memo; runtime hashing consumes the same discovery alongside source
1164
+ * bytes. Nested packages own their sources separately. Computed paths remain explicit inputs.
1165
+ */
1166
+ export function compiledInFiles(root: string, cache: Map<string, CompiledCargoInput[]>): CompiledCargoInput[] {
1167
+ const cached = cache.get(root);
1168
+ if (cached !== undefined) return cached;
1169
+ const found = new Map<string, CompiledCargoInput>();
1170
+ for (const source of compiledSourceFiles(root)) collectCompiledInputs(source, readFileSync(source, 'utf8'), found);
1107
1171
  const files = [...found.values()].sort((left, right) =>
1108
1172
  left.file < right.file ? -1 : left.file > right.file ? 1 : 0,
1109
1173
  );
@@ -220,13 +220,23 @@ describe('Linux cross-check policy', () => {
220
220
 
221
221
  /**
222
222
  * The managed pre-push hook, run for real in a fixture repository whose `tooling/direnv/repo-path` puts stand-ins
223
- * first on PATH: `uname` says Darwin, `smoo` passes the denylist, and `nx` prints what a cross run printed and exits
224
- * with its status. The hook must call a miss a miss and every other Nx failure what it is.
223
+ * first on PATH: `uname` says Darwin, `smoo` passes the denylist, `direnv` enters the checkout's environment, and `nx`
224
+ * prints what a cross run printed and exits with its status, refusing to run in any environment but the checkout's.
225
+ * The hook must ask in the checkout's environment and call a miss a miss and every other failure what it is.
225
226
  */
226
227
  describe('managed pre-push cross probe', () => {
227
228
  const hook = join(import.meta.dir, '../managed/raw/tooling/git-hooks/pre-push.sh');
229
+ /** The SDK the checkout's environment names; the caller's shell names another. */
230
+ const CHECKOUT_SDK = '/checkout/MacOSX.sdk';
228
231
 
229
- async function push(nxOutput: string, nxStatus: number): Promise<{ status: number | null; stderr: string }> {
232
+ interface Push {
233
+ readonly nxOutput: string;
234
+ readonly nxStatus: number;
235
+ /** Whether direnv is on the hook's PATH. */
236
+ readonly direnv?: boolean;
237
+ }
238
+
239
+ async function push({ nxOutput, nxStatus, direnv = true }: Push): Promise<{ status: number | null; stderr: string }> {
230
240
  const root = await mkdtemp(join(tmpdir(), 'pre-push-cross-'));
231
241
  try {
232
242
  const bin = join(root, 'bin');
@@ -238,14 +248,29 @@ describe('managed pre-push cross probe', () => {
238
248
  };
239
249
  await stub('uname', 'echo Darwin');
240
250
  await stub('smoo', 'exit 0');
241
- await stub('nx', `printf '%s\\n' "$NX_STUB_OUTPUT"; exit ${nxStatus}`);
242
- await writeFile(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), `#!/bin/sh\necho "${bin}:$PATH"\n`);
251
+ // Nx keys on the environment it runs in: outside the checkout's, it is not the gate's run.
252
+ await stub(
253
+ 'nx',
254
+ `[ "$SDKROOT" = '${CHECKOUT_SDK}' ] || { echo "nx stub: keyed on the caller's SDKROOT $SDKROOT" >&2; exit 1; }\nprintf '%s\\n' "$NX_STUB_OUTPUT"; exit ${nxStatus}`,
255
+ );
256
+ if (direnv) {
257
+ // The checkout's .envrc: `direnv exec <checkout>` enters the environment that checkout declares.
258
+ await stub(
259
+ 'direnv',
260
+ `[ "$1" = exec ] && [ "$2" = "$PWD" ] || { echo "direnv stub: unexpected $*" >&2; exit 64; }\nexport SDKROOT='${CHECKOUT_SDK}'\nshift 2\nexec "$@"`,
261
+ );
262
+ }
263
+ // Without direnv, PATH holds the stand-ins alone: no host direnv is found, and no coreutils either, as on a
264
+ // Nix-built Linux host whose /usr/bin and /bin hold none. The refusal must still be said.
265
+ const path = direnv ? `${bin}:$PATH` : bin;
266
+ await writeFile(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), `#!/bin/sh\necho "${path}"\n`);
243
267
  await chmod(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), 0o755);
244
268
  await writeFile(join(root, 'repo', 'tooling', 'direnv', 'ttsc-env.sh'), '');
245
269
  expect(spawnSync('git', ['init', '-q', join(root, 'repo')]).status).toBe(0);
246
270
  const run = spawnSync('bash', [hook, 'origin'], {
247
271
  cwd: join(root, 'repo'),
248
- env: { ...process.env, NX_STUB_OUTPUT: nxOutput },
272
+ // The caller's shell carries another directory's SDK, as a shell inside a parent directory's devenv does.
273
+ env: { ...process.env, NX_STUB_OUTPUT: nxOutput, SDKROOT: '/nix/store/caller-apple-sdk/MacOSX.sdk' },
249
274
  input: '',
250
275
  encoding: 'utf8',
251
276
  });
@@ -255,21 +280,37 @@ describe('managed pre-push cross probe', () => {
255
280
  }
256
281
  }
257
282
 
258
- it('passes a hit', async () => {
259
- expect((await push('Successfully ran target cargo-lint-cross', 0)).status).toBe(0);
283
+ it("passes a hit, asked in the checkout's environment rather than the caller's", async () => {
284
+ const { status, stderr } = await push({ nxOutput: 'Successfully ran target cargo-lint-cross', nxStatus: 0 });
285
+ expect(stderr).not.toContain("keyed on the caller's SDKROOT");
286
+ expect(status).toBe(0);
260
287
  });
261
288
 
262
289
  it('names a miss when the cross target refused to compile', async () => {
263
- const { status, stderr } = await push(CARGO_CROSS_LINT_REFUSAL, 1);
290
+ const { status, stderr } = await push({ nxOutput: CARGO_CROSS_LINT_REFUSAL, nxStatus: 1 });
264
291
  expect(status).toBe(1);
265
292
  expect(stderr).toContain('NOT cached for this tree');
266
293
  expect(stderr).toContain('bun run check:linux');
267
294
  });
268
295
 
269
- it('names any other Nx failure as that, never as a cache miss', async () => {
270
- const { status, stderr } = await push('NX NX_WORKSPACE_ROOT_PATH names another workspace', 1);
296
+ it('names any other failure as that, never as a cache miss', async () => {
297
+ const { status, stderr } = await push({
298
+ nxOutput: 'NX NX_WORKSPACE_ROOT_PATH names another workspace',
299
+ nxStatus: 1,
300
+ });
301
+ expect(status).toBe(1);
302
+ expect(stderr).toContain('failed (exit 1) before it reached');
303
+ expect(stderr).not.toContain('NOT cached');
304
+ });
305
+
306
+ it("refuses, naming direnv, when it cannot enter the checkout's environment", async () => {
307
+ const { status, stderr } = await push({
308
+ nxOutput: 'Successfully ran target cargo-lint-cross',
309
+ nxStatus: 0,
310
+ direnv: false,
311
+ });
271
312
  expect(status).toBe(1);
272
- expect(stderr).toContain('Nx failed (exit 1) before the Linux cross-compile check reached');
313
+ expect(stderr).toContain('direnv is not on PATH');
273
314
  expect(stderr).not.toContain('NOT cached');
274
315
  });
275
316
  });
@@ -22,15 +22,20 @@ import { expandOutputs, WorkspaceContext } from 'nx/src/native';
22
22
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory';
23
23
  import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
24
24
  import typia from 'typia';
25
+ import { pastTick } from './__tests__/past-tick.js';
25
26
  import {
26
27
  censusStart,
28
+ type GraphInstallationCensus,
27
29
  type GraphWitness,
28
30
  graphInstallationIdentity,
31
+ installationIdentityOf,
29
32
  movedNodes,
30
33
  type NodeWitness,
31
34
  nodeStamp,
32
35
  nodesUnchanged,
36
+ provenInstallationCensus,
33
37
  readGraphWitness,
38
+ recordedInstallationCensus,
34
39
  recordedStamp,
35
40
  reusableGraph,
36
41
  SAME_TICK_NS,
@@ -68,10 +73,11 @@ afterEach(async () => {
68
73
 
69
74
  function witness(): GraphWitness {
70
75
  return {
71
- version: 1,
76
+ version: 2,
72
77
  workspaceRoot: root,
73
78
  environment: 'env',
74
79
  installation: 'installed',
80
+ installationUnproven: [],
75
81
  snapshot: snapshot(),
76
82
  projectGraph: graph,
77
83
  };
@@ -151,7 +157,7 @@ describe('authoritative graph witness', () => {
151
157
  } finally {
152
158
  await chmod(path, 0o600);
153
159
  }
154
- await writeFile(path, JSON.stringify({ ...witness(), version: 2 }));
160
+ await writeFile(path, JSON.stringify({ ...witness(), version: 1 }));
155
161
  expect(readGraphWitness(path)).toBeNull();
156
162
  });
157
163
  });
@@ -339,6 +345,51 @@ describe('installed graph input identity', () => {
339
345
  original.files.find(({ path }) => path === output)?.sha256,
340
346
  );
341
347
  });
348
+
349
+ it('takes only a proven, unmoved file’s hash from a previous census and reads every other file again', async () => {
350
+ const { plugin, resolver } = await packages();
351
+ const output = join(plugin, 'dist', 'index.js');
352
+ const census = (previous?: GraphInstallationCensus) =>
353
+ typia.assert<GraphInstallationCensus>(
354
+ provenInstallationCensus(
355
+ root,
356
+ { plugins: ['@smoothbricks/nx-plugin'] },
357
+ resolver,
358
+ expandOutputs,
359
+ null,
360
+ undefined,
361
+ previous,
362
+ ),
363
+ );
364
+ const hashOf = ({ files }: GraphInstallationCensus) => files.find(({ path }) => path === output)?.sha256;
365
+ const bytes = (text: string) => createHash('sha256').update(text).digest('hex');
366
+ await pastTick();
367
+ const first = census();
368
+ expect(first.unproven).toEqual([]);
369
+ // A recorded hash that is not the file's: a census that takes it did not read the file.
370
+ const recorded = {
371
+ ...first,
372
+ files: first.files.map((file) => (file.path === output ? { ...file, sha256: 'recorded' } : file)),
373
+ };
374
+ expect(hashOf(census(recorded))).toBe('recorded');
375
+ // The witness keeps the census as its identity and its unproven files beside it.
376
+ expect(recordedInstallationCensus(installationIdentityOf(recorded, null), [])).toEqual(recorded);
377
+ expect(hashOf(census({ ...recorded, unproven: [output] }))).toBe(bytes('exports.value = 1;'));
378
+ // Only its ctime moved: a same-mode chmod, as Bun's bin linking makes.
379
+ chmodSync(output, lstatSync(output).mode & 0o7777);
380
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 1;'));
381
+ // A same-size rewrite that keeps its mtime moves its ctime: read again.
382
+ const before = await stat(output);
383
+ await writeFile(output, 'exports.value = 2;');
384
+ await utimes(output, before.atime, before.mtime);
385
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 2;'));
386
+ // Dated ahead of the census, as a change in its tick is: recorded unproven, and read again by the next census.
387
+ const ahead = new Date(Date.now() + 60_000);
388
+ await utimes(output, ahead, ahead);
389
+ const dated = census();
390
+ expect(dated.unproven).toEqual([output]);
391
+ expect(hashOf(census({ ...dated, files: recorded.files }))).toBe(bytes('exports.value = 2;'));
392
+ });
342
393
  it('proves a declared dist link by its physical output tree and rejects changed public bytes', async () => {
343
394
  const { plugin, resolver } = await packages();
344
395
  const published = join(root, 'published-build-volume');