@smoothbricks/nx-plugin 0.4.26 → 0.4.28

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 (343) hide show
  1. package/README.md +994 -117
  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 +15 -73
  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 +3 -2
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +16 -15
  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 +43 -6
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +309 -73
  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 +101 -5
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +291 -10
  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 +207 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +576 -202
  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 +370 -138
  102. package/dist/managed-files/ci-workflow.d.ts +13 -0
  103. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.js +44 -13
  105. package/dist/managed-files/files.d.ts +7 -1
  106. package/dist/managed-files/files.d.ts.map +1 -1
  107. package/dist/managed-files/files.js +62 -8
  108. package/dist/managed-files/json-edit.d.ts +21 -0
  109. package/dist/managed-files/json-edit.d.ts.map +1 -0
  110. package/dist/managed-files/json-edit.js +295 -0
  111. package/dist/managed-files/managed-format.d.ts +33 -1
  112. package/dist/managed-files/managed-format.d.ts.map +1 -1
  113. package/dist/managed-files/managed-format.js +82 -2
  114. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  115. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  116. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  117. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/publish-workflow.js +231 -48
  119. package/dist/managed-files/tree.d.ts +5 -0
  120. package/dist/managed-files/tree.d.ts.map +1 -1
  121. package/dist/managed-files/tree.js +5 -3
  122. package/dist/manifest-hash.d.ts.map +1 -1
  123. package/dist/manifest-hash.js +2 -5
  124. package/dist/nextest-code-bound.d.ts +62 -0
  125. package/dist/nextest-code-bound.d.ts.map +1 -0
  126. package/dist/nextest-code-bound.js +154 -0
  127. package/dist/nextest-extraction.d.ts +24 -8
  128. package/dist/nextest-extraction.d.ts.map +1 -1
  129. package/dist/nextest-extraction.js +42 -7
  130. package/dist/package-target-policy.d.ts.map +1 -1
  131. package/dist/package-target-policy.js +3 -5
  132. package/dist/release-config-policy.d.ts.map +1 -1
  133. package/dist/release-config-policy.js +3 -5
  134. package/dist/runtime-input.d.ts +14 -0
  135. package/dist/runtime-input.d.ts.map +1 -0
  136. package/dist/runtime-input.js +15 -0
  137. package/dist/rust-tokens.d.ts +11 -0
  138. package/dist/rust-tokens.d.ts.map +1 -0
  139. package/dist/rust-tokens.js +81 -0
  140. package/dist/task-results.d.ts +6 -0
  141. package/dist/task-results.d.ts.map +1 -0
  142. package/dist/task-results.js +61 -0
  143. package/dist/test-program.d.ts +54 -0
  144. package/dist/test-program.d.ts.map +1 -0
  145. package/dist/test-program.js +292 -0
  146. package/dist/test-timings.d.ts +71 -0
  147. package/dist/test-timings.d.ts.map +1 -0
  148. package/dist/test-timings.js +393 -0
  149. package/dist/test-walk-policy.d.ts +103 -0
  150. package/dist/test-walk-policy.d.ts.map +1 -0
  151. package/dist/test-walk-policy.js +657 -0
  152. package/dist/testing.d.ts +177 -0
  153. package/dist/testing.d.ts.map +1 -0
  154. package/dist/testing.js +824 -0
  155. package/dist/type-guards.d.ts +3 -0
  156. package/dist/type-guards.d.ts.map +1 -0
  157. package/dist/type-guards.js +4 -0
  158. package/dist/typecheck-test-policy.d.ts +8 -5
  159. package/dist/typecheck-test-policy.d.ts.map +1 -1
  160. package/dist/typecheck-test-policy.js +113 -100
  161. package/dist/workspace-config-policy.d.ts +8 -0
  162. package/dist/workspace-config-policy.d.ts.map +1 -1
  163. package/dist/workspace-config-policy.js +175 -63
  164. package/dist/workspace-index.d.ts +12 -0
  165. package/dist/workspace-index.d.ts.map +1 -0
  166. package/dist/workspace-index.js +17 -0
  167. package/dist/workspace-manifest.d.ts +37 -0
  168. package/dist/workspace-manifest.d.ts.map +1 -1
  169. package/executors.json +5 -0
  170. package/loader.js +13 -11
  171. package/managed/raw/envrc +33 -67
  172. package/managed/raw/git-format-staged.yml +30 -35
  173. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  174. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  175. package/managed/raw/tooling/direnv/developer-links.ts +569 -11
  176. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
  177. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  178. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  179. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  180. package/managed/raw/tooling/direnv/repo-path +14 -2
  181. package/managed/raw/tooling/direnv/setup-environment.ts +1146 -159
  182. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  183. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  184. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  185. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  186. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  187. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  188. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  189. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  190. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  191. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  192. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  194. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  196. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  197. package/managed/templates/github/workflows/managed-files.yml +1 -2
  198. package/nextest.toml +8 -41
  199. package/package.json +74 -12
  200. package/runtime-input.sh +81 -0
  201. package/src/__tests__/counted-cargo.ts +33 -14
  202. package/src/__tests__/fixture-nx-env.ts +111 -46
  203. package/src/__tests__/progress-clock.ts +43 -0
  204. package/src/__tests__/runtime-input.ts +23 -0
  205. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  206. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  207. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  208. package/src/bin/smoo-nx-exec.ts +16 -74
  209. package/src/bin/smoo-nx-failures.ts +4 -0
  210. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  211. package/src/bin/smoo-nx-test-walks.ts +42 -0
  212. package/src/bounded-test-policy.test.ts +268 -21
  213. package/src/bounded-test-policy.ts +114 -42
  214. package/src/build-state-output-policy.test.ts +115 -0
  215. package/src/build-state-output-policy.ts +90 -0
  216. package/src/bun-bootstrap.test.ts +57 -3
  217. package/src/bun-file-packages.test.ts +47 -0
  218. package/src/cargo-closure-input.test.ts +138 -66
  219. package/src/cargo-closure-input.ts +17 -15
  220. package/src/cargo-dev-profile.ts +137 -0
  221. package/src/cargo-resolve-cache.test.ts +70 -1
  222. package/src/cargo-source-hash.test.ts +193 -97
  223. package/src/cargo-source-hash.ts +345 -68
  224. package/src/cargo-toolchain-policy.ts +6 -0
  225. package/src/cargo-workspace.test.ts +320 -0
  226. package/src/cargo-workspace.ts +336 -10
  227. package/src/cross-check-policy.test.ts +59 -1
  228. package/src/cross-check-policy.ts +25 -6
  229. package/src/disk-command-policy.test.ts +61 -0
  230. package/src/disk-command-policy.ts +107 -0
  231. package/src/ensure-built-receipt.test.ts +184 -0
  232. package/src/ensure-built-receipt.ts +263 -0
  233. package/src/ensure-built-witness.test.ts +344 -0
  234. package/src/ensure-built-witness.ts +252 -0
  235. package/src/ensure-built.test.ts +1036 -578
  236. package/src/ensure-built.ts +820 -224
  237. package/src/eslint-file-set.ts +9 -0
  238. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  239. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  240. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  241. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  242. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  243. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  244. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  245. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  246. package/src/executors/bounded-exec/executor.ts +565 -67
  247. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  248. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  249. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  250. package/src/executors/bounded-exec/progress.test.ts +79 -0
  251. package/src/executors/bounded-exec/progress.ts +118 -0
  252. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  253. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  254. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  255. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  256. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  257. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  258. package/src/executors/bounded-exec/schema.json +11 -5
  259. package/src/executors/bounded-exec/schema.ts +27 -11
  260. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  261. package/src/executors/bounded-exec/verdict.ts +433 -0
  262. package/src/executors/shard-timings/executor.ts +36 -0
  263. package/src/executors/shard-timings/schema.json +21 -0
  264. package/src/executors/shard-timings/schema.ts +4 -0
  265. package/src/failure-summary.ts +119 -0
  266. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  267. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  268. package/src/generators/create-package/generator.test.ts +6 -0
  269. package/src/generators/create-package/generator.ts +6 -2
  270. package/src/host-controller-wiring.test.ts +31 -0
  271. package/src/index.test.ts +909 -295
  272. package/src/index.ts +441 -143
  273. package/src/linked-snapshots.nx.test.ts +172 -0
  274. package/src/managed-files/ci-workflow.ts +54 -13
  275. package/src/managed-files/files.ts +65 -9
  276. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  277. package/src/managed-files/generator.test.ts +60 -1
  278. package/src/managed-files/json-edit.test.ts +216 -0
  279. package/src/managed-files/json-edit.ts +290 -0
  280. package/src/managed-files/managed-format-json.test.ts +308 -0
  281. package/src/managed-files/managed-format.ts +90 -2
  282. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  283. package/src/managed-files/publish-workflow.ts +285 -67
  284. package/src/managed-files/repo-path.test.ts +129 -0
  285. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  286. package/src/managed-files/tree.ts +11 -3
  287. package/src/manifest-hash.ts +2 -6
  288. package/src/nextest-code-bound.test.ts +155 -0
  289. package/src/nextest-code-bound.ts +195 -0
  290. package/src/nextest-extraction.test.ts +37 -5
  291. package/src/nextest-extraction.ts +51 -9
  292. package/src/nextest-partitions.test.ts +427 -82
  293. package/src/nx-cache-bound.test.ts +5 -10
  294. package/src/nx-cache-restore-mtime.test.ts +170 -0
  295. package/src/nx-daemon-claim.test.ts +277 -0
  296. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  297. package/src/nx-daemon-idle-policy.test.ts +51 -0
  298. package/src/nx-daemon-ignore-files.test.ts +210 -0
  299. package/src/nx-daemon-root-normalization.test.ts +105 -0
  300. package/src/nx-daemon-validity.test.ts +925 -0
  301. package/src/nx-dot-input-policy.test.ts +353 -0
  302. package/src/nx-failure-life-cycles.test.ts +207 -0
  303. package/src/nx-files-archive-racy.test.ts +97 -0
  304. package/src/nx-foreign-workspace-env.test.ts +329 -0
  305. package/src/nx-incomplete-run-summary.test.ts +70 -0
  306. package/src/nx-plugin-load-errors.test.ts +120 -0
  307. package/src/nx-project-resolution-cache.test.ts +176 -0
  308. package/src/nx-raw-inputs.test.ts +166 -0
  309. package/src/nx-resident-graph-workers.test.ts +7 -16
  310. package/src/nx-run-information.test.ts +118 -0
  311. package/src/nx-skipped-task-exit.test.ts +180 -0
  312. package/src/nx-state-location.test.ts +63 -0
  313. package/src/nx-store-resolution.test.ts +26 -25
  314. package/src/nx-task-graph-cycles.test.ts +148 -0
  315. package/src/nx-task-output-lease.test.ts +414 -0
  316. package/src/nx-use-local.test.ts +103 -0
  317. package/src/package-target-policy.ts +3 -6
  318. package/src/release-config-policy.ts +3 -6
  319. package/src/runtime-input.test.ts +215 -0
  320. package/src/runtime-input.ts +17 -0
  321. package/src/rust-tokens.ts +78 -0
  322. package/src/serial-group-wiring.test.ts +326 -0
  323. package/src/task-history-namespace.test.ts +87 -89
  324. package/src/task-results.test.ts +226 -0
  325. package/src/task-results.ts +75 -0
  326. package/src/test-program.test.ts +303 -0
  327. package/src/test-program.ts +341 -0
  328. package/src/test-timings.test.ts +240 -0
  329. package/src/test-timings.ts +311 -0
  330. package/src/test-walk-policy.test.ts +585 -0
  331. package/src/test-walk-policy.ts +731 -0
  332. package/src/testing.test.ts +509 -0
  333. package/src/testing.ts +922 -0
  334. package/src/type-guards.ts +4 -0
  335. package/src/typecheck-test-policy.test.ts +496 -11
  336. package/src/typecheck-test-policy.ts +137 -103
  337. package/src/workspace-config-policy.test.ts +212 -13
  338. package/src/workspace-config-policy.ts +101 -73
  339. package/src/workspace-index.ts +18 -0
  340. package/src/workspace-manifest.ts +41 -0
  341. package/test-timings.json +85 -30
  342. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
  343. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
@@ -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, realpathSync } 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
16
  import { dirname, isAbsolute, join, resolve } from 'node:path';
7
-
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,48 +396,48 @@ 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 };
242
- // Whether the environment this process was launched with belongs to the
243
- // workspace asked for, decided before the chdir and the rebinding below. The
244
- // caller's workspace is the one its own Nx would bind: `NX_WORKSPACE_ROOT_PATH`
245
- // when set, else the nearest `nx.json` above where it stands (none, when it
246
- // stands in no workspace).
247
- const callerRoot = callerEnv.NX_WORKSPACE_ROOT_PATH
248
- ? resolve(callerCwd, callerEnv.NX_WORKSPACE_ROOT_PATH)
249
- : findNxWorkspaceRoot(callerCwd);
250
- const callerOwnsRoot = callerRoot !== null && isSameDirectory(callerRoot, workspaceRoot);
419
+ const callerOwnsRoot = callerOwnsWorkspace(callerEnv, callerCwd, workspaceRoot);
251
420
  try {
252
- // `cd <root> && nx run ...` is the invocation whose hashes are in the
253
- // cache. Everything below — plugin hooks, `runtime` inputs, and the hasher,
254
- // which keys against `process.cwd()` — has to see the same directory or the
255
- // probe keys differently from the CLI and never hits.
421
+ // Nx's hooks, runtime inputs and hasher see the provider's own workspace.
256
422
  process.chdir(workspaceRoot);
257
- return await ensureBuiltInWorkspace(workspaceRoot, selector, options.onMiss ?? 'stream', callerOwnsRoot);
258
- } finally {
259
- // Nx configures itself through the environment and this function runs
260
- // in-process, so without this the caller — and anything it goes on to exec
261
- // — would inherit NX_WORKSPACE_ROOT_PATH, the stream/prefix flags, and
262
- // whatever a plugin's preTasksExecution hook injected. The wrapper this
263
- // replaces got that isolation for free by running Nx in a child.
264
- process.chdir(callerCwd);
265
- for (const key of Object.keys(process.env)) {
266
- if (!(key in callerEnv)) {
267
- delete process.env[key];
268
- }
269
- }
270
- for (const [key, value] of Object.entries(callerEnv)) {
271
- if (process.env[key] !== value) {
272
- process.env[key] = value;
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;
273
432
  }
274
433
  }
434
+ assignEnvironment(process.env, environment);
435
+ return await operation(workspaceRoot, selector);
436
+ } finally {
437
+ // Never lend this provider's cwd, Nx binding or hook-injected environment
438
+ // to the caller or whatever it executes after this operation.
439
+ process.chdir(callerCwd);
440
+ assignEnvironment(process.env, callerEnv);
275
441
  }
276
442
  }
277
443
 
@@ -292,6 +458,97 @@ const WORKSPACE_STATE_ENV_KEYS = [
292
458
  'NX_CACHE_DIRECTORY',
293
459
  ] as const;
294
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
+
295
552
  /**
296
553
  * The nearest directory at or above `from` holding an `nx.json`, or null.
297
554
  * The marker this wrapper roots a workspace at when `--workspace-root` is
@@ -322,84 +579,144 @@ function isSameDirectory(left: string, right: string): boolean {
322
579
  }
323
580
 
324
581
  /**
325
- * Point every Nx this call starts — the daemon client loaded below and the
326
- * `nx` CLI a miss spawns — at `workspaceRoot`, and give it that workspace's
327
- * state locations. A caller that was set up for this same root keeps its
328
- * overrides: they are the owner's cache and sandbox boundary. A caller set up
329
- * for another root, or for none, passes none of them on, so Nx falls back to
330
- * the per-root defaults. Nothing is reset or bypassed: the root's own daemon
331
- * and cache are used as they stand. `ensureBuilt` restores the caller's
332
- * environment when it returns.
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.
333
587
  */
334
- function isolateWorkspaceEnvironment(workspaceRoot: string, callerOwnsRoot: boolean): void {
335
- process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot;
336
- if (callerOwnsRoot) {
337
- return;
338
- }
339
- for (const key of WORKSPACE_STATE_ENV_KEYS) {
340
- delete process.env[key];
341
- }
588
+ export function callerOwnsWorkspace(
589
+ callerEnv: Readonly<NodeJS.ProcessEnv>,
590
+ callerCwd: string,
591
+ workspaceRoot: string,
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);
342
597
  }
343
598
 
344
- async function ensureBuiltInWorkspace(
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>,
345
613
  workspaceRoot: string,
346
- selector: TargetSelector,
347
- onMiss: 'stream',
348
614
  callerOwnsRoot: boolean,
349
- ): Promise<EnsureBuiltResult> {
350
- isolateWorkspaceEnvironment(workspaceRoot, callerOwnsRoot);
351
- // The environment has precedence over nx.json. Avoid loading Nx at all on
352
- // this explicit fallback path, which also lets a deliberately minimal
353
- // checkout-local CLI stand in for Nx.
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.
354
690
  if (process.env.NX_DAEMON === 'false') {
355
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
691
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
356
692
  }
357
- // Nx rejects daemon access when the client package's version differs from
358
- // the workspace's running daemon. Resolve every runtime module from the
359
- // target checkout rather than this package's own dependency: a reusable
360
- // 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.
361
694
  const requireNx = createWorkspaceNxRequire(workspaceRoot);
362
695
  bindWorkspaceRoot(workspaceRoot, requireNx);
363
696
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
364
697
  if (!daemonClient.enabled()) {
365
- return runViaCli(workspaceRoot, selector, { kind: 'no-daemon', taskId: selectorTaskId(selector) });
366
- }
367
-
368
- // An outer Nx task exports its own cache-bypass setting to the process it
369
- // launches. That setting describes the outer task, not the nested target:
370
- // inheriting it makes the inner runner refuse cache writes forever. Outside
371
- // an Nx task child, however, the same variables are the caller's explicit
372
- // request to force this target to rebuild and must remain authoritative.
373
- if (process.env.NX_TASK_TARGET_PROJECT !== undefined || process.env.NX_TASK_TARGET_TARGET !== undefined) {
374
- delete process.env.NX_SKIP_NX_CACHE;
375
- delete process.env.NX_DISABLE_NX_CACHE;
698
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
376
699
  }
700
+ assignEnvironment(process.env, withoutOuterTaskCacheBypass(process.env));
377
701
 
378
702
  const { readNxJson } = requireNx('nx/src/config/nx-json');
379
703
  const { splitArgsIntoNxArgsAndOverrides } = requireNx('nx/src/utils/command-line-utils');
380
704
  const { setEnvVarsBasedOnArgs } = requireNx('nx/src/tasks-runner/run-command');
381
705
  const { createTaskGraph } = requireNx('nx/src/tasks-runner/create-task-graph');
382
- const hooks = requireNx('nx/src/project-graph/plugins/tasks-execution-hooks');
383
-
384
- const nxJson = readNxJson();
385
- // Reproduce `nx run <selector> --outputStyle=<style>` exactly. Task hashes
386
- // are derived from these arguments, so anything hand-rolled here instead of
387
- // routed through Nx's own argument normalization would key the cache
388
- // 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.
389
708
  const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
390
- { targets: [selector.target], configuration: selector.configuration, outputStyle: onMiss },
709
+ { targets: [selector.target], configuration: selector.configuration, outputStyle: 'stream' },
391
710
  'run-one',
392
711
  { printWarnings: false },
393
712
  nxJson,
394
713
  );
395
714
  const loadDotEnvFiles = process.env.NX_LOAD_DOT_ENV_FILES !== 'false';
396
- // Sets NX_LOAD_DOT_ENV_FILES and the stream/prefix flags, which the hasher
397
- // reads through each task's environment. It has to happen before hashing or
398
- // the probe keys differently from the CLI.
399
715
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
400
716
 
401
717
  performance.mark('ensureBuilt:graph:start');
402
- const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
718
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx);
719
+ nxJson = readNxJson();
403
720
  requireProject(projectGraph, selector.project);
404
721
  const taskGraph = createTaskGraph(
405
722
  projectGraph,
@@ -412,40 +729,32 @@ async function ensureBuiltInWorkspace(
412
729
  );
413
730
  const tasks = Object.values(taskGraph.tasks);
414
731
  performance.measure('ensureBuilt:graph', 'ensureBuilt:graph:start');
732
+ return { kind: 'ready', nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx };
733
+ }
415
734
 
416
- const inputsChanged = await refreshWorkspaceContext(workspaceRoot, tasks, requireNx);
417
-
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');
418
742
  const runId = randomUUID();
419
743
  const startTime = Date.now();
420
- // Plugin `preTasksExecution` hooks inject environment variables, and declared
421
- // `env` inputs hash against them. Skipping the hook would not merely cost
422
- // hits: the run below would then execute with a different environment than
423
- // 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.
424
745
  await hooks.runPreTasksExecution({
425
746
  id: runId,
426
747
  workspaceRoot,
427
- nxJsonConfiguration: nxJson,
748
+ nxJsonConfiguration: prepared.nxJson,
428
749
  argv: process.argv,
429
750
  });
430
-
431
- performance.mark('ensureBuilt:probe:start');
432
- let reason: MissReason | null;
433
- if (process.env.NX_SKIP_NX_CACHE === 'true' || process.env.NX_DISABLE_NX_CACHE === 'true') {
434
- reason = { kind: 'cache-disabled', taskId: selectorTaskId(selector) };
435
- } else if (inputsChanged) {
436
- reason = { kind: 'stale-inputs', taskId: selectorTaskId(selector) };
437
- } else {
438
- reason = await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx);
439
- }
440
- performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
441
- // The CLI runs the hooks around its own run, under its own id. This process
442
- // ran none of the tasks, so it reports none for the id it opened.
443
- 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.
444
753
  await hooks.runPostTasksExecution({
445
754
  id: runId,
446
755
  taskResults: NO_TASK_RESULTS,
447
756
  workspaceRoot,
448
- nxJsonConfiguration: nxJson,
757
+ nxJsonConfiguration: prepared.nxJson,
449
758
  argv: process.argv,
450
759
  startTime,
451
760
  endTime: Date.now(),
@@ -453,7 +762,192 @@ async function ensureBuiltInWorkspace(
453
762
  return result;
454
763
  }
455
764
 
456
- 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 {
457
951
  return selector.configuration === undefined
458
952
  ? `${selector.project}:${selector.target}`
459
953
  : `${selector.project}:${selector.target}:${selector.configuration}`;
@@ -465,8 +959,8 @@ function selectorTaskId(selector: TargetSelector): string {
465
959
  * a module-level singleton that reads `nx.json` from that root. So the root has
466
960
  * to be bound before the first Nx module loads, which is why this module
467
961
  * imports Nx dynamically throughout. A static import at the top of the file
468
- * would bind the caller's launch directory instead. `isolateWorkspaceEnvironment`
469
- * has already set `NX_WORKSPACE_ROOT_PATH`; this loads Nx and checks it took.
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.
470
964
  */
471
965
  function bindWorkspaceRoot(workspaceRoot: string, requireNx: WorkspaceNxRequire): void {
472
966
  if (process.env.NX_PERF_LOGGING === 'true') {
@@ -486,62 +980,88 @@ function bindWorkspaceRoot(workspaceRoot: string, requireNx: WorkspaceNxRequire)
486
980
  }
487
981
  }
488
982
 
489
- /**
490
- * The daemon drains delivered watcher events before serving its graph, but an
491
- * OS event can still be in flight after a write has completed. Its graph and
492
- * hashes can then agree with each other while both describe yesterday's files.
493
- * Take Nx's native, ignore-aware disk snapshot and compare it with the
494
- * daemon's file table; its metadata cache reuses unchanged file hashes.
495
- *
496
- * Any difference is handed to the daemon so its next graph is current, but
497
- * that only schedules a recomputation, and a probe meanwhile would hash
498
- * against the file map it already has. So the caller does not probe: if any
499
- * differing path is not a declared output of a task in the graph, this
500
- * returns true and the target is run, which lets Nx's own runner wait for the
501
- * recomputation. Output paths are exempt because a build's own writes are the
502
- * commonest thing to reach here before the watcher does, and treating them as
503
- * 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.
504
987
  */
505
- async function refreshWorkspaceContext(
988
+ async function currentProjectGraph(
506
989
  workspaceRoot: string,
507
- tasks: Task[],
990
+ nxJson: NxJsonConfiguration,
508
991
  requireNx: WorkspaceNxRequire,
509
- ): Promise<boolean> {
992
+ ): Promise<ProjectGraph> {
510
993
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
511
- const { WorkspaceContext, matchOutputPaths } = requireNx('nx/src/native');
994
+ const { WorkspaceContext } = requireNx('nx/src/native');
512
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');
513
1003
  performance.mark('ensureBuilt:inputs:start');
514
- const previousFiles = await daemonClient.getWorkspaceContextFileData();
515
- const previousHashes = new Map<string, string>();
516
- for (const { file, hash } of previousFiles) {
517
- 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);
518
1011
  }
519
- // A context is a snapshot, not a watcher. Do not reuse one across calls.
520
- const context = new WorkspaceContext(workspaceRoot, workspaceDataDirectoryForWorkspace(workspaceRoot));
521
- const createdFiles: string[] = [];
522
- const updatedFiles: string[] = [];
523
- for (const { file, hash } of context.allFileData()) {
524
- const previousHash = previousHashes.get(file);
525
- if (previousHash === undefined) {
526
- createdFiles.push(file);
527
- } else if (previousHash !== hash) {
528
- 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);
529
1061
  }
530
- previousHashes.delete(file);
531
- }
532
- const deletedFiles = [...previousHashes.keys()];
533
- const changed = [...createdFiles, ...updatedFiles, ...deletedFiles];
534
- let inputsChanged = false;
535
- if (changed.length > 0) {
536
- await daemonClient.updateWorkspaceContext(createdFiles, updatedFiles, deletedFiles);
537
- // The same matcher the task runner uses to collect outputs, over the union
538
- // of every task's declared outputs. A negation declared by one task then
539
- // also excludes another task's output, which errs toward a miss.
540
- const outputs = tasks.flatMap((task) => task.outputs);
541
- inputsChanged = matchOutputPaths(outputs, changed).includes(false);
1062
+ daemonHashes.delete(file);
542
1063
  }
543
- performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
544
- return inputsChanged;
1064
+ return { created, updated, deleted: [...daemonHashes.keys()] };
545
1065
  }
546
1066
 
547
1067
  /** `null` when the target is already built; otherwise the first reason it is not. */
@@ -553,6 +1073,7 @@ async function probe(
553
1073
  taskGraph: TaskGraph,
554
1074
  tasks: Task[],
555
1075
  requireNx: WorkspaceNxRequire,
1076
+ verifyAllArtifacts = false,
556
1077
  ): Promise<MissReason | null> {
557
1078
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
558
1079
  const { DaemonBasedTaskHasher } = requireNx('nx/src/hasher/task-hasher');
@@ -561,6 +1082,7 @@ async function probe(
561
1082
  const { getCache } = requireNx('nx/src/tasks-runner/cache');
562
1083
  const { getRunnerOptions } = requireNx('nx/src/tasks-runner/run-command');
563
1084
  const { getExecutorNameForTask } = requireNx('nx/src/tasks-runner/utils');
1085
+ const { expandOutputs } = requireNx('nx/src/native');
564
1086
 
565
1087
  // `isCloudDefault: false` because these options are only read here by the
566
1088
  // hasher, which looks at `selectivelyHashTsConfig`; the cloud credentials the
@@ -588,14 +1110,7 @@ async function probe(
588
1110
  await hashTasks(hasher, projectGraph, taskGraph, perTaskEnvs, getTaskDetails(), tasks);
589
1111
  performance.measure('ensureBuilt:hash', 'ensureBuilt:hash:start');
590
1112
 
591
- // An `nx:noop` task has no command: Nx completes it without spawning
592
- // anything, and normalizes a command-less target that only names dependencies
593
- // to one. A cache record would vouch for no work — the tasks it aggregates are
594
- // in this graph and answer for themselves — so none is required. Demanding one
595
- // made every call through an uncacheable aggregate hand the whole graph to Nx,
596
- // which replays each dependency's cached log. Outputs a noop declares are
597
- // still verified below, with every other task's.
598
- const recordedTasks = tasks.filter((task) => getExecutorNameForTask(task, projectGraph) !== 'nx:noop');
1113
+ const recorded = recordedTasks(tasks, (task) => getExecutorNameForTask(task, projectGraph));
599
1114
 
600
1115
  // Nx's own factory, but deliberately never `init()`ed: that is what makes
601
1116
  // this a local-only, side-effect-free question. `init()` attaches the remote
@@ -605,12 +1120,12 @@ async function probe(
605
1120
  // outputs against, and an artifact missing from it is a miss.
606
1121
  performance.mark('ensureBuilt:cache:start');
607
1122
  const cache = getCache(runnerOptions);
608
- 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));
609
1124
  const cachedCodeByHash = new Map<string, number>();
610
1125
  for (const [hash, result] of cachedResults) {
611
1126
  cachedCodeByHash.set(hash, result.code);
612
1127
  }
613
- const cacheMiss = firstCacheMiss(recordedTasks, cachedCodeByHash);
1128
+ const cacheMiss = firstCacheMiss(recorded, cachedCodeByHash);
614
1129
  performance.measure('ensureBuilt:cache', 'ensureBuilt:cache:start');
615
1130
  if (cacheMiss) {
616
1131
  return cacheMiss;
@@ -621,16 +1136,11 @@ async function probe(
621
1136
  // lives in the working tree, where anything may have deleted or rewritten it
622
1137
  // since. The daemon answers that cheaply from the output hashes it recorded
623
1138
  // when Nx last wrote them.
624
- const claims: OutputClaim[] = [];
625
- for (const task of tasks) {
626
- if (task.outputs.length > 0 && task.hash !== undefined) {
627
- claims.push({ taskId: task.id, outputs: task.outputs, hash: task.hash });
628
- }
629
- }
1139
+ const claims = outputClaims(tasks);
630
1140
  performance.mark('ensureBuilt:outputs:start');
631
1141
  const verdicts = await daemonClient.outputsHashesMatchBatch(claims.map(({ outputs, hash }) => ({ outputs, hash })));
632
1142
  performance.measure('ensureBuilt:outputs', 'ensureBuilt:outputs:start');
633
- const doubted = unvouched(claims, verdicts);
1143
+ const doubted = verifyAllArtifacts ? claims : unvouched(claims, verdicts);
634
1144
  if (doubted.length === 0) {
635
1145
  return null;
636
1146
  }
@@ -642,12 +1152,96 @@ async function probe(
642
1152
  // And a write the daemon processes more than 2 s after a record erases it,
643
1153
  // which a daemon busy hashing does to a restore's own writes — so each
644
1154
  // restore guarantees the next one. What settles the question is what a hit
645
- // would restore: Nx's own local artifact for this exact hash. A working tree
646
- // that already holds every entry of it is what the restore would leave.
647
- // Anything else — no artifact, a difference, an error while reading — stays
648
- // a miss.
1155
+ // would restore: Nx's own local artifact for this exact hash.
649
1156
  performance.mark('ensureBuilt:artifacts:start');
650
- 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
+ }
651
1245
  const comparison: ArtifactComparison = {
652
1246
  expandOutputs,
653
1247
  left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
@@ -655,37 +1249,40 @@ async function probe(
655
1249
  observed: [],
656
1250
  realDirectories: new Set(),
657
1251
  };
658
- for (const claim of doubted) {
659
- // `recordedTasks` excludes `nx:noop`, so a noop's declared outputs have no
660
- // artifact here and stay a miss.
661
- const artifact = cachedResults.get(claim.hash);
1252
+ for (const claim of claims) {
1253
+ const artifact = artifacts.get(claim.hash);
662
1254
  if (artifact === undefined || !outputsMatchArtifact(comparison, workspaceRoot, artifact.outputsPath, claim)) {
663
1255
  return { kind: 'stale-outputs', taskId: claim.taskId };
664
1256
  }
665
1257
  }
666
- // A write that landed while the bytes were being compared would otherwise be
667
- // recorded as current.
668
- const movedBeforeRecord = firstMovedClaim(comparison.observed);
669
- if (movedBeforeRecord !== null) {
670
- 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);
671
1268
  }
672
- // Re-arm the daemon the way Nx's runner does after restoring outputs, so the
673
- // next call is back on the cheap path.
674
- await daemonClient.recordOutputsHashBatch(doubted.map(({ outputs, hash }) => ({ outputs, hash })));
675
- const movedDuringRecord = firstMovedClaim(comparison.observed);
676
- performance.measure('ensureBuilt:artifacts', 'ensureBuilt:artifacts:start');
677
- return movedDuringRecord === null ? null : { kind: 'stale-outputs', taskId: movedDuringRecord };
1269
+ return { kind: 'restorable', observed: comparison.observed };
678
1270
  }
679
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
+
680
1277
  /** A task's declared outputs, resolved to workspace-relative paths, and the hash they were cached under. */
681
- interface OutputClaim {
1278
+ export interface OutputClaim {
682
1279
  readonly taskId: string;
683
1280
  readonly outputs: string[];
684
1281
  readonly hash: string;
685
1282
  }
686
1283
 
687
- /** A working-tree node as it was when compared, so a later write to it can be noticed. */
688
- interface ObservedNode {
1284
+ /** A working-tree or cache node as compared, so a later write or replacement can be noticed. */
1285
+ export interface ObservedNode {
689
1286
  readonly taskId: string;
690
1287
  readonly path: string;
691
1288
  readonly stats: BigIntStats;
@@ -732,7 +1329,8 @@ function outputsMatchArtifact(
732
1329
  entries.length > 0 &&
733
1330
  entries.every(
734
1331
  (entry) =>
735
- throughRealDirectories(comparison.realDirectories, workspaceRoot, entry) &&
1332
+ throughRealDirectories(comparison, claim.taskId, workspaceRoot, entry) &&
1333
+ throughRealDirectories(comparison, claim.taskId, artifactRoot, entry) &&
736
1334
  sameTree(comparison, claim.taskId, join(workspaceRoot, entry), join(artifactRoot, entry)),
737
1335
  )
738
1336
  );
@@ -750,18 +1348,15 @@ function outputsMatchArtifact(
750
1348
  * an entry through one would compare bytes the restore does not leave there —
751
1349
  * and would read the filesystem behind a link.
752
1350
  */
753
- function throughRealDirectories(realDirectories: Set<string>, workspaceRoot: string, entry: string): boolean {
754
- let directory = workspaceRoot;
755
- const segments = entry.split('/');
756
- 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)]) {
757
1354
  directory = join(directory, segment);
758
- if (realDirectories.has(directory)) {
759
- continue;
760
- }
761
- if (lstatSync(directory, { throwIfNoEntry: false })?.isDirectory() !== true) {
762
- return false;
763
- }
764
- 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);
765
1360
  }
766
1361
  return true;
767
1362
  }
@@ -780,6 +1375,7 @@ function sameTree(comparison: ArtifactComparison, taskId: string, actual: string
780
1375
  return false;
781
1376
  }
782
1377
  comparison.observed.push({ taskId, path: actual, stats: actualStats });
1378
+ comparison.observed.push({ taskId, path: expected, stats: expectedStats });
783
1379
  if (expectedStats.isSymbolicLink()) {
784
1380
  return actualStats.isSymbolicLink() && readlinkSync(actual) === readlinkSync(expected);
785
1381
  }
@@ -841,7 +1437,7 @@ function sameBytes(comparison: ArtifactComparison, actual: string, expected: str
841
1437
  * compared as: replaced (device, inode), rewritten (size, mtime) or otherwise
842
1438
  * touched (mode, ctime).
843
1439
  */
844
- function firstMovedClaim(observed: readonly ObservedNode[]): string | null {
1440
+ export function firstMovedClaim(observed: readonly ObservedNode[]): string | null {
845
1441
  for (const { taskId, path, stats } of observed) {
846
1442
  let current: BigIntStats | undefined;
847
1443
  try {