@smoothbricks/nx-plugin 0.4.25 → 0.4.27

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 (348) hide show
  1. package/README.md +1078 -98
  2. package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
  3. package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-bound-failures.js +31 -0
  5. package/dist/bin/smoo-nx-cargo-hash.js +28 -3
  6. package/dist/bin/smoo-nx-exec.js +16 -85
  7. package/dist/bin/smoo-nx-failures.d.ts +3 -0
  8. package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
  9. package/dist/bin/smoo-nx-failures.js +3 -0
  10. package/dist/bin/smoo-nx-nextest-extract.js +3 -2
  11. package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
  12. package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
  13. package/dist/bin/smoo-nx-test-walks.js +34 -0
  14. package/dist/bounded-test-policy.d.ts +7 -1
  15. package/dist/bounded-test-policy.d.ts.map +1 -1
  16. package/dist/bounded-test-policy.js +101 -40
  17. package/dist/build-state-output-policy.d.ts +25 -0
  18. package/dist/build-state-output-policy.d.ts.map +1 -0
  19. package/dist/build-state-output-policy.js +64 -0
  20. package/dist/cargo-closure-input.d.ts +16 -17
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +37 -44
  23. package/dist/cargo-dev-profile.d.ts +37 -0
  24. package/dist/cargo-dev-profile.d.ts.map +1 -0
  25. package/dist/cargo-dev-profile.js +108 -0
  26. package/dist/cargo-source-hash.d.ts +50 -8
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +691 -71
  29. package/dist/cargo-toolchain-policy.d.ts +4 -0
  30. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  31. package/dist/cargo-toolchain-policy.js +5 -0
  32. package/dist/cargo-workspace.d.ts +108 -12
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +299 -18
  35. package/dist/cross-check-policy.d.ts +21 -3
  36. package/dist/cross-check-policy.d.ts.map +1 -1
  37. package/dist/cross-check-policy.js +24 -6
  38. package/dist/disk-command-policy.d.ts +7 -0
  39. package/dist/disk-command-policy.d.ts.map +1 -0
  40. package/dist/disk-command-policy.js +98 -0
  41. package/dist/ensure-built-receipt.d.ts +25 -0
  42. package/dist/ensure-built-receipt.d.ts.map +1 -0
  43. package/dist/ensure-built-receipt.js +261 -0
  44. package/dist/ensure-built-witness.d.ts +57 -0
  45. package/dist/ensure-built-witness.d.ts.map +1 -0
  46. package/dist/ensure-built-witness.js +412 -0
  47. package/dist/ensure-built.d.ts +213 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +624 -177
  50. package/dist/eslint-file-set.d.ts +7 -0
  51. package/dist/eslint-file-set.d.ts.map +1 -0
  52. package/dist/eslint-file-set.js +12 -0
  53. package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
  54. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
  55. package/dist/executors/bounded-exec/cpu-pool.js +379 -0
  56. package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
  57. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
  58. package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
  59. package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
  60. package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/disk-lease.js +91 -0
  62. package/dist/executors/bounded-exec/executor.d.ts +45 -2
  63. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  64. package/dist/executors/bounded-exec/executor.js +447 -52
  65. package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
  66. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
  67. package/dist/executors/bounded-exec/gateway-lease.js +143 -0
  68. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
  69. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
  70. package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
  71. package/dist/executors/bounded-exec/progress.d.ts +35 -0
  72. package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
  73. package/dist/executors/bounded-exec/progress.js +79 -0
  74. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
  75. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
  76. package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
  77. package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
  78. package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
  79. package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
  80. package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
  81. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
  82. package/dist/executors/bounded-exec/ram-temp.js +1528 -0
  83. package/dist/executors/bounded-exec/schema.d.ts +29 -11
  84. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  85. package/dist/executors/bounded-exec/verdict.d.ts +162 -0
  86. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/verdict.js +318 -0
  88. package/dist/executors/shard-timings/executor.d.ts +14 -0
  89. package/dist/executors/shard-timings/executor.d.ts.map +1 -0
  90. package/dist/executors/shard-timings/executor.js +25 -0
  91. package/dist/executors/shard-timings/schema.d.ts +4 -0
  92. package/dist/executors/shard-timings/schema.d.ts.map +1 -0
  93. package/dist/executors/shard-timings/schema.js +1 -0
  94. package/dist/failure-summary.d.ts +5 -0
  95. package/dist/failure-summary.d.ts.map +1 -0
  96. package/dist/failure-summary.js +118 -0
  97. package/dist/generators/create-package/generator.d.ts.map +1 -1
  98. package/dist/generators/create-package/generator.js +6 -2
  99. package/dist/index.d.ts +14 -2
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +435 -230
  102. package/dist/managed-assets.d.ts +6 -0
  103. package/dist/managed-assets.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.d.ts +13 -0
  105. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  106. package/dist/managed-files/ci-workflow.js +44 -13
  107. package/dist/managed-files/files.d.ts +7 -1
  108. package/dist/managed-files/files.d.ts.map +1 -1
  109. package/dist/managed-files/files.js +62 -8
  110. package/dist/managed-files/json-edit.d.ts +21 -0
  111. package/dist/managed-files/json-edit.d.ts.map +1 -0
  112. package/dist/managed-files/json-edit.js +295 -0
  113. package/dist/managed-files/managed-format.d.ts +33 -1
  114. package/dist/managed-files/managed-format.d.ts.map +1 -1
  115. package/dist/managed-files/managed-format.js +82 -2
  116. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  117. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  119. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  120. package/dist/managed-files/publish-workflow.js +231 -48
  121. package/dist/managed-files/tree.d.ts +5 -0
  122. package/dist/managed-files/tree.d.ts.map +1 -1
  123. package/dist/managed-files/tree.js +5 -3
  124. package/dist/manifest-hash.js +3 -2
  125. package/dist/nextest-code-bound.d.ts +62 -0
  126. package/dist/nextest-code-bound.d.ts.map +1 -0
  127. package/dist/nextest-code-bound.js +154 -0
  128. package/dist/nextest-extraction.d.ts +24 -8
  129. package/dist/nextest-extraction.d.ts.map +1 -1
  130. package/dist/nextest-extraction.js +42 -7
  131. package/dist/package-target-policy.d.ts.map +1 -1
  132. package/dist/package-target-policy.js +3 -5
  133. package/dist/release-config-policy.d.ts.map +1 -1
  134. package/dist/release-config-policy.js +3 -5
  135. package/dist/runtime-input.d.ts +14 -0
  136. package/dist/runtime-input.d.ts.map +1 -0
  137. package/dist/runtime-input.js +15 -0
  138. package/dist/rust-tokens.d.ts +11 -0
  139. package/dist/rust-tokens.d.ts.map +1 -0
  140. package/dist/rust-tokens.js +81 -0
  141. package/dist/task-results.d.ts +6 -0
  142. package/dist/task-results.d.ts.map +1 -0
  143. package/dist/task-results.js +61 -0
  144. package/dist/test-program.d.ts +54 -0
  145. package/dist/test-program.d.ts.map +1 -0
  146. package/dist/test-program.js +292 -0
  147. package/dist/test-timings.d.ts +71 -0
  148. package/dist/test-timings.d.ts.map +1 -0
  149. package/dist/test-timings.js +393 -0
  150. package/dist/test-walk-policy.d.ts +103 -0
  151. package/dist/test-walk-policy.d.ts.map +1 -0
  152. package/dist/test-walk-policy.js +657 -0
  153. package/dist/testing.d.ts +177 -0
  154. package/dist/testing.d.ts.map +1 -0
  155. package/dist/testing.js +824 -0
  156. package/dist/type-guards.d.ts +3 -0
  157. package/dist/type-guards.d.ts.map +1 -0
  158. package/dist/type-guards.js +4 -0
  159. package/dist/typecheck-test-policy.d.ts +8 -5
  160. package/dist/typecheck-test-policy.d.ts.map +1 -1
  161. package/dist/typecheck-test-policy.js +113 -100
  162. package/dist/workspace-config-policy.d.ts +8 -0
  163. package/dist/workspace-config-policy.d.ts.map +1 -1
  164. package/dist/workspace-config-policy.js +175 -63
  165. package/dist/workspace-index.d.ts +12 -0
  166. package/dist/workspace-index.d.ts.map +1 -0
  167. package/dist/workspace-index.js +17 -0
  168. package/dist/workspace-manifest.d.ts +37 -1
  169. package/dist/workspace-manifest.d.ts.map +1 -1
  170. package/executors.json +5 -0
  171. package/loader.js +13 -11
  172. package/managed/raw/envrc +33 -62
  173. package/managed/raw/git-format-staged.yml +30 -35
  174. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  175. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  176. package/managed/raw/tooling/direnv/developer-links.ts +607 -17
  177. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -92
  178. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  179. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  180. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  181. package/managed/raw/tooling/direnv/repo-path +14 -2
  182. package/managed/raw/tooling/direnv/setup-environment.ts +1009 -118
  183. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  184. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  185. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  186. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  187. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  188. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  189. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  190. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  191. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  192. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  194. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  196. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  197. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  198. package/managed/templates/github/workflows/managed-files.yml +1 -2
  199. package/nextest.toml +8 -75
  200. package/package.json +75 -13
  201. package/runtime-input.sh +81 -0
  202. package/src/__tests__/counted-cargo-process.ts +286 -0
  203. package/src/__tests__/counted-cargo.ts +730 -0
  204. package/src/__tests__/fixture-cargo-home.ts +36 -0
  205. package/src/__tests__/fixture-nx-env.ts +157 -2
  206. package/src/__tests__/progress-clock.ts +43 -0
  207. package/src/__tests__/runtime-input.ts +23 -0
  208. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  209. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  210. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  211. package/src/bin/smoo-nx-exec.ts +17 -86
  212. package/src/bin/smoo-nx-failures.ts +4 -0
  213. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  214. package/src/bin/smoo-nx-test-walks.ts +42 -0
  215. package/src/bounded-test-policy.test.ts +268 -22
  216. package/src/bounded-test-policy.ts +114 -42
  217. package/src/build-state-output-policy.test.ts +115 -0
  218. package/src/build-state-output-policy.ts +90 -0
  219. package/src/bun-bootstrap.test.ts +57 -3
  220. package/src/bun-file-packages.test.ts +47 -0
  221. package/src/cargo-closure-input.test.ts +432 -62
  222. package/src/cargo-closure-input.ts +51 -57
  223. package/src/cargo-dev-profile.ts +137 -0
  224. package/src/cargo-resolve-cache.test.ts +1044 -0
  225. package/src/cargo-source-hash.test.ts +203 -93
  226. package/src/cargo-source-hash.ts +762 -75
  227. package/src/cargo-toolchain-policy.ts +6 -0
  228. package/src/cargo-workspace.test.ts +320 -0
  229. package/src/cargo-workspace.ts +344 -18
  230. package/src/cross-check-policy.test.ts +59 -1
  231. package/src/cross-check-policy.ts +25 -6
  232. package/src/disk-command-policy.test.ts +61 -0
  233. package/src/disk-command-policy.ts +107 -0
  234. package/src/ensure-built-receipt.test.ts +184 -0
  235. package/src/ensure-built-receipt.ts +263 -0
  236. package/src/ensure-built-witness.test.ts +344 -0
  237. package/src/ensure-built-witness.ts +252 -0
  238. package/src/ensure-built.test.ts +1043 -483
  239. package/src/ensure-built.ts +870 -197
  240. package/src/eslint-file-set.ts +9 -0
  241. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  242. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  243. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  244. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  245. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  246. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  247. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  248. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  249. package/src/executors/bounded-exec/executor.ts +565 -67
  250. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  251. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  252. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  253. package/src/executors/bounded-exec/progress.test.ts +79 -0
  254. package/src/executors/bounded-exec/progress.ts +118 -0
  255. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  256. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  257. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  258. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  259. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  260. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  261. package/src/executors/bounded-exec/schema.json +11 -5
  262. package/src/executors/bounded-exec/schema.ts +27 -11
  263. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  264. package/src/executors/bounded-exec/verdict.ts +433 -0
  265. package/src/executors/shard-timings/executor.ts +36 -0
  266. package/src/executors/shard-timings/schema.json +21 -0
  267. package/src/executors/shard-timings/schema.ts +4 -0
  268. package/src/failure-summary.ts +119 -0
  269. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  270. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  271. package/src/generators/create-package/generator.test.ts +6 -0
  272. package/src/generators/create-package/generator.ts +6 -2
  273. package/src/host-controller-wiring.test.ts +31 -0
  274. package/src/index.test.ts +1070 -360
  275. package/src/index.ts +524 -255
  276. package/src/linked-snapshots.nx.test.ts +172 -0
  277. package/src/managed-assets.ts +12 -0
  278. package/src/managed-files/ci-workflow.ts +54 -13
  279. package/src/managed-files/files.ts +65 -9
  280. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  281. package/src/managed-files/generator.test.ts +60 -1
  282. package/src/managed-files/json-edit.test.ts +216 -0
  283. package/src/managed-files/json-edit.ts +290 -0
  284. package/src/managed-files/managed-format-json.test.ts +308 -0
  285. package/src/managed-files/managed-format.ts +90 -2
  286. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  287. package/src/managed-files/publish-workflow.ts +285 -67
  288. package/src/managed-files/repo-path.test.ts +129 -0
  289. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  290. package/src/managed-files/tree.ts +11 -3
  291. package/src/manifest-hash.ts +3 -2
  292. package/src/nextest-code-bound.test.ts +155 -0
  293. package/src/nextest-code-bound.ts +195 -0
  294. package/src/nextest-extraction.test.ts +37 -5
  295. package/src/nextest-extraction.ts +51 -9
  296. package/src/nextest-partitions.test.ts +495 -0
  297. package/src/nx-cache-bound.test.ts +109 -0
  298. package/src/nx-cache-restore-mtime.test.ts +170 -0
  299. package/src/nx-daemon-claim.test.ts +277 -0
  300. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  301. package/src/nx-daemon-idle-policy.test.ts +51 -0
  302. package/src/nx-daemon-ignore-files.test.ts +210 -0
  303. package/src/nx-daemon-root-normalization.test.ts +105 -0
  304. package/src/nx-daemon-validity.test.ts +925 -0
  305. package/src/nx-dot-input-policy.test.ts +353 -0
  306. package/src/nx-failure-life-cycles.test.ts +207 -0
  307. package/src/nx-files-archive-racy.test.ts +97 -0
  308. package/src/nx-foreign-workspace-env.test.ts +329 -0
  309. package/src/nx-incomplete-run-summary.test.ts +70 -0
  310. package/src/nx-plugin-load-errors.test.ts +120 -0
  311. package/src/nx-project-resolution-cache.test.ts +176 -0
  312. package/src/nx-raw-inputs.test.ts +166 -0
  313. package/src/nx-resident-graph-workers.test.ts +112 -0
  314. package/src/nx-run-information.test.ts +118 -0
  315. package/src/nx-skipped-task-exit.test.ts +180 -0
  316. package/src/nx-state-location.test.ts +63 -0
  317. package/src/nx-store-resolution.test.ts +99 -0
  318. package/src/nx-task-graph-cycles.test.ts +148 -0
  319. package/src/nx-task-output-lease.test.ts +414 -0
  320. package/src/nx-use-local.test.ts +103 -0
  321. package/src/package-target-policy.ts +3 -6
  322. package/src/release-config-policy.ts +3 -6
  323. package/src/runtime-input.test.ts +215 -0
  324. package/src/runtime-input.ts +17 -0
  325. package/src/rust-tokens.ts +78 -0
  326. package/src/serial-group-wiring.test.ts +326 -0
  327. package/src/task-history-namespace.test.ts +181 -0
  328. package/src/task-results.test.ts +226 -0
  329. package/src/task-results.ts +75 -0
  330. package/src/test-program.test.ts +303 -0
  331. package/src/test-program.ts +341 -0
  332. package/src/test-timings.test.ts +240 -0
  333. package/src/test-timings.ts +311 -0
  334. package/src/test-walk-policy.test.ts +585 -0
  335. package/src/test-walk-policy.ts +731 -0
  336. package/src/testing.test.ts +509 -0
  337. package/src/testing.ts +922 -0
  338. package/src/type-guards.ts +4 -0
  339. package/src/typecheck-test-policy.test.ts +496 -11
  340. package/src/typecheck-test-policy.ts +137 -103
  341. package/src/workspace-config-policy.test.ts +212 -13
  342. package/src/workspace-config-policy.ts +101 -73
  343. package/src/workspace-index.ts +18 -0
  344. package/src/workspace-manifest.ts +41 -1
  345. package/test-timings.json +85 -28
  346. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -615
  347. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
  348. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -1,8 +1,12 @@
1
- import { spawn } from 'node:child_process';
2
- import { randomUUID } from 'node:crypto';
3
- import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync } from 'node:fs';
1
+ import { spawn, spawnSync } from 'node:child_process';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
+ import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync, realpathSync, statSync, } from 'node:fs';
4
4
  import { createRequire } from 'node:module';
5
- import { isAbsolute, join, resolve } from 'node:path';
5
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
6
+ import { inspect } from 'node:util';
7
+ import { boundedReceiptRuntimes, providerReceipt } from './ensure-built-receipt.js';
8
+ import { graphInstallationIdentity, nodeStamp, nodesUnchanged, readArtifactWitness, readGraphWitness, reusableGraph, snapshotsEqual, workspaceSnapshot, writeWitness, } from './ensure-built-witness.js';
9
+ import { managedAssetsRoot } from './managed-assets.js';
6
10
  function createWorkspaceNxRequire(workspaceRoot) {
7
11
  const requireFromWorkspace = createRequire(join(workspaceRoot, 'package.json'));
8
12
  return function requireNx(specifier) {
@@ -36,14 +40,80 @@ export function describeMiss(reason) {
36
40
  return `${reason.taskId} last failed with exit code ${reason.code}`;
37
41
  case 'stale-outputs':
38
42
  return `${reason.taskId} outputs are missing or modified on disk`;
39
- case 'stale-inputs':
40
- return `${reason.taskId} inputs changed since the Nx daemon last looked`;
41
43
  case 'cache-disabled':
42
44
  return `the Nx cache is disabled, so ${reason.taskId} must run`;
43
45
  case 'no-daemon':
44
46
  return `the Nx daemon is disabled, so ${reason.taskId} cannot be checked without running it`;
45
47
  }
46
48
  }
49
+ /**
50
+ * Read `smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>]
51
+ * [-- <binary> [args...]]`. A flag may be spelled `--flag value` or
52
+ * `--flag=value`. Without `--` there is nothing to exec: the wrapper builds
53
+ * what is stale and exits, so a caller that only needs the target current pays
54
+ * the probe, not an Nx CLI start that replays every cached task's log.
55
+ */
56
+ export function parseExecArguments(argv) {
57
+ const separator = argv.indexOf('--');
58
+ const command = separator === -1 ? [] : argv.slice(separator + 1);
59
+ if (separator !== -1 && command.length === 0) {
60
+ return { ok: false, usage: 'no binary given after `--`' };
61
+ }
62
+ let target;
63
+ let workspaceRoot;
64
+ // Only leading `--` forms are split at `=`: a value may legitimately contain one.
65
+ const tokens = (separator === -1 ? argv : argv.slice(0, separator)).flatMap((arg) => {
66
+ const equals = arg.startsWith('--') ? arg.indexOf('=') : -1;
67
+ return equals === -1 ? [arg] : [arg.slice(0, equals), arg.slice(equals + 1)];
68
+ });
69
+ for (let index = 0; index < tokens.length; index += 1) {
70
+ const token = tokens[index];
71
+ if (token === '--workspace-root') {
72
+ index += 1;
73
+ if (index === tokens.length) {
74
+ return { ok: false, usage: '--workspace-root needs a directory' };
75
+ }
76
+ workspaceRoot = tokens[index];
77
+ }
78
+ else if (token.startsWith('-')) {
79
+ return { ok: false, usage: `unknown flag ${token}` };
80
+ }
81
+ else if (target !== undefined) {
82
+ return { ok: false, usage: `more than one target given: ${target} and ${token}` };
83
+ }
84
+ else {
85
+ target = token;
86
+ }
87
+ }
88
+ if (target === undefined) {
89
+ return { ok: false, usage: 'no project:target given' };
90
+ }
91
+ if (parseTargetSelector(target) === null) {
92
+ return { ok: false, usage: `'${target}' is not a project:target[:configuration] selector` };
93
+ }
94
+ // `execve` does no PATH lookup, so a bare name would fail as a missing file in
95
+ // the current directory. Say which mistake was made instead.
96
+ if (command.length > 0 && !command[0].includes('/')) {
97
+ return { ok: false, usage: `'${command[0]}' must be a path to the binary, not a name to look up on PATH` };
98
+ }
99
+ return { ok: true, invocation: { target, workspaceRoot, command } };
100
+ }
101
+ /**
102
+ * The message of whatever `ensureBuilt` rejected with. Nx rejects with plain
103
+ * objects as well as errors (its daemon client's `{ stack, message }`), and
104
+ * `String()` of one is `[object Object]`.
105
+ */
106
+ export function describeError(error) {
107
+ if (error instanceof Error) {
108
+ return error.message;
109
+ }
110
+ if (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string') {
111
+ return error.message;
112
+ }
113
+ return typeof error === 'string'
114
+ ? error
115
+ : inspect(error, { breakLength: Number.POSITIVE_INFINITY, colors: false, depth: 5 });
116
+ }
47
117
  /**
48
118
  * First reason the task set cannot be replayed, considering only recorded
49
119
  * results — no filesystem access. `null` means every task is a recorded
@@ -100,130 +170,433 @@ export function unvouched(entries, verdicts) {
100
170
  * the workspace's own `nx` CLI unconditionally.
101
171
  */
102
172
  export async function ensureBuilt(options) {
173
+ return withWorkspaceBinding(options, ensureBuiltInWorkspace);
174
+ }
175
+ /** Read current producer evidence without running tasks or writing producer outputs. */
176
+ export async function probeBuilt(options) {
177
+ return withWorkspaceBinding(options, probeBuiltInWorkspace);
178
+ }
179
+ async function withWorkspaceBinding(options, operation) {
103
180
  const selector = parseTargetSelector(options.target);
104
181
  if (!selector) {
105
182
  throw new Error(`ensureBuilt: '${options.target}' is not a project:target[:configuration] selector`);
106
183
  }
107
- // Resolved before the chdir below, so a relative `cwd` still means what the
108
- // caller meant.
184
+ // Resolve before chdir so a relative cwd still means what the caller meant.
109
185
  const workspaceRoot = resolve(options.cwd);
110
186
  const callerCwd = process.cwd();
111
187
  const callerEnv = { ...process.env };
188
+ const callerOwnsRoot = callerOwnsWorkspace(callerEnv, callerCwd, workspaceRoot);
112
189
  try {
113
- // `cd <root> && nx run ...` is the invocation whose hashes are in the
114
- // cache. Everything below — plugin hooks, `runtime` inputs, and the hasher,
115
- // which keys against `process.cwd()` — has to see the same directory or the
116
- // probe keys differently from the CLI and never hits.
190
+ // Nx's hooks, runtime inputs and hasher see the provider's own workspace.
117
191
  process.chdir(workspaceRoot);
118
- return await ensureBuiltInWorkspace(workspaceRoot, selector, options.onMiss ?? 'stream');
192
+ const environment = workspaceEnvironment(callerEnv, workspaceRoot, callerOwnsRoot);
193
+ if (!callerOwnsRoot) {
194
+ const binding = bindWorkspaceSocketDirectory(workspaceRoot, selectorTaskId(selector), environment);
195
+ if (binding !== undefined) {
196
+ if (typeof binding !== 'string') {
197
+ process.stderr.write(`ensureBuilt: ${binding.reason.workspaceRoot}: ${binding.reason.detail}\n`);
198
+ return binding;
199
+ }
200
+ environment.NX_SOCKET_DIR = binding;
201
+ }
202
+ }
203
+ assignEnvironment(process.env, environment);
204
+ return await operation(workspaceRoot, selector);
119
205
  }
120
206
  finally {
121
- // Nx configures itself through the environment and this function runs
122
- // in-process, so without this the caller — and anything it goes on to exec
123
- // — would inherit NX_WORKSPACE_ROOT_PATH, the stream/prefix flags, and
124
- // whatever a plugin's preTasksExecution hook injected. The wrapper this
125
- // replaces got that isolation for free by running Nx in a child.
207
+ // Never lend this provider's cwd, Nx binding or hook-injected environment
208
+ // to the caller or whatever it executes after this operation.
126
209
  process.chdir(callerCwd);
127
- for (const key of Object.keys(process.env)) {
128
- if (!(key in callerEnv)) {
129
- delete process.env[key];
130
- }
210
+ assignEnvironment(process.env, callerEnv);
211
+ }
212
+ }
213
+ /**
214
+ * Nx's own overrides for where one workspace keeps its daemon socket, daemon
215
+ * record, task database and cache. Each names the state of a single
216
+ * workspace, and Nx takes it literally: a daemon spawned for another root
217
+ * with `NX_SOCKET_DIR` inherited listens on, and on stop deletes, the socket
218
+ * directory of the workspace that exported it; with
219
+ * `NX_WORKSPACE_DATA_DIRECTORY` it reads and overwrites that workspace's
220
+ * daemon record and task database; and the cache has to move with the
221
+ * database or a hit names artifacts the run never wrote.
222
+ */
223
+ const WORKSPACE_STATE_ENV_KEYS = [
224
+ 'NX_SOCKET_DIR',
225
+ 'NX_DAEMON_SOCKET_DIR',
226
+ 'NX_WORKSPACE_DATA_DIRECTORY',
227
+ 'NX_CACHE_DIRECTORY',
228
+ ];
229
+ /** A socket binding refusal is the existing failed outcome, including the owner's real exit. */
230
+ function workspaceSocketRefusal(workspaceRoot, taskId, detail, exitCode, signal) {
231
+ return {
232
+ disposition: 'failed',
233
+ reason: { kind: 'workspace-socket', taskId, workspaceRoot, detail },
234
+ exitCode,
235
+ signal,
236
+ };
237
+ }
238
+ /**
239
+ * A foreign-root call inside a managed shell asks the same packaged socket
240
+ * owner that shell uses. The owner reads the provider's binding or derives a
241
+ * namespace from its canonical checkout spelling under DEVENV_RUNTIME.
242
+ * Outside a managed shell Nx keeps its normal defaults. No caller socket is reused.
243
+ */
244
+ function bindWorkspaceSocketDirectory(workspaceRoot, taskId, environment) {
245
+ const runtime = environment.DEVENV_RUNTIME;
246
+ if (runtime === undefined || runtime === '')
247
+ return undefined;
248
+ if (!isAbsolute(runtime))
249
+ return workspaceSocketRefusal(workspaceRoot, taskId, 'DEVENV_RUNTIME must name the managed shell runtime directory', 1, null);
250
+ const script = join(managedAssetsRoot, 'raw/tooling/direnv/nx-socket-dir.sh');
251
+ const result = spawnSync('/bin/sh', ['-c', '. "$1" && printf %s "$NX_SOCKET_DIR"', 'sh', script], {
252
+ cwd: workspaceRoot,
253
+ env: { ...environment, PWD: workspaceRoot },
254
+ encoding: 'utf8',
255
+ });
256
+ if (result.error !== undefined)
257
+ return workspaceSocketRefusal(workspaceRoot, taskId, `socket owner could not start: ${result.error.message}; stdout: ${result.stdout}; stderr: ${result.stderr}`, 1, null);
258
+ if (result.status !== 0) {
259
+ return workspaceSocketRefusal(workspaceRoot, taskId, `socket owner ended ${result.signal === null ? `with exit ${result.status}` : `by ${result.signal}`}: ${result.stderr}; stdout: ${result.stdout}`, result.status ?? 1, result.signal);
260
+ }
261
+ const directory = result.stdout;
262
+ if (directory === '' || !isAbsolute(directory))
263
+ return workspaceSocketRefusal(workspaceRoot, taskId, `socket owner returned no absolute directory: ${directory}; stderr: ${result.stderr}`, 1, null);
264
+ try {
265
+ if (!statSync(directory).isDirectory())
266
+ return workspaceSocketRefusal(workspaceRoot, taskId, `socket owner returned a non-directory: ${directory}; stderr: ${result.stderr}`, 1, null);
267
+ }
268
+ catch (error) {
269
+ return workspaceSocketRefusal(workspaceRoot, taskId, `socket owner returned an unavailable directory ${directory}: ${error instanceof Error ? error.message : String(error)}; stderr: ${result.stderr}`, 1, null);
270
+ }
271
+ if (result.stderr !== '')
272
+ process.stderr.write(result.stderr);
273
+ return directory;
274
+ }
275
+ /**
276
+ * The nearest directory at or above `from` holding an `nx.json`, or null.
277
+ * The marker this wrapper roots a workspace at when `--workspace-root` is
278
+ * not given.
279
+ */
280
+ export function findNxWorkspaceRoot(from) {
281
+ let directory = from;
282
+ for (;;) {
283
+ if (existsSync(join(directory, 'nx.json'))) {
284
+ return directory;
131
285
  }
132
- for (const [key, value] of Object.entries(callerEnv)) {
133
- if (process.env[key] !== value) {
134
- process.env[key] = value;
135
- }
286
+ const parent = dirname(directory);
287
+ if (parent === directory) {
288
+ return null;
289
+ }
290
+ directory = parent;
291
+ }
292
+ }
293
+ function isSameDirectory(left, right) {
294
+ try {
295
+ return realpathSync(left) === realpathSync(right);
296
+ }
297
+ catch {
298
+ // A path that cannot be resolved cannot be shown to be the same
299
+ // workspace, and "not the same" is the isolating answer.
300
+ return false;
301
+ }
302
+ }
303
+ /**
304
+ * Whether the environment a caller was launched with belongs to
305
+ * `workspaceRoot`. The caller's workspace is the one its own Nx would bind:
306
+ * `NX_WORKSPACE_ROOT_PATH` when set (relative to where it stands), else the
307
+ * nearest `nx.json` at or above where it stands — none, when it stands in no
308
+ * workspace.
309
+ */
310
+ export function callerOwnsWorkspace(callerEnv, callerCwd, workspaceRoot) {
311
+ const callerRoot = callerEnv.NX_WORKSPACE_ROOT_PATH
312
+ ? resolve(callerCwd, callerEnv.NX_WORKSPACE_ROOT_PATH)
313
+ : findNxWorkspaceRoot(callerCwd);
314
+ return callerRoot !== null && isSameDirectory(callerRoot, workspaceRoot);
315
+ }
316
+ /**
317
+ * The environment every Nx this call starts runs under — the daemon client
318
+ * loaded below and the `nx` CLI a miss spawns: the caller's, pointed at
319
+ * `workspaceRoot`, with that workspace's state locations. A caller that was set
320
+ * up for this same root keeps its overrides: they are the owner's cache and
321
+ * sandbox boundary. A caller set up for another root, or for none, passes none
322
+ * of them on, so Nx falls back to the per-root defaults. Nothing is reset or
323
+ * bypassed: the root's own daemon and cache are used as they stand. Every one of
324
+ * them answers from the root's installed Nx (`NX_USE_LOCAL=true`): a daemon
325
+ * otherwise installs `nx@latest` to answer Nx Console's status, and a client
326
+ * without the variable recomputes the daemon's graph.
327
+ */
328
+ export function workspaceEnvironment(callerEnv, workspaceRoot, callerOwnsRoot) {
329
+ const env = { ...callerEnv, NX_WORKSPACE_ROOT_PATH: workspaceRoot, NX_USE_LOCAL: 'true' };
330
+ if (!callerOwnsRoot) {
331
+ for (const key of WORKSPACE_STATE_ENV_KEYS) {
332
+ delete env[key];
333
+ }
334
+ }
335
+ return env;
336
+ }
337
+ /**
338
+ * Make `target` hold exactly the variables of `env`: each one missing from
339
+ * `env` removed, each one different set. Nx reads its configuration from
340
+ * `process.env` in this process, so that is where an environment is put in
341
+ * place, and where the caller's is put back.
342
+ */
343
+ export function assignEnvironment(target, env) {
344
+ for (const key of Object.keys(target)) {
345
+ if (env[key] === undefined) {
346
+ delete target[key];
347
+ }
348
+ }
349
+ for (const [key, value] of Object.entries(env)) {
350
+ if (value !== undefined && target[key] !== value) {
351
+ target[key] = value;
136
352
  }
137
353
  }
138
354
  }
139
- async function ensureBuiltInWorkspace(workspaceRoot, selector, onMiss) {
140
- // The environment has precedence over nx.json. Avoid loading Nx at all on
141
- // this explicit fallback path, which also lets a deliberately minimal
142
- // checkout-local CLI stand in for Nx.
355
+ /**
356
+ * An outer Nx task exports its own cache-bypass setting to the process it
357
+ * launches. That setting describes the outer task, not the nested target:
358
+ * inheriting it makes the inner runner refuse cache writes forever. Outside an
359
+ * Nx task child, however, the same variables are the caller's explicit request
360
+ * to force this target to rebuild and remain authoritative.
361
+ */
362
+ export function withoutOuterTaskCacheBypass(env) {
363
+ const nested = { ...env };
364
+ if (nested.NX_TASK_TARGET_PROJECT !== undefined || nested.NX_TASK_TARGET_TARGET !== undefined) {
365
+ delete nested.NX_SKIP_NX_CACHE;
366
+ delete nested.NX_DISABLE_NX_CACHE;
367
+ }
368
+ return nested;
369
+ }
370
+ /**
371
+ * A caller-requested cache bypass is the only miss known before hashing.
372
+ * Workspace changes first refresh the authoritative graph; the probe then
373
+ * decides whether these current inputs have matching artifacts.
374
+ */
375
+ export function missBeforeProbe(env, taskId) {
376
+ if (env.NX_SKIP_NX_CACHE === 'true' || env.NX_DISABLE_NX_CACHE === 'true') {
377
+ return { kind: 'cache-disabled', taskId };
378
+ }
379
+ return null;
380
+ }
381
+ async function prepareWorkspaceProbe(workspaceRoot, selector) {
382
+ const taskId = selectorTaskId(selector);
383
+ // This explicit fallback path loads no Nx. The build operation may run the
384
+ // checkout-local CLI; the read-only operation can only report the miss.
143
385
  if (process.env.NX_DAEMON === 'false') {
144
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
386
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
145
387
  }
146
- // Nx rejects daemon access when the client package's version differs from
147
- // the workspace's running daemon. Resolve every runtime module from the
148
- // target checkout rather than this package's own dependency: a reusable
149
- // wrapper must keep using the daemon after the workspace takes an Nx patch.
388
+ // Runtime modules always belong to the provider's installed Nx version.
150
389
  const requireNx = createWorkspaceNxRequire(workspaceRoot);
151
390
  bindWorkspaceRoot(workspaceRoot, requireNx);
152
391
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
153
392
  if (!daemonClient.enabled()) {
154
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
155
- }
156
- // An outer Nx task exports its own cache-bypass setting to the process it
157
- // launches. That setting describes the outer task, not the nested target:
158
- // inheriting it makes the inner runner refuse cache writes forever. Outside
159
- // an Nx task child, however, the same variables are the caller's explicit
160
- // request to force this target to rebuild and must remain authoritative.
161
- if (process.env.NX_TASK_TARGET_PROJECT !== undefined || process.env.NX_TASK_TARGET_TARGET !== undefined) {
162
- delete process.env.NX_SKIP_NX_CACHE;
163
- delete process.env.NX_DISABLE_NX_CACHE;
393
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
164
394
  }
395
+ assignEnvironment(process.env, withoutOuterTaskCacheBypass(process.env));
165
396
  const { readNxJson } = requireNx('nx/src/config/nx-json');
166
397
  const { splitArgsIntoNxArgsAndOverrides } = requireNx('nx/src/utils/command-line-utils');
167
398
  const { setEnvVarsBasedOnArgs } = requireNx('nx/src/tasks-runner/run-command');
168
399
  const { createTaskGraph } = requireNx('nx/src/tasks-runner/create-task-graph');
169
- const hooks = requireNx('nx/src/project-graph/plugins/tasks-execution-hooks');
170
- const nxJson = readNxJson();
171
- // Reproduce `nx run <selector> --outputStyle=<style>` exactly. Task hashes
172
- // are derived from these arguments, so anything hand-rolled here instead of
173
- // routed through Nx's own argument normalization would key the cache
174
- // differently from the CLI and turn every probe into a miss.
175
- const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides({ targets: [selector.target], configuration: selector.configuration, outputStyle: onMiss }, 'run-one', { printWarnings: false }, nxJson);
400
+ let nxJson = readNxJson();
401
+ // Both operations key exactly like nx run <selector> --outputStyle=stream.
402
+ const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides({ targets: [selector.target], configuration: selector.configuration, outputStyle: 'stream' }, 'run-one', { printWarnings: false }, nxJson);
176
403
  const loadDotEnvFiles = process.env.NX_LOAD_DOT_ENV_FILES !== 'false';
177
- // Sets NX_LOAD_DOT_ENV_FILES and the stream/prefix flags, which the hasher
178
- // reads through each task's environment. It has to happen before hashing or
179
- // the probe keys differently from the CLI.
180
404
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
181
405
  performance.mark('ensureBuilt:graph:start');
182
- const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
406
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx);
407
+ nxJson = readNxJson();
183
408
  requireProject(projectGraph, selector.project);
184
409
  const taskGraph = createTaskGraph(projectGraph, {}, [selector.project], [selector.target], selector.configuration, overrides, false);
185
410
  const tasks = Object.values(taskGraph.tasks);
186
411
  performance.measure('ensureBuilt:graph', 'ensureBuilt:graph:start');
187
- const inputsChanged = await refreshWorkspaceContext(workspaceRoot, tasks, requireNx);
412
+ return { kind: 'ready', nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx };
413
+ }
414
+ async function withTaskHooks(workspaceRoot, selector, prepared, operation) {
415
+ const hooks = prepared.requireNx('nx/src/project-graph/plugins/tasks-execution-hooks');
188
416
  const runId = randomUUID();
189
417
  const startTime = Date.now();
190
- // Plugin `preTasksExecution` hooks inject environment variables, and declared
191
- // `env` inputs hash against them. Skipping the hook would not merely cost
192
- // hits: the run below would then execute with a different environment than
193
- // the CLI does, record a hash the CLI never looks up, and thrash the cache.
418
+ // Hook-injected env participates in hashing for both real operations.
194
419
  await hooks.runPreTasksExecution({
195
420
  id: runId,
196
421
  workspaceRoot,
197
- nxJsonConfiguration: nxJson,
422
+ nxJsonConfiguration: prepared.nxJson,
198
423
  argv: process.argv,
199
424
  });
200
- performance.mark('ensureBuilt:probe:start');
201
- let reason;
202
- if (process.env.NX_SKIP_NX_CACHE === 'true' || process.env.NX_DISABLE_NX_CACHE === 'true') {
203
- reason = { kind: 'cache-disabled', taskId: selectorTaskId(selector) };
204
- }
205
- else if (inputsChanged) {
206
- reason = { kind: 'stale-inputs', taskId: selectorTaskId(selector) };
207
- }
208
- else {
209
- reason = await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx);
210
- }
211
- performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
212
- // The CLI runs the hooks around its own run, under its own id. This process
213
- // ran none of the tasks, so it reports none for the id it opened.
214
- const result = reason === null ? HIT : await runViaCli(workspaceRoot, selector, reason);
425
+ const result = await operation(workspaceRoot, selector, prepared);
426
+ // Any runner owns its own hook id; this process executed no tasks.
215
427
  await hooks.runPostTasksExecution({
216
428
  id: runId,
217
429
  taskResults: NO_TASK_RESULTS,
218
430
  workspaceRoot,
219
- nxJsonConfiguration: nxJson,
431
+ nxJsonConfiguration: prepared.nxJson,
220
432
  argv: process.argv,
221
433
  startTime,
222
434
  endTime: Date.now(),
223
435
  });
224
436
  return result;
225
437
  }
226
- function selectorTaskId(selector) {
438
+ async function ensureBuiltInWorkspace(workspaceRoot, selector) {
439
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
440
+ if (prepared.kind === 'no-daemon')
441
+ return runViaCli(workspaceRoot, selector, prepared.reason);
442
+ return withTaskHooks(workspaceRoot, selector, prepared, buildPreparedTarget);
443
+ }
444
+ async function buildPreparedTarget(workspaceRoot, selector, prepared) {
445
+ const { nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx } = prepared;
446
+ performance.mark('ensureBuilt:probe:start');
447
+ const reason = missBeforeProbe(process.env, selectorTaskId(selector)) ??
448
+ (await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx));
449
+ performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
450
+ return reason === null ? HIT : runViaCli(workspaceRoot, selector, reason);
451
+ }
452
+ async function probeBuiltInWorkspace(workspaceRoot, selector) {
453
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
454
+ if (prepared.kind === 'no-daemon')
455
+ return { disposition: 'miss', reason: prepared.reason };
456
+ const probeSource = captureProbeSource(workspaceRoot, prepared.requireNx);
457
+ return withTaskHooks(workspaceRoot, selector, prepared, async (root, selected, current) => {
458
+ const { nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx } = current;
459
+ const taskId = selectorTaskId(selected);
460
+ performance.mark('ensureBuilt:probe:start');
461
+ const reason = missBeforeProbe(process.env, taskId) ??
462
+ (await probe(root, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx, true));
463
+ performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
464
+ if (reason !== null)
465
+ return { disposition: 'miss', reason };
466
+ let result = providerReceipt(root, taskId, tasks, providerInputProof(root, nxJson, projectGraph, taskGraph, requireNx), requireNx('nx/src/native').expandOutputs);
467
+ if (result.disposition === 'hit') {
468
+ const after = captureProbeSource(root, requireNx);
469
+ if (!snapshotsEqual(probeSource, after) || !nodesUnchanged(probeSource.nodes)) {
470
+ result = {
471
+ disposition: 'failed',
472
+ reason: {
473
+ kind: 'provider-proof',
474
+ taskId,
475
+ workspaceRoot: root,
476
+ detail: 'Provider source nodes or membership moved across Nx hashing/receipt capture',
477
+ },
478
+ exitCode: 1,
479
+ signal: null,
480
+ };
481
+ }
482
+ }
483
+ if (result.disposition === 'failed') {
484
+ process.stderr.write(`probeBuilt: ${result.reason.workspaceRoot}: ${result.reason.detail}\n`);
485
+ }
486
+ return result;
487
+ });
488
+ }
489
+ function captureProbeSource(workspaceRoot, requireNx) {
490
+ const { WorkspaceContext } = requireNx('nx/src/native');
491
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
492
+ const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
493
+ const previous = readGraphWitness(join(data, 'ensure-built-graph.json'));
494
+ return workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), previous?.snapshot);
495
+ }
496
+ /** Nx's native plan is the FILE selection oracle, not a second glob planner.
497
+ * Config identities skipped by the inspector are included explicitly. A
498
+ * runtime identity is bounded only by the owning toolchain recipe + a declared
499
+ * pin; opaque commands and unavailable native proof categorically refuse.
500
+ */
501
+ function providerInputProof(workspaceRoot, nxJson, projectGraph, taskGraph, requireNx) {
502
+ const { WorkspaceContext, HashPlanInspector, HashPlanner, transferProjectGraph } = requireNx('nx/src/native');
503
+ const { transformProjectGraphForRust } = requireNx('nx/src/native/transform-objects');
504
+ const { createProjectRootMappings } = requireNx('nx/src/project-graph/utils/find-project-for-path');
505
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
506
+ const context = new WorkspaceContext(workspaceRoot, workspaceDataDirectoryForWorkspace(workspaceRoot));
507
+ const { externalReferences } = context.getWorkspaceFiles(Object.fromEntries(createProjectRootMappings(projectGraph.nodes)));
508
+ if (externalReferences === undefined) {
509
+ return {
510
+ kind: 'conservative-provider-source',
511
+ files: [],
512
+ inputs: {},
513
+ runtimeSource: { kind: 'opaque', detail: 'Nx supplied no native file-input proof' },
514
+ };
515
+ }
516
+ const planner = new HashPlanner(nxJson, transferProjectGraph(transformProjectGraphForRust(projectGraph)));
517
+ const inspector = new HashPlanInspector(externalReferences.allWorkspaceFiles, externalReferences.projectFiles, workspaceRoot);
518
+ const inputs = inspector.inspectInputs(planner.getPlansReference(Object.keys(taskGraph.tasks), taskGraph));
519
+ const files = new Set();
520
+ const directories = new Set();
521
+ const projects = new Set();
522
+ function includeProject(name) {
523
+ if (projects.has(name))
524
+ return;
525
+ projects.add(name);
526
+ const node = projectGraph.nodes[name];
527
+ if (node === undefined)
528
+ return;
529
+ directories.add(node.data.root);
530
+ for (const file of ['project.json', 'package.json']) {
531
+ const path = join(node.data.root, file);
532
+ if (existsSync(join(workspaceRoot, path)))
533
+ files.add(path);
534
+ }
535
+ for (const dependency of projectGraph.dependencies[name] ?? [])
536
+ includeProject(dependency.target);
537
+ }
538
+ let conservative = false;
539
+ const projectConfigs = Object.fromEntries(Object.entries(projectGraph.nodes).map(([name, node]) => [name, node.data]));
540
+ const { getCustomHasher } = requireNx('nx/src/tasks-runner/utils');
541
+ for (const task of Object.values(taskGraph.tasks)) {
542
+ const input = inputs[task.id];
543
+ if (input === undefined || getCustomHasher(task, projectConfigs) !== null) {
544
+ return {
545
+ kind: 'conservative-provider-source',
546
+ files: [...files],
547
+ inputs,
548
+ runtimeSource: { kind: 'opaque', detail: `No complete native FILE/runtime plan for ${task.id}` },
549
+ };
550
+ }
551
+ for (const file of input.files)
552
+ files.add(file);
553
+ if (input.runtime.length > 0 || input.environment.length > 0)
554
+ conservative = true;
555
+ const node = projectGraph.nodes[task.target.project];
556
+ if (node === undefined)
557
+ throw new Error(`Lost project for receipt ${task.id}`);
558
+ includeProject(node.name);
559
+ const target = node.data.targets?.[task.target.target];
560
+ const options = {
561
+ ...target?.options,
562
+ ...(task.target.configuration === undefined ? {} : target?.configurations?.[task.target.configuration]),
563
+ ...task.overrides,
564
+ };
565
+ if (typeof options.cwd === 'string') {
566
+ directories.add(options.cwd);
567
+ directories.add(options.cwd.replaceAll('{projectRoot}', node.data.root).replaceAll('{workspaceRoot}', '.'));
568
+ }
569
+ for (const name of ['project.json', 'package.json']) {
570
+ const path = join(node.data.root, name);
571
+ if (existsSync(join(workspaceRoot, path)))
572
+ files.add(path);
573
+ }
574
+ }
575
+ for (const path of [
576
+ 'nx.json',
577
+ 'package.json',
578
+ 'bun.lock',
579
+ 'bun.lockb',
580
+ 'package-lock.json',
581
+ 'pnpm-lock.yaml',
582
+ 'yarn.lock',
583
+ 'tsconfig.json',
584
+ 'tsconfig.base.json',
585
+ ]) {
586
+ if (existsSync(join(workspaceRoot, path)))
587
+ files.add(path);
588
+ }
589
+ const installation = graphInstallationIdentity(workspaceRoot, nxJson, requireNx('nx/src/utils/package-json').readModulePackageJson);
590
+ const runtimeSource = boundedReceiptRuntimes(inputs, [...directories], [...files], installation !== null);
591
+ return {
592
+ kind: conservative ? 'conservative-provider-source' : 'exact-file-inputs',
593
+ files: [...files],
594
+ inputs,
595
+ runtimeSource,
596
+ };
597
+ }
598
+ /** The id Nx gives the task `selector` names, and the one a miss runs. */
599
+ export function selectorTaskId(selector) {
227
600
  return selector.configuration === undefined
228
601
  ? `${selector.project}:${selector.target}`
229
602
  : `${selector.project}:${selector.target}:${selector.configuration}`;
@@ -234,10 +607,10 @@ function selectorTaskId(selector) {
234
607
  * a module-level singleton that reads `nx.json` from that root. So the root has
235
608
  * to be bound before the first Nx module loads, which is why this module
236
609
  * imports Nx dynamically throughout. A static import at the top of the file
237
- * would bind the caller's launch directory instead.
610
+ * would bind the caller's launch directory instead. `ensureBuilt` has already
611
+ * set `NX_WORKSPACE_ROOT_PATH`; this loads Nx and checks it took.
238
612
  */
239
613
  function bindWorkspaceRoot(workspaceRoot, requireNx) {
240
- process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot;
241
614
  if (process.env.NX_PERF_LOGGING === 'true') {
242
615
  // Installs Nx's PerformanceObserver, which reports every `performance
243
616
  // .measure` this module and Nx itself record. Loading it unconditionally
@@ -252,62 +625,78 @@ function bindWorkspaceRoot(workspaceRoot, requireNx) {
252
625
  throw new Error(`ensureBuilt: Nx is already bound to workspace root ${boundRoot}, cannot switch to ${workspaceRoot}`);
253
626
  }
254
627
  }
255
- /**
256
- * The daemon drains delivered watcher events before serving its graph, but an
257
- * OS event can still be in flight after a write has completed. Its graph and
258
- * hashes can then agree with each other while both describe yesterday's files.
259
- * Take Nx's native, ignore-aware disk snapshot and compare it with the
260
- * daemon's file table; its metadata cache reuses unchanged file hashes.
261
- *
262
- * Any difference is handed to the daemon so its next graph is current, but
263
- * that only schedules a recomputation, and a probe meanwhile would hash
264
- * against the file map it already has. So the caller does not probe: if any
265
- * differing path is not a declared output of a task in the graph, this
266
- * returns true and the target is run, which lets Nx's own runner wait for the
267
- * recomputation. Output paths are exempt because a build's own writes are the
268
- * commonest thing to reach here before the watcher does, and treating them as
269
- * a miss would make every hit after a build replay the cached log.
628
+ /** An exact graph-input witness skips graph transfer, never task hashing.
629
+ * Reconcile disk with the daemon BEFORE asking for a graph; publish evidence
630
+ * only when the filesystem and plugin installation stay identical across the
631
+ * awaited authoritative generation. No disk-cache age or existence is proof.
270
632
  */
271
- async function refreshWorkspaceContext(workspaceRoot, tasks, requireNx) {
633
+ async function currentProjectGraph(workspaceRoot, nxJson, requireNx) {
272
634
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
273
- const { WorkspaceContext, matchOutputPaths } = requireNx('nx/src/native');
635
+ const { WorkspaceContext } = requireNx('nx/src/native');
274
636
  const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
637
+ const packageResolution = requireNx('nx/src/utils/package-json');
638
+ const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
639
+ const witnessPath = join(data, 'ensure-built-graph.json');
640
+ const witness = readGraphWitness(witnessPath);
641
+ const environment = createHash('sha256')
642
+ .update(JSON.stringify(Object.entries(process.env).sort(([left], [right]) => left.localeCompare(right))))
643
+ .digest('hex');
275
644
  performance.mark('ensureBuilt:inputs:start');
276
- const previousFiles = await daemonClient.getWorkspaceContextFileData();
277
- const previousHashes = new Map();
278
- for (const { file, hash } of previousFiles) {
279
- previousHashes.set(file, hash);
280
- }
281
- // A context is a snapshot, not a watcher. Do not reuse one across calls.
282
- const context = new WorkspaceContext(workspaceRoot, workspaceDataDirectoryForWorkspace(workspaceRoot));
283
- const createdFiles = [];
284
- const updatedFiles = [];
285
- for (const { file, hash } of context.allFileData()) {
286
- const previousHash = previousHashes.get(file);
287
- if (previousHash === undefined) {
288
- createdFiles.push(file);
289
- }
290
- else if (previousHash !== hash) {
291
- updatedFiles.push(file);
292
- }
293
- previousHashes.delete(file);
294
- }
295
- const deletedFiles = [...previousHashes.keys()];
296
- const changed = [...createdFiles, ...updatedFiles, ...deletedFiles];
297
- let inputsChanged = false;
298
- if (changed.length > 0) {
299
- await daemonClient.updateWorkspaceContext(createdFiles, updatedFiles, deletedFiles);
300
- // The same matcher the task runner uses to collect outputs, over the union
301
- // of every task's declared outputs. A negation declared by one task then
302
- // also excludes another task's output, which errs toward a miss.
303
- const outputs = tasks.flatMap((task) => task.outputs);
304
- inputsChanged = matchOutputPaths(outputs, changed).includes(false);
645
+ // The daemon's file-table RPC is retained even on reuse: task hashing must
646
+ // see writes the watcher has not delivered, independently of graph reuse.
647
+ const daemonFiles = await daemonClient.getWorkspaceContextFileData();
648
+ const before = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), witness?.snapshot);
649
+ const { created, updated, deleted } = workspaceFileChanges(daemonFiles, before.files);
650
+ if (created.length + updated.length + deleted.length > 0) {
651
+ await daemonClient.updateWorkspaceContext(created, updated, deleted);
305
652
  }
306
653
  performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
307
- return inputsChanged;
654
+ const installation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
655
+ const cached = nodesUnchanged(before.nodes)
656
+ ? reusableGraph(witness, workspaceRoot, environment, installation, before)
657
+ : null;
658
+ if (cached !== null)
659
+ return cached;
660
+ const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
661
+ const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
662
+ const finalInstallation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
663
+ const stableGeneration = snapshotsEqual(before, after) && nodesUnchanged(before.nodes);
664
+ if (installation !== null && installation === finalInstallation && stableGeneration) {
665
+ const next = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
666
+ writeWitness(witnessPath, next);
667
+ }
668
+ // A write during the await cannot authorize a saved graph. Feed the final
669
+ // snapshot to Nx and await its successor before current task hashing.
670
+ if (!stableGeneration) {
671
+ const currentDaemonFiles = await daemonClient.getWorkspaceContextFileData();
672
+ const changes = workspaceFileChanges(currentDaemonFiles, after.files);
673
+ await daemonClient.updateWorkspaceContext(changes.created, changes.updated, changes.deleted);
674
+ return (await daemonClient.getProjectGraphAndSourceMaps()).projectGraph;
675
+ }
676
+ return projectGraph;
677
+ }
678
+ /** Compare the daemon's file table with a snapshot of the disk, both as Nx lists them: path and content hash. */
679
+ export function workspaceFileChanges(daemonFiles, diskFiles) {
680
+ const daemonHashes = new Map();
681
+ for (const { file, hash } of daemonFiles) {
682
+ daemonHashes.set(file, hash);
683
+ }
684
+ const created = [];
685
+ const updated = [];
686
+ for (const { file, hash } of diskFiles) {
687
+ const daemonHash = daemonHashes.get(file);
688
+ if (daemonHash === undefined) {
689
+ created.push(file);
690
+ }
691
+ else if (daemonHash !== hash) {
692
+ updated.push(file);
693
+ }
694
+ daemonHashes.delete(file);
695
+ }
696
+ return { created, updated, deleted: [...daemonHashes.keys()] };
308
697
  }
309
698
  /** `null` when the target is already built; otherwise the first reason it is not. */
310
- async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx) {
699
+ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx, verifyAllArtifacts = false) {
311
700
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
312
701
  const { DaemonBasedTaskHasher } = requireNx('nx/src/hasher/task-hasher');
313
702
  const { getTaskDetails, hashTasks } = requireNx('nx/src/hasher/hash-task');
@@ -315,6 +704,7 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
315
704
  const { getCache } = requireNx('nx/src/tasks-runner/cache');
316
705
  const { getRunnerOptions } = requireNx('nx/src/tasks-runner/run-command');
317
706
  const { getExecutorNameForTask } = requireNx('nx/src/tasks-runner/utils');
707
+ const { expandOutputs } = requireNx('nx/src/native');
318
708
  // `isCloudDefault: false` because these options are only read here by the
319
709
  // hasher, which looks at `selectivelyHashTsConfig`; the cloud credentials the
320
710
  // flag would add belong to the cloud runner, and the run path calls Nx's own
@@ -340,14 +730,7 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
340
730
  }
341
731
  await hashTasks(hasher, projectGraph, taskGraph, perTaskEnvs, getTaskDetails(), tasks);
342
732
  performance.measure('ensureBuilt:hash', 'ensureBuilt:hash:start');
343
- // An `nx:noop` task has no command: Nx completes it without spawning
344
- // anything, and normalizes a command-less target that only names dependencies
345
- // to one. A cache record would vouch for no work — the tasks it aggregates are
346
- // in this graph and answer for themselves — so none is required. Demanding one
347
- // made every call through an uncacheable aggregate hand the whole graph to Nx,
348
- // which replays each dependency's cached log. Outputs a noop declares are
349
- // still verified below, with every other task's.
350
- const recordedTasks = tasks.filter((task) => getExecutorNameForTask(task, projectGraph) !== 'nx:noop');
733
+ const recorded = recordedTasks(tasks, (task) => getExecutorNameForTask(task, projectGraph));
351
734
  // Nx's own factory, but deliberately never `init()`ed: that is what makes
352
735
  // this a local-only, side-effect-free question. `init()` attaches the remote
353
736
  // cache — a network round-trip, and a download is precisely the work this
@@ -356,12 +739,12 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
356
739
  // outputs against, and an artifact missing from it is a miss.
357
740
  performance.mark('ensureBuilt:cache:start');
358
741
  const cache = getCache(runnerOptions);
359
- const cachedResults = await cache.getBatch(recordedTasks.filter((task) => task.cache && task.hash));
742
+ const cachedResults = await cache.getBatch(recorded.filter((task) => task.cache && task.hash));
360
743
  const cachedCodeByHash = new Map();
361
744
  for (const [hash, result] of cachedResults) {
362
745
  cachedCodeByHash.set(hash, result.code);
363
746
  }
364
- const cacheMiss = firstCacheMiss(recordedTasks, cachedCodeByHash);
747
+ const cacheMiss = firstCacheMiss(recorded, cachedCodeByHash);
365
748
  performance.measure('ensureBuilt:cache', 'ensureBuilt:cache:start');
366
749
  if (cacheMiss) {
367
750
  return cacheMiss;
@@ -371,16 +754,11 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
371
754
  // lives in the working tree, where anything may have deleted or rewritten it
372
755
  // since. The daemon answers that cheaply from the output hashes it recorded
373
756
  // when Nx last wrote them.
374
- const claims = [];
375
- for (const task of tasks) {
376
- if (task.outputs.length > 0 && task.hash !== undefined) {
377
- claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
378
- }
379
- }
757
+ const claims = outputClaims(tasks);
380
758
  performance.mark('ensureBuilt:outputs:start');
381
759
  const verdicts = await daemonClient.outputsHashesMatchBatch(claims.map(({ outputs, hash }) => ({ outputs, hash })));
382
760
  performance.measure('ensureBuilt:outputs', 'ensureBuilt:outputs:start');
383
- const doubted = unvouched(claims, verdicts);
761
+ const doubted = verifyAllArtifacts ? claims : unvouched(claims, verdicts);
384
762
  if (doubted.length === 0) {
385
763
  return null;
386
764
  }
@@ -391,12 +769,81 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
391
769
  // And a write the daemon processes more than 2 s after a record erases it,
392
770
  // which a daemon busy hashing does to a restore's own writes — so each
393
771
  // restore guarantees the next one. What settles the question is what a hit
394
- // would restore: Nx's own local artifact for this exact hash. A working tree
395
- // that already holds every entry of it is what the restore would leave.
396
- // Anything else — no artifact, a difference, an error while reading — stays
397
- // a miss.
772
+ // would restore: Nx's own local artifact for this exact hash.
398
773
  performance.mark('ensureBuilt:artifacts:start');
399
- const { expandOutputs } = requireNx('nx/src/native');
774
+ // `recorded` excludes `nx:noop`, so a noop's declared outputs have no
775
+ // artifact here and stay a miss.
776
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
777
+ const verdict = compareWithArtifacts(workspaceRoot, doubted, cachedResults, expandOutputs, join(workspaceDataDirectoryForWorkspace(workspaceRoot), 'ensure-built-outputs.json'));
778
+ if (verdict.kind === 'stale-outputs') {
779
+ return verdict;
780
+ }
781
+ // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
782
+ // next call is back on the cheap path.
783
+ await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
784
+ const movedDuringRecord = firstMovedClaim(verdict.observed);
785
+ performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
786
+ return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
787
+ }
788
+ /**
789
+ * The tasks a cache record must answer for. An `nx:noop` task has no command:
790
+ * Nx completes it without spawning anything, and normalizes a command-less
791
+ * target that only names dependencies to one. A cache record would vouch for
792
+ * no work — the tasks it aggregates are in this graph and answer for
793
+ * themselves — so none is required. Demanding one made every call through an
794
+ * uncacheable aggregate hand the whole graph to Nx, which replays each
795
+ * dependency's cached log. Outputs a noop declares are still verified, with
796
+ * every other task's. `executorOf` is Nx's `getExecutorNameForTask` over the
797
+ * project graph.
798
+ */
799
+ export function recordedTasks(tasks, executorOf) {
800
+ return tasks.filter((task) => executorOf(task) !== 'nx:noop');
801
+ }
802
+ /** What the working tree must hold: the declared outputs of every hashed task that declares any. */
803
+ export function outputClaims(tasks) {
804
+ const claims = [];
805
+ for (const task of tasks) {
806
+ if (task.outputs.length > 0 && task.hash !== undefined) {
807
+ claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
808
+ }
809
+ }
810
+ return claims;
811
+ }
812
+ /**
813
+ * Whether restoring every claim from its cache artifact would leave the
814
+ * working tree as it is. `artifacts` maps a hash to its artifact as Nx's local
815
+ * cache lists it (`getBatch`). A working tree that already holds every entry of
816
+ * each artifact is what the restore would leave: `restorable`, with each node
817
+ * compared, so a write after the comparison can still be caught. Anything else
818
+ * — no artifact, a difference, an error while reading, a write while comparing
819
+ * — is the first claim it happened to.
820
+ */
821
+ export function compareWithArtifacts(workspaceRoot, claims, artifacts, expandOutputs, witnessPath) {
822
+ const binding = JSON.stringify(claims.map((claim) => [claim, artifacts.get(claim.hash)?.outputsPath]));
823
+ const witness = witnessPath === undefined ? null : readArtifactWitness(witnessPath);
824
+ if (witness !== null &&
825
+ witness.version === 1 &&
826
+ witness.workspaceRoot === workspaceRoot &&
827
+ witness.claims === binding &&
828
+ witness.nodes.length > 0 &&
829
+ nodesUnchanged(witness.nodes)) {
830
+ try {
831
+ const observed = witness.nodes.map(({ taskId, path }) => ({
832
+ taskId,
833
+ path,
834
+ stats: lstatSync(path, { bigint: true }),
835
+ }));
836
+ // Recheck both node trees after collecting live stats. A read race
837
+ // invalidates optional evidence and returns to the full comparison.
838
+ if (observed.every((node, index) => nodeStamp(node.stats) === witness.nodes[index].stamp)) {
839
+ return { kind: 'restorable', observed };
840
+ }
841
+ }
842
+ catch (error) {
843
+ if (!isSystemError(error))
844
+ throw error;
845
+ }
846
+ }
400
847
  const comparison = {
401
848
  expandOutputs,
402
849
  left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
@@ -404,26 +851,25 @@ async function probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tas
404
851
  observed: [],
405
852
  realDirectories: new Set(),
406
853
  };
407
- for (const claim of doubted) {
408
- // `recordedTasks` excludes `nx:noop`, so a noop's declared outputs have no
409
- // artifact here and stay a miss.
410
- const artifact = cachedResults.get(claim.hash);
854
+ for (const claim of claims) {
855
+ const artifact = artifacts.get(claim.hash);
411
856
  if (artifact === undefined || !outputsMatchArtifact(comparison, workspaceRoot, artifact.outputsPath, claim)) {
412
857
  return { kind: 'stale-outputs', taskId: claim.taskId };
413
858
  }
414
859
  }
415
- // A write that landed while the bytes were being compared would otherwise be
416
- // recorded as current.
417
- const movedBeforeRecord = firstMovedClaim(comparison.observed);
418
- if (movedBeforeRecord !== null) {
419
- return { kind: 'stale-outputs', taskId: movedBeforeRecord };
860
+ const moved = firstMovedClaim(comparison.observed);
861
+ if (moved !== null)
862
+ return { kind: 'stale-outputs', taskId: moved };
863
+ if (witnessPath !== undefined) {
864
+ const next = {
865
+ version: 1,
866
+ workspaceRoot,
867
+ claims: binding,
868
+ nodes: comparison.observed.map(({ taskId, path, stats }) => ({ taskId, path, stamp: nodeStamp(stats) })),
869
+ };
870
+ writeWitness(witnessPath, next);
420
871
  }
421
- // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
422
- // next call is back on the cheap path.
423
- await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
424
- const movedDuringRecord = firstMovedClaim(comparison.observed);
425
- performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
426
- return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
872
+ return { kind: 'restorable', observed: comparison.observed };
427
873
  }
428
874
  /** Read size for byte comparison: large enough to stream a binary quickly, small enough never to load one whole. */
429
875
  const COMPARE_CHUNK_BYTES = 1 << 20;
@@ -448,7 +894,8 @@ function outputsMatchArtifact(comparison, workspaceRoot, artifactRoot, claim) {
448
894
  try {
449
895
  const entries = comparison.expandOutputs(artifactRoot, claim.outputs);
450
896
  return (entries.length > 0 &&
451
- entries.every((entry) => throughRealDirectories(comparison.realDirectories, workspaceRoot, entry) &&
897
+ entries.every((entry) => throughRealDirectories(comparison, claim.taskId, workspaceRoot, entry) &&
898
+ throughRealDirectories(comparison, claim.taskId, artifactRoot, entry) &&
452
899
  sameTree(comparison, claim.taskId, join(workspaceRoot, entry), join(artifactRoot, entry))));
453
900
  }
454
901
  catch (error) {
@@ -464,18 +911,17 @@ function outputsMatchArtifact(comparison, workspaceRoot, artifactRoot, claim) {
464
911
  * an entry through one would compare bytes the restore does not leave there —
465
912
  * and would read the filesystem behind a link.
466
913
  */
467
- function throughRealDirectories(realDirectories, workspaceRoot, entry) {
468
- let directory = workspaceRoot;
469
- const segments = entry.split('/');
470
- for (const segment of segments.slice(0, -1)) {
914
+ function throughRealDirectories(comparison, taskId, root, entry) {
915
+ let directory = root;
916
+ for (const segment of ['', ...entry.split('/').slice(0, -1)]) {
471
917
  directory = join(directory, segment);
472
- if (realDirectories.has(directory)) {
918
+ if (comparison.realDirectories.has(directory))
473
919
  continue;
474
- }
475
- if (lstatSync(directory, { throwIfNoEntry: false })?.isDirectory() !== true) {
920
+ const stats = lstatSync(directory, { bigint: true, throwIfNoEntry: false });
921
+ if (stats?.isDirectory() !== true)
476
922
  return false;
477
- }
478
- realDirectories.add(directory);
923
+ comparison.observed.push({ taskId, path: directory, stats });
924
+ comparison.realDirectories.add(directory);
479
925
  }
480
926
  return true;
481
927
  }
@@ -493,6 +939,7 @@ function sameTree(comparison, taskId, actual, expected) {
493
939
  return false;
494
940
  }
495
941
  comparison.observed.push({ taskId, path: actual, stats: actualStats });
942
+ comparison.observed.push({ taskId, path: expected, stats: expectedStats });
496
943
  if (expectedStats.isSymbolicLink()) {
497
944
  return actualStats.isSymbolicLink() && readlinkSync(actual) === readlinkSync(expected);
498
945
  }
@@ -550,7 +997,7 @@ function sameBytes(comparison, actual, expected, size) {
550
997
  * compared as: replaced (device, inode), rewritten (size, mtime) or otherwise
551
998
  * touched (mode, ctime).
552
999
  */
553
- function firstMovedClaim(observed) {
1000
+ export function firstMovedClaim(observed) {
554
1001
  for (const { taskId, path, stats } of observed) {
555
1002
  let current;
556
1003
  try {