@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,10 +1,20 @@
1
- import { spawn } from 'node:child_process';
2
- import { randomUUID } from 'node:crypto';
1
+ import { spawn, spawnSync } from 'node:child_process';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import type { BigIntStats } from 'node:fs';
4
- import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync } from 'node:fs';
4
+ import {
5
+ closeSync,
6
+ existsSync,
7
+ lstatSync,
8
+ openSync,
9
+ readdirSync,
10
+ readlinkSync,
11
+ readSync,
12
+ realpathSync,
13
+ statSync,
14
+ } from 'node:fs';
5
15
  import { createRequire } from 'node:module';
6
- import { isAbsolute, join, resolve } from 'node:path';
7
-
16
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
17
+ import { inspect } from 'node:util';
8
18
  import type * as NxConfiguration from 'nx/src/config/nx-json';
9
19
  import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
10
20
  import type { ProjectGraph, ProjectGraphProjectNode } from 'nx/src/config/project-graph';
@@ -13,7 +23,9 @@ import type * as NxDaemonClient from 'nx/src/daemon/client/client';
13
23
  import type * as NxHashTask from 'nx/src/hasher/hash-task';
14
24
  import type * as NxTaskHasher from 'nx/src/hasher/task-hasher';
15
25
  import type * as NxNative from 'nx/src/native';
26
+ import type * as NxTransformObjects from 'nx/src/native/transform-objects';
16
27
  import type * as NxExecutionHooks from 'nx/src/project-graph/plugins/tasks-execution-hooks';
28
+ import type * as NxProjectRootMappings from 'nx/src/project-graph/utils/find-project-for-path';
17
29
  import type * as NxCache from 'nx/src/tasks-runner/cache';
18
30
  import type * as NxCreateTaskGraph from 'nx/src/tasks-runner/create-task-graph';
19
31
  import type { TaskResults } from 'nx/src/tasks-runner/life-cycle';
@@ -23,8 +35,24 @@ import type * as NxTasksRunnerUtils from 'nx/src/tasks-runner/utils';
23
35
  import type * as NxCacheDirectory from 'nx/src/utils/cache-directory';
24
36
  import type * as NxCommandLineUtils from 'nx/src/utils/command-line-utils';
25
37
  import type { NxArgs } from 'nx/src/utils/command-line-utils';
38
+ import type * as NxPackageJson from 'nx/src/utils/package-json';
26
39
  import type * as NxPerfLogging from 'nx/src/utils/perf-logging';
27
40
  import type * as NxWorkspaceRoot from 'nx/src/utils/workspace-root';
41
+ import { boundedReceiptRuntimes, type ProviderInputProof, providerReceipt } from './ensure-built-receipt.js';
42
+ import {
43
+ type ArtifactWitness,
44
+ type GraphWitness,
45
+ graphInstallationIdentity,
46
+ nodeStamp,
47
+ nodesUnchanged,
48
+ readArtifactWitness,
49
+ readGraphWitness,
50
+ reusableGraph,
51
+ snapshotsEqual,
52
+ workspaceSnapshot,
53
+ writeWitness,
54
+ } from './ensure-built-witness.js';
55
+ import { managedAssetsRoot } from './managed-assets.js';
28
56
 
29
57
  interface NxRuntimeModules {
30
58
  readonly 'nx/src/config/nx-json': typeof NxConfiguration;
@@ -32,6 +60,9 @@ interface NxRuntimeModules {
32
60
  readonly 'nx/src/hasher/hash-task': typeof NxHashTask;
33
61
  readonly 'nx/src/hasher/task-hasher': typeof NxTaskHasher;
34
62
  readonly 'nx/src/native': typeof NxNative;
63
+ readonly 'nx/src/native/transform-objects': typeof NxTransformObjects;
64
+ readonly 'nx/src/project-graph/utils/find-project-for-path': typeof NxProjectRootMappings;
65
+ readonly 'nx/src/utils/package-json': typeof NxPackageJson;
35
66
  readonly 'nx/src/project-graph/plugins/tasks-execution-hooks': typeof NxExecutionHooks;
36
67
  readonly 'nx/src/tasks-runner/cache': typeof NxCache;
37
68
  readonly 'nx/src/tasks-runner/create-task-graph': typeof NxCreateTaskGraph;
@@ -83,10 +114,29 @@ export type MissReason =
83
114
  | { readonly kind: 'not-cached'; readonly taskId: string }
84
115
  | { readonly kind: 'cached-failure'; readonly taskId: string; readonly code: number }
85
116
  | { readonly kind: 'stale-outputs'; readonly taskId: string }
86
- | { readonly kind: 'stale-inputs'; readonly taskId: string }
87
117
  | { readonly kind: 'cache-disabled'; readonly taskId: string }
88
118
  | { readonly kind: 'no-daemon'; readonly taskId: string };
89
119
 
120
+ /** The provider's socket owner refused before any task or daemon client was started. */
121
+ export interface WorkspaceSocketFailure {
122
+ readonly kind: 'workspace-socket';
123
+ readonly taskId: string;
124
+ readonly workspaceRoot: string;
125
+ readonly detail: string;
126
+ }
127
+
128
+ export interface EnsureBuiltFailure {
129
+ readonly disposition: 'failed';
130
+ readonly reason: MissReason | WorkspaceSocketFailure;
131
+ readonly exitCode: number;
132
+ readonly signal: NodeJS.Signals | null;
133
+ }
134
+
135
+ /** A precise socket-owner refusal; its task has not run. */
136
+ export interface WorkspaceSocketRefusal extends EnsureBuiltFailure {
137
+ readonly reason: WorkspaceSocketFailure;
138
+ }
139
+
90
140
  /**
91
141
  * `hit` means nothing ran: every task in the graph was a recorded success and
92
142
  * every recorded output is still byte-identical on disk, so the artifacts are
@@ -98,19 +148,14 @@ export type MissReason =
98
148
  * order to decide whether to stay quiet. A run writes only to stderr: stdout
99
149
  * belongs to whatever the caller goes on to run.
100
150
  *
101
- * `failed` is an operational outcome, not an exception: the target ran and did
102
- * not succeed. A caller re-raises `signal` when there is one and exits with
103
- * `exitCode` otherwise, so a Ctrl-C during a build stays a Ctrl-C.
151
+ * `failed` is an operational outcome, not an exception: the provider's socket
152
+ * binding or the target did not succeed. A caller re-raises `signal` when there
153
+ * is one and exits with `exitCode` otherwise, so a Ctrl-C stays a Ctrl-C.
104
154
  */
105
155
  export type EnsureBuiltResult =
106
156
  | { readonly disposition: 'hit' }
107
157
  | { readonly disposition: 'built'; readonly reason: MissReason }
108
- | {
109
- readonly disposition: 'failed';
110
- readonly reason: MissReason;
111
- readonly exitCode: number;
112
- readonly signal: NodeJS.Signals | null;
113
- };
158
+ | EnsureBuiltFailure;
114
159
 
115
160
  export interface EnsureBuiltOptions {
116
161
  /** `project:target` or `project:target:configuration`. */
@@ -130,6 +175,48 @@ export interface EnsureBuiltOptions {
130
175
  readonly onMiss?: 'stream';
131
176
  }
132
177
 
178
+ export type EnsureBuiltOutputReceipt =
179
+ | { readonly path: string; readonly type: 'file'; readonly mode: number; readonly sha256: string }
180
+ | { readonly path: string; readonly type: 'symlink'; readonly mode: number; readonly linkText: string }
181
+ | {
182
+ readonly path: string;
183
+ readonly type: 'directory';
184
+ readonly mode: number;
185
+ readonly entries: readonly string[];
186
+ readonly sha256: string;
187
+ };
188
+
189
+ /** Git provenance covers file inputs only; runtime/env identity remains Nx's current task hash. */
190
+ export interface EnsureBuiltReceipt {
191
+ readonly workspaceRoot: string;
192
+ readonly head: string;
193
+ readonly headTree: string;
194
+ readonly fileInputProvenance: 'exact-file-inputs' | 'conservative-provider-source';
195
+ readonly runtimeEnvironmentProvenance: 'nx-task-hash-only';
196
+ readonly tasks: readonly {
197
+ readonly id: string;
198
+ readonly hash: string;
199
+ readonly outputs: readonly EnsureBuiltOutputReceipt[];
200
+ }[];
201
+ }
202
+
203
+ export interface EnsureBuiltProbeFailure {
204
+ readonly disposition: 'failed';
205
+ readonly reason: {
206
+ readonly kind: 'provider-proof';
207
+ readonly taskId: string;
208
+ readonly workspaceRoot: string;
209
+ readonly detail: string;
210
+ };
211
+ readonly exitCode: 1;
212
+ readonly signal: null;
213
+ }
214
+
215
+ export type EnsureBuiltProbeResult =
216
+ | { readonly disposition: 'hit'; readonly receipt: EnsureBuiltReceipt }
217
+ | { readonly disposition: 'miss'; readonly reason: MissReason }
218
+ | EnsureBuiltProbeFailure
219
+ | WorkspaceSocketRefusal;
133
220
  /** A full hit carries no payload, so it is the same value every time. */
134
221
  const HIT: EnsureBuiltResult = Object.freeze({ disposition: 'hit' });
135
222
 
@@ -160,8 +247,6 @@ export function describeMiss(reason: MissReason): string {
160
247
  return `${reason.taskId} last failed with exit code ${reason.code}`;
161
248
  case 'stale-outputs':
162
249
  return `${reason.taskId} outputs are missing or modified on disk`;
163
- case 'stale-inputs':
164
- return `${reason.taskId} inputs changed since the Nx daemon last looked`;
165
250
  case 'cache-disabled':
166
251
  return `the Nx cache is disabled, so ${reason.taskId} must run`;
167
252
  case 'no-daemon':
@@ -169,6 +254,87 @@ export function describeMiss(reason: MissReason): string {
169
254
  }
170
255
  }
171
256
 
257
+ /** A `smoo-nx-exec` command line: the target, the workspace root it names, and the binary to exec with its arguments. */
258
+ export interface ExecInvocation {
259
+ readonly target: string;
260
+ /** As given; absent when the wrapper is to find the root at or above the caller's directory. */
261
+ readonly workspaceRoot: string | undefined;
262
+ /** Empty when there is nothing to exec, only the target to make current. */
263
+ readonly command: readonly string[];
264
+ }
265
+
266
+ /** A command line read as an invocation, or the usage mistake it makes. */
267
+ export type ExecArguments =
268
+ | { readonly ok: true; readonly invocation: ExecInvocation }
269
+ | { readonly ok: false; readonly usage: string };
270
+
271
+ /**
272
+ * Read `smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>]
273
+ * [-- <binary> [args...]]`. A flag may be spelled `--flag value` or
274
+ * `--flag=value`. Without `--` there is nothing to exec: the wrapper builds
275
+ * what is stale and exits, so a caller that only needs the target current pays
276
+ * the probe, not an Nx CLI start that replays every cached task's log.
277
+ */
278
+ export function parseExecArguments(argv: readonly string[]): ExecArguments {
279
+ const separator = argv.indexOf('--');
280
+ const command = separator === -1 ? [] : argv.slice(separator + 1);
281
+ if (separator !== -1 && command.length === 0) {
282
+ return { ok: false, usage: 'no binary given after `--`' };
283
+ }
284
+ let target: string | undefined;
285
+ let workspaceRoot: string | undefined;
286
+ // Only leading `--` forms are split at `=`: a value may legitimately contain one.
287
+ const tokens = (separator === -1 ? argv : argv.slice(0, separator)).flatMap((arg) => {
288
+ const equals = arg.startsWith('--') ? arg.indexOf('=') : -1;
289
+ return equals === -1 ? [arg] : [arg.slice(0, equals), arg.slice(equals + 1)];
290
+ });
291
+ for (let index = 0; index < tokens.length; index += 1) {
292
+ const token = tokens[index];
293
+ if (token === '--workspace-root') {
294
+ index += 1;
295
+ if (index === tokens.length) {
296
+ return { ok: false, usage: '--workspace-root needs a directory' };
297
+ }
298
+ workspaceRoot = tokens[index];
299
+ } else if (token.startsWith('-')) {
300
+ return { ok: false, usage: `unknown flag ${token}` };
301
+ } else if (target !== undefined) {
302
+ return { ok: false, usage: `more than one target given: ${target} and ${token}` };
303
+ } else {
304
+ target = token;
305
+ }
306
+ }
307
+ if (target === undefined) {
308
+ return { ok: false, usage: 'no project:target given' };
309
+ }
310
+ if (parseTargetSelector(target) === null) {
311
+ return { ok: false, usage: `'${target}' is not a project:target[:configuration] selector` };
312
+ }
313
+ // `execve` does no PATH lookup, so a bare name would fail as a missing file in
314
+ // the current directory. Say which mistake was made instead.
315
+ if (command.length > 0 && !command[0].includes('/')) {
316
+ return { ok: false, usage: `'${command[0]}' must be a path to the binary, not a name to look up on PATH` };
317
+ }
318
+ return { ok: true, invocation: { target, workspaceRoot, command } };
319
+ }
320
+
321
+ /**
322
+ * The message of whatever `ensureBuilt` rejected with. Nx rejects with plain
323
+ * objects as well as errors (its daemon client's `{ stack, message }`), and
324
+ * `String()` of one is `[object Object]`.
325
+ */
326
+ export function describeError(error: unknown): string {
327
+ if (error instanceof Error) {
328
+ return error.message;
329
+ }
330
+ if (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string') {
331
+ return error.message;
332
+ }
333
+ return typeof error === 'string'
334
+ ? error
335
+ : inspect(error, { breakLength: Number.POSITIVE_INFINITY, colors: false, depth: 5 });
336
+ }
337
+
172
338
  /**
173
339
  * First reason the task set cannot be replayed, considering only recorded
174
340
  * results — no filesystem access. `null` means every task is a recorded
@@ -230,99 +396,327 @@ export function unvouched<Entry>(entries: readonly Entry[], verdicts: readonly b
230
396
  * the workspace's own `nx` CLI unconditionally.
231
397
  */
232
398
  export async function ensureBuilt(options: EnsureBuiltOptions): Promise<EnsureBuiltResult> {
399
+ return withWorkspaceBinding(options, ensureBuiltInWorkspace);
400
+ }
401
+
402
+ /** Read current producer evidence without running tasks or writing producer outputs. */
403
+ export async function probeBuilt(options: Pick<EnsureBuiltOptions, 'target' | 'cwd'>): Promise<EnsureBuiltProbeResult> {
404
+ return withWorkspaceBinding(options, probeBuiltInWorkspace);
405
+ }
406
+
407
+ async function withWorkspaceBinding<Result>(
408
+ options: Pick<EnsureBuiltOptions, 'target' | 'cwd'>,
409
+ operation: (workspaceRoot: string, selector: TargetSelector) => Promise<Result>,
410
+ ): Promise<Result | WorkspaceSocketRefusal> {
233
411
  const selector = parseTargetSelector(options.target);
234
412
  if (!selector) {
235
413
  throw new Error(`ensureBuilt: '${options.target}' is not a project:target[:configuration] selector`);
236
414
  }
237
- // Resolved before the chdir below, so a relative `cwd` still means what the
238
- // caller meant.
415
+ // Resolve before chdir so a relative cwd still means what the caller meant.
239
416
  const workspaceRoot = resolve(options.cwd);
240
417
  const callerCwd = process.cwd();
241
418
  const callerEnv = { ...process.env };
419
+ const callerOwnsRoot = callerOwnsWorkspace(callerEnv, callerCwd, workspaceRoot);
242
420
  try {
243
- // `cd <root> && nx run ...` is the invocation whose hashes are in the
244
- // cache. Everything below — plugin hooks, `runtime` inputs, and the hasher,
245
- // which keys against `process.cwd()` — has to see the same directory or the
246
- // probe keys differently from the CLI and never hits.
421
+ // Nx's hooks, runtime inputs and hasher see the provider's own workspace.
247
422
  process.chdir(workspaceRoot);
248
- return await ensureBuiltInWorkspace(workspaceRoot, selector, options.onMiss ?? 'stream');
423
+ const environment = workspaceEnvironment(callerEnv, workspaceRoot, callerOwnsRoot);
424
+ if (!callerOwnsRoot) {
425
+ const binding = bindWorkspaceSocketDirectory(workspaceRoot, selectorTaskId(selector), environment);
426
+ if (binding !== undefined) {
427
+ if (typeof binding !== 'string') {
428
+ process.stderr.write(`ensureBuilt: ${binding.reason.workspaceRoot}: ${binding.reason.detail}\n`);
429
+ return binding;
430
+ }
431
+ environment.NX_SOCKET_DIR = binding;
432
+ }
433
+ }
434
+ assignEnvironment(process.env, environment);
435
+ return await operation(workspaceRoot, selector);
249
436
  } finally {
250
- // Nx configures itself through the environment and this function runs
251
- // in-process, so without this the caller — and anything it goes on to exec
252
- // — would inherit NX_WORKSPACE_ROOT_PATH, the stream/prefix flags, and
253
- // whatever a plugin's preTasksExecution hook injected. The wrapper this
254
- // replaces got that isolation for free by running Nx in a child.
437
+ // Never lend this provider's cwd, Nx binding or hook-injected environment
438
+ // to the caller or whatever it executes after this operation.
255
439
  process.chdir(callerCwd);
256
- for (const key of Object.keys(process.env)) {
257
- if (!(key in callerEnv)) {
258
- delete process.env[key];
259
- }
440
+ assignEnvironment(process.env, callerEnv);
441
+ }
442
+ }
443
+
444
+ /**
445
+ * Nx's own overrides for where one workspace keeps its daemon socket, daemon
446
+ * record, task database and cache. Each names the state of a single
447
+ * workspace, and Nx takes it literally: a daemon spawned for another root
448
+ * with `NX_SOCKET_DIR` inherited listens on, and on stop deletes, the socket
449
+ * directory of the workspace that exported it; with
450
+ * `NX_WORKSPACE_DATA_DIRECTORY` it reads and overwrites that workspace's
451
+ * daemon record and task database; and the cache has to move with the
452
+ * database or a hit names artifacts the run never wrote.
453
+ */
454
+ const WORKSPACE_STATE_ENV_KEYS = [
455
+ 'NX_SOCKET_DIR',
456
+ 'NX_DAEMON_SOCKET_DIR',
457
+ 'NX_WORKSPACE_DATA_DIRECTORY',
458
+ 'NX_CACHE_DIRECTORY',
459
+ ] as const;
460
+
461
+ /** A socket binding refusal is the existing failed outcome, including the owner's real exit. */
462
+ function workspaceSocketRefusal(
463
+ workspaceRoot: string,
464
+ taskId: string,
465
+ detail: string,
466
+ exitCode: number,
467
+ signal: NodeJS.Signals | null,
468
+ ): WorkspaceSocketRefusal {
469
+ return {
470
+ disposition: 'failed',
471
+ reason: { kind: 'workspace-socket', taskId, workspaceRoot, detail },
472
+ exitCode,
473
+ signal,
474
+ };
475
+ }
476
+
477
+ /**
478
+ * A foreign-root call inside a managed shell asks the same packaged socket
479
+ * owner that shell uses. The owner reads the provider's binding or derives a
480
+ * namespace from its canonical checkout spelling under DEVENV_RUNTIME.
481
+ * Outside a managed shell Nx keeps its normal defaults. No caller socket is reused.
482
+ */
483
+ function bindWorkspaceSocketDirectory(
484
+ workspaceRoot: string,
485
+ taskId: string,
486
+ environment: Readonly<NodeJS.ProcessEnv>,
487
+ ): string | WorkspaceSocketRefusal | undefined {
488
+ const runtime = environment.DEVENV_RUNTIME;
489
+ if (runtime === undefined || runtime === '') return undefined;
490
+ if (!isAbsolute(runtime))
491
+ return workspaceSocketRefusal(
492
+ workspaceRoot,
493
+ taskId,
494
+ 'DEVENV_RUNTIME must name the managed shell runtime directory',
495
+ 1,
496
+ null,
497
+ );
498
+ const script = join(managedAssetsRoot, 'raw/tooling/direnv/nx-socket-dir.sh');
499
+ const result = spawnSync('/bin/sh', ['-c', '. "$1" && printf %s "$NX_SOCKET_DIR"', 'sh', script], {
500
+ cwd: workspaceRoot,
501
+ env: { ...environment, PWD: workspaceRoot },
502
+ encoding: 'utf8',
503
+ });
504
+ if (result.error !== undefined)
505
+ return workspaceSocketRefusal(
506
+ workspaceRoot,
507
+ taskId,
508
+ `socket owner could not start: ${result.error.message}; stdout: ${result.stdout}; stderr: ${result.stderr}`,
509
+ 1,
510
+ null,
511
+ );
512
+ if (result.status !== 0) {
513
+ return workspaceSocketRefusal(
514
+ workspaceRoot,
515
+ taskId,
516
+ `socket owner ended ${result.signal === null ? `with exit ${result.status}` : `by ${result.signal}`}: ${result.stderr}; stdout: ${result.stdout}`,
517
+ result.status ?? 1,
518
+ result.signal,
519
+ );
520
+ }
521
+ const directory = result.stdout;
522
+ if (directory === '' || !isAbsolute(directory))
523
+ return workspaceSocketRefusal(
524
+ workspaceRoot,
525
+ taskId,
526
+ `socket owner returned no absolute directory: ${directory}; stderr: ${result.stderr}`,
527
+ 1,
528
+ null,
529
+ );
530
+ try {
531
+ if (!statSync(directory).isDirectory())
532
+ return workspaceSocketRefusal(
533
+ workspaceRoot,
534
+ taskId,
535
+ `socket owner returned a non-directory: ${directory}; stderr: ${result.stderr}`,
536
+ 1,
537
+ null,
538
+ );
539
+ } catch (error) {
540
+ return workspaceSocketRefusal(
541
+ workspaceRoot,
542
+ taskId,
543
+ `socket owner returned an unavailable directory ${directory}: ${error instanceof Error ? error.message : String(error)}; stderr: ${result.stderr}`,
544
+ 1,
545
+ null,
546
+ );
547
+ }
548
+ if (result.stderr !== '') process.stderr.write(result.stderr);
549
+ return directory;
550
+ }
551
+
552
+ /**
553
+ * The nearest directory at or above `from` holding an `nx.json`, or null.
554
+ * The marker this wrapper roots a workspace at when `--workspace-root` is
555
+ * not given.
556
+ */
557
+ export function findNxWorkspaceRoot(from: string): string | null {
558
+ let directory = from;
559
+ for (;;) {
560
+ if (existsSync(join(directory, 'nx.json'))) {
561
+ return directory;
260
562
  }
261
- for (const [key, value] of Object.entries(callerEnv)) {
262
- if (process.env[key] !== value) {
263
- process.env[key] = value;
264
- }
563
+ const parent = dirname(directory);
564
+ if (parent === directory) {
565
+ return null;
265
566
  }
567
+ directory = parent;
568
+ }
569
+ }
570
+
571
+ function isSameDirectory(left: string, right: string): boolean {
572
+ try {
573
+ return realpathSync(left) === realpathSync(right);
574
+ } catch {
575
+ // A path that cannot be resolved cannot be shown to be the same
576
+ // workspace, and "not the same" is the isolating answer.
577
+ return false;
266
578
  }
267
579
  }
268
580
 
269
- async function ensureBuiltInWorkspace(
581
+ /**
582
+ * Whether the environment a caller was launched with belongs to
583
+ * `workspaceRoot`. The caller's workspace is the one its own Nx would bind:
584
+ * `NX_WORKSPACE_ROOT_PATH` when set (relative to where it stands), else the
585
+ * nearest `nx.json` at or above where it stands — none, when it stands in no
586
+ * workspace.
587
+ */
588
+ export function callerOwnsWorkspace(
589
+ callerEnv: Readonly<NodeJS.ProcessEnv>,
590
+ callerCwd: string,
270
591
  workspaceRoot: string,
271
- selector: TargetSelector,
272
- onMiss: 'stream',
273
- ): Promise<EnsureBuiltResult> {
274
- // The environment has precedence over nx.json. Avoid loading Nx at all on
275
- // this explicit fallback path, which also lets a deliberately minimal
276
- // checkout-local CLI stand in for Nx.
592
+ ): boolean {
593
+ const callerRoot = callerEnv.NX_WORKSPACE_ROOT_PATH
594
+ ? resolve(callerCwd, callerEnv.NX_WORKSPACE_ROOT_PATH)
595
+ : findNxWorkspaceRoot(callerCwd);
596
+ return callerRoot !== null && isSameDirectory(callerRoot, workspaceRoot);
597
+ }
598
+
599
+ /**
600
+ * The environment every Nx this call starts runs under — the daemon client
601
+ * loaded below and the `nx` CLI a miss spawns: the caller's, pointed at
602
+ * `workspaceRoot`, with that workspace's state locations. A caller that was set
603
+ * up for this same root keeps its overrides: they are the owner's cache and
604
+ * sandbox boundary. A caller set up for another root, or for none, passes none
605
+ * of them on, so Nx falls back to the per-root defaults. Nothing is reset or
606
+ * bypassed: the root's own daemon and cache are used as they stand. Every one of
607
+ * them answers from the root's installed Nx (`NX_USE_LOCAL=true`): a daemon
608
+ * otherwise installs `nx@latest` to answer Nx Console's status, and a client
609
+ * without the variable recomputes the daemon's graph.
610
+ */
611
+ export function workspaceEnvironment(
612
+ callerEnv: Readonly<NodeJS.ProcessEnv>,
613
+ workspaceRoot: string,
614
+ callerOwnsRoot: boolean,
615
+ ): NodeJS.ProcessEnv {
616
+ const env: NodeJS.ProcessEnv = { ...callerEnv, NX_WORKSPACE_ROOT_PATH: workspaceRoot, NX_USE_LOCAL: 'true' };
617
+ if (!callerOwnsRoot) {
618
+ for (const key of WORKSPACE_STATE_ENV_KEYS) {
619
+ delete env[key];
620
+ }
621
+ }
622
+ return env;
623
+ }
624
+
625
+ /**
626
+ * Make `target` hold exactly the variables of `env`: each one missing from
627
+ * `env` removed, each one different set. Nx reads its configuration from
628
+ * `process.env` in this process, so that is where an environment is put in
629
+ * place, and where the caller's is put back.
630
+ */
631
+ export function assignEnvironment(target: NodeJS.ProcessEnv, env: Readonly<NodeJS.ProcessEnv>): void {
632
+ for (const key of Object.keys(target)) {
633
+ if (env[key] === undefined) {
634
+ delete target[key];
635
+ }
636
+ }
637
+ for (const [key, value] of Object.entries(env)) {
638
+ if (value !== undefined && target[key] !== value) {
639
+ target[key] = value;
640
+ }
641
+ }
642
+ }
643
+
644
+ /**
645
+ * An outer Nx task exports its own cache-bypass setting to the process it
646
+ * launches. That setting describes the outer task, not the nested target:
647
+ * inheriting it makes the inner runner refuse cache writes forever. Outside an
648
+ * Nx task child, however, the same variables are the caller's explicit request
649
+ * to force this target to rebuild and remain authoritative.
650
+ */
651
+ export function withoutOuterTaskCacheBypass(env: Readonly<NodeJS.ProcessEnv>): NodeJS.ProcessEnv {
652
+ const nested = { ...env };
653
+ if (nested.NX_TASK_TARGET_PROJECT !== undefined || nested.NX_TASK_TARGET_TARGET !== undefined) {
654
+ delete nested.NX_SKIP_NX_CACHE;
655
+ delete nested.NX_DISABLE_NX_CACHE;
656
+ }
657
+ return nested;
658
+ }
659
+
660
+ /**
661
+ * A caller-requested cache bypass is the only miss known before hashing.
662
+ * Workspace changes first refresh the authoritative graph; the probe then
663
+ * decides whether these current inputs have matching artifacts.
664
+ */
665
+ export function missBeforeProbe(env: Readonly<NodeJS.ProcessEnv>, taskId: string): MissReason | null {
666
+ if (env.NX_SKIP_NX_CACHE === 'true' || env.NX_DISABLE_NX_CACHE === 'true') {
667
+ return { kind: 'cache-disabled', taskId };
668
+ }
669
+ return null;
670
+ }
671
+
672
+ interface ReadyWorkspaceProbe {
673
+ readonly kind: 'ready';
674
+ readonly nxJson: NxJsonConfiguration;
675
+ readonly nxArgs: NxArgs;
676
+ readonly projectGraph: ProjectGraph;
677
+ readonly taskGraph: TaskGraph;
678
+ readonly tasks: Task[];
679
+ readonly requireNx: WorkspaceNxRequire;
680
+ }
681
+
682
+ type PreparedWorkspaceProbe =
683
+ | ReadyWorkspaceProbe
684
+ | { readonly kind: 'no-daemon'; readonly reason: Extract<MissReason, { kind: 'no-daemon' }> };
685
+
686
+ async function prepareWorkspaceProbe(workspaceRoot: string, selector: TargetSelector): Promise<PreparedWorkspaceProbe> {
687
+ const taskId = selectorTaskId(selector);
688
+ // This explicit fallback path loads no Nx. The build operation may run the
689
+ // checkout-local CLI; the read-only operation can only report the miss.
277
690
  if (process.env.NX_DAEMON === 'false') {
278
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
691
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
279
692
  }
280
- // Nx rejects daemon access when the client package's version differs from
281
- // the workspace's running daemon. Resolve every runtime module from the
282
- // target checkout rather than this package's own dependency: a reusable
283
- // wrapper must keep using the daemon after the workspace takes an Nx patch.
693
+ // Runtime modules always belong to the provider's installed Nx version.
284
694
  const requireNx = createWorkspaceNxRequire(workspaceRoot);
285
695
  bindWorkspaceRoot(workspaceRoot, requireNx);
286
696
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
287
697
  if (!daemonClient.enabled()) {
288
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
289
- }
290
-
291
- // An outer Nx task exports its own cache-bypass setting to the process it
292
- // launches. That setting describes the outer task, not the nested target:
293
- // inheriting it makes the inner runner refuse cache writes forever. Outside
294
- // an Nx task child, however, the same variables are the caller's explicit
295
- // request to force this target to rebuild and must remain authoritative.
296
- if (process.env.NX_TASK_TARGET_PROJECT !== undefined || process.env.NX_TASK_TARGET_TARGET !== undefined) {
297
- delete process.env.NX_SKIP_NX_CACHE;
298
- delete process.env.NX_DISABLE_NX_CACHE;
698
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
299
699
  }
700
+ assignEnvironment(process.env, withoutOuterTaskCacheBypass(process.env));
300
701
 
301
702
  const { readNxJson } = requireNx('nx/src/config/nx-json');
302
703
  const { splitArgsIntoNxArgsAndOverrides } = requireNx('nx/src/utils/command-line-utils');
303
704
  const { setEnvVarsBasedOnArgs } = requireNx('nx/src/tasks-runner/run-command');
304
705
  const { createTaskGraph } = requireNx('nx/src/tasks-runner/create-task-graph');
305
- const hooks = requireNx('nx/src/project-graph/plugins/tasks-execution-hooks');
306
-
307
- const nxJson = readNxJson();
308
- // Reproduce `nx run <selector> --outputStyle=<style>` exactly. Task hashes
309
- // are derived from these arguments, so anything hand-rolled here instead of
310
- // routed through Nx's own argument normalization would key the cache
311
- // differently from the CLI and turn every probe into a miss.
706
+ let nxJson = readNxJson();
707
+ // Both operations key exactly like nx run <selector> --outputStyle=stream.
312
708
  const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
313
- { targets: [selector.target], configuration: selector.configuration, outputStyle: onMiss },
709
+ { targets: [selector.target], configuration: selector.configuration, outputStyle: 'stream' },
314
710
  'run-one',
315
711
  { printWarnings: false },
316
712
  nxJson,
317
713
  );
318
714
  const loadDotEnvFiles = process.env.NX_LOAD_DOT_ENV_FILES !== 'false';
319
- // Sets NX_LOAD_DOT_ENV_FILES and the stream/prefix flags, which the hasher
320
- // reads through each task's environment. It has to happen before hashing or
321
- // the probe keys differently from the CLI.
322
715
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
323
716
 
324
717
  performance.mark('ensureBuilt:graph:start');
325
- const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
718
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx);
719
+ nxJson = readNxJson();
326
720
  requireProject(projectGraph, selector.project);
327
721
  const taskGraph = createTaskGraph(
328
722
  projectGraph,
@@ -335,40 +729,32 @@ async function ensureBuiltInWorkspace(
335
729
  );
336
730
  const tasks = Object.values(taskGraph.tasks);
337
731
  performance.measure('ensureBuilt:graph', 'ensureBuilt:graph:start');
732
+ return { kind: 'ready', nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx };
733
+ }
338
734
 
339
- const inputsChanged = await refreshWorkspaceContext(workspaceRoot, tasks, requireNx);
340
-
735
+ async function withTaskHooks<Result>(
736
+ workspaceRoot: string,
737
+ selector: TargetSelector,
738
+ prepared: ReadyWorkspaceProbe,
739
+ operation: (workspaceRoot: string, selector: TargetSelector, prepared: ReadyWorkspaceProbe) => Promise<Result>,
740
+ ): Promise<Result> {
741
+ const hooks = prepared.requireNx('nx/src/project-graph/plugins/tasks-execution-hooks');
341
742
  const runId = randomUUID();
342
743
  const startTime = Date.now();
343
- // Plugin `preTasksExecution` hooks inject environment variables, and declared
344
- // `env` inputs hash against them. Skipping the hook would not merely cost
345
- // hits: the run below would then execute with a different environment than
346
- // the CLI does, record a hash the CLI never looks up, and thrash the cache.
744
+ // Hook-injected env participates in hashing for both real operations.
347
745
  await hooks.runPreTasksExecution({
348
746
  id: runId,
349
747
  workspaceRoot,
350
- nxJsonConfiguration: nxJson,
748
+ nxJsonConfiguration: prepared.nxJson,
351
749
  argv: process.argv,
352
750
  });
353
-
354
- performance.mark('ensureBuilt:probe:start');
355
- let reason: MissReason | null;
356
- if (process.env.NX_SKIP_NX_CACHE === 'true' || process.env.NX_DISABLE_NX_CACHE === 'true') {
357
- reason = { kind: 'cache-disabled', taskId: selectorTaskId(selector) };
358
- } else if (inputsChanged) {
359
- reason = { kind: 'stale-inputs', taskId: selectorTaskId(selector) };
360
- } else {
361
- reason = await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx);
362
- }
363
- performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
364
- // The CLI runs the hooks around its own run, under its own id. This process
365
- // ran none of the tasks, so it reports none for the id it opened.
366
- const result = reason === null ? HIT : await runViaCli(workspaceRoot, selector, reason);
751
+ const result = await operation(workspaceRoot, selector, prepared);
752
+ // Any runner owns its own hook id; this process executed no tasks.
367
753
  await hooks.runPostTasksExecution({
368
754
  id: runId,
369
755
  taskResults: NO_TASK_RESULTS,
370
756
  workspaceRoot,
371
- nxJsonConfiguration: nxJson,
757
+ nxJsonConfiguration: prepared.nxJson,
372
758
  argv: process.argv,
373
759
  startTime,
374
760
  endTime: Date.now(),
@@ -376,7 +762,192 @@ async function ensureBuiltInWorkspace(
376
762
  return result;
377
763
  }
378
764
 
379
- function selectorTaskId(selector: TargetSelector): string {
765
+ async function ensureBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltResult> {
766
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
767
+ if (prepared.kind === 'no-daemon') return runViaCli(workspaceRoot, selector, prepared.reason);
768
+ return withTaskHooks(workspaceRoot, selector, prepared, buildPreparedTarget);
769
+ }
770
+
771
+ async function buildPreparedTarget(
772
+ workspaceRoot: string,
773
+ selector: TargetSelector,
774
+ prepared: ReadyWorkspaceProbe,
775
+ ): Promise<EnsureBuiltResult> {
776
+ const { nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx } = prepared;
777
+ performance.mark('ensureBuilt:probe:start');
778
+ const reason =
779
+ missBeforeProbe(process.env, selectorTaskId(selector)) ??
780
+ (await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx));
781
+ performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
782
+ return reason === null ? HIT : runViaCli(workspaceRoot, selector, reason);
783
+ }
784
+
785
+ async function probeBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltProbeResult> {
786
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
787
+ if (prepared.kind === 'no-daemon') return { disposition: 'miss', reason: prepared.reason };
788
+ const probeSource = captureProbeSource(workspaceRoot, prepared.requireNx);
789
+ return withTaskHooks(workspaceRoot, selector, prepared, async (root, selected, current) => {
790
+ const { nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx } = current;
791
+ const taskId = selectorTaskId(selected);
792
+ performance.mark('ensureBuilt:probe:start');
793
+ const reason =
794
+ missBeforeProbe(process.env, taskId) ??
795
+ (await probe(root, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx, true));
796
+ performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
797
+ if (reason !== null) return { disposition: 'miss', reason } satisfies EnsureBuiltProbeResult;
798
+ let result = providerReceipt(
799
+ root,
800
+ taskId,
801
+ tasks,
802
+ providerInputProof(root, nxJson, projectGraph, taskGraph, requireNx),
803
+ requireNx('nx/src/native').expandOutputs,
804
+ );
805
+ if (result.disposition === 'hit') {
806
+ const after = captureProbeSource(root, requireNx);
807
+ if (!snapshotsEqual(probeSource, after) || !nodesUnchanged(probeSource.nodes)) {
808
+ result = {
809
+ disposition: 'failed',
810
+ reason: {
811
+ kind: 'provider-proof',
812
+ taskId,
813
+ workspaceRoot: root,
814
+ detail: 'Provider source nodes or membership moved across Nx hashing/receipt capture',
815
+ },
816
+ exitCode: 1,
817
+ signal: null,
818
+ };
819
+ }
820
+ }
821
+ if (result.disposition === 'failed') {
822
+ process.stderr.write(`probeBuilt: ${result.reason.workspaceRoot}: ${result.reason.detail}\n`);
823
+ }
824
+ return result;
825
+ });
826
+ }
827
+
828
+ function captureProbeSource(
829
+ workspaceRoot: string,
830
+ requireNx: WorkspaceNxRequire,
831
+ ): ReturnType<typeof workspaceSnapshot> {
832
+ const { WorkspaceContext } = requireNx('nx/src/native');
833
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
834
+ const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
835
+ const previous = readGraphWitness(join(data, 'ensure-built-graph.json'));
836
+ return workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), previous?.snapshot);
837
+ }
838
+ /** Nx's native plan is the FILE selection oracle, not a second glob planner.
839
+ * Config identities skipped by the inspector are included explicitly. A
840
+ * runtime identity is bounded only by the owning toolchain recipe + a declared
841
+ * pin; opaque commands and unavailable native proof categorically refuse.
842
+ */
843
+ function providerInputProof(
844
+ workspaceRoot: string,
845
+ nxJson: NxJsonConfiguration,
846
+ projectGraph: ProjectGraph,
847
+ taskGraph: TaskGraph,
848
+ requireNx: WorkspaceNxRequire,
849
+ ): ProviderInputProof {
850
+ const { WorkspaceContext, HashPlanInspector, HashPlanner, transferProjectGraph } = requireNx('nx/src/native');
851
+ const { transformProjectGraphForRust } = requireNx('nx/src/native/transform-objects');
852
+ const { createProjectRootMappings } = requireNx('nx/src/project-graph/utils/find-project-for-path');
853
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
854
+ const context = new WorkspaceContext(workspaceRoot, workspaceDataDirectoryForWorkspace(workspaceRoot));
855
+ const { externalReferences } = context.getWorkspaceFiles(
856
+ Object.fromEntries(createProjectRootMappings(projectGraph.nodes)),
857
+ );
858
+ if (externalReferences === undefined) {
859
+ return {
860
+ kind: 'conservative-provider-source',
861
+ files: [],
862
+ inputs: {},
863
+ runtimeSource: { kind: 'opaque', detail: 'Nx supplied no native file-input proof' },
864
+ };
865
+ }
866
+ const planner = new HashPlanner(nxJson, transferProjectGraph(transformProjectGraphForRust(projectGraph)));
867
+ const inspector = new HashPlanInspector(
868
+ externalReferences.allWorkspaceFiles,
869
+ externalReferences.projectFiles,
870
+ workspaceRoot,
871
+ );
872
+ const inputs = inspector.inspectInputs(planner.getPlansReference(Object.keys(taskGraph.tasks), taskGraph));
873
+ const files = new Set<string>();
874
+ const directories = new Set<string>();
875
+ const projects = new Set<string>();
876
+ function includeProject(name: string): void {
877
+ if (projects.has(name)) return;
878
+ projects.add(name);
879
+ const node = projectGraph.nodes[name];
880
+ if (node === undefined) return;
881
+ directories.add(node.data.root);
882
+ for (const file of ['project.json', 'package.json']) {
883
+ const path = join(node.data.root, file);
884
+ if (existsSync(join(workspaceRoot, path))) files.add(path);
885
+ }
886
+ for (const dependency of projectGraph.dependencies[name] ?? []) includeProject(dependency.target);
887
+ }
888
+ let conservative = false;
889
+ const projectConfigs = Object.fromEntries(
890
+ Object.entries(projectGraph.nodes).map(([name, node]) => [name, node.data]),
891
+ );
892
+ const { getCustomHasher } = requireNx('nx/src/tasks-runner/utils');
893
+ for (const task of Object.values(taskGraph.tasks)) {
894
+ const input = inputs[task.id];
895
+ if (input === undefined || getCustomHasher(task, projectConfigs) !== null) {
896
+ return {
897
+ kind: 'conservative-provider-source',
898
+ files: [...files],
899
+ inputs,
900
+ runtimeSource: { kind: 'opaque', detail: `No complete native FILE/runtime plan for ${task.id}` },
901
+ };
902
+ }
903
+ for (const file of input.files) files.add(file);
904
+ if (input.runtime.length > 0 || input.environment.length > 0) conservative = true;
905
+ const node = projectGraph.nodes[task.target.project];
906
+ if (node === undefined) throw new Error(`Lost project for receipt ${task.id}`);
907
+ includeProject(node.name);
908
+ const target = node.data.targets?.[task.target.target];
909
+ const options = {
910
+ ...target?.options,
911
+ ...(task.target.configuration === undefined ? {} : target?.configurations?.[task.target.configuration]),
912
+ ...task.overrides,
913
+ };
914
+ if (typeof options.cwd === 'string') {
915
+ directories.add(options.cwd);
916
+ directories.add(options.cwd.replaceAll('{projectRoot}', node.data.root).replaceAll('{workspaceRoot}', '.'));
917
+ }
918
+ for (const name of ['project.json', 'package.json']) {
919
+ const path = join(node.data.root, name);
920
+ if (existsSync(join(workspaceRoot, path))) files.add(path);
921
+ }
922
+ }
923
+ for (const path of [
924
+ 'nx.json',
925
+ 'package.json',
926
+ 'bun.lock',
927
+ 'bun.lockb',
928
+ 'package-lock.json',
929
+ 'pnpm-lock.yaml',
930
+ 'yarn.lock',
931
+ 'tsconfig.json',
932
+ 'tsconfig.base.json',
933
+ ]) {
934
+ if (existsSync(join(workspaceRoot, path))) files.add(path);
935
+ }
936
+ const installation = graphInstallationIdentity(
937
+ workspaceRoot,
938
+ nxJson,
939
+ requireNx('nx/src/utils/package-json').readModulePackageJson,
940
+ );
941
+ const runtimeSource = boundedReceiptRuntimes(inputs, [...directories], [...files], installation !== null);
942
+ return {
943
+ kind: conservative ? 'conservative-provider-source' : 'exact-file-inputs',
944
+ files: [...files],
945
+ inputs,
946
+ runtimeSource,
947
+ };
948
+ }
949
+ /** The id Nx gives the task `selector` names, and the one a miss runs. */
950
+ export function selectorTaskId(selector: TargetSelector): string {
380
951
  return selector.configuration === undefined
381
952
  ? `${selector.project}:${selector.target}`
382
953
  : `${selector.project}:${selector.target}:${selector.configuration}`;
@@ -388,10 +959,10 @@ function selectorTaskId(selector: TargetSelector): string {
388
959
  * a module-level singleton that reads `nx.json` from that root. So the root has
389
960
  * to be bound before the first Nx module loads, which is why this module
390
961
  * imports Nx dynamically throughout. A static import at the top of the file
391
- * would bind the caller's launch directory instead.
962
+ * would bind the caller's launch directory instead. `ensureBuilt` has already
963
+ * set `NX_WORKSPACE_ROOT_PATH`; this loads Nx and checks it took.
392
964
  */
393
965
  function bindWorkspaceRoot(workspaceRoot: string, requireNx: WorkspaceNxRequire): void {
394
- process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot;
395
966
  if (process.env.NX_PERF_LOGGING === 'true') {
396
967
  // Installs Nx's PerformanceObserver, which reports every `performance
397
968
  // .measure` this module and Nx itself record. Loading it unconditionally
@@ -409,62 +980,88 @@ function bindWorkspaceRoot(workspaceRoot: string, requireNx: WorkspaceNxRequire)
409
980
  }
410
981
  }
411
982
 
412
- /**
413
- * The daemon drains delivered watcher events before serving its graph, but an
414
- * OS event can still be in flight after a write has completed. Its graph and
415
- * hashes can then agree with each other while both describe yesterday's files.
416
- * Take Nx's native, ignore-aware disk snapshot and compare it with the
417
- * daemon's file table; its metadata cache reuses unchanged file hashes.
418
- *
419
- * Any difference is handed to the daemon so its next graph is current, but
420
- * that only schedules a recomputation, and a probe meanwhile would hash
421
- * against the file map it already has. So the caller does not probe: if any
422
- * differing path is not a declared output of a task in the graph, this
423
- * returns true and the target is run, which lets Nx's own runner wait for the
424
- * recomputation. Output paths are exempt because a build's own writes are the
425
- * commonest thing to reach here before the watcher does, and treating them as
426
- * a miss would make every hit after a build replay the cached log.
983
+ /** An exact graph-input witness skips graph transfer, never task hashing.
984
+ * Reconcile disk with the daemon BEFORE asking for a graph; publish evidence
985
+ * only when the filesystem and plugin installation stay identical across the
986
+ * awaited authoritative generation. No disk-cache age or existence is proof.
427
987
  */
428
- async function refreshWorkspaceContext(
988
+ async function currentProjectGraph(
429
989
  workspaceRoot: string,
430
- tasks: Task[],
990
+ nxJson: NxJsonConfiguration,
431
991
  requireNx: WorkspaceNxRequire,
432
- ): Promise<boolean> {
992
+ ): Promise<ProjectGraph> {
433
993
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
434
- const { WorkspaceContext, matchOutputPaths } = requireNx('nx/src/native');
994
+ const { WorkspaceContext } = requireNx('nx/src/native');
435
995
  const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
996
+ const packageResolution = requireNx('nx/src/utils/package-json');
997
+ const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
998
+ const witnessPath = join(data, 'ensure-built-graph.json');
999
+ const witness = readGraphWitness(witnessPath);
1000
+ const environment = createHash('sha256')
1001
+ .update(JSON.stringify(Object.entries(process.env).sort(([left], [right]) => left.localeCompare(right))))
1002
+ .digest('hex');
436
1003
  performance.mark('ensureBuilt:inputs:start');
437
- const previousFiles = await daemonClient.getWorkspaceContextFileData();
438
- const previousHashes = new Map<string, string>();
439
- for (const { file, hash } of previousFiles) {
440
- previousHashes.set(file, hash);
1004
+ // The daemon's file-table RPC is retained even on reuse: task hashing must
1005
+ // see writes the watcher has not delivered, independently of graph reuse.
1006
+ const daemonFiles = await daemonClient.getWorkspaceContextFileData();
1007
+ const before = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), witness?.snapshot);
1008
+ const { created, updated, deleted } = workspaceFileChanges(daemonFiles, before.files);
1009
+ if (created.length + updated.length + deleted.length > 0) {
1010
+ await daemonClient.updateWorkspaceContext(created, updated, deleted);
441
1011
  }
442
- // A context is a snapshot, not a watcher. Do not reuse one across calls.
443
- const context = new WorkspaceContext(workspaceRoot, workspaceDataDirectoryForWorkspace(workspaceRoot));
444
- const createdFiles: string[] = [];
445
- const updatedFiles: string[] = [];
446
- for (const { file, hash } of context.allFileData()) {
447
- const previousHash = previousHashes.get(file);
448
- if (previousHash === undefined) {
449
- createdFiles.push(file);
450
- } else if (previousHash !== hash) {
451
- updatedFiles.push(file);
1012
+ performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
1013
+ const installation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
1014
+ const cached = nodesUnchanged(before.nodes)
1015
+ ? reusableGraph(witness, workspaceRoot, environment, installation, before)
1016
+ : null;
1017
+ if (cached !== null) return cached;
1018
+ const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
1019
+ const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
1020
+ const finalInstallation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
1021
+ const stableGeneration = snapshotsEqual(before, after) && nodesUnchanged(before.nodes);
1022
+ if (installation !== null && installation === finalInstallation && stableGeneration) {
1023
+ const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1024
+ writeWitness(witnessPath, next);
1025
+ }
1026
+ // A write during the await cannot authorize a saved graph. Feed the final
1027
+ // snapshot to Nx and await its successor before current task hashing.
1028
+ if (!stableGeneration) {
1029
+ const currentDaemonFiles = await daemonClient.getWorkspaceContextFileData();
1030
+ const changes = workspaceFileChanges(currentDaemonFiles, after.files);
1031
+ await daemonClient.updateWorkspaceContext(changes.created, changes.updated, changes.deleted);
1032
+ return (await daemonClient.getProjectGraphAndSourceMaps()).projectGraph;
1033
+ }
1034
+ return projectGraph;
1035
+ }
1036
+
1037
+ /** How the files on disk differ from the daemon's file table, by workspace-relative path. */
1038
+ export interface WorkspaceFileChanges {
1039
+ readonly created: string[];
1040
+ readonly updated: string[];
1041
+ readonly deleted: string[];
1042
+ }
1043
+
1044
+ /** Compare the daemon's file table with a snapshot of the disk, both as Nx lists them: path and content hash. */
1045
+ export function workspaceFileChanges(
1046
+ daemonFiles: readonly NxNative.FileData[],
1047
+ diskFiles: readonly NxNative.FileData[],
1048
+ ): WorkspaceFileChanges {
1049
+ const daemonHashes = new Map<string, string>();
1050
+ for (const { file, hash } of daemonFiles) {
1051
+ daemonHashes.set(file, hash);
1052
+ }
1053
+ const created: string[] = [];
1054
+ const updated: string[] = [];
1055
+ for (const { file, hash } of diskFiles) {
1056
+ const daemonHash = daemonHashes.get(file);
1057
+ if (daemonHash === undefined) {
1058
+ created.push(file);
1059
+ } else if (daemonHash !== hash) {
1060
+ updated.push(file);
452
1061
  }
453
- previousHashes.delete(file);
454
- }
455
- const deletedFiles = [...previousHashes.keys()];
456
- const changed = [...createdFiles, ...updatedFiles, ...deletedFiles];
457
- let inputsChanged = false;
458
- if (changed.length > 0) {
459
- await daemonClient.updateWorkspaceContext(createdFiles, updatedFiles, deletedFiles);
460
- // The same matcher the task runner uses to collect outputs, over the union
461
- // of every task's declared outputs. A negation declared by one task then
462
- // also excludes another task's output, which errs toward a miss.
463
- const outputs = tasks.flatMap((task) => task.outputs);
464
- inputsChanged = matchOutputPaths(outputs, changed).includes(false);
1062
+ daemonHashes.delete(file);
465
1063
  }
466
- performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
467
- return inputsChanged;
1064
+ return { created, updated, deleted: [...daemonHashes.keys()] };
468
1065
  }
469
1066
 
470
1067
  /** `null` when the target is already built; otherwise the first reason it is not. */
@@ -476,6 +1073,7 @@ async function probe(
476
1073
  taskGraph: TaskGraph,
477
1074
  tasks: Task[],
478
1075
  requireNx: WorkspaceNxRequire,
1076
+ verifyAllArtifacts = false,
479
1077
  ): Promise<MissReason | null> {
480
1078
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
481
1079
  const { DaemonBasedTaskHasher } = requireNx('nx/src/hasher/task-hasher');
@@ -484,6 +1082,7 @@ async function probe(
484
1082
  const { getCache } = requireNx('nx/src/tasks-runner/cache');
485
1083
  const { getRunnerOptions } = requireNx('nx/src/tasks-runner/run-command');
486
1084
  const { getExecutorNameForTask } = requireNx('nx/src/tasks-runner/utils');
1085
+ const { expandOutputs } = requireNx('nx/src/native');
487
1086
 
488
1087
  // `isCloudDefault: false` because these options are only read here by the
489
1088
  // hasher, which looks at `selectivelyHashTsConfig`; the cloud credentials the
@@ -511,14 +1110,7 @@ async function probe(
511
1110
  await hashTasks(hasher, projectGraph, taskGraph, perTaskEnvs, getTaskDetails(), tasks);
512
1111
  performance.measure('ensureBuilt:hash', 'ensureBuilt:hash:start');
513
1112
 
514
- // An `nx:noop` task has no command: Nx completes it without spawning
515
- // anything, and normalizes a command-less target that only names dependencies
516
- // to one. A cache record would vouch for no work — the tasks it aggregates are
517
- // in this graph and answer for themselves — so none is required. Demanding one
518
- // made every call through an uncacheable aggregate hand the whole graph to Nx,
519
- // which replays each dependency's cached log. Outputs a noop declares are
520
- // still verified below, with every other task's.
521
- const recordedTasks = tasks.filter((task) => getExecutorNameForTask(task, projectGraph) !== 'nx:noop');
1113
+ const recorded = recordedTasks(tasks, (task) => getExecutorNameForTask(task, projectGraph));
522
1114
 
523
1115
  // Nx's own factory, but deliberately never `init()`ed: that is what makes
524
1116
  // this a local-only, side-effect-free question. `init()` attaches the remote
@@ -528,12 +1120,12 @@ async function probe(
528
1120
  // outputs against, and an artifact missing from it is a miss.
529
1121
  performance.mark('ensureBuilt:cache:start');
530
1122
  const cache = getCache(runnerOptions);
531
- const cachedResults = await cache.getBatch(recordedTasks.filter((task) => task.cache && task.hash));
1123
+ const cachedResults = await cache.getBatch(recorded.filter((task) => task.cache && task.hash));
532
1124
  const cachedCodeByHash = new Map<string, number>();
533
1125
  for (const [hash, result] of cachedResults) {
534
1126
  cachedCodeByHash.set(hash, result.code);
535
1127
  }
536
- const cacheMiss = firstCacheMiss(recordedTasks, cachedCodeByHash);
1128
+ const cacheMiss = firstCacheMiss(recorded, cachedCodeByHash);
537
1129
  performance.measure('ensureBuilt:cache', 'ensureBuilt:cache:start');
538
1130
  if (cacheMiss) {
539
1131
  return cacheMiss;
@@ -544,16 +1136,11 @@ async function probe(
544
1136
  // lives in the working tree, where anything may have deleted or rewritten it
545
1137
  // since. The daemon answers that cheaply from the output hashes it recorded
546
1138
  // when Nx last wrote them.
547
- const claims: OutputClaim[] = [];
548
- for (const task of tasks) {
549
- if (task.outputs.length > 0 && task.hash !== undefined) {
550
- claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
551
- }
552
- }
1139
+ const claims = outputClaims(tasks);
553
1140
  performance.mark('ensureBuilt:outputs:start');
554
1141
  const verdicts = await daemonClient.outputsHashesMatchBatch(claims.map(({ outputs, hash }) => ({ outputs, hash })));
555
1142
  performance.measure('ensureBuilt:outputs', 'ensureBuilt:outputs:start');
556
- const doubted = unvouched(claims, verdicts);
1143
+ const doubted = verifyAllArtifacts ? claims : unvouched(claims, verdicts);
557
1144
  if (doubted.length === 0) {
558
1145
  return null;
559
1146
  }
@@ -565,12 +1152,96 @@ async function probe(
565
1152
  // And a write the daemon processes more than 2 s after a record erases it,
566
1153
  // which a daemon busy hashing does to a restore's own writes — so each
567
1154
  // restore guarantees the next one. What settles the question is what a hit
568
- // would restore: Nx's own local artifact for this exact hash. A working tree
569
- // that already holds every entry of it is what the restore would leave.
570
- // Anything else — no artifact, a difference, an error while reading — stays
571
- // a miss.
1155
+ // would restore: Nx's own local artifact for this exact hash.
572
1156
  performance.mark('ensureBuilt:artifacts:start');
573
- const { expandOutputs } = requireNx('nx/src/native');
1157
+ // `recorded` excludes `nx:noop`, so a noop's declared outputs have no
1158
+ // artifact here and stay a miss.
1159
+ const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
1160
+ const verdict = compareWithArtifacts(
1161
+ workspaceRoot,
1162
+ doubted,
1163
+ cachedResults,
1164
+ expandOutputs,
1165
+ join(workspaceDataDirectoryForWorkspace(workspaceRoot), 'ensure-built-outputs.json'),
1166
+ );
1167
+ if (verdict.kind === 'stale-outputs') {
1168
+ return verdict;
1169
+ }
1170
+ // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
1171
+ // next call is back on the cheap path.
1172
+ await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
1173
+ const movedDuringRecord = firstMovedClaim(verdict.observed);
1174
+ performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
1175
+ return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
1176
+ }
1177
+
1178
+ /**
1179
+ * The tasks a cache record must answer for. An `nx:noop` task has no command:
1180
+ * Nx completes it without spawning anything, and normalizes a command-less
1181
+ * target that only names dependencies to one. A cache record would vouch for
1182
+ * no work — the tasks it aggregates are in this graph and answer for
1183
+ * themselves — so none is required. Demanding one made every call through an
1184
+ * uncacheable aggregate hand the whole graph to Nx, which replays each
1185
+ * dependency's cached log. Outputs a noop declares are still verified, with
1186
+ * every other task's. `executorOf` is Nx's `getExecutorNameForTask` over the
1187
+ * project graph.
1188
+ */
1189
+ export function recordedTasks(tasks: readonly Task[], executorOf: (task: Task) => string): Task[] {
1190
+ return tasks.filter((task) => executorOf(task) !== 'nx:noop');
1191
+ }
1192
+
1193
+ /** What the working tree must hold: the declared outputs of every hashed task that declares any. */
1194
+ export function outputClaims(tasks: readonly Task[]): OutputClaim[] {
1195
+ const claims: OutputClaim[] = [];
1196
+ for (const task of tasks) {
1197
+ if (task.outputs.length > 0 && task.hash !== undefined) {
1198
+ claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
1199
+ }
1200
+ }
1201
+ return claims;
1202
+ }
1203
+
1204
+ /**
1205
+ * Whether restoring every claim from its cache artifact would leave the
1206
+ * working tree as it is. `artifacts` maps a hash to its artifact as Nx's local
1207
+ * cache lists it (`getBatch`). A working tree that already holds every entry of
1208
+ * each artifact is what the restore would leave: `restorable`, with each node
1209
+ * compared, so a write after the comparison can still be caught. Anything else
1210
+ * — no artifact, a difference, an error while reading, a write while comparing
1211
+ * — is the first claim it happened to.
1212
+ */
1213
+ export function compareWithArtifacts(
1214
+ workspaceRoot: string,
1215
+ claims: readonly OutputClaim[],
1216
+ artifacts: ReadonlyMap<string, { readonly outputsPath: string }>,
1217
+ expandOutputs: typeof NxNative.expandOutputs,
1218
+ witnessPath?: string,
1219
+ ): ArtifactVerdict {
1220
+ const binding = JSON.stringify(claims.map((claim) => [claim, artifacts.get(claim.hash)?.outputsPath]));
1221
+ const witness = witnessPath === undefined ? null : readArtifactWitness(witnessPath);
1222
+ if (
1223
+ witness !== null &&
1224
+ witness.version === 1 &&
1225
+ witness.workspaceRoot === workspaceRoot &&
1226
+ witness.claims === binding &&
1227
+ witness.nodes.length > 0 &&
1228
+ nodesUnchanged(witness.nodes)
1229
+ ) {
1230
+ try {
1231
+ const observed = witness.nodes.map(({ taskId, path }) => ({
1232
+ taskId,
1233
+ path,
1234
+ stats: lstatSync(path, { bigint: true }),
1235
+ }));
1236
+ // Recheck both node trees after collecting live stats. A read race
1237
+ // invalidates optional evidence and returns to the full comparison.
1238
+ if (observed.every((node, index) => nodeStamp(node.stats) === witness.nodes[index].stamp)) {
1239
+ return { kind: 'restorable', observed };
1240
+ }
1241
+ } catch (error) {
1242
+ if (!isSystemError(error)) throw error;
1243
+ }
1244
+ }
574
1245
  const comparison: ArtifactComparison = {
575
1246
  expandOutputs,
576
1247
  left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
@@ -578,37 +1249,40 @@ async function probe(
578
1249
  observed: [],
579
1250
  realDirectories: new Set(),
580
1251
  };
581
- for (const claim of doubted) {
582
- // `recordedTasks` excludes `nx:noop`, so a noop's declared outputs have no
583
- // artifact here and stay a miss.
584
- const artifact = cachedResults.get(claim.hash);
1252
+ for (const claim of claims) {
1253
+ const artifact = artifacts.get(claim.hash);
585
1254
  if (artifact === undefined || !outputsMatchArtifact(comparison, workspaceRoot, artifact.outputsPath, claim)) {
586
1255
  return { kind: 'stale-outputs', taskId: claim.taskId };
587
1256
  }
588
1257
  }
589
- // A write that landed while the bytes were being compared would otherwise be
590
- // recorded as current.
591
- const movedBeforeRecord = firstMovedClaim(comparison.observed);
592
- if (movedBeforeRecord !== null) {
593
- return { kind: 'stale-outputs', taskId: movedBeforeRecord };
1258
+ const moved = firstMovedClaim(comparison.observed);
1259
+ if (moved !== null) return { kind: 'stale-outputs', taskId: moved };
1260
+ if (witnessPath !== undefined) {
1261
+ const next: ArtifactWitness = {
1262
+ version: 1,
1263
+ workspaceRoot,
1264
+ claims: binding,
1265
+ nodes: comparison.observed.map(({ taskId, path, stats }) => ({ taskId, path, stamp: nodeStamp(stats) })),
1266
+ };
1267
+ writeWitness(witnessPath, next);
594
1268
  }
595
- // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
596
- // next call is back on the cheap path.
597
- await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
598
- const movedDuringRecord = firstMovedClaim(comparison.observed);
599
- performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
600
- return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
1269
+ return { kind: 'restorable', observed: comparison.observed };
601
1270
  }
602
1271
 
1272
+ /** What comparing the working tree with the cache artifacts found: the first stale claim, or every node compared. */
1273
+ export type ArtifactVerdict =
1274
+ | { readonly kind: 'stale-outputs'; readonly taskId: string }
1275
+ | { readonly kind: 'restorable'; readonly observed: readonly ObservedNode[] };
1276
+
603
1277
  /** A task's declared outputs, resolved to workspace-relative paths, and the hash they were cached under. */
604
- interface OutputClaim {
1278
+ export interface OutputClaim {
605
1279
  readonly taskId: string;
606
1280
  readonly outputs: string[];
607
1281
  readonly hash: string;
608
1282
  }
609
1283
 
610
- /** A working-tree node as it was when compared, so a later write to it can be noticed. */
611
- interface ObservedNode {
1284
+ /** A working-tree or cache node as compared, so a later write or replacement can be noticed. */
1285
+ export interface ObservedNode {
612
1286
  readonly taskId: string;
613
1287
  readonly path: string;
614
1288
  readonly stats: BigIntStats;
@@ -655,7 +1329,8 @@ function outputsMatchArtifact(
655
1329
  entries.length > 0 &&
656
1330
  entries.every(
657
1331
  (entry) =>
658
- throughRealDirectories(comparison.realDirectories, workspaceRoot, entry) &&
1332
+ throughRealDirectories(comparison, claim.taskId, workspaceRoot, entry) &&
1333
+ throughRealDirectories(comparison, claim.taskId, artifactRoot, entry) &&
659
1334
  sameTree(comparison, claim.taskId, join(workspaceRoot, entry), join(artifactRoot, entry)),
660
1335
  )
661
1336
  );
@@ -673,18 +1348,15 @@ function outputsMatchArtifact(
673
1348
  * an entry through one would compare bytes the restore does not leave there —
674
1349
  * and would read the filesystem behind a link.
675
1350
  */
676
- function throughRealDirectories(realDirectories: Set<string>, workspaceRoot: string, entry: string): boolean {
677
- let directory = workspaceRoot;
678
- const segments = entry.split('/');
679
- for (const segment of segments.slice(0, -1)) {
1351
+ function throughRealDirectories(comparison: ArtifactComparison, taskId: string, root: string, entry: string): boolean {
1352
+ let directory = root;
1353
+ for (const segment of ['', ...entry.split('/').slice(0, -1)]) {
680
1354
  directory = join(directory, segment);
681
- if (realDirectories.has(directory)) {
682
- continue;
683
- }
684
- if (lstatSync(directory, { throwIfNoEntry: false })?.isDirectory() !== true) {
685
- return false;
686
- }
687
- realDirectories.add(directory);
1355
+ if (comparison.realDirectories.has(directory)) continue;
1356
+ const stats = lstatSync(directory, { bigint: true, throwIfNoEntry: false });
1357
+ if (stats?.isDirectory() !== true) return false;
1358
+ comparison.observed.push({ taskId, path: directory, stats });
1359
+ comparison.realDirectories.add(directory);
688
1360
  }
689
1361
  return true;
690
1362
  }
@@ -703,6 +1375,7 @@ function sameTree(comparison: ArtifactComparison, taskId: string, actual: string
703
1375
  return false;
704
1376
  }
705
1377
  comparison.observed.push({ taskId, path: actual, stats: actualStats });
1378
+ comparison.observed.push({ taskId, path: expected, stats: expectedStats });
706
1379
  if (expectedStats.isSymbolicLink()) {
707
1380
  return actualStats.isSymbolicLink() && readlinkSync(actual) === readlinkSync(expected);
708
1381
  }
@@ -764,7 +1437,7 @@ function sameBytes(comparison: ArtifactComparison, actual: string, expected: str
764
1437
  * compared as: replaced (device, inode), rewritten (size, mtime) or otherwise
765
1438
  * touched (mode, ctime).
766
1439
  */
767
- function firstMovedClaim(observed: readonly ObservedNode[]): string | null {
1440
+ export function firstMovedClaim(observed: readonly ObservedNode[]): string | null {
768
1441
  for (const { taskId, path, stats } of observed) {
769
1442
  let current: BigIntStats | undefined;
770
1443
  try {