@smoothbricks/nx-plugin 0.4.27 → 0.4.29

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 (124) hide show
  1. package/README.md +203 -83
  2. package/dist/bounded-test-policy.d.ts +10 -3
  3. package/dist/bounded-test-policy.d.ts.map +1 -1
  4. package/dist/bounded-test-policy.js +19 -25
  5. package/dist/cargo-closure-input.d.ts +12 -0
  6. package/dist/cargo-closure-input.d.ts.map +1 -1
  7. package/dist/cargo-closure-input.js +29 -7
  8. package/dist/cargo-source-hash.d.ts.map +1 -1
  9. package/dist/cargo-source-hash.js +42 -19
  10. package/dist/ensure-built-receipt.d.ts +34 -6
  11. package/dist/ensure-built-receipt.d.ts.map +1 -1
  12. package/dist/ensure-built-receipt.js +265 -159
  13. package/dist/ensure-built-witness.d.ts +27 -2
  14. package/dist/ensure-built-witness.d.ts.map +1 -1
  15. package/dist/ensure-built-witness.js +2176 -15
  16. package/dist/ensure-built.d.ts +99 -1
  17. package/dist/ensure-built.d.ts.map +1 -1
  18. package/dist/ensure-built.js +34 -53
  19. package/dist/executors/bounded-exec/cpu-pool.d.ts +16 -5
  20. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/cpu-pool.js +43 -19
  22. package/dist/executors/bounded-exec/executor.d.ts +11 -2
  23. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/executor.js +139 -39
  25. package/dist/executors/bounded-exec/ram-temp.d.ts +17 -11
  26. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/ram-temp.js +27 -13
  28. package/dist/generators/bun-test-tracing/generator.js +2 -2
  29. package/dist/graph-input-contract.d.ts +98 -0
  30. package/dist/graph-input-contract.d.ts.map +1 -0
  31. package/dist/graph-input-contract.js +624 -0
  32. package/dist/index.d.ts +12 -5
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +17 -3
  35. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  36. package/dist/managed-files/ci-workflow.js +5 -2
  37. package/dist/managed-files/publish-workflow.d.ts +11 -1
  38. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  39. package/dist/producer-receipts.d.ts +19 -0
  40. package/dist/producer-receipts.d.ts.map +1 -0
  41. package/dist/producer-receipts.js +921 -0
  42. package/dist/testing.d.ts +8 -0
  43. package/dist/testing.d.ts.map +1 -1
  44. package/dist/testing.js +58 -0
  45. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  46. package/managed/raw/tooling/direnv/developer-links.ts +147 -21
  47. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  48. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  49. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  50. package/managed/raw/tooling/direnv/repo-path +15 -2
  51. package/managed/raw/tooling/direnv/setup-environment.ts +363 -125
  52. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  53. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  54. package/nextest.toml +1 -1
  55. package/package.json +11 -6
  56. package/src/__tests__/fixture-nx-env.ts +7 -0
  57. package/src/__tests__/owned-installation.ts +174 -0
  58. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  59. package/src/bounded-test-policy.test.ts +36 -22
  60. package/src/bounded-test-policy.ts +19 -27
  61. package/src/bun-bootstrap.test.ts +1 -1
  62. package/src/cargo-closure-input.test.ts +60 -11
  63. package/src/cargo-closure-input.ts +28 -6
  64. package/src/cargo-resolve-cache.test.ts +140 -25
  65. package/src/cargo-source-hash.test.ts +78 -3
  66. package/src/cargo-source-hash.ts +48 -30
  67. package/src/cross-check-policy.test.ts +2 -2
  68. package/src/ensure-built-receipt.test.ts +111 -16
  69. package/src/ensure-built-receipt.ts +251 -149
  70. package/src/ensure-built-witness.test.ts +197 -13
  71. package/src/ensure-built-witness.ts +187 -15
  72. package/src/ensure-built.test.ts +156 -102
  73. package/src/ensure-built.ts +114 -64
  74. package/src/executors/bounded-exec/cpu-pool.test.ts +116 -32
  75. package/src/executors/bounded-exec/cpu-pool.ts +55 -17
  76. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  77. package/src/executors/bounded-exec/executor-host.test.ts +51 -41
  78. package/src/executors/bounded-exec/executor.test.ts +155 -35
  79. package/src/executors/bounded-exec/executor.ts +156 -45
  80. package/src/executors/bounded-exec/progress.test.ts +1 -1
  81. package/src/executors/bounded-exec/ram-temp-host.test.ts +46 -17
  82. package/src/executors/bounded-exec/ram-temp.test.ts +65 -3
  83. package/src/executors/bounded-exec/ram-temp.ts +32 -16
  84. package/src/executors/bounded-exec/verdict.test.ts +17 -12
  85. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  86. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  87. package/src/generators/bun-test-tracing/generator.ts +2 -2
  88. package/src/graph-input-contract.test.ts +371 -0
  89. package/src/graph-input-contract.ts +605 -0
  90. package/src/index.test.ts +3 -3
  91. package/src/index.ts +24 -3
  92. package/src/linked-snapshots.nx.test.ts +112 -30
  93. package/src/managed-files/ci-workflow.ts +5 -2
  94. package/src/managed-files/publish-workflow.ts +12 -1
  95. package/src/managed-files/repo-path.test.ts +44 -1
  96. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  97. package/src/nextest-code-bound.test.ts +23 -17
  98. package/src/nextest-partitions.test.ts +7 -1
  99. package/src/nx-cache-restore-mtime.test.ts +2 -2
  100. package/src/nx-daemon-claim.test.ts +5 -5
  101. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  102. package/src/nx-daemon-idle-policy.test.ts +56 -0
  103. package/src/nx-daemon-ignore-files.test.ts +2 -2
  104. package/src/nx-daemon-validity.test.ts +31 -9
  105. package/src/nx-dot-input-policy.test.ts +5 -5
  106. package/src/nx-files-archive-racy.test.ts +1 -1
  107. package/src/nx-incomplete-run-summary.test.ts +1 -1
  108. package/src/nx-plugin-load-errors.test.ts +2 -2
  109. package/src/nx-project-graph-errors.test.ts +70 -0
  110. package/src/nx-skipped-task-exit.test.ts +2 -2
  111. package/src/nx-store-resolution.test.ts +7 -25
  112. package/src/nx-task-output-lease.test.ts +3 -3
  113. package/src/nx-use-local.test.ts +7 -5
  114. package/src/producer-receipts.test.ts +164 -0
  115. package/src/producer-receipts.ts +631 -0
  116. package/src/runtime-input.test.ts +1 -1
  117. package/src/serial-group-wiring.test.ts +4 -4
  118. package/src/task-results.test.ts +18 -16
  119. package/src/test-timings.test.ts +2 -2
  120. package/src/testing.test.ts +5 -5
  121. package/src/testing.ts +59 -0
  122. package/src/typecheck-test-policy.test.ts +1 -1
  123. package/src/typescript-resolution.test.ts +76 -0
  124. package/test-timings.json +5 -0
@@ -13,6 +13,7 @@ import {
13
13
  readdirSync,
14
14
  readFileSync,
15
15
  readlinkSync,
16
+ readSync,
16
17
  realpathSync,
17
18
  renameSync,
18
19
  rmSync,
@@ -32,6 +33,7 @@ import {
32
33
  planDeclaredLinks,
33
34
  reportDeveloperLinks,
34
35
  reportUnavailableDeclaredLinks,
36
+ unreadableDeveloperLinks,
35
37
  } from './developer-links.ts';
36
38
  import {
37
39
  type DeferredSecret,
@@ -204,7 +206,8 @@ const POST_COMMIT_BLOCK = [
204
206
  // (cargoFetcher) live in smoo's user cache and count only while CARGO_HOME
205
207
  // names the directory they were fetched into, which no checkout-relative
206
208
  // change detection can see.
207
- // `--links` repairs stale install links (including external Nx aliases), then
209
+ // `--links` repairs stale install links (including external Nx aliases and
210
+ // store links whose install-cache entry is gone), then
208
211
  // points the packages `smoo.developerLinks` declares at their local checkouts.
209
212
  // devenv's smoo:developer-links task uses `--links --check` as its status:
210
213
  // links can change without any smoo:install input changing.
@@ -247,12 +250,20 @@ try {
247
250
  // A refused or unclaimed snapshot is current only when said: the exec that follows names its cause.
248
251
  const current =
249
252
  plan.changes.length === 0 && plan.unavailable.every((unavailable) => unavailable.kind === 'absent');
250
- process.exit(current && !hasStaleDeveloperLinks(projectRoot, ['nx']) ? 0 : 1);
253
+ process.exit(
254
+ current && !hasStaleDeveloperLinks(projectRoot, ['nx']) && danglingStoreLinks(projectRoot).length === 0 ? 0 : 1,
255
+ );
251
256
  }
252
257
  await withInstallLock(async () => {
253
258
  // Recheck under the install lock: another shell may already have repaired
254
259
  // the aliases. Use the same installer as smoo:install, never a second linker.
255
- if (hasStaleDeveloperLinks(projectRoot, ['nx'])) {
260
+ const store = danglingStoreLinks(projectRoot);
261
+ for (const link of store) {
262
+ console.error(
263
+ `! ${link} -> ${readlinkSync(link)}: that Bun install-cache entry no longer exists; reinstalling`,
264
+ );
265
+ }
266
+ if (store.length > 0 || hasStaleDeveloperLinks(projectRoot, ['nx'])) {
256
267
  const error = await installLocalDependencies([bunInstaller(bunInstallInputs())]);
257
268
  if (error !== undefined) reportSetupFailure(error);
258
269
  ensureTypeScriptApiPackage(projectRoot);
@@ -906,6 +917,9 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
906
917
  let actualPath = store;
907
918
  let expectedTarget = store;
908
919
  let operation: BunInstallIntegrityOperation = 'inspect isolated store';
920
+ // Developer links whose target this process may not read (developer-links.ts): the install
921
+ // did not make them and nothing here can read what they name, so they are not admitted here.
922
+ let unreadable: ReadonlySet<string> = new Set();
909
923
 
910
924
  const select = (candidate: string, symbolicLink: boolean): void => {
911
925
  actualPath = candidate;
@@ -919,6 +933,7 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
919
933
  realpathSync(candidate);
920
934
  };
921
935
  const manifest = (candidate: string, symbolicLink: boolean): void => {
936
+ if (unreadable.has(candidate)) return;
922
937
  select(candidate, symbolicLink);
923
938
  operation = 'read installed package manifest';
924
939
  JSON.parse(readFileSync(path.join(candidate, 'package.json'), 'utf8'));
@@ -951,6 +966,7 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
951
966
  // at. Same package and version alone is never reachability proof.
952
967
  const identityOf = (name: string): string | null => /^(.+@\d[^+]*)(\+.*)?$/.exec(name)?.[1] ?? null;
953
968
  try {
969
+ unreadable = new Set(unreadableDeveloperLinks(root));
954
970
  const state = lstatSync(store, { throwIfNoEntry: false });
955
971
  // A hoisted install has no isolated store to admit.
956
972
  if (state === undefined) return undefined;
@@ -963,112 +979,8 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
963
979
  const identity = identityOf(entry.name) ?? entry.name;
964
980
  identityCounts.set(identity, (identityCounts.get(identity) ?? 0) + 1);
965
981
  }
966
- // Every link in the installed tree that resolves into a store entry makes that entry live.
967
- const referenced = new Set<string>();
968
- const knownEntries = new Set(entries.filter((entry) => entry.name !== 'node_modules').map((entry) => entry.name));
969
- // Where each store entry actually lives: a link can reach an entry through its install
970
- // cache target (whose basename carries a cache suffix the entry name does not), so live
971
- // reachability is also containment inside an entry's real directory, not name matching.
972
- const entryReals = new Map<string, string>();
973
- for (const entry of entries) {
974
- if (entry.name === 'node_modules' || !(entry.isDirectory() || entry.isSymbolicLink())) continue;
975
- try {
976
- entryReals.set(entry.name, realpathSync(path.join(store, entry.name)));
977
- } catch {
978
- // A dangling entry has no real directory for a live alias to land inside.
979
- }
980
- }
981
- const scanLinks = (dir: string, owner: string | null): void => {
982
- let children: Dirent[];
983
- try {
984
- children = readdirSync(dir, { withFileTypes: true });
985
- } catch {
986
- return;
987
- }
988
- for (const child of children) {
989
- if (child.name.startsWith('.')) continue;
990
- const childPath = path.join(dir, child.name);
991
- if (child.name.startsWith('@')) {
992
- // A scope is a namespace, symlinked or not: traverse it, never treat it as a package.
993
- let isDirectory = false;
994
- try {
995
- isDirectory = statSync(childPath).isDirectory();
996
- } catch {
997
- isDirectory = false;
998
- }
999
- if (isDirectory) {
1000
- scanLinks(childPath, owner);
1001
- continue;
1002
- }
1003
- }
1004
- if (!child.isSymbolicLink()) continue;
1005
- let target: string;
1006
- try {
1007
- target = readlinkSync(childPath);
1008
- } catch {
1009
- continue;
1010
- }
1011
- // The alias's canonical endpoint is what it actually reaches — through intermediate
1012
- // symlinks as well as its literal target — and is what select() already witnesses.
1013
- // A dangling alias has no endpoint; fall back to resolving its literal target
1014
- // against the physical parent, so a live reference to a broken entry still marks
1015
- // that entry referenced.
1016
- let physicalDir = path.dirname(childPath);
1017
- try {
1018
- physicalDir = realpathSync(physicalDir);
1019
- } catch {
1020
- // Keep the lexical parent; the segment mark below still applies.
1021
- }
1022
- let endpoint: string | null = null;
1023
- try {
1024
- endpoint = realpathSync(childPath);
1025
- } catch {
1026
- endpoint = null;
1027
- }
1028
- const resolved = endpoint ?? path.resolve(physicalDir, target);
1029
- if (endpoint !== null) {
1030
- // The canonical endpoint is authoritative: the entry whose real directory
1031
- // contains it is the live target, whatever any path segment happens to name.
1032
- for (const [name, real] of entryReals) {
1033
- if (name !== owner && (endpoint === real || endpoint.startsWith(`${real}${path.sep}`))) {
1034
- referenced.add(name);
1035
- break;
1036
- }
1037
- }
1038
- } else {
1039
- for (const segment of resolved.split(path.sep)) {
1040
- if (knownEntries.has(segment) && segment !== owner) {
1041
- referenced.add(segment);
1042
- break;
1043
- }
1044
- }
1045
- }
1046
- }
1047
- };
1048
- scanLinks(path.join(root, 'node_modules'), null);
1049
- scanLinks(path.join(store, 'node_modules'), null);
1050
- // Workspace members keep their own node_modules beside their manifests; links there are
1051
- // as live as root links, so the closure starts from them too. The enumeration is the one
1052
- // bunInstallInputs owns: both workspace forms, full glob patterns, exclusions.
1053
- const rootManifest = parseOrUndefined(() => JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')));
1054
- const workspaces = field(rootManifest, 'workspaces');
1055
- const memberPatterns = Array.isArray(workspaces)
1056
- ? stringList(workspaces)
1057
- : stringList(field(workspaces, 'packages'));
1058
- for (const manifestPath of workspaceFiles(memberPatterns, 'package.json')) {
1059
- scanLinks(path.join(root, path.dirname(manifestPath), 'node_modules'), null);
1060
- }
1061
- let grew = true;
1062
- while (grew) {
1063
- grew = false;
1064
- for (const entry of entries) {
1065
- if (entry.name === 'node_modules' || !(entry.isDirectory() || entry.isSymbolicLink())) continue;
1066
- if (!referenced.has(entry.name)) continue;
1067
- const before = referenced.size;
1068
- scanLinks(path.join(store, entry.name, 'node_modules'), entry.name);
1069
- if (referenced.size > before) grew = true;
1070
- }
1071
- }
982
+ const members = workspaceMemberModules(root);
983
+ const referenced = referencedStoreEntries(root, store, entries, members);
1072
984
  // Package aliases are what every consumer actually resolves: each package they name must
1073
985
  // resolve to a readable manifest, wherever it points — into the store, into the install
1074
986
  // cache, or at a workspace member. The same strict walk applies at the root and in every
@@ -1098,11 +1010,10 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
1098
1010
  }
1099
1011
  };
1100
1012
  walkPackageAliases(path.join(root, 'node_modules'));
1101
- for (const manifestPath of workspaceFiles(memberPatterns, 'package.json')) {
1102
- const memberModules = path.join(root, path.dirname(manifestPath), 'node_modules');
1013
+ for (const modules of members) {
1103
1014
  // A member with nothing installed has no aliases to validate; a present directory is
1104
1015
  // walked with the root's strictness.
1105
- if (existsSync(memberModules)) walkPackageAliases(memberModules);
1016
+ if (existsSync(modules)) walkPackageAliases(modules);
1106
1017
  }
1107
1018
  for (const entry of entries) {
1108
1019
  const candidate = path.join(store, entry.name);
@@ -1124,6 +1035,157 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
1124
1035
  }
1125
1036
  }
1126
1037
 
1038
+ /**
1039
+ * The store entries the installed tree reaches. Every link in the root's node_modules, the
1040
+ * store's own and each workspace member's that resolves into an entry makes that entry live,
1041
+ * and so does every link in a live entry's own node_modules, to a fixed point. Member links
1042
+ * are as live as root links.
1043
+ */
1044
+ function referencedStoreEntries(
1045
+ root: string,
1046
+ store: string,
1047
+ entries: readonly Dirent[],
1048
+ members: readonly string[],
1049
+ ): Set<string> {
1050
+ const referenced = new Set<string>();
1051
+ const knownEntries = new Set(entries.filter((entry) => entry.name !== 'node_modules').map((entry) => entry.name));
1052
+ // Where each store entry actually lives: a link can reach an entry through its install
1053
+ // cache target (whose basename carries a cache suffix the entry name does not), so live
1054
+ // reachability is also containment inside an entry's real directory, not name matching.
1055
+ const entryReals = new Map<string, string>();
1056
+ for (const entry of entries) {
1057
+ if (entry.name === 'node_modules' || !(entry.isDirectory() || entry.isSymbolicLink())) continue;
1058
+ try {
1059
+ entryReals.set(entry.name, realpathSync(path.join(store, entry.name)));
1060
+ } catch {
1061
+ // A dangling entry has no real directory for a live alias to land inside.
1062
+ }
1063
+ }
1064
+ const scanLinks = (dir: string, owner: string | null): void => {
1065
+ let children: Dirent[];
1066
+ try {
1067
+ children = readdirSync(dir, { withFileTypes: true });
1068
+ } catch {
1069
+ return;
1070
+ }
1071
+ for (const child of children) {
1072
+ if (child.name.startsWith('.')) continue;
1073
+ const childPath = path.join(dir, child.name);
1074
+ if (child.name.startsWith('@')) {
1075
+ // A scope is a namespace, symlinked or not: traverse it, never treat it as a package.
1076
+ let isDirectory = false;
1077
+ try {
1078
+ isDirectory = statSync(childPath).isDirectory();
1079
+ } catch {
1080
+ isDirectory = false;
1081
+ }
1082
+ if (isDirectory) {
1083
+ scanLinks(childPath, owner);
1084
+ continue;
1085
+ }
1086
+ }
1087
+ if (!child.isSymbolicLink()) continue;
1088
+ let target: string;
1089
+ try {
1090
+ target = readlinkSync(childPath);
1091
+ } catch {
1092
+ continue;
1093
+ }
1094
+ // The alias's canonical endpoint is what it actually reaches — through intermediate
1095
+ // symlinks as well as its literal target — and is what select() already witnesses.
1096
+ // A dangling alias has no endpoint; fall back to resolving its literal target
1097
+ // against the physical parent, so a live reference to a broken entry still marks
1098
+ // that entry referenced.
1099
+ let physicalDir = path.dirname(childPath);
1100
+ try {
1101
+ physicalDir = realpathSync(physicalDir);
1102
+ } catch {
1103
+ // Keep the lexical parent; the segment mark below still applies.
1104
+ }
1105
+ let endpoint: string | null = null;
1106
+ try {
1107
+ endpoint = realpathSync(childPath);
1108
+ } catch {
1109
+ endpoint = null;
1110
+ }
1111
+ const resolved = endpoint ?? path.resolve(physicalDir, target);
1112
+ if (endpoint !== null) {
1113
+ // The canonical endpoint is authoritative: the entry whose real directory
1114
+ // contains it is the live target, whatever any path segment happens to name.
1115
+ for (const [name, real] of entryReals) {
1116
+ if (name !== owner && (endpoint === real || endpoint.startsWith(`${real}${path.sep}`))) {
1117
+ referenced.add(name);
1118
+ break;
1119
+ }
1120
+ }
1121
+ } else {
1122
+ for (const segment of resolved.split(path.sep)) {
1123
+ if (knownEntries.has(segment) && segment !== owner) {
1124
+ referenced.add(segment);
1125
+ break;
1126
+ }
1127
+ }
1128
+ }
1129
+ }
1130
+ };
1131
+ scanLinks(path.join(root, 'node_modules'), null);
1132
+ scanLinks(path.join(store, 'node_modules'), null);
1133
+ for (const modules of members) scanLinks(modules, null);
1134
+ let grew = true;
1135
+ while (grew) {
1136
+ grew = false;
1137
+ for (const entry of entries) {
1138
+ if (entry.name === 'node_modules' || !(entry.isDirectory() || entry.isSymbolicLink())) continue;
1139
+ if (!referenced.has(entry.name)) continue;
1140
+ const before = referenced.size;
1141
+ scanLinks(path.join(store, entry.name, 'node_modules'), entry.name);
1142
+ if (referenced.size > before) grew = true;
1143
+ }
1144
+ }
1145
+ return referenced;
1146
+ }
1147
+
1148
+ /**
1149
+ * The node_modules directory of every workspace member, beside its manifest. The enumeration
1150
+ * is the one bunInstallInputs owns: both workspace forms, full glob patterns, exclusions.
1151
+ */
1152
+ function workspaceMemberModules(root: string): string[] {
1153
+ const rootManifest = parseOrUndefined(() => JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')));
1154
+ const workspaces = field(rootManifest, 'workspaces');
1155
+ const memberPatterns = Array.isArray(workspaces) ? stringList(workspaces) : stringList(field(workspaces, 'packages'));
1156
+ return workspaceFiles(memberPatterns, 'package.json').map((manifestPath) =>
1157
+ path.join(root, path.dirname(manifestPath), 'node_modules'),
1158
+ );
1159
+ }
1160
+
1161
+ /**
1162
+ * The store links the installed tree reaches whose target in Bun's install cache is gone. Under
1163
+ * `globalStore` each node_modules/.bun/<entry> is a link into that cache, and one that dangles
1164
+ * names a cache entry that no longer exists: the install is out of date, as it is while a
1165
+ * developer link dangles, and `bun install` links the entry again (measured, Bun 1.4.3). A
1166
+ * dangling variant nothing live reaches is superseded garbage the install leaves in place (see
1167
+ * verifyInstalledBunLinks), so it never makes the install stale: no install would change it.
1168
+ * The closure is walked only when some store link dangles.
1169
+ */
1170
+ function danglingStoreLinks(root: string): string[] {
1171
+ const store = path.join(root, 'node_modules', '.bun');
1172
+ let entries: Dirent[];
1173
+ try {
1174
+ entries = readdirSync(store, { withFileTypes: true }).filter((entry) => !entry.name.startsWith('.'));
1175
+ } catch (error) {
1176
+ // A hoisted install, or none yet, has no store to link from.
1177
+ if (error instanceof Error && 'code' in error && (error.code === 'ENOENT' || error.code === 'ENOTDIR')) return [];
1178
+ throw error;
1179
+ }
1180
+ const dangling = entries.filter(
1181
+ (entry) =>
1182
+ entry.isSymbolicLink() && statSync(path.join(store, entry.name), { throwIfNoEntry: false }) === undefined,
1183
+ );
1184
+ if (dangling.length === 0) return [];
1185
+ const referenced = referencedStoreEntries(root, store, entries, workspaceMemberModules(root));
1186
+ return dangling.filter((entry) => referenced.has(entry.name)).map((entry) => path.join(store, entry.name));
1187
+ }
1188
+
1127
1189
  /**
1128
1190
  * `bun install`. Its result is location-independent: the isolated linker
1129
1191
  * links packages from the install cache by absolute path and workspace
@@ -1134,7 +1196,10 @@ function verifyInstalledBunLinks(root: string): BunInstallIntegrityError | undef
1134
1196
  * was pruned out from under node_modules. The native compiler binary ttsc
1135
1197
  * runs is witnessed the same way when the manifest declares it: a pruned
1136
1198
  * restore that kept the stamp and the API package but lost that binary
1137
- * otherwise reads as current and no install ever repairs it. A dangling developer target, or a live link to Nx, also invalidates a
1199
+ * otherwise reads as current and no install ever repairs it. A dangling
1200
+ * developer target, a live link to Nx, or a store link the installed tree
1201
+ * reaches whose install-cache entry is gone (danglingStoreLinks) also
1202
+ * invalidates a matching stamp.
1138
1203
  *
1139
1204
  * Frozen: shell entry installs what bun.lock records and never rewrites it. An unfrozen install migrated a
1140
1205
  * repository's lockfile format and re-resolved its ranges, a lockfile diff nobody asked for. A manifest that needs a
@@ -1149,7 +1214,8 @@ function bunInstaller(inputs: readonly string[]): Installer {
1149
1214
  findInstalledTypeScriptApiPackage(projectRoot) !== null &&
1150
1215
  (!declaresNativeTypeScript(projectRoot) ||
1151
1216
  existsSync(path.join(projectRoot, 'node_modules', '@typescript', 'native', 'bin', 'tsc'))) &&
1152
- !hasStaleDeveloperLinks(projectRoot, ['nx']),
1217
+ !hasStaleDeveloperLinks(projectRoot, ['nx']) &&
1218
+ danglingStoreLinks(projectRoot).length === 0,
1153
1219
  install: async ({ quiet }) => {
1154
1220
  await keepDeveloperLinks(
1155
1221
  projectRoot,
@@ -1203,6 +1269,178 @@ function bunInstaller(inputs: readonly string[]): Installer {
1203
1269
  * in the checkout is part of the identity: the relative `.pth` lines hold only
1204
1270
  * there.
1205
1271
  */
1272
+ function sitePackagesDirectories(environment: string): string[] {
1273
+ const lib = path.join(environment, 'lib');
1274
+ const directories: string[] = [];
1275
+ for (const python of existsSync(lib) ? readdirSync(lib) : []) {
1276
+ if (!python.startsWith('python')) continue;
1277
+ const sitePackages = path.join(lib, python, 'site-packages');
1278
+ if (existsSync(sitePackages)) directories.push(sitePackages);
1279
+ }
1280
+ return directories;
1281
+ }
1282
+
1283
+ /**
1284
+ * Whether an existing environment is the relocatable one install would leave: its config says
1285
+ * so, no launcher or interpreter link names an environment or a checkout (its own, a foreign
1286
+ * one, or a checkout's .devenv profile, in a shebang, a wrapper's exec argv, or the link
1287
+ * itself), and no `.pth` line is an absolute path. The stamp alone cannot witness this — a
1288
+ * copied checkout carries a matching
1289
+ * stamp and an old absolute launcher together, and admits the launcher forever.
1290
+ */
1291
+ /** One shell command line as its words: quotes group, backslash escapes, no expansion. */
1292
+ function shellWords(line: string): string[] {
1293
+ const words: string[] = [];
1294
+ let word = '';
1295
+ let quote: string | null = null;
1296
+ let started = false;
1297
+ for (let index = 0; index < line.length; index += 1) {
1298
+ const char = line[index] ?? '';
1299
+ if (quote !== null) {
1300
+ if (char === quote) quote = null;
1301
+ else if (char === '\\' && quote === '"' && index + 1 < line.length) word += line[++index];
1302
+ else word += char;
1303
+ continue;
1304
+ }
1305
+ if (char === "'" || char === '"') {
1306
+ quote = char;
1307
+ started = true;
1308
+ } else if (char === '\\' && index + 1 < line.length) {
1309
+ word += line[++index];
1310
+ started = true;
1311
+ } else if (/\s/.test(char)) {
1312
+ if (started || word !== '') words.push(word);
1313
+ word = '';
1314
+ started = false;
1315
+ } else {
1316
+ word += char;
1317
+ }
1318
+ }
1319
+ if (started || word !== '') words.push(word);
1320
+ return words;
1321
+ }
1322
+
1323
+ function uvEnvironmentRelocatable(environment: string): boolean {
1324
+ const config = readFileIfPresent(path.join(environment, 'pyvenv.cfg'));
1325
+ if (config === null || !/^relocatable\s*=\s*true\s*$/m.test(config.toString('utf8'))) return false;
1326
+ const environmentReal = realpathSync(environment);
1327
+ // Whether an absolute interpreter path names an environment or a checkout, as opposed to a
1328
+ // store or system interpreter, which names neither: it is inside this environment, inside
1329
+ // another environment (its home carries pyvenv.cfg), under a checkout's .devenv state, or
1330
+ // it no longer exists at all.
1331
+ const namesEnvironment = (interpreter: string): boolean => {
1332
+ if (interpreter.startsWith(`${environment}/`) || interpreter.startsWith(`${environmentReal}/`)) return true;
1333
+ if (!existsSync(interpreter)) return true;
1334
+ if (interpreter.split(path.sep).includes('.devenv')) return true;
1335
+ const home = path.dirname(path.dirname(interpreter));
1336
+ return existsSync(path.join(home, 'pyvenv.cfg')) && home !== environment && home !== environmentReal;
1337
+ };
1338
+ const bin = path.join(environment, 'bin');
1339
+ if (existsSync(bin)) {
1340
+ const buffer = Buffer.alloc(256);
1341
+ for (const name of readdirSync(bin)) {
1342
+ const file = path.join(bin, name);
1343
+ let size: number;
1344
+ let isFile: boolean;
1345
+ let isLink = false;
1346
+ try {
1347
+ const stat = statSync(file);
1348
+ size = stat.size;
1349
+ isFile = stat.isFile();
1350
+ isLink = lstatSync(file).isSymbolicLink();
1351
+ } catch {
1352
+ return false;
1353
+ }
1354
+ if (!isFile) continue;
1355
+ if (isLink) {
1356
+ // The interpreter link itself is witnessed lexically, but only when it leaves the
1357
+ // environment: a relative alias (bin/python3 -> python) is the layout uv itself
1358
+ // writes and stays inside it. An absolute link into another checkout's .devenv
1359
+ // state names that checkout even when its final target is a store interpreter,
1360
+ // and no shebang reading ever sees it.
1361
+ let linkTarget: string;
1362
+ try {
1363
+ linkTarget = readlinkSync(file);
1364
+ } catch {
1365
+ return false;
1366
+ }
1367
+ if (path.isAbsolute(linkTarget)) {
1368
+ const lexical = path.resolve(bin, linkTarget);
1369
+ if (lexical.split(path.sep).includes('.devenv')) return false;
1370
+ const linkHome = path.dirname(path.dirname(lexical));
1371
+ if (existsSync(path.join(linkHome, 'pyvenv.cfg')) && linkHome !== environment && linkHome !== environmentReal)
1372
+ return false;
1373
+ } else {
1374
+ // A relative alias that resolves outside the environment is judged by where it
1375
+ // lands, like an absolute one; only aliases staying inside are uv's own layout.
1376
+ const lexical = path.resolve(bin, linkTarget);
1377
+ if (
1378
+ !lexical.startsWith(`${environment}${path.sep}`) &&
1379
+ !lexical.startsWith(`${environmentReal}${path.sep}`) &&
1380
+ namesEnvironment(lexical)
1381
+ )
1382
+ return false;
1383
+ }
1384
+ }
1385
+ let head: string;
1386
+ try {
1387
+ const fd = openSync(file, 'r');
1388
+ try {
1389
+ head = buffer.toString('utf8', 0, readSync(fd, buffer, 0, buffer.length, 0));
1390
+ } finally {
1391
+ closeSync(fd);
1392
+ }
1393
+ } catch {
1394
+ return false;
1395
+ }
1396
+ const shebang = /^#!\s*(\/\S+)(?:\s+(.*))?$/.exec(head.split('\n')[0] ?? '');
1397
+ if (shebang === null) continue;
1398
+ const interpreter = shebang[1] ?? '';
1399
+ const program = path.basename(interpreter);
1400
+ if (program === 'env') {
1401
+ // An env trampoline naming a bare program resolves through PATH; one naming an
1402
+ // absolute interpreter (after any flags, -S included) is judged as that interpreter.
1403
+ const tokens = shellWords(shebang[2] ?? '');
1404
+ let index = 0;
1405
+ while (index < tokens.length && tokens[index]?.startsWith('-')) index += tokens[index] === '-u' ? 2 : 1;
1406
+ const target = tokens[index];
1407
+ if (target !== undefined && target.startsWith('/') && namesEnvironment(target)) return false;
1408
+ continue;
1409
+ }
1410
+ if (['sh', 'bash', 'dash', 'zsh'].includes(program)) {
1411
+ // A relocatable wrapper execs a self-locating interpreter; a legacy one execs an
1412
+ // absolute environment path. Only exec lines are judged, tokenized as shell words
1413
+ // (quotes group, so spaced paths stay whole; a $VAR expansion is not an absolute
1414
+ // path and is never classified).
1415
+ if (size > 262_144) return false;
1416
+ let body: string;
1417
+ try {
1418
+ body = readFileSync(file, 'utf8');
1419
+ } catch {
1420
+ return false;
1421
+ }
1422
+ for (const line of body.split('\n')) {
1423
+ // Producer headers are shell words too: a uv console script's polyglot first
1424
+ // line tokenizes to the same exec argv as a plain exec line.
1425
+ const words = shellWords(line);
1426
+ if (words[0] !== 'exec') continue;
1427
+ const target = words.find((word) => word.includes('/bin/python')) ?? words[1];
1428
+ if (target !== undefined && target.startsWith('/') && namesEnvironment(target)) return false;
1429
+ }
1430
+ continue;
1431
+ }
1432
+ if (namesEnvironment(interpreter)) return false;
1433
+ }
1434
+ }
1435
+ for (const sitePackages of sitePackagesDirectories(environment)) {
1436
+ for (const name of readdirSync(sitePackages).filter((entry) => entry.endsWith('.pth'))) {
1437
+ const content = readFileSync(path.join(sitePackages, name), 'utf8');
1438
+ if (content.split('\n').some((line) => path.isAbsolute(line))) return false;
1439
+ }
1440
+ }
1441
+ return true;
1442
+ }
1443
+
1206
1444
  function uvInstaller(inputs: readonly string[], options: { locked: boolean }): Installer | null {
1207
1445
  const python = flags.python;
1208
1446
  if (python === undefined || !existsSync(path.join(projectRoot, 'pyproject.toml'))) {
@@ -1226,15 +1464,20 @@ function uvInstaller(inputs: readonly string[], options: { locked: boolean }): I
1226
1464
  uv === null ? 'uv not on PATH' : realpathSync(uv),
1227
1465
  ];
1228
1466
  return {
1229
- isCurrent: () => readInstallStamp(stampPath)?.inputs === inputsDigest(identity, inputs),
1467
+ isCurrent: () => {
1468
+ const stampOk = readInstallStamp(stampPath)?.inputs === inputsDigest(identity, inputs);
1469
+ const witnessOk = uvEnvironmentRelocatable(environment);
1470
+ return stampOk && witnessOk;
1471
+ },
1230
1472
  install: async ({ quiet }) => {
1231
1473
  // An inherited VIRTUAL_ENV names some other environment; uv would warn
1232
1474
  // that it does not match the project environment and ignore it.
1233
1475
  const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => name !== 'VIRTUAL_ENV'));
1234
- // pyvenv.cfg records `relocatable = true` for an environment uv created
1235
- // with --relocatable. uv venv refuses a directory that already holds one.
1236
- const config = readFileIfPresent(path.join(environment, 'pyvenv.cfg'));
1237
- if (config === null || !/^relocatable\s*=\s*true\s*$/m.test(config.toString('utf8'))) {
1476
+ // Replacement is gated on the same witness currentness uses, not only on the config
1477
+ // line: a valid config beside an absolute launcher or .pth would otherwise sync over
1478
+ // the escape and stamp it. uv venv refuses a directory that already holds an
1479
+ // environment, so a witness failure removes it first.
1480
+ if (!uvEnvironmentRelocatable(environment)) {
1238
1481
  rmSync(environment, { recursive: true, force: true });
1239
1482
  await runSetupCommand(`uv ${create.join(' ')}`, $`uv ${create}`.env(env), { quiet });
1240
1483
  }
@@ -1314,12 +1557,7 @@ function cargoFetcher(workspace: string): Installer {
1314
1557
  */
1315
1558
  function relativizeCheckoutPaths(environment: string): void {
1316
1559
  const root = `${realpathSync(projectRoot)}${path.sep}`;
1317
- const lib = path.join(environment, 'lib');
1318
- for (const python of existsSync(lib) ? readdirSync(lib) : []) {
1319
- const sitePackages = path.join(lib, python, 'site-packages');
1320
- if (!python.startsWith('python') || !existsSync(sitePackages)) {
1321
- continue;
1322
- }
1560
+ for (const sitePackages of sitePackagesDirectories(environment)) {
1323
1561
  const site = realpathSync(sitePackages);
1324
1562
  for (const name of readdirSync(site).filter((entry) => entry.endsWith('.pth'))) {
1325
1563
  const file = path.join(site, name);
@@ -17,3 +17,12 @@ runs:
17
17
  key: ${{ runner.os }}-${{ runner.arch }}-node-v2-{{NODE_MODULES_CACHE_KEY}}
18
18
  restore-keys: |
19
19
  ${{ runner.os }}-${{ runner.arch }}-node-v2-
20
+
21
+ # The archive holds node_modules/.smoo-install, the stamp devenv's smoo:install watches, but not the
22
+ # workspace members' own node_modules links. A restored tree is a seed: only an install in this
23
+ # checkout may claim it current. A `!node_modules/.smoo-install` path cannot do this: actions/cache
24
+ # archives a listed directory whole and drops excludes inside it (actions/toolkit#713), so Publish
25
+ # run 39143439571 still restored the stamp and skipped the install.
26
+ - name: Drop the restored install stamp
27
+ shell: bash
28
+ run: rm -f node_modules/.smoo-install