@smoothbricks/nx-plugin 0.5.6 → 0.6.0

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 (440) hide show
  1. package/README.md +1095 -360
  2. package/dist/.build-inputs.json +138 -0
  3. package/dist/bin/smoo-nx-cargo-toolchain.d.ts +3 -0
  4. package/dist/bin/smoo-nx-cargo-toolchain.d.ts.map +1 -0
  5. package/dist/bin/smoo-nx-cargo-toolchain.js +8 -0
  6. package/dist/bin/smoo-nx-forbidden-spellings.d.ts +3 -0
  7. package/dist/bin/smoo-nx-forbidden-spellings.d.ts.map +1 -0
  8. package/dist/bin/smoo-nx-forbidden-spellings.js +32 -0
  9. package/dist/bin/smoo-nx-nextest-timings.d.ts +3 -0
  10. package/dist/bin/smoo-nx-nextest-timings.d.ts.map +1 -0
  11. package/dist/bin/smoo-nx-nextest-timings.js +28 -0
  12. package/dist/bounded-test-policy.d.ts +1 -1
  13. package/dist/bounded-test-policy.d.ts.map +1 -1
  14. package/dist/bounded-test-policy.js +14 -15
  15. package/dist/build-state-output-policy.d.ts +95 -1
  16. package/dist/build-state-output-policy.d.ts.map +1 -1
  17. package/dist/build-state-output-policy.js +368 -30
  18. package/dist/bun-lock-scope.d.ts +44 -0
  19. package/dist/bun-lock-scope.d.ts.map +1 -0
  20. package/dist/bun-lock-scope.js +97 -0
  21. package/dist/cargo-build-script-env.d.ts +49 -0
  22. package/dist/cargo-build-script-env.d.ts.map +1 -0
  23. package/dist/cargo-build-script-env.js +324 -0
  24. package/dist/cargo-closure-input.d.ts +31 -4
  25. package/dist/cargo-closure-input.d.ts.map +1 -1
  26. package/dist/cargo-closure-input.js +66 -6
  27. package/dist/cargo-retention-hook.d.ts +17 -0
  28. package/dist/cargo-retention-hook.d.ts.map +1 -0
  29. package/dist/cargo-retention-hook.js +85 -0
  30. package/dist/cargo-source-hash.d.ts +25 -7
  31. package/dist/cargo-source-hash.d.ts.map +1 -1
  32. package/dist/cargo-source-hash.js +108 -43
  33. package/dist/cargo-toolchain-paths.d.ts +40 -0
  34. package/dist/cargo-toolchain-paths.d.ts.map +1 -0
  35. package/dist/cargo-toolchain-paths.js +84 -0
  36. package/dist/cargo-toolchain-policy.d.ts +25 -8
  37. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  38. package/dist/cargo-toolchain-policy.js +39 -5
  39. package/dist/cargo-workspace.d.ts +10 -4
  40. package/dist/cargo-workspace.d.ts.map +1 -1
  41. package/dist/cargo-workspace.js +65 -95
  42. package/dist/cross-check-policy.d.ts +9 -9
  43. package/dist/cross-check-policy.d.ts.map +1 -1
  44. package/dist/cross-check-policy.js +11 -11
  45. package/dist/disk-command-policy.d.ts.map +1 -1
  46. package/dist/disk-command-policy.js +1 -0
  47. package/dist/ensure-built-receipt.d.ts +13 -5
  48. package/dist/ensure-built-receipt.d.ts.map +1 -1
  49. package/dist/ensure-built-receipt.js +126 -47
  50. package/dist/ensure-built-witness.d.ts +27 -8
  51. package/dist/ensure-built-witness.d.ts.map +1 -1
  52. package/dist/ensure-built-witness.js +45 -15
  53. package/dist/ensure-built.d.ts.map +1 -1
  54. package/dist/ensure-built.js +9 -1
  55. package/dist/eslint-config-closure.d.ts +7 -0
  56. package/dist/eslint-config-closure.d.ts.map +1 -0
  57. package/dist/eslint-config-closure.js +346 -0
  58. package/dist/executors/all-verdicts/executor.d.ts +35 -0
  59. package/dist/executors/all-verdicts/executor.d.ts.map +1 -0
  60. package/dist/executors/all-verdicts/executor.js +133 -0
  61. package/dist/executors/all-verdicts/schema.d.ts +10 -0
  62. package/dist/executors/all-verdicts/schema.d.ts.map +1 -0
  63. package/dist/executors/all-verdicts/schema.js +2 -0
  64. package/dist/executors/bounded-exec/bounds.d.ts +56 -0
  65. package/dist/executors/bounded-exec/bounds.d.ts.map +1 -0
  66. package/dist/executors/bounded-exec/bounds.js +43 -0
  67. package/dist/executors/bounded-exec/cpu-pool.d.ts +33 -36
  68. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  69. package/dist/executors/bounded-exec/cpu-pool.js +54 -119
  70. package/dist/executors/bounded-exec/executor.d.ts +19 -12
  71. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  72. package/dist/executors/bounded-exec/executor.js +371 -239
  73. package/dist/executors/bounded-exec/gateway-lease.d.ts +22 -19
  74. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -1
  75. package/dist/executors/bounded-exec/gateway-lease.js +81 -28
  76. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +18 -1
  77. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
  78. package/dist/executors/bounded-exec/hang-diagnostics.js +148 -30
  79. package/dist/executors/bounded-exec/lease-wait.d.ts +43 -0
  80. package/dist/executors/bounded-exec/lease-wait.d.ts.map +1 -0
  81. package/dist/executors/bounded-exec/lease-wait.js +68 -0
  82. package/dist/executors/bounded-exec/lock-wait.d.ts +51 -0
  83. package/dist/executors/bounded-exec/lock-wait.d.ts.map +1 -0
  84. package/dist/executors/bounded-exec/lock-wait.js +108 -0
  85. package/dist/executors/bounded-exec/owned-subtree.d.ts +84 -0
  86. package/dist/executors/bounded-exec/owned-subtree.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/owned-subtree.js +178 -0
  88. package/dist/executors/bounded-exec/progress.d.ts +4 -1
  89. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  90. package/dist/executors/bounded-exec/progress.js +5 -2
  91. package/dist/executors/bounded-exec/ram-attach.d.ts +19 -0
  92. package/dist/executors/bounded-exec/ram-attach.d.ts.map +1 -0
  93. package/dist/executors/bounded-exec/ram-attach.js +207 -0
  94. package/dist/executors/bounded-exec/ram-detach.d.ts +25 -0
  95. package/dist/executors/bounded-exec/ram-detach.d.ts.map +1 -0
  96. package/dist/executors/bounded-exec/ram-detach.js +207 -0
  97. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +76 -0
  98. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -1
  99. package/dist/executors/bounded-exec/ram-temp-fixtures.js +296 -0
  100. package/dist/executors/bounded-exec/ram-temp.d.ts +388 -88
  101. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  102. package/dist/executors/bounded-exec/ram-temp.js +1213 -399
  103. package/dist/executors/bounded-exec/run-cpu-pool.d.ts +4 -4
  104. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  105. package/dist/executors/bounded-exec/run-cpu-pool.js +20 -30
  106. package/dist/executors/bounded-exec/runner-hook.js +14 -5
  107. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +14 -1
  108. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -1
  109. package/dist/executors/bounded-exec/runner-lifecycle.js +27 -6
  110. package/dist/executors/bounded-exec/{cpu-tokens.d.ts → runner-sizing.d.ts} +14 -42
  111. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -0
  112. package/dist/executors/bounded-exec/{cpu-tokens.js → runner-sizing.js} +5 -35
  113. package/dist/executors/bounded-exec/runtime-pin.d.ts +73 -0
  114. package/dist/executors/bounded-exec/runtime-pin.d.ts.map +1 -0
  115. package/dist/executors/bounded-exec/runtime-pin.js +167 -0
  116. package/dist/executors/bounded-exec/schema.d.ts +18 -1
  117. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  118. package/dist/executors/bounded-exec/verdict.d.ts +40 -11
  119. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  120. package/dist/executors/bounded-exec/verdict.js +88 -19
  121. package/dist/executors/cargo-zigbuild/executor.d.ts +21 -0
  122. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -1
  123. package/dist/executors/cargo-zigbuild/executor.js +67 -12
  124. package/dist/executors/clean-outputs/executor.js +1 -0
  125. package/dist/executors/scriptc-native/executor.d.ts +79 -0
  126. package/dist/executors/scriptc-native/executor.d.ts.map +1 -0
  127. package/dist/executors/scriptc-native/executor.js +367 -0
  128. package/dist/executors/scriptc-native/ffi-paths.d.ts +9 -0
  129. package/dist/executors/scriptc-native/ffi-paths.d.ts.map +1 -0
  130. package/dist/executors/scriptc-native/ffi-paths.js +25 -0
  131. package/dist/executors/scriptc-native/schema.d.ts +34 -0
  132. package/dist/executors/scriptc-native/schema.d.ts.map +1 -0
  133. package/dist/executors/scriptc-native/schema.js +1 -0
  134. package/dist/executors/typescript-emit/executor.d.ts +3 -1
  135. package/dist/executors/typescript-emit/executor.d.ts.map +1 -1
  136. package/dist/executors/typescript-emit/executor.js +164 -33
  137. package/dist/forbidden-spelling-policy.d.ts +56 -0
  138. package/dist/forbidden-spelling-policy.d.ts.map +1 -0
  139. package/dist/forbidden-spelling-policy.js +176 -0
  140. package/dist/generators/create-package/generator.d.ts.map +1 -1
  141. package/dist/generators/create-package/generator.js +3 -4
  142. package/dist/graph-input-contract.d.ts +14 -0
  143. package/dist/graph-input-contract.d.ts.map +1 -1
  144. package/dist/graph-input-contract.js +67 -2
  145. package/dist/index.d.ts +23 -6
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +676 -215
  148. package/dist/managed-assets.d.ts +2 -0
  149. package/dist/managed-assets.d.ts.map +1 -1
  150. package/dist/managed-files/files.d.ts +3 -3
  151. package/dist/managed-files/files.d.ts.map +1 -1
  152. package/dist/managed-files/files.js +28 -3
  153. package/dist/managed-files/managed-format.d.ts.map +1 -1
  154. package/dist/managed-files/managed-format.js +1 -0
  155. package/dist/nextest-code-bound.js +1 -0
  156. package/dist/nextest-timings.d.ts +35 -0
  157. package/dist/nextest-timings.d.ts.map +1 -0
  158. package/dist/nextest-timings.js +168 -0
  159. package/dist/owned-installation.d.ts +6 -5
  160. package/dist/owned-installation.d.ts.map +1 -1
  161. package/dist/owned-installation.js +150 -26
  162. package/dist/package-target-policy.d.ts.map +1 -1
  163. package/dist/package-target-policy.js +2 -3
  164. package/dist/process-children.d.ts +7 -0
  165. package/dist/process-children.d.ts.map +1 -0
  166. package/dist/process-children.darwin.d.ts +7 -0
  167. package/dist/process-children.darwin.d.ts.map +1 -0
  168. package/dist/process-children.darwin.js +13 -0
  169. package/dist/process-children.js +14 -0
  170. package/dist/process-children.linux.d.ts +8 -0
  171. package/dist/process-children.linux.d.ts.map +1 -0
  172. package/dist/process-children.linux.js +25 -0
  173. package/dist/process-group.d.ts +108 -0
  174. package/dist/process-group.d.ts.map +1 -0
  175. package/dist/process-group.js +171 -0
  176. package/dist/process-listing.d.ts +7 -0
  177. package/dist/process-listing.d.ts.map +1 -0
  178. package/dist/process-listing.js +51 -0
  179. package/dist/producer-receipts.d.ts +8 -2
  180. package/dist/producer-receipts.d.ts.map +1 -1
  181. package/dist/producer-receipts.js +75 -48
  182. package/dist/task-graph-policy.d.ts +21 -0
  183. package/dist/task-graph-policy.d.ts.map +1 -0
  184. package/dist/task-graph-policy.js +91 -0
  185. package/dist/test-timings.js +4 -4
  186. package/dist/test-walk-policy.d.ts +14 -5
  187. package/dist/test-walk-policy.d.ts.map +1 -1
  188. package/dist/test-walk-policy.js +8 -2
  189. package/dist/testing.d.ts +73 -28
  190. package/dist/testing.d.ts.map +1 -1
  191. package/dist/testing.js +217 -223
  192. package/dist/tool-bins.d.ts +33 -0
  193. package/dist/tool-bins.d.ts.map +1 -0
  194. package/dist/tool-bins.js +72 -0
  195. package/dist/typecheck-test-policy.d.ts.map +1 -1
  196. package/dist/typecheck-test-policy.js +3 -4
  197. package/dist/wait-why-policy.d.ts +28 -0
  198. package/dist/wait-why-policy.d.ts.map +1 -0
  199. package/dist/wait-why-policy.js +223 -0
  200. package/dist/workspace-config-policy.d.ts +14 -8
  201. package/dist/workspace-config-policy.d.ts.map +1 -1
  202. package/dist/workspace-config-policy.js +60 -16
  203. package/dist/workspace-manifest.d.ts +41 -6
  204. package/dist/workspace-manifest.d.ts.map +1 -1
  205. package/dist/workspace-manifest.js +57 -0
  206. package/executors.json +21 -1
  207. package/loader.js +2 -0
  208. package/managed/raw/envrc +11 -0
  209. package/managed/raw/patches/nx@23.2.1.patch +2840 -199
  210. package/managed/raw/patches/ttsc@0.34.1.patch +119 -0
  211. package/managed/raw/tooling/direnv/cargo-fetch-bound.ts +245 -0
  212. package/managed/raw/tooling/direnv/cargo-fetch-run.ts +299 -0
  213. package/managed/raw/tooling/direnv/compiler-variables.sh +42 -0
  214. package/managed/raw/tooling/direnv/developer-links.ts +464 -25
  215. package/managed/raw/tooling/direnv/devenv.smoo.nix +78 -38
  216. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -12
  217. package/managed/raw/tooling/direnv/installed-bun-links.ts +308 -0
  218. package/managed/raw/tooling/direnv/linked-snapshots.ts +62 -3
  219. package/managed/raw/tooling/direnv/nx-socket-dir.sh +56 -17
  220. package/managed/raw/tooling/direnv/repo-path +68 -6
  221. package/managed/raw/tooling/direnv/secret-references.ts +13 -4
  222. package/managed/raw/tooling/direnv/setup-environment.ts +128 -537
  223. package/managed/raw/tooling/direnv/toolchain-stamp.ts +4 -2
  224. package/managed/raw/tooling/direnv/ttsc-env.sh +9 -0
  225. package/managed/raw/tooling/direnv/uv-relocation.ts +227 -0
  226. package/managed/raw/tooling/git-hooks/commit-msg.sh +2 -1
  227. package/managed/raw/tooling/git-hooks/pre-commit.sh +2 -1
  228. package/managed/raw/tooling/git-hooks/pre-push.sh +2 -1
  229. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +85 -7
  230. package/managed/templates/github/actions/save-nix-devenv/action.yml +12 -2
  231. package/managed/templates/github/actions/setup-devenv/action.yml +2 -0
  232. package/package.json +77 -13
  233. package/src/bin/smoo-nx-cargo-toolchain.ts +11 -0
  234. package/src/bin/smoo-nx-forbidden-spellings.ts +33 -0
  235. package/src/bin/smoo-nx-nextest-timings.ts +31 -0
  236. package/src/bounded-test-policy.ts +12 -14
  237. package/src/build-state-output-policy.ts +458 -34
  238. package/src/bun-lock-scope.ts +108 -0
  239. package/src/cargo-build-script-env.ts +265 -0
  240. package/src/cargo-closure-input.ts +74 -7
  241. package/src/cargo-retention-hook.ts +94 -0
  242. package/src/cargo-source-hash.ts +138 -41
  243. package/src/cargo-toolchain-paths.ts +95 -0
  244. package/src/cargo-toolchain-policy.ts +49 -5
  245. package/src/cargo-workspace.ts +72 -123
  246. package/src/cross-check-policy.ts +12 -10
  247. package/src/disk-command-policy.ts +1 -0
  248. package/src/ensure-built-receipt.ts +132 -39
  249. package/src/ensure-built-witness.ts +62 -10
  250. package/src/ensure-built.ts +11 -1
  251. package/src/eslint-config-closure.ts +357 -0
  252. package/src/executors/all-verdicts/executor.ts +164 -0
  253. package/src/executors/all-verdicts/schema.json +22 -0
  254. package/src/executors/all-verdicts/schema.ts +10 -0
  255. package/src/executors/bounded-exec/bounds.ts +76 -0
  256. package/src/executors/bounded-exec/cpu-pool.ts +51 -117
  257. package/src/executors/bounded-exec/executor.ts +423 -259
  258. package/src/executors/bounded-exec/gateway-lease.ts +82 -33
  259. package/src/executors/bounded-exec/hang-diagnostics.ts +173 -31
  260. package/src/executors/bounded-exec/lease-wait.ts +89 -0
  261. package/src/executors/bounded-exec/lock-wait.ts +141 -0
  262. package/src/executors/bounded-exec/owned-subtree.ts +235 -0
  263. package/src/executors/bounded-exec/progress.ts +9 -3
  264. package/src/executors/bounded-exec/ram-attach.ts +238 -0
  265. package/src/executors/bounded-exec/ram-detach.ts +226 -0
  266. package/src/executors/bounded-exec/ram-temp-fixtures.ts +358 -0
  267. package/src/executors/bounded-exec/ram-temp-registry.txt +440 -0
  268. package/src/executors/bounded-exec/ram-temp.ts +1497 -460
  269. package/src/executors/bounded-exec/run-cpu-pool.ts +22 -30
  270. package/src/executors/bounded-exec/runner-hook.ts +14 -6
  271. package/src/executors/bounded-exec/runner-lifecycle.ts +31 -8
  272. package/src/executors/bounded-exec/{cpu-tokens.ts → runner-sizing.ts} +13 -59
  273. package/src/executors/bounded-exec/runtime-pin.ts +232 -0
  274. package/src/executors/bounded-exec/schema.json +11 -2
  275. package/src/executors/bounded-exec/schema.ts +19 -1
  276. package/src/executors/bounded-exec/verdict.ts +137 -34
  277. package/src/executors/cargo-zigbuild/executor.ts +82 -8
  278. package/src/executors/clean-outputs/executor.ts +1 -0
  279. package/src/executors/scriptc-native/executor.ts +394 -0
  280. package/src/executors/scriptc-native/ffi-paths.ts +29 -0
  281. package/src/executors/scriptc-native/schema.json +60 -0
  282. package/src/executors/scriptc-native/schema.ts +34 -0
  283. package/src/executors/typescript-emit/executor.ts +205 -38
  284. package/src/forbidden-spelling-policy.ts +226 -0
  285. package/src/generators/create-package/generator.ts +7 -7
  286. package/src/graph-input-contract.ts +61 -2
  287. package/src/index.ts +792 -225
  288. package/src/managed-assets.ts +3 -0
  289. package/src/managed-files/files.ts +28 -3
  290. package/src/managed-files/managed-format.ts +1 -0
  291. package/src/nextest-code-bound.ts +1 -0
  292. package/src/nextest-timings.ts +106 -0
  293. package/src/owned-installation.ts +150 -23
  294. package/src/package-target-policy.ts +2 -3
  295. package/src/process-children.darwin.ts +14 -0
  296. package/src/process-children.linux.ts +33 -0
  297. package/src/process-children.ts +22 -0
  298. package/src/process-group.ts +209 -0
  299. package/src/process-listing.ts +54 -0
  300. package/src/producer-receipts.ts +68 -45
  301. package/src/task-graph-policy.ts +98 -0
  302. package/src/test-walk-policy.ts +23 -8
  303. package/src/testing.ts +240 -232
  304. package/src/tool-bins.ts +108 -0
  305. package/src/typecheck-test-policy.ts +2 -3
  306. package/src/wait-why-policy.ts +226 -0
  307. package/src/workspace-config-policy.ts +63 -15
  308. package/src/workspace-manifest.ts +89 -6
  309. package/test-timings.json +123 -98
  310. package/dist/bin/smoo-nx-failures.d.ts +0 -3
  311. package/dist/bin/smoo-nx-failures.d.ts.map +0 -1
  312. package/dist/bin/smoo-nx-failures.js +0 -3
  313. package/dist/bin/smoo-nx-nextest-extract.d.ts +0 -3
  314. package/dist/bin/smoo-nx-nextest-extract.d.ts.map +0 -1
  315. package/dist/bin/smoo-nx-nextest-extract.js +0 -42
  316. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +0 -1
  317. package/dist/failure-summary.d.ts +0 -5
  318. package/dist/failure-summary.d.ts.map +0 -1
  319. package/dist/failure-summary.js +0 -118
  320. package/dist/nextest-extraction.d.ts +0 -117
  321. package/dist/nextest-extraction.d.ts.map +0 -1
  322. package/dist/nextest-extraction.js +0 -303
  323. package/managed/raw/patches/ttsc@0.30.4.patch +0 -2255
  324. package/src/__tests__/cargo-closure-filesets.ts +0 -94
  325. package/src/__tests__/cargo-closure-fixture.ts +0 -113
  326. package/src/__tests__/counted-cargo-process.ts +0 -286
  327. package/src/__tests__/counted-cargo.ts +0 -713
  328. package/src/__tests__/directory-change.ts +0 -27
  329. package/src/__tests__/fixture-cargo-home.ts +0 -36
  330. package/src/__tests__/fixture-nx-env.ts +0 -193
  331. package/src/__tests__/owned-installation.ts +0 -13
  332. package/src/__tests__/past-tick.ts +0 -12
  333. package/src/__tests__/progress-clock.ts +0 -43
  334. package/src/__tests__/runtime-input.ts +0 -23
  335. package/src/bin/smoo-nx-bound-failures.test.ts +0 -127
  336. package/src/bin/smoo-nx-failures.ts +0 -4
  337. package/src/bin/smoo-nx-nextest-extract.ts +0 -44
  338. package/src/bounded-test-policy.test.ts +0 -1029
  339. package/src/build-state-output-policy.test.ts +0 -115
  340. package/src/bun-bootstrap.test.ts +0 -102
  341. package/src/bun-file-packages.test.ts +0 -47
  342. package/src/cargo-closure-filesets.test.ts +0 -63
  343. package/src/cargo-closure-input.property.test.ts +0 -50
  344. package/src/cargo-closure-input.test.ts +0 -506
  345. package/src/cargo-closure-key.test.ts +0 -240
  346. package/src/cargo-resolve-cache.test.ts +0 -1181
  347. package/src/cargo-source-hash.test.ts +0 -707
  348. package/src/cargo-workspace.test.ts +0 -615
  349. package/src/cross-check-policy.test.ts +0 -316
  350. package/src/disk-command-policy.test.ts +0 -61
  351. package/src/ensure-built-receipt.test.ts +0 -279
  352. package/src/ensure-built-witness.test.ts +0 -688
  353. package/src/ensure-built.test.ts +0 -1462
  354. package/src/executors/bounded-exec/cargo-progress-host.test.ts +0 -332
  355. package/src/executors/bounded-exec/cpu-pool.test.ts +0 -684
  356. package/src/executors/bounded-exec/cpu-tokens.test.ts +0 -519
  357. package/src/executors/bounded-exec/disk-lease.test.ts +0 -219
  358. package/src/executors/bounded-exec/executor-host.test.ts +0 -800
  359. package/src/executors/bounded-exec/executor.test.ts +0 -1819
  360. package/src/executors/bounded-exec/go-progress-host.test.ts +0 -171
  361. package/src/executors/bounded-exec/hang-diagnostics.test.ts +0 -291
  362. package/src/executors/bounded-exec/progress.test.ts +0 -139
  363. package/src/executors/bounded-exec/ram-temp-held.test.ts +0 -264
  364. package/src/executors/bounded-exec/ram-temp-host.test.ts +0 -635
  365. package/src/executors/bounded-exec/ram-temp.test.ts +0 -750
  366. package/src/executors/bounded-exec/run-cpu-pool.test.ts +0 -79
  367. package/src/executors/bounded-exec/runner-hook.test.ts +0 -193
  368. package/src/executors/bounded-exec/runner-lifecycle.test.ts +0 -82
  369. package/src/executors/bounded-exec/verdict.test.ts +0 -545
  370. package/src/executors/cargo-zigbuild/executor.test.ts +0 -129
  371. package/src/executors/clean-outputs/executor.test.ts +0 -20
  372. package/src/executors/typescript-emit/executor.test.ts +0 -83
  373. package/src/failure-summary.ts +0 -119
  374. package/src/generators/bounded-test-targets/generator.test.ts +0 -138
  375. package/src/generators/bun-test-tracing/generator.test.ts +0 -250
  376. package/src/generators/create-package/generator.test.ts +0 -190
  377. package/src/generators/make-public/generator.test.ts +0 -137
  378. package/src/graph-input-contract.test.ts +0 -379
  379. package/src/host-controller-wiring.test.ts +0 -31
  380. package/src/index.test.ts +0 -3319
  381. package/src/linked-snapshots.nx.test.ts +0 -254
  382. package/src/managed-files/forgejo-artifact.test.ts +0 -395
  383. package/src/managed-files/generator.test.ts +0 -243
  384. package/src/managed-files/github-host-cache-isolation.test.ts +0 -86
  385. package/src/managed-files/json-edit.test.ts +0 -216
  386. package/src/managed-files/managed-content.test.ts +0 -166
  387. package/src/managed-files/managed-format-json.test.ts +0 -308
  388. package/src/managed-files/repo-path.test.ts +0 -172
  389. package/src/managed-files/setup-devenv-free-disk.test.ts +0 -95
  390. package/src/managed-files/trace-db-artifact.test.ts +0 -7
  391. package/src/managed-files/tree-permissions.test.ts +0 -109
  392. package/src/managed-files/tree.test.ts +0 -123
  393. package/src/manifest-hash.test.ts +0 -219
  394. package/src/nextest-code-bound.test.ts +0 -161
  395. package/src/nextest-extraction.test.ts +0 -216
  396. package/src/nextest-extraction.ts +0 -371
  397. package/src/nextest-partitions.test.ts +0 -501
  398. package/src/nx-cache-bound.test.ts +0 -109
  399. package/src/nx-cache-restore-mtime.test.ts +0 -170
  400. package/src/nx-daemon-claim.test.ts +0 -279
  401. package/src/nx-daemon-idle-cpu.test.ts +0 -80
  402. package/src/nx-daemon-idle-policy.test.ts +0 -111
  403. package/src/nx-daemon-ignore-files.test.ts +0 -210
  404. package/src/nx-daemon-root-normalization.test.ts +0 -105
  405. package/src/nx-daemon-start-never.test.ts +0 -292
  406. package/src/nx-daemon-validity.test.ts +0 -1055
  407. package/src/nx-dot-input-policy.test.ts +0 -353
  408. package/src/nx-failure-life-cycles.test.ts +0 -207
  409. package/src/nx-files-archive-racy.test.ts +0 -97
  410. package/src/nx-foreign-workspace-env.test.ts +0 -329
  411. package/src/nx-incomplete-run-summary.test.ts +0 -70
  412. package/src/nx-plugin-load-errors.test.ts +0 -120
  413. package/src/nx-project-graph-errors.test.ts +0 -70
  414. package/src/nx-project-resolution-cache.test.ts +0 -176
  415. package/src/nx-raw-inputs.test.ts +0 -166
  416. package/src/nx-resident-graph-workers.test.ts +0 -112
  417. package/src/nx-run-information.test.ts +0 -118
  418. package/src/nx-skipped-task-exit.test.ts +0 -180
  419. package/src/nx-state-location.test.ts +0 -63
  420. package/src/nx-store-resolution.test.ts +0 -81
  421. package/src/nx-task-graph-cycles.test.ts +0 -148
  422. package/src/nx-task-output-lease.test.ts +0 -550
  423. package/src/nx-use-local.test.ts +0 -105
  424. package/src/owned-installation.test.ts +0 -105
  425. package/src/package-target-policy.test.ts +0 -1014
  426. package/src/producer-receipts.test.ts +0 -285
  427. package/src/release-config-policy.test.ts +0 -311
  428. package/src/runtime-input.test.ts +0 -393
  429. package/src/serial-group-wiring.test.ts +0 -326
  430. package/src/task-history-namespace.test.ts +0 -181
  431. package/src/task-results.test.ts +0 -228
  432. package/src/test-clock-policy.test.ts +0 -338
  433. package/src/test-program.test.ts +0 -303
  434. package/src/test-timings.test.ts +0 -240
  435. package/src/test-walk-policy.test.ts +0 -620
  436. package/src/testing.test.ts +0 -509
  437. package/src/typecheck-test-policy.test.ts +0 -1468
  438. package/src/typescript-resolution.test.ts +0 -76
  439. package/src/version-actions.test.ts +0 -80
  440. package/src/workspace-config-policy.test.ts +0 -737
@@ -1,3 +1,19 @@
1
+ diff --git a/dist/bin/init-local.js b/dist/bin/init-local.js
2
+ index 68912ab62bc8cdcc83ff599cebcb06aca501b781..ed990d930c104cdc27d5ffc135003cb55e47334c 100644
3
+ --- a/dist/bin/init-local.js
4
+ +++ b/dist/bin/init-local.js
5
+ @@ -30,8 +30,9 @@ async function initLocal(workspace) {
6
+ handleAngularCLIFallbacks(workspace);
7
+ return;
8
+ }
9
+ - // Skip per-TAB shell completion calls — those must not spawn the daemon.
10
+ - if (!process.env.NX_COMPLETE) {
11
+ + // Skip per-TAB shell completion calls — those must not spawn the daemon. Only a client on a terminal can
12
+ + // show the Nx Console prompt, so only it asks the daemon, whose answer runs each editor's CLI.
13
+ + if (!process.env.NX_COMPLETE && process.stdout.isTTY && process.stdin.isTTY) {
14
+ try {
15
+ await ensureNxConsoleInstalledViaDaemon();
16
+ }
1
17
  diff --git a/dist/bin/nx.js b/dist/bin/nx.js
2
18
  index 5a59f762f2002f61f84d0966c8fb48d48b3bdce2..f190d2b93cc7c1d0f59dcb1e76ef7d3efb01c3f9 100755
3
19
  --- a/dist/bin/nx.js
@@ -384,7 +400,7 @@ index 9bd7b61bf9ad94c72a198dfe6f15fbcb68dfaac3..6b982fae848cc82a310eee9a1036be1a
384
400
  flushSyncGeneratorChangesToDisk(generators: string[]): Promise<FlushSyncGeneratorChangesResult>;
385
401
  getRegisteredSyncGenerators(): Promise<{
386
402
  diff --git a/dist/src/daemon/client/client.js b/dist/src/daemon/client/client.js
387
- index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0de1f5005f 100644
403
+ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..8f26121c957183135bc75ad428fb7ef809a5471e 100644
388
404
  --- a/dist/src/daemon/client/client.js
389
405
  +++ b/dist/src/daemon/client/client.js
390
406
  @@ -40,7 +40,6 @@ const nx_console_1 = require("../message-types/nx-console");
@@ -395,7 +411,25 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
395
411
  const update_workspace_context_1 = require("../message-types/update-workspace-context");
396
412
  const tmp_dir_1 = require("../tmp-dir");
397
413
  const sandbox_socket_hint_1 = require("../sandbox-socket-hint");
398
- @@ -135,12 +134,51 @@ class DaemonClient {
414
+ @@ -53,6 +52,8 @@ var DaemonStatus;
415
+ DaemonStatus[DaemonStatus["DISCONNECTED"] = 1] = "DISCONNECTED";
416
+ DaemonStatus[DaemonStatus["CONNECTED"] = 2] = "CONNECTED";
417
+ })(DaemonStatus || (DaemonStatus = {}));
418
+ +/** How often a request waiting on the daemon checks that the daemon its record names still runs. */
419
+ +const DAEMON_LIVENESS_MS = 5000;
420
+ const WAIT_FOR_SERVER_CONFIG = {
421
+ delayMs: 10,
422
+ maxAttempts: 6000, // 6000 * 10ms = 60 seconds
423
+ @@ -92,6 +93,8 @@ class DaemonClient {
424
+ this.projectGraphListenerFramingFailures = 0;
425
+ this.projectGraphListenerCallbacks = new Map();
426
+ this.envReflectionSent = false;
427
+ + /** The error that ended this process's use of the daemon (`loseDaemon`); null while it has one. */
428
+ + this.lostDaemon = null;
429
+ try {
430
+ this.nxJson = (0, configuration_1.readNxJson)();
431
+ }
432
+ @@ -135,12 +138,55 @@ class DaemonClient {
399
433
  });
400
434
  this._enabled = false;
401
435
  }
@@ -436,18 +470,127 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
436
470
  + /**
437
471
  + * Lets the stopped daemon's connection go, and says so once per process: every
438
472
  + * request still queued for it fails the same way and continues the same way.
473
+ + * The daemon is off for this process alone from here on: a tagged error the
474
+ + * daemon itself sent (its watcher stream stopped) leaves the daemon record in
475
+ + * place while it stops, and a later request would dial a socket that is gone.
439
476
  + */
440
477
  + continueWithoutDaemon(error) {
441
478
  + if (!this.continuedWithoutDaemon) {
442
479
  + this.continuedWithoutDaemon = true;
443
480
  + output_1.output.note({ title: `${error.message} Continuing without the daemon.` });
444
481
  + }
482
+ + (0, tmp_dir_1.disableDaemonForThisProcess)(error.message);
445
483
  + this.reset();
446
484
  + }
447
485
  reset() {
448
486
  this.socketMessenger?.close();
449
487
  this.socketMessenger = null;
450
- @@ -668,27 +706,6 @@ class DaemonClient {
488
+ @@ -157,6 +203,23 @@ class DaemonClient {
489
+ this._daemonStatus = DaemonStatus.DISCONNECTED;
490
+ this._waitForDaemonReady = new Promise((resolve) => (this._daemonReady = resolve));
491
+ }
492
+ + /**
493
+ + * Ends this process's use of the daemon with `error`: the request waiting on
494
+ + * it, every request parked on a reconnect (`startDaemonIfNecessary`), and
495
+ + * every later request fail with it at once. Stock 23.2.1 left a failed
496
+ + * reconnect CONNECTING with its ready signal never given, so every later
497
+ + * request waited for good: a land gate's run sat 2 h 48 min with no task
498
+ + * running after its daemon's SIGTERM (pid 23683, 2026-10-25). Under
499
+ + * NX_DAEMON_START=never a lost connection arrives tagged
500
+ + * `daemonStartForbidden`, and `orWithoutDaemon` continues each request in
501
+ + * this process; a `daemonGraphChanged` error never continues anywhere.
502
+ + */
503
+ + loseDaemon(error) {
504
+ + this.lostDaemon = error;
505
+ + this._daemonStatus = DaemonStatus.DISCONNECTED;
506
+ + this.currentReject?.(error);
507
+ + this._daemonReady();
508
+ + }
509
+ getSocketPath() {
510
+ const daemonProcessJson = (0, cache_1.readDaemonProcessJsonCache)();
511
+ if (daemonProcessJson?.socketPath) {
512
+ @@ -295,6 +358,7 @@ class DaemonClient {
513
+ // failure would otherwise leave this channel silent forever.
514
+ this.fileWatcherMessenger?.close();
515
+ });
516
+ + this.watchStreamDaemon('fileWatcherMessenger', this.fileWatcherCallbacks, 'REGISTER_FILE_WATCHER', 'closed');
517
+ this.fileWatcherMessenger.sendMessage({
518
+ type: 'REGISTER_FILE_WATCHER',
519
+ config,
520
+ @@ -311,6 +375,48 @@ class DaemonClient {
521
+ }
522
+ };
523
+ }
524
+ + /**
525
+ + * A file-watcher or project-graph-listener stream waits on the daemon its
526
+ + * record named when the stream connected, as a request does
527
+ + * (`sendMessageToDaemon`). A listener that outlived that daemon accepts the
528
+ + * stream and never closes it, so no reconnect ever runs. Once that pid no
529
+ + * longer runs, every callback gets the loss naming the pid and the socket, and
530
+ + * the stream is let go. `ended` is the stream's own end-of-stream signal, sent
531
+ + * after the loss to consumers that end only on it ('closed' for file watchers,
532
+ + * which `nx watch` exits on). A project-graph listener gets the loss alone: its
533
+ + * consumer debounces its callback, keeps only the last call, and reports an
534
+ + * error itself. A record that names no pid arms no check.
535
+ + */
536
+ + watchStreamDaemon(field, callbacks, type, ended) {
537
+ + const messenger = this[field];
538
+ + const daemonPid = (0, cache_1.getDaemonProcessIdSync)();
539
+ + if (!messenger || !Number.isInteger(daemonPid)) {
540
+ + return;
541
+ + }
542
+ + const socketPath = (0, cache_1.readDaemonProcessJsonCache)()?.socketPath ?? null;
543
+ + const liveness = setInterval(() => {
544
+ + if (this[field] !== messenger) {
545
+ + clearInterval(liveness);
546
+ + return;
547
+ + }
548
+ + if (daemonRuns(daemonPid)) {
549
+ + return;
550
+ + }
551
+ + clearInterval(liveness);
552
+ + const error = daemonGone(daemonPid, socketPath, type);
553
+ + const waiting = [...callbacks.values()];
554
+ + // Emptied before the close, so its reconnect finds nobody to reconnect.
555
+ + callbacks.clear();
556
+ + for (const cb of waiting) {
557
+ + cb(error, null);
558
+ + if (ended !== undefined) {
559
+ + cb(ended, null);
560
+ + }
561
+ + }
562
+ + messenger.close();
563
+ + }, DAEMON_LIVENESS_MS);
564
+ + liveness.unref();
565
+ + }
566
+ async reconnectFileWatcher() {
567
+ // Guard against concurrent reconnection attempts
568
+ if (this.fileWatcherReconnecting) {
569
+ @@ -399,6 +505,7 @@ class DaemonClient {
570
+ // not emit, so close explicitly to hand off to it.
571
+ this.fileWatcherMessenger?.close();
572
+ });
573
+ + this.watchStreamDaemon('fileWatcherMessenger', this.fileWatcherCallbacks, 'REGISTER_FILE_WATCHER', 'closed');
574
+ // Re-register all stored configs
575
+ for (const cfg of this.fileWatcherConfigs.values()) {
576
+ this.fileWatcherMessenger.sendMessage({
577
+ @@ -475,6 +582,7 @@ class DaemonClient {
578
+ }
579
+ this.projectGraphListenerMessenger?.close();
580
+ });
581
+ + this.watchStreamDaemon('projectGraphListenerMessenger', this.projectGraphListenerCallbacks, register_project_graph_listener_1.REGISTER_PROJECT_GRAPH_LISTENER);
582
+ this.projectGraphListenerMessenger.sendMessage({
583
+ type: register_project_graph_listener_1.REGISTER_PROJECT_GRAPH_LISTENER,
584
+ });
585
+ @@ -575,6 +683,7 @@ class DaemonClient {
586
+ // not emit, so close explicitly to hand off to it.
587
+ this.projectGraphListenerMessenger?.close();
588
+ });
589
+ + this.watchStreamDaemon('projectGraphListenerMessenger', this.projectGraphListenerCallbacks, register_project_graph_listener_1.REGISTER_PROJECT_GRAPH_LISTENER);
590
+ // Re-register
591
+ this.projectGraphListenerMessenger.sendMessage({
592
+ type: register_project_graph_listener_1.REGISTER_PROJECT_GRAPH_LISTENER,
593
+ @@ -668,27 +777,6 @@ class DaemonClient {
451
594
  };
452
595
  return this.sendToDaemonViaQueue(message);
453
596
  }
@@ -475,7 +618,7 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
475
618
  getSyncGeneratorChanges(generators) {
476
619
  const message = {
477
620
  type: get_sync_generator_changes_1.GET_SYNC_GENERATOR_CHANGES,
478
- @@ -813,8 +830,15 @@ class DaemonClient {
621
+ @@ -813,8 +901,15 @@ class DaemonClient {
479
622
  }
480
623
  }
481
624
  if (!probe.available) {
@@ -493,7 +636,16 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
493
636
  }
494
637
  this.setUpConnection();
495
638
  this._daemonStatus = DaemonStatus.CONNECTED;
496
- @@ -831,13 +855,24 @@ class DaemonClient {
639
+ @@ -825,19 +920,38 @@ class DaemonClient {
640
+ }
641
+ else if (this._daemonStatus == DaemonStatus.CONNECTING) {
642
+ await this._waitForDaemonReady;
643
+ + if (this.lostDaemon) {
644
+ + throw this.lostDaemon;
645
+ + }
646
+ const daemonPid = (0, cache_1.getDaemonProcessIdSync)();
647
+ // Fire-and-forget - don't block daemon connection by waiting for metrics registration
648
+ this.registerDaemonProcessWithMetricsService(daemonPid);
497
649
  }
498
650
  }
499
651
  async sendToDaemonViaQueue(messageToDaemon, parser) {
@@ -514,18 +666,23 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
514
666
  + });
515
667
  + }
516
668
  + finally {
517
- + // A plugin worker is a client of its daemon only while it waits on a
518
- + // reply. The daemon keeps a worker with graph hooks alive, and an open
519
- + // request connection keeps the daemon from its idle stop, so a worker
520
- + // that stayed connected would keep its own host alive forever.
521
- + if (global.NX_PLUGIN_WORKER && this.queue.isEmpty()) {
669
+ + // A plugin worker, or the daemon itself, is a client of the daemon only
670
+ + // while it waits on a reply. The daemon keeps a worker with graph hooks
671
+ + // alive, and an open request connection keeps the daemon from its idle
672
+ + // stop, so a worker that stayed connected would keep its own host alive
673
+ + // forever. The daemon is its own client when code it runs (a sync
674
+ + // generator, a plugin) reaches Nx through another copy of the nx
675
+ + // package, whose project graph module has no graph in memory and so
676
+ + // asks the daemon over its socket: that connection would hold the
677
+ + // daemon open for the rest of its life.
678
+ + if ((global.NX_PLUGIN_WORKER || global.NX_DAEMON) && this.queue.isEmpty()) {
522
679
  + this.reset();
523
680
  + }
524
681
  + }
525
682
  }
526
683
  setUpConnection() {
527
684
  const socketPath = this.getSocketPath();
528
- @@ -846,7 +881,13 @@ class DaemonClient {
685
+ @@ -846,7 +960,13 @@ class DaemonClient {
529
686
  // sendMessageToDaemon method uses a keep-alive setTimeout to
530
687
  // explicitly hold the event loop open while awaiting a response.
531
688
  socket.unref();
@@ -540,7 +697,7 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
540
697
  // it's ok for the daemon to terminate if the client doesn't wait on
541
698
  // any messages from the daemon
542
699
  if (this.queue.isEmpty()) {
543
- @@ -892,6 +933,12 @@ class DaemonClient {
700
+ @@ -892,12 +1012,22 @@ class DaemonClient {
544
701
  else {
545
702
  error = daemonProcessException(err.toString());
546
703
  }
@@ -553,16 +710,108 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
553
710
  this.currentReject(error);
554
711
  this.socketMessenger?.close();
555
712
  });
556
- @@ -936,7 +983,7 @@ class DaemonClient {
713
+ }
714
+ async handleConnectionError(error) {
715
+ logger_1.clientLogger.log(`[Reconnect] Connection error detected: ${error.message}`);
716
+ + if (this.lostDaemon) {
717
+ + // The daemon said why it stopped first (`loseDaemon`): nothing to wait for.
718
+ + return;
719
+ + }
720
+ // Create a new ready promise for new requests to wait on
721
+ this._waitForDaemonReady = new Promise((resolve) => (this._daemonReady = resolve));
722
+ // Set status to CONNECTING so new requests will wait for reconnection
723
+ @@ -911,9 +1041,7 @@ class DaemonClient {
724
+ catch (err) {
725
+ if (err instanceof daemon_socket_messenger_1.VersionMismatchError) {
726
+ // New daemon has different version - reject with error so caller can handle
727
+ - if (this.currentReject) {
728
+ - this.currentReject(err);
729
+ - }
730
+ + this.loseDaemon(err);
731
+ return;
732
+ }
733
+ throw err;
734
+ @@ -934,10 +1062,8 @@ class DaemonClient {
735
+ }
736
+ }
557
737
  else {
558
- // Failed to reconnect after all attempts, reject the pending request
559
- if (this.currentReject) {
738
+ - // Failed to reconnect after all attempts, reject the pending request
739
+ - if (this.currentReject) {
560
740
  - this.currentReject(error);
561
- + this.currentReject(process.env.NX_DAEMON_START === 'never' ? daemonStartForbidden() : error);
562
- }
741
+ - }
742
+ + // No daemon took over within the wait: this request and every later one fail.
743
+ + this.loseDaemon(process.env.NX_DAEMON_START === 'never' ? daemonStartForbidden(error) : error);
563
744
  }
564
745
  }
565
- @@ -1132,8 +1179,11 @@ class DaemonClient {
746
+ establishConnection() {
747
+ @@ -998,6 +1124,9 @@ class DaemonClient {
748
+ return { available: false, refusal };
749
+ }
750
+ async sendMessageToDaemon(message, force) {
751
+ + if (this.lostDaemon) {
752
+ + throw this.lostDaemon;
753
+ + }
754
+ // the first message sent to the daemon includes an env prop
755
+ // that updates the process.env values on the daemon.
756
+ if (!this.envReflectionSent && !global.NX_PLUGIN_WORKER) {
757
+ @@ -1005,7 +1134,15 @@ class DaemonClient {
758
+ this.envReflectionSent = true;
759
+ }
760
+ await this.startDaemonIfNecessary();
761
+ + // The request waits on the daemon its record names. Its socket can outlive it: a
762
+ + // listener a surviving child inherited accepts the request and never answers, and the
763
+ + // keep-alive below would wait 20 min at no CPU. So while the request waits, that pid is
764
+ + // checked every DAEMON_LIVENESS_MS, and a daemon that is gone ends the request naming
765
+ + // its pid and socket (smoothbricks specs/cowshed/05_gateway.md, "Deadlines").
766
+ + const daemonPid = (0, cache_1.getDaemonProcessIdSync)();
767
+ + const socketPath = (0, cache_1.readDaemonProcessJsonCache)()?.socketPath ?? null;
768
+ let keepAlive;
769
+ + let liveness;
770
+ return new Promise((resolve, reject) => {
771
+ perf_hooks_1.performance.mark('sendMessageToDaemon-start');
772
+ // An open promise isn't enough to keep the event loop
773
+ @@ -1016,12 +1153,25 @@ class DaemonClient {
774
+ keepAlive = setTimeout(() => {
775
+ reject(new Error('The daemon timed out while processing ' + message.type));
776
+ }, 20 * 60 * 1000);
777
+ + liveness = Number.isInteger(daemonPid) && setInterval(() => {
778
+ + if (daemonRuns(daemonPid)) {
779
+ + return;
780
+ + }
781
+ + const error = daemonGone(daemonPid, socketPath, message.type);
782
+ + this.loseDaemon(process.env.NX_DAEMON_START === 'never' ? daemonStartForbidden(error) : error);
783
+ + }, DAEMON_LIVENESS_MS);
784
+ + if (liveness) {
785
+ + liveness.unref();
786
+ + }
787
+ this.currentMessage = message;
788
+ this.currentResolve = resolve;
789
+ this.currentReject = reject;
790
+ this.socketMessenger.sendMessage(message, force);
791
+ }).finally(() => {
792
+ clearTimeout(keepAlive);
793
+ + if (liveness) {
794
+ + clearInterval(liveness);
795
+ + }
796
+ });
797
+ }
798
+ async registerDaemonProcessWithMetricsService(daemonPid) {
799
+ @@ -1055,7 +1205,13 @@ class DaemonClient {
800
+ console[parsedResult.level](parsedResult.message);
801
+ return;
802
+ }
803
+ - if (parsedResult.error) {
804
+ + if (parsedResult.error?.daemonGraphChanged) {
805
+ + // The daemon is stopping because the code that computed this
806
+ + // command's graph changed; it says so to every command it serves,
807
+ + // idle or waiting, before it closes their connections.
808
+ + this.loseDaemon(parsedResult.error);
809
+ + }
810
+ + else if (parsedResult.error) {
811
+ this.currentReject(parsedResult.error);
812
+ }
813
+ else {
814
+ @@ -1132,8 +1288,11 @@ class DaemonClient {
566
815
  */
567
816
  const { available, refusal: polled } = await this.waitForServerToBeAvailable({ ignoreVersionMismatch: true });
568
817
  if (available) {
@@ -576,18 +825,65 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
576
825
  }
577
826
  else {
578
827
  // A permission refusal from either source wins, then the poll's errno,
579
- @@ -1200,6 +1250,26 @@ function isDocker() {
828
+ @@ -1161,19 +1320,31 @@ class DaemonClient {
829
+ ].join('\n'));
830
+ }
831
+ }
832
+ + /**
833
+ + * Stops the daemon its record names: SIGTERM to that pid, then its socket
834
+ + * directory goes. A pid no process holds any more (ESRCH) is a daemon that
835
+ + * has already stopped. Any other refusal leaves the daemon running and
836
+ + * serving its socket, so it is thrown, naming the pid and the errno, and
837
+ + * the socket directory stays: deleting it would hide a live daemon from
838
+ + * its clients, and the next one would start a second daemon for the
839
+ + * workspace. The kernel refuses another user's daemon so, and a sandbox
840
+ + * that may signal only its own processes (a cowshed job) refuses a daemon
841
+ + * started outside it, which its starter (the shed's keeper) stops.
842
+ + */
843
+ async stop() {
844
+ - try {
845
+ - const pid = (0, cache_1.getDaemonProcessIdSync)();
846
+ - if (pid) {
847
+ + const pid = (0, cache_1.getDaemonProcessIdSync)();
848
+ + if (pid) {
849
+ + try {
850
+ process.kill(pid, 'SIGTERM');
851
+ }
852
+ - }
853
+ - catch (err) {
854
+ - if (err.code !== 'ESRCH') {
855
+ - output_1.output.error({
856
+ - title: err?.message ||
857
+ - 'Something unexpected went wrong when stopping the daemon server',
858
+ - });
859
+ + catch (err) {
860
+ + if (err?.code !== 'ESRCH') {
861
+ + throw new Error([
862
+ + `Nx Daemon not stopped: signalling its process ${pid} failed with ${err?.code ?? err}.`,
863
+ + 'The daemon is still running and serving its socket, which is kept.',
864
+ + 'Only a process allowed to signal it can stop it: its own user, or for a daemon started outside this sandbox, its starter (under cowshed, the shed keeper).',
865
+ + ].join('\n'), { cause: err });
866
+ + }
867
+ }
868
+ }
869
+ (0, tmp_dir_1.removeSocketDir)();
870
+ @@ -1200,6 +1371,44 @@ function isDocker() {
580
871
  function nxJsonIsNotPresent() {
581
872
  return !(0, nx_json_1.hasNxJson)(workspace_root_1.workspaceRoot);
582
873
  }
583
874
  +/**
584
- + * Whether the process the daemon record names still runs. Read only under
585
- + * NX_DAEMON_START=never, before any request, to choose between that daemon and
875
+ + * Whether the process the daemon record names still runs. Read under
876
+ + * NX_DAEMON_START=never before any request, to choose between that daemon and
586
877
  + * none: a record whose daemon has gone is a reason to run without one, never to
587
878
  + * start one.
588
879
  + */
589
880
  +function recordedDaemonRuns() {
590
- + const pid = (0, cache_1.getDaemonProcessIdSync)();
881
+ + return daemonRuns((0, cache_1.getDaemonProcessIdSync)());
882
+ +}
883
+ +/**
884
+ + * Whether process `pid` runs. A record that names no pid names nothing to wait on.
885
+ + */
886
+ +function daemonRuns(pid) {
591
887
  + if (!Number.isInteger(pid) || pid < 1) {
592
888
  + return false;
593
889
  + }
@@ -599,11 +895,24 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
599
895
  + // EPERM: a process this one may not signal holds the pid, so it runs.
600
896
  + return err.code === 'EPERM';
601
897
  + }
898
+ +}
899
+ +/**
900
+ + * The daemon a request waited on is gone while its socket still took the request:
901
+ + * nothing will answer it. Names the pid and the socket, so the reader needs no
902
+ + * second run to see which daemon and which leftover listener.
903
+ + */
904
+ +function daemonGone(pid, socketPath, type) {
905
+ + const error = new Error([
906
+ + `Nx daemon process ${pid ?? '(none recorded)'} is not running, but its socket ${socketPath ?? '(none recorded)'} took this client's ${type} request: nothing will answer it.`,
907
+ + 'A process that outlived the daemon may still hold its socket; nx daemon --stop removes the socket directory.',
908
+ + ].join('\n'));
909
+ + error.daemonGone = true;
910
+ + return error;
602
911
  +}
603
912
  /**
604
913
  * The operating system refused the connection. Most often the socket belongs to
605
914
  * another user, which is the guarantee the owner-only socket directory buys —
606
- @@ -1226,6 +1296,21 @@ function daemonPermissionException(socketPath, cause) {
915
+ @@ -1226,6 +1435,21 @@ function daemonPermissionException(socketPath, cause) {
607
916
  error.daemonPermissionError = true;
608
917
  return error;
609
918
  }
@@ -625,6 +934,289 @@ index cebfd6d1f8ddb2c5b93dcb847904aa9ab0553de6..64c9d05890139ad8259e3d01ee86dd0d
625
934
  /**
626
935
  * Exported for testing: the `internalDaemonError` tag decides whether a daemon
627
936
  * failure degrades to a daemonless graph build or aborts the command.
937
+ diff --git a/dist/src/daemon/client/daemon-environment.d.ts b/dist/src/daemon/client/daemon-environment.d.ts
938
+ index 65ae9cfd686c3c676cf321cf402bbbf03fc87dab..9a68ae94318403ce70aef08f08a3af9cb5cbb6ad 100644
939
+ --- a/dist/src/daemon/client/daemon-environment.d.ts
940
+ +++ b/dist/src/daemon/client/daemon-environment.d.ts
941
+ @@ -3,7 +3,9 @@
942
+ * `getDaemonEnv` would send, minus the required settings the daemon pins
943
+ * itself. Skipping those yields the same digest whether the process is a
944
+ * daemon plugin worker (which has them set) or a daemonless one (which
945
+ - * typically does not).
946
+ + * typically does not). It keys a plugin's cache of what it inferred, so it is
947
+ + * the env's graph identity (normalizeDaemonEnvironmentForGraph): a package
948
+ + * manager's run scaffolding does not move it.
949
+ */
950
+ export declare function hashDaemonClientEnv(): string;
951
+ export declare function getDaemonEnv(): NodeJS.ProcessEnv & {
952
+ @@ -63,12 +65,28 @@ export declare function getAppliedDaemonClientEnv(): {
953
+ */
954
+ export declare function applyDaemonEnvFromClient(newEnv: NodeJS.ProcessEnv): string[];
955
+ /**
956
+ - * Yarn Berry creates a fresh BERRY_BIN_FOLDER for every invocation and puts it
957
+ - * first on PATH. The wrappers inside are runtime state the daemon and workers
958
+ - * must receive, but the folder's random name is not project-graph identity:
959
+ - * two invocations whose environments differ only by it would compute the same
960
+ - * graph. Strips the folder from PATH and pins the var to a sentinel so such
961
+ - * environments compare equal. Returns a copy; never mutates runtime env.
962
+ + * An env as project-graph identity: the env without what a package manager
963
+ + * adds to run one command in this workspace. The daemon and its workers still
964
+ + * receive all of it, but two clients whose envs differ only by it compute the
965
+ + * same graph, so a switch between them keeps the cached graph:
966
+ + * - Yarn Berry's BERRY_BIN_FOLDER, fresh for every invocation and first on
967
+ + * PATH: the folder leaves PATH and the var is pinned to a sentinel.
968
+ + * - The `node_modules/.bin` directories `bun run`, `npm run`, pnpm and Yarn
969
+ + * put in front of PATH for the run's directory and each of its ancestors:
970
+ + * one that lies in the workspace, whose packages its lockfile identifies,
971
+ + * and one in or above it proved absent (ENOENT or ENOTDIR), which resolves
972
+ + * nothing. Where an entry lies is judged on its spelling and on its real
973
+ + * path: a link inside the workspace (its own `node_modules` linked to an
974
+ + * install elsewhere) is the workspace's. Another checkout's (present or
975
+ + * absent), an existing or unknown one above the workspace, one with `.` or
976
+ + * `..` segments and a relative one stay identity.
977
+ + * - NODE, when it is the `node` PATH resolves: `bun run` sets it to the one it
978
+ + * found, so PATH already carries that choice. With none on PATH, Bun makes a
979
+ + * `bun-node-<hash>` directory whose `node` is Bun, puts it on PATH and names
980
+ + * it in NODE; that directory leaves PATH with NODE when its `node` is the one
981
+ + * PATH resolves. Any other NODE stays, and so does every other PATH entry.
982
+ + * The order of the remaining PATH entries is kept. Returns a copy; never
983
+ + * mutates runtime env.
984
+ */
985
+ export declare function normalizeDaemonEnvironmentForGraph(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform): NodeJS.ProcessEnv;
986
+ export declare function getChangedEnvKeys(before: NodeJS.ProcessEnv, after: NodeJS.ProcessEnv): string[];
987
+ diff --git a/dist/src/daemon/client/daemon-environment.js b/dist/src/daemon/client/daemon-environment.js
988
+ index 427ced7822fb05ba966d0543c224c5760b016572..303548db369472e0f99f31c3bc66ae9b92426e15 100644
989
+ --- a/dist/src/daemon/client/daemon-environment.js
990
+ +++ b/dist/src/daemon/client/daemon-environment.js
991
+ @@ -8,7 +8,10 @@ exports.getAppliedDaemonClientEnv = getAppliedDaemonClientEnv;
992
+ exports.applyDaemonEnvFromClient = applyDaemonEnvFromClient;
993
+ exports.normalizeDaemonEnvironmentForGraph = normalizeDaemonEnvironmentForGraph;
994
+ exports.getChangedEnvKeys = getChangedEnvKeys;
995
+ +const fs_1 = require("fs");
996
+ +const path_1 = require("path");
997
+ const file_hasher_1 = require("../../hasher/file-hasher");
998
+ +const workspace_root_1 = require("../../utils/workspace-root");
999
+ const DAEMON_ENV_REQUIRED_SETTINGS = {
1000
+ NX_PROJECT_GLOB_CACHE: 'false',
1001
+ NX_CACHE_PROJECTS_CONFIG: 'false',
1002
+ @@ -243,7 +246,9 @@ function isExcludedEnvVar(key) {
1003
+ * `getDaemonEnv` would send, minus the required settings the daemon pins
1004
+ * itself. Skipping those yields the same digest whether the process is a
1005
+ * daemon plugin worker (which has them set) or a daemonless one (which
1006
+ - * typically does not).
1007
+ + * typically does not). It keys a plugin's cache of what it inferred, so it is
1008
+ + * the env's graph identity (normalizeDaemonEnvironmentForGraph): a package
1009
+ + * manager's run scaffolding does not move it.
1010
+ */
1011
+ function hashDaemonClientEnv() {
1012
+ const env = {};
1013
+ @@ -254,7 +259,7 @@ function hashDaemonClientEnv() {
1014
+ }
1015
+ }
1016
+ env.NX_LOAD_DOT_ENV_FILES = normalizedLoadDotEnvFiles();
1017
+ - return (0, file_hasher_1.hashObject)(env);
1018
+ + return (0, file_hasher_1.hashObject)(normalizeDaemonEnvironmentForGraph(env));
1019
+ }
1020
+ /**
1021
+ * `NX_LOAD_DOT_ENV_FILES` as its meaning rather than its spelling. `run-command`
1022
+ @@ -379,36 +384,174 @@ function applyDaemonEnvFromClient(newEnv) {
1023
+ return [...changedKeys];
1024
+ }
1025
+ const BERRY_BIN_FOLDER_GRAPH_IDENTITY = '<YARN_BERRY_BIN_FOLDER>';
1026
+ +/** The directory Bun makes for one run when it finds no `node` on PATH. */
1027
+ +const BUN_NODE_SHIM = /^bun-node-[0-9a-f]+$/;
1028
+ +/** Errors that prove a path names nothing; any other leaves it unknown. */
1029
+ +function provesAbsent(error) {
1030
+ + return error?.code === 'ENOENT' || error?.code === 'ENOTDIR';
1031
+ +}
1032
+ +/**
1033
+ + * Where `path` lies as the kernel resolves it, links before `..`: its real
1034
+ + * path when it exists; for a path proved absent, the real path of its nearest
1035
+ + * existing ancestor joined with the rest. `unknown` when a lookup fails
1036
+ + * otherwise. The literal path is walked, never normalized first, since `..`
1037
+ + * after a link leads out of the link's target, not back to where it was.
1038
+ + */
1039
+ +function locate(path, paths) {
1040
+ + const rest = [];
1041
+ + for (let current = path;; current = paths.dirname(current)) {
1042
+ + try {
1043
+ + const real = (0, fs_1.realpathSync)(current);
1044
+ + return { status: rest.length === 0 ? 'present' : 'absent', real: paths.join(real, ...rest) };
1045
+ + }
1046
+ + catch (error) {
1047
+ + if (!provesAbsent(error)) {
1048
+ + return { status: 'unknown' };
1049
+ + }
1050
+ + if (paths.dirname(current) === current) {
1051
+ + return { status: 'absent', real: paths.join(current, ...rest) };
1052
+ + }
1053
+ + rest.unshift(paths.basename(current));
1054
+ + }
1055
+ + }
1056
+ +}
1057
+ +/**
1058
+ + * What the filesystem says about an env's PATH, read once so that the
1059
+ + * identity decision below stays a function of its inputs: where the
1060
+ + * workspace and each absolute entry lie ({@link locate}), and the `node` a
1061
+ + * command lookup of PATH finds, as the runner that set NODE looked it up (the
1062
+ + * first entry holding an executable `node`, `node.exe` on Windows), or
1063
+ + * `undefined` when there is none or a lookup step could not tell. An entry
1064
+ + * with `.` or `..` segments is `unknown`: the kernel resolves `..` after a
1065
+ + * link, out of the link's target, and a runtime's realpath may collapse it
1066
+ + * lexically first, so no lookup here can say where it lies. Package managers
1067
+ + * write their entries in normal form.
1068
+ + */
1069
+ +function environmentFacts(entries, platform, workspaceRoot) {
1070
+ + const paths = platform === 'win32' ? path_1.win32 : path_1.posix;
1071
+ + const binary = platform === 'win32' ? 'node.exe' : 'node';
1072
+ + const located = new Map();
1073
+ + for (const entry of entries) {
1074
+ + if (entry && paths.isAbsolute(entry) && !located.has(entry)) {
1075
+ + const normal = !entry.split(/[\\/]/).some((segment) => segment === '.' || segment === '..');
1076
+ + located.set(entry, normal ? locate(entry, paths) : { status: 'unknown' });
1077
+ + }
1078
+ + }
1079
+ + let resolvedNode;
1080
+ + for (const entry of entries) {
1081
+ + const fact = located.get(entry);
1082
+ + if (fact?.status === 'absent') {
1083
+ + continue;
1084
+ + }
1085
+ + if (fact?.status !== 'present') {
1086
+ + // A relative entry depends on the client's directory, and an
1087
+ + // unknown one may hold a node: the lookup cannot tell.
1088
+ + break;
1089
+ + }
1090
+ + const candidate = paths.join(entry, binary);
1091
+ + try {
1092
+ + if ((0, fs_1.statSync)(candidate).isFile()) {
1093
+ + (0, fs_1.accessSync)(candidate, fs_1.constants.X_OK);
1094
+ + resolvedNode = candidate;
1095
+ + break;
1096
+ + }
1097
+ + }
1098
+ + catch (error) {
1099
+ + // execvp moves on past a missing or non-executable candidate; any
1100
+ + // other failure leaves the lookup unknown.
1101
+ + if (!provesAbsent(error) && error?.code !== 'EACCES') {
1102
+ + break;
1103
+ + }
1104
+ + }
1105
+ + }
1106
+ + return { workspace: locate(workspaceRoot, paths), entries: located, resolvedNode };
1107
+ +}
1108
+ /**
1109
+ - * Yarn Berry creates a fresh BERRY_BIN_FOLDER for every invocation and puts it
1110
+ - * first on PATH. The wrappers inside are runtime state the daemon and workers
1111
+ - * must receive, but the folder's random name is not project-graph identity:
1112
+ - * two invocations whose environments differ only by it would compute the same
1113
+ - * graph. Strips the folder from PATH and pins the var to a sentinel so such
1114
+ - * environments compare equal. Returns a copy; never mutates runtime env.
1115
+ + * An env as project-graph identity: the env without what a package manager
1116
+ + * adds to run one command in this workspace. The daemon and its workers still
1117
+ + * receive all of it, but two clients whose envs differ only by it compute the
1118
+ + * same graph, so a switch between them keeps the cached graph:
1119
+ + * - Yarn Berry's BERRY_BIN_FOLDER, fresh for every invocation and first on
1120
+ + * PATH: the folder leaves PATH and the var is pinned to a sentinel.
1121
+ + * - The `node_modules/.bin` directories `bun run`, `npm run`, pnpm and Yarn
1122
+ + * put in front of PATH for the run's directory and each of its ancestors:
1123
+ + * one that lies in the workspace, whose packages its lockfile identifies,
1124
+ + * and one in or above it proved absent (ENOENT or ENOTDIR), which resolves
1125
+ + * nothing. Where an entry lies is judged on its spelling and on its real
1126
+ + * path: a link inside the workspace (its own `node_modules` linked to an
1127
+ + * install elsewhere) is the workspace's. Another checkout's (present or
1128
+ + * absent), an existing or unknown one above the workspace, one with `.` or
1129
+ + * `..` segments and a relative one stay identity.
1130
+ + * - NODE, when it is the `node` PATH resolves: `bun run` sets it to the one it
1131
+ + * found, so PATH already carries that choice. With none on PATH, Bun makes a
1132
+ + * `bun-node-<hash>` directory whose `node` is Bun, puts it on PATH and names
1133
+ + * it in NODE; that directory leaves PATH with NODE when its `node` is the one
1134
+ + * PATH resolves. Any other NODE stays, and so does every other PATH entry.
1135
+ + * The order of the remaining PATH entries is kept. Returns a copy; never
1136
+ + * mutates runtime env.
1137
+ */
1138
+ function normalizeDaemonEnvironmentForGraph(env, platform = process.platform) {
1139
+ + const pathKey = Object.keys(env).find((key) => key.toUpperCase() === 'PATH');
1140
+ + const entries = pathKey && env[pathKey]
1141
+ + ? env[pathKey].split(platform === 'win32' ? ';' : ':')
1142
+ + : [];
1143
+ + return graphIdentity(env, platform, workspace_root_1.workspaceRoot, pathKey, entries, environmentFacts(entries, platform, workspace_root_1.workspaceRoot));
1144
+ +}
1145
+ +function graphIdentity(env, platform, workspaceRoot, pathKey, entries, facts) {
1146
+ const normalized = { ...env };
1147
+ - const berryBinFolder = normalized.BERRY_BIN_FOLDER;
1148
+ - if (!berryBinFolder) {
1149
+ - return normalized;
1150
+ + const windows = platform === 'win32';
1151
+ + const paths = windows ? path_1.win32 : path_1.posix;
1152
+ + const canonicalize = (value) => windows ? value.replaceAll('/', '\\').toLowerCase() : value;
1153
+ + const scaffolding = new Set();
1154
+ + if (normalized.BERRY_BIN_FOLDER) {
1155
+ + scaffolding.add(canonicalize(normalized.BERRY_BIN_FOLDER));
1156
+ + normalized.BERRY_BIN_FOLDER = BERRY_BIN_FOLDER_GRAPH_IDENTITY;
1157
+ + }
1158
+ + if (normalized.NODE && facts.resolvedNode !== undefined) {
1159
+ + const node = canonicalize(normalized.NODE);
1160
+ + const shim = BUN_NODE_SHIM.test(paths.basename(node)) &&
1161
+ + entries.some((entry) => canonicalize(entry) === node);
1162
+ + const selected = shim
1163
+ + ? paths.join(node, windows ? 'node.exe' : 'node')
1164
+ + : node;
1165
+ + if (canonicalize(facts.resolvedNode) === selected) {
1166
+ + if (shim) {
1167
+ + scaffolding.add(node);
1168
+ + }
1169
+ + delete normalized.NODE;
1170
+ + }
1171
+ }
1172
+ - const pathKey = Object.keys(normalized).find((key) => key.toUpperCase() === 'PATH');
1173
+ - if (pathKey && normalized[pathKey]) {
1174
+ - const pathDelimiter = platform === 'win32' ? ';' : ':';
1175
+ - const canonicalize = (value) => {
1176
+ - const withPlatformSeparators = platform === 'win32' ? value.replaceAll('/', '\\') : value;
1177
+ - return platform === 'win32'
1178
+ - ? withPlatformSeparators.toLowerCase()
1179
+ - : withPlatformSeparators;
1180
+ + if (entries.length > 0) {
1181
+ + const within = (path, directory) => path === directory ||
1182
+ + path.startsWith(directory.endsWith(paths.sep) ? directory : directory + paths.sep);
1183
+ + const roots = [paths.resolve(workspaceRoot)];
1184
+ + if (facts.workspace.status === 'present') {
1185
+ + roots.push(facts.workspace.real);
1186
+ + }
1187
+ + const workspaceRoots = roots.map(canonicalize);
1188
+ + const isRunScaffolding = (entry) => {
1189
+ + if (scaffolding.has(canonicalize(entry))) {
1190
+ + return true;
1191
+ + }
1192
+ + const fact = facts.entries.get(entry);
1193
+ + if (fact === undefined || fact.status === 'unknown') {
1194
+ + return false;
1195
+ + }
1196
+ + // A link inside the workspace (its own `node_modules` linked to an
1197
+ + // install elsewhere) is the workspace's, so an entry spelled in
1198
+ + // the workspace counts there as well as where it resolves.
1199
+ + const owners = [entry, fact.real]
1200
+ + .map(canonicalize)
1201
+ + .filter((location) => paths.basename(location) === '.bin' &&
1202
+ + paths.basename(paths.dirname(location)) === 'node_modules')
1203
+ + .map((location) => paths.dirname(paths.dirname(location)));
1204
+ + return owners.some((owner) => workspaceRoots.some((root) => within(owner, root) ||
1205
+ + (fact.status === 'absent' && within(root, owner))));
1206
+ };
1207
+ - const canonicalBerryBinFolder = canonicalize(berryBinFolder);
1208
+ - normalized[pathKey] = normalized[pathKey]
1209
+ - .split(pathDelimiter)
1210
+ - .filter((entry) => canonicalize(entry) !== canonicalBerryBinFolder)
1211
+ - .join(pathDelimiter);
1212
+ + normalized[pathKey] = entries
1213
+ + .filter((entry) => !isRunScaffolding(entry))
1214
+ + .join(windows ? ';' : ':');
1215
+ }
1216
+ - normalized.BERRY_BIN_FOLDER = BERRY_BIN_FOLDER_GRAPH_IDENTITY;
1217
+ return normalized;
1218
+ }
1219
+ function getChangedEnvKeys(before, after) {
628
1220
  diff --git a/dist/src/daemon/message-types/daemon-message.d.ts b/dist/src/daemon/message-types/daemon-message.d.ts
629
1221
  index 9dd6123ad6d9adb20a07f7db56fc476be78cbcca..0b1ee217729d63a29806cf6c38d0fc5d4a2161a5 100644
630
1222
  --- a/dist/src/daemon/message-types/daemon-message.d.ts
@@ -826,10 +1418,18 @@ index ef00ae3212498e75d6bb6371aec4b113970c0c68..06eb2bf7436cc20dad1d646be44ba990
826
1418
  logger_1.serverLogger.log('[LATEST-NX]: Reusing cached Nx installation from', latestNxTmpPath);
827
1419
  return latestNxTmpPath;
828
1420
  diff --git a/dist/src/daemon/server/project-graph-incremental-recomputation.js b/dist/src/daemon/server/project-graph-incremental-recomputation.js
829
- index c2a2f8f70fe71940e47c97931c5925bac1fbd480..39582a8fc342ae59decd3d4ca25179f2bc478691 100644
1421
+ index c2a2f8f70fe71940e47c97931c5925bac1fbd480..00774360849ccb5f0ed64ba4cf6d572d11c44493 100644
830
1422
  --- a/dist/src/daemon/server/project-graph-incremental-recomputation.js
831
1423
  +++ b/dist/src/daemon/server/project-graph-incremental-recomputation.js
832
- @@ -27,6 +27,7 @@ const file_watcher_sockets_1 = require("./file-watching/file-watcher-sockets");
1424
+ @@ -15,6 +15,7 @@ const error_types_1 = require("../../project-graph/error-types");
1425
+ const file_map_utils_1 = require("../../project-graph/file-map-utils");
1426
+ const nx_deps_cache_1 = require("../../project-graph/nx-deps-cache");
1427
+ const get_plugins_1 = require("../../project-graph/plugins/get-plugins");
1428
+ +const local_plugin_source_1 = require("../../project-graph/plugins/local-plugin-source");
1429
+ const retrieve_workspace_files_1 = require("../../project-graph/utils/retrieve-workspace-files");
1430
+ const fileutils_1 = require("../../utils/fileutils");
1431
+ const workspace_context_1 = require("../../utils/workspace-context");
1432
+ @@ -27,6 +28,7 @@ const file_watcher_sockets_1 = require("./file-watching/file-watcher-sockets");
833
1433
  const project_graph_listener_sockets_1 = require("./project-graph-listener-sockets");
834
1434
  const watcher_1 = require("./watcher");
835
1435
  const logger_1 = require("../logger");
@@ -837,7 +1437,32 @@ index c2a2f8f70fe71940e47c97931c5925bac1fbd480..39582a8fc342ae59decd3d4ca25179f2
837
1437
  let cachedSerializedProjectGraphPromise;
838
1438
  // Maps file path to a version counter that increments on each modification.
839
1439
  // This lets us detect mid-flight re-modifications when clearing processed files.
840
- @@ -241,6 +242,11 @@ function scheduleProjectGraphRecomputation(createdFiles, updatedFiles, deletedFi
1440
+ @@ -76,7 +78,7 @@ function kickOffRecompute() {
1441
+ // the plugin loader's) into one, so the snap hash and the plugin
1442
+ // set the compute uses always reflect the same disk state.
1443
+ const nxJson = (0, nx_json_1.readNxJson)(workspace_root_1.workspaceRoot);
1444
+ - const myPluginsHash = (0, file_hasher_1.hashObject)(nxJson.plugins ?? []);
1445
+ + const myPluginsHash = (0, get_plugins_1.getPluginsConfigurationHash)(nxJson, workspace_root_1.workspaceRoot);
1446
+ const plugins = await (0, get_plugins_1.getPluginsSeparated)(nxJson, workspace_root_1.workspaceRoot);
1447
+ // Plugin set we just loaded may already be stale vs disk.
1448
+ if (isStale(myPluginsHash))
1449
+ @@ -114,13 +116,13 @@ function isStale(expectedHash) {
1450
+ * pointer so awaiters chain onto it.
1451
+ */
1452
+ function chainToSuccessor(myPromise) {
1453
+ - logger_1.serverLogger.log('Discarding stale recompute result (nx.json plugins changed mid-compute).');
1454
+ + logger_1.serverLogger.log('Discarding stale recompute result (plugin configuration or local source changed mid-compute).');
1455
+ if (cachedSerializedProjectGraphPromise === myPromise)
1456
+ kickOffRecompute();
1457
+ return cachedSerializedProjectGraphPromise;
1458
+ }
1459
+ function readNxJsonPluginsHash() {
1460
+ - return (0, file_hasher_1.hashObject)((0, nx_json_1.readNxJson)(workspace_root_1.workspaceRoot).plugins ?? []);
1461
+ + return (0, get_plugins_1.getPluginsConfigurationHash)((0, nx_json_1.readNxJson)(workspace_root_1.workspaceRoot), workspace_root_1.workspaceRoot);
1462
+ }
1463
+ async function getCachedSerializedProjectGraphPromise(socket) {
1464
+ // Subscribe the requesting client to the graph-construction topic
1465
+ @@ -241,6 +243,12 @@ function scheduleProjectGraphRecomputation(createdFiles, updatedFiles, deletedFi
841
1466
  : {};
842
1467
  perf_hooks_1.performance.mark('hash-watched-changes-end');
843
1468
  perf_hooks_1.performance.measure('hash changed files from watcher', 'hash-watched-changes-start', 'hash-watched-changes-end');
@@ -846,10 +1471,11 @@ index c2a2f8f70fe71940e47c97931c5925bac1fbd480..39582a8fc342ae59decd3d4ca25179f2
846
1471
  + (0, restart_checks_1.restartDaemonIfLockFilesChanged)(deletedFiles)) {
847
1472
  + return;
848
1473
  + }
1474
+ + (0, local_plugin_source_1.invalidateLocalPluginSources)(workspace_root_1.workspaceRoot, [...changedPaths, ...deletedFiles]);
849
1475
  for (const [f, hash] of Object.entries(changedFileHashes)) {
850
1476
  collectedDeletedFiles.delete(f);
851
1477
  collectedUpdatedFiles.set(f, { version: fileChangeCounter, hash });
852
- @@ -251,7 +257,7 @@ function scheduleProjectGraphRecomputation(createdFiles, updatedFiles, deletedFi
1478
+ @@ -251,7 +259,7 @@ function scheduleProjectGraphRecomputation(createdFiles, updatedFiles, deletedFi
853
1479
  }
854
1480
  // The native watcher already coalesces a burst of events into one batch,
855
1481
  // so socket + listener notifications dispatch immediately.
@@ -860,26 +1486,28 @@ index c2a2f8f70fe71940e47c97931c5925bac1fbd480..39582a8fc342ae59decd3d4ca25179f2
860
1486
  // Bump generation synchronously so any in-flight compute fails its
861
1487
  diff --git a/dist/src/daemon/server/restart-checks.d.ts b/dist/src/daemon/server/restart-checks.d.ts
862
1488
  new file mode 100644
863
- index 0000000000000000000000000000000000000000..28f263f82b7b698ec1bf88943924696b3d9a91e7
1489
+ index 0000000000000000000000000000000000000000..5a83ab16c263a4563b5e8c7452f254ded8061d91
864
1490
  --- /dev/null
865
1491
  +++ b/dist/src/daemon/server/restart-checks.d.ts
866
- @@ -0,0 +1,10 @@
1492
+ @@ -0,0 +1,12 @@
867
1493
  +import type { Server, Socket } from 'net';
868
1494
  +import type { WatchEvent } from '../../native';
869
1495
  +export declare const DAEMON_VALIDITY_SAFETY_INTERVAL_MS = 5000;
870
1496
  +export declare const relativeServerProcess: string;
871
1497
  +export declare function registerDaemonForRestartChecks(server: Server, sockets: Iterable<Socket>, socketPath: string): void;
872
- +export declare function recordInstallationBaseline(): void;
1498
+ +export declare function recordInstallationBaseline(): Promise<void>;
873
1499
  +export declare function restartDaemonIfLockFilesChanged(paths: string[]): boolean;
874
1500
  +export declare function stopDaemonIfReplaced(events: WatchEvent[]): boolean;
875
1501
  +export declare function stopDaemonIfOutdated(): boolean;
876
1502
  +export declare function stopDaemonIfWorkspaceGone(): boolean;
1503
+ +/** Stops the daemon, naming `reason` in its log: its file watcher stopped delivering. */
1504
+ +export declare function stopDaemonForStoppedWatcher(reason: string): void;
877
1505
  diff --git a/dist/src/daemon/server/restart-checks.js b/dist/src/daemon/server/restart-checks.js
878
1506
  new file mode 100644
879
- index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3d4475ada
1507
+ index 0000000000000000000000000000000000000000..0c065105dff1ed0847e28672661b1792f80cea29
880
1508
  --- /dev/null
881
1509
  +++ b/dist/src/daemon/server/restart-checks.js
882
- @@ -0,0 +1,306 @@
1510
+ @@ -0,0 +1,391 @@
883
1511
  +"use strict";
884
1512
  +Object.defineProperty(exports, "__esModule", { value: true });
885
1513
  +exports.relativeServerProcess = exports.DAEMON_VALIDITY_SAFETY_INTERVAL_MS = void 0;
@@ -889,6 +1517,7 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
889
1517
  +exports.stopDaemonIfReplaced = stopDaemonIfReplaced;
890
1518
  +exports.stopDaemonIfOutdated = stopDaemonIfOutdated;
891
1519
  +exports.stopDaemonIfWorkspaceGone = stopDaemonIfWorkspaceGone;
1520
+ +exports.stopDaemonForStoppedWatcher = stopDaemonForStoppedWatcher;
892
1521
  +const fs_1 = require("fs");
893
1522
  +const path_1 = require("path");
894
1523
  +const file_hasher_1 = require("../../hasher/file-hasher");
@@ -930,11 +1559,11 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
930
1559
  +}
931
1560
  +// The startup baseline of everything a restart judges by bytes: the root
932
1561
  +// lockfiles and the linked plugins below.
933
- +function recordInstallationBaseline() {
1562
+ +async function recordInstallationBaseline() {
934
1563
  + lockFileHash = hashLockFiles();
935
1564
  + const roots = linkedPluginRoots() ?? [];
936
1565
  + linkedPluginRootKey = JSON.stringify(roots);
937
- + linkedPluginFiles = hashCensus(linkedPluginCensus(roots), new Map());
1566
+ + linkedPluginFiles = hashCensus(await linkedPluginCensus(roots), new Map());
938
1567
  + linkedPluginCensusAt = Date.now();
939
1568
  +}
940
1569
  +function lockFileHashChanged() {
@@ -951,18 +1580,23 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
951
1580
  +// developer link into another checkout) changes behind that link with no
952
1581
  +// lockfile change here. The daemon kept serving the graph the old plugin code
953
1582
  +// inferred, so every task hashed after the change was keyed on a configuration
954
- +// no fresh process computes. Its published files and the lockfiles of the
955
- +// checkout it lives in are judged like the root lockfiles: a file whose stat
956
- +// moved is hashed again, and only changed bytes restart the daemon. Files are
957
- +// keyed by plugin and package-relative path, so a link moved to a copy with the
958
- +// same bytes keeps the daemon. A request resolves each link and stats the files
959
- +// already known; a moved link, and at most once per safety interval anyway, takes
960
- +// the full census, which finds a file added beside them. A package inside the
1583
+ +// no fresh process computes. The files npm would publish of it — its `files`,
1584
+ +// negations and globs as `npm pack` reads them, from npm-packlist resolved in
1585
+ +// the checkout it lives in — and that checkout's lockfiles are judged like the
1586
+ +// root lockfiles: a file whose stat moved is hashed again, and only changed
1587
+ +// bytes restart the daemon. Files are keyed by plugin and package-relative
1588
+ +// path, so a link moved to a copy with the same bytes keeps the daemon. A
1589
+ +// request resolves each link and stats the files already known at the link's
1590
+ +// current target; a moved link, and at most once per safety interval anyway,
1591
+ +// starts the full census in the background, which finds a file added beside
1592
+ +// them and restarts the daemon when one changed. A package inside the
961
1593
  +// workspace changes only with its lockfile.
962
1594
  +let linkedPluginRootKey;
963
1595
  +let linkedPluginFiles;
964
1596
  +let linkedPluginCensusAt = 0;
965
- +const PACKAGE_GLOB = /[*?[{]/;
1597
+ +let linkedPluginCensusRunning = false;
1598
+ +const linkedPluginCheckouts = new Map();
1599
+ +const linkedPluginsWithoutPacklist = new Set();
966
1600
  +function pluginSpecifiers() {
967
1601
  + let plugins;
968
1602
  + try {
@@ -1022,30 +1656,65 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1022
1656
  + census.set(`${prefix}${(0, path_1.relative)(base, path)}`, { path, stat: statKey(stat) });
1023
1657
  + }
1024
1658
  +}
1025
- +// Every file each linked plugin publishes and its checkout's lockfiles.
1026
- +function linkedPluginCensus(roots) {
1659
+ +// The checkout a linked package lives in: the nearest directory above it that
1660
+ +// holds a lockfile, or the file system's root.
1661
+ +function checkoutOf(root) {
1662
+ + let checkout = linkedPluginCheckouts.get(root);
1663
+ + if (checkout !== undefined) return checkout;
1664
+ + for (checkout = root; ; checkout = (0, path_1.dirname)(checkout)) {
1665
+ + const parent = (0, path_1.dirname)(checkout);
1666
+ + if (parent === checkout || LOCK_FILE_NAMES.some((lock) => (0, fs_1.existsSync)((0, path_1.join)(checkout, lock)))) break;
1667
+ + }
1668
+ + linkedPluginCheckouts.set(root, checkout);
1669
+ + return checkout;
1670
+ +}
1671
+ +// npm's own reading of what a package publishes (npm-packlist, as `npm pack`
1672
+ +// and `npm publish` read it), from the checkout that publishes the package;
1673
+ +// null when that checkout has none.
1674
+ +function npmPacklist(checkout) {
1675
+ + let resolved;
1676
+ + try {
1677
+ + resolved = require.resolve('npm-packlist', { paths: [checkout] });
1678
+ + }
1679
+ + catch (error) {
1680
+ + if (error.code === 'MODULE_NOT_FOUND') return null;
1681
+ + throw error;
1682
+ + }
1683
+ + return require(resolved);
1684
+ +}
1685
+ +// Every file each linked plugin publishes and its checkout's lockfiles. A
1686
+ +// package whose manifest does not parse, or whose checkout has no npm-packlist
1687
+ +// to read it, is judged whole.
1688
+ +async function linkedPluginCensus(roots) {
1027
1689
  + const census = new Map();
1028
1690
  + for (const [name, root] of roots) {
1029
1691
  + const manifest = (0, path_1.join)(root, 'package.json');
1030
1692
  + addFiles(census, `${name}\0`, root, manifest);
1031
- + // A manifest with no plain `files` list (absent, a glob, or bytes that do
1032
- + // not parse, which the census holds too) publishes the whole package.
1033
- + let files;
1693
+ + const checkout = checkoutOf(root);
1694
+ + let packageJson;
1034
1695
  + try {
1035
- + files = JSON.parse((0, fs_1.readFileSync)(manifest, 'utf8')).files;
1696
+ + packageJson = JSON.parse((0, fs_1.readFileSync)(manifest, 'utf8'));
1036
1697
  + }
1037
1698
  + catch {
1038
- + files = undefined;
1699
+ + packageJson = undefined;
1039
1700
  + }
1040
- + const listed = Array.isArray(files) ? files.filter((file) => typeof file !== 'string' || !file.startsWith('!')) : [];
1041
- + const published = listed.length > 0 && listed.every((file) => typeof file === 'string' && !PACKAGE_GLOB.test(file));
1042
- + for (const file of published ? listed : ['.']) addFiles(census, `${name}\0`, root, (0, path_1.join)(root, file));
1043
- + for (let directory = root; ; directory = (0, path_1.dirname)(directory)) {
1044
- + const locks = LOCK_FILE_NAMES.map((lock) => (0, path_1.join)(directory, lock)).filter((lock) => (0, fs_1.existsSync)(lock));
1045
- + if (locks.length > 0 || (0, path_1.dirname)(directory) === directory) {
1046
- + for (const lock of locks) addFiles(census, `${name}\0lock\0`, directory, lock);
1047
- + break;
1701
+ + const packlist = packageJson && typeof packageJson === 'object' ? npmPacklist(checkout) : null;
1702
+ + if (packlist) {
1703
+ + // A project-root node of npm's tree with no bundled dependencies: what
1704
+ + // npm-packlist reads of a package it packs alone.
1705
+ + const files = await packlist({ path: root, package: packageJson, isProjectRoot: true, edgesOut: new Map(), workspaces: null });
1706
+ + for (const file of files) addFiles(census, `${name}\0`, root, (0, path_1.join)(root, file));
1707
+ + }
1708
+ + else {
1709
+ + if (packageJson && !linkedPluginsWithoutPacklist.has(name)) {
1710
+ + linkedPluginsWithoutPacklist.add(name);
1711
+ + logger_1.serverLogger.log(`[Server] linked plugin ${name}: ${checkout} has no npm-packlist to read what it publishes, so every file of the package is judged`);
1048
1712
  + }
1713
+ + addFiles(census, `${name}\0`, root, root);
1714
+ + }
1715
+ + for (const lock of LOCK_FILE_NAMES) {
1716
+ + const path = (0, path_1.join)(checkout, lock);
1717
+ + if ((0, fs_1.existsSync)(path)) addFiles(census, `${name}\0lock\0`, checkout, path);
1049
1718
  + }
1050
1719
  + }
1051
1720
  + return census;
@@ -1062,10 +1731,18 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1062
1731
  + }
1063
1732
  + return files;
1064
1733
  +}
1065
- +// The known files' stats, a vanished one left out.
1066
- +function knownFileStats() {
1734
+ +// The known files' stats at each link's current target, a vanished one and one
1735
+ +// of a plugin no longer linked left out.
1736
+ +function knownFileStats(roots) {
1737
+ + const located = new Map(roots);
1067
1738
  + const census = new Map();
1068
- + for (const [key, { path }] of linkedPluginFiles) {
1739
+ + for (const key of linkedPluginFiles.keys()) {
1740
+ + const [name, ...rest] = key.split('\0');
1741
+ + const root = located.get(name);
1742
+ + if (root === undefined) continue;
1743
+ + const path = rest.length === 2 && rest[0] === 'lock'
1744
+ + ? (0, path_1.join)(checkoutOf(root), rest[1])
1745
+ + : (0, path_1.join)(root, rest[0]);
1069
1746
  + let stat;
1070
1747
  + try {
1071
1748
  + stat = (0, fs_1.statSync)(path, { bigint: true });
@@ -1078,18 +1755,10 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1078
1755
  + }
1079
1756
  + return census;
1080
1757
  +}
1081
- +function linkedPluginsChanged() {
1082
- + if (linkedPluginFiles === undefined) return false;
1083
- + const roots = linkedPluginRoots();
1084
- + if (roots === null) return false;
1085
- + const rootKey = JSON.stringify(roots);
1086
- + const now = Date.now();
1087
- + const full = rootKey !== linkedPluginRootKey || now - linkedPluginCensusAt >= exports.DAEMON_VALIDITY_SAFETY_INTERVAL_MS;
1088
- + const census = full ? linkedPluginCensus(roots) : knownFileStats();
1089
- + if (full) {
1090
- + linkedPluginRootKey = rootKey;
1091
- + linkedPluginCensusAt = now;
1092
- + }
1758
+ +// Judge `census` against the files known so far, which it then replaces: the
1759
+ +// first file that appeared, changed its bytes or disappeared, described; null
1760
+ +// when none did.
1761
+ +function judgeLinkedPlugins(census) {
1093
1762
  + const files = hashCensus(census, linkedPluginFiles);
1094
1763
  + let change = null;
1095
1764
  + for (const [key, { path, hash }] of files) {
@@ -1101,9 +1770,43 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1101
1770
  + if (!files.has(key)) change ??= `${path} disappeared`;
1102
1771
  + }
1103
1772
  + linkedPluginFiles = files;
1104
- + if (change === null) return false;
1105
- + logger_1.serverLogger.log(`[Server] linked plugin file ${change}`);
1106
- + return true;
1773
+ + if (change !== null) logger_1.serverLogger.log(`[Server] linked plugin file ${change}`);
1774
+ + return change;
1775
+ +}
1776
+ +// The first linked plugin file whose bytes moved since the baseline among the
1777
+ +// files already known, described; null when none did. A moved link, or the
1778
+ +// safety interval, starts the full census in the background.
1779
+ +function linkedPluginsChanged() {
1780
+ + if (linkedPluginFiles === undefined) return null;
1781
+ + const roots = linkedPluginRoots();
1782
+ + if (roots === null) return null;
1783
+ + const rootKey = JSON.stringify(roots);
1784
+ + const now = Date.now();
1785
+ + if (!linkedPluginCensusRunning &&
1786
+ + (rootKey !== linkedPluginRootKey || now - linkedPluginCensusAt >= exports.DAEMON_VALIDITY_SAFETY_INTERVAL_MS)) {
1787
+ + linkedPluginRootKey = rootKey;
1788
+ + linkedPluginCensusAt = now;
1789
+ + linkedPluginCensusRunning = true;
1790
+ + linkedPluginCensus(roots).then((census) => {
1791
+ + linkedPluginCensusRunning = false;
1792
+ + const change = judgeLinkedPlugins(census);
1793
+ + if (change !== null) restartForLinkedPlugin(change);
1794
+ + }, (error) => {
1795
+ + linkedPluginCensusRunning = false;
1796
+ + logger_1.serverLogger.log(`[Server] the linked plugin census failed, so a file added beside the known ones is not seen before the next census: ${error.message}`);
1797
+ + });
1798
+ + }
1799
+ + return judgeLinkedPlugins(knownFileStats(roots));
1800
+ +}
1801
+ +// Every graph this daemon served, and so every task a connected command
1802
+ +// planned and keyed by it, came from the plugin code before the change. A
1803
+ +// successor, or the command's own process, would load the new code, so no
1804
+ +// command resumes: each is told why and fails (DaemonClient.loseDaemon).
1805
+ +function restartForLinkedPlugin(change) {
1806
+ + const reason = `LINKED_PLUGIN_CHANGED (linked plugin file ${change})`;
1807
+ + const notice = new Error(`The Nx daemon (pid ${process.pid}) stopped: ${reason}. This command's project graph and task hashes came from the plugin before that change; run it again.`);
1808
+ + notice.daemonGraphChanged = true;
1809
+ + restartDaemon('LINKED_PLUGIN_CHANGED', notice);
1107
1810
  +}
1108
1811
  +// Applied paths have been hashed by the native workspace context; same-byte
1109
1812
  +// rewrites were dropped already. No lockfile is read on this watcher path.
@@ -1158,8 +1861,9 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1158
1861
  + restartDaemon('LOCK_FILES_CHANGED');
1159
1862
  + return true;
1160
1863
  + }
1161
- + if (linkedPluginsChanged()) {
1162
- + restartDaemon('LINKED_PLUGIN_CHANGED');
1864
+ + const change = linkedPluginsChanged();
1865
+ + if (change !== null) {
1866
+ + restartForLinkedPlugin(change);
1163
1867
  + return true;
1164
1868
  + }
1165
1869
  + return false;
@@ -1167,27 +1871,36 @@ index 0000000000000000000000000000000000000000..6bd6df6e26cdbc306b33c01b363dc4b3
1167
1871
  +function isReplaced() {
1168
1872
  + return (0, cache_1.getDaemonProcessIdSync)() !== process.pid;
1169
1873
  +}
1170
- +function stopDaemon(reason) {
1874
+ +function stopDaemon(reason, notice) {
1171
1875
  + if (!daemon || terminating) return;
1172
1876
  + terminating = true;
1173
- + (0, shutdown_utils_1.handleServerProcessTermination)({ server: daemon.server, reason, sockets: daemon.sockets });
1877
+ + (0, shutdown_utils_1.handleServerProcessTermination)({ server: daemon.server, reason, sockets: daemon.sockets, notice });
1878
+ +}
1879
+ +// A file watcher that stopped delivering leaves the daemon blind to every
1880
+ +// later change, so it stops rather than restarts: a successor it spawned
1881
+ +// would inherit whatever stopped its stream (a sandbox that denies FSEvents,
1882
+ +// say), and the next client or the keeper starts one in its own context.
1883
+ +function stopDaemonForStoppedWatcher(reason) {
1884
+ + logger_1.serverLogger.watcherLog(reason);
1885
+ + stopDaemon(reason);
1174
1886
  +}
1175
- +function restartDaemon(reason) {
1887
+ +// `notice`, when given, is the error each connected command fails with.
1888
+ +function restartDaemon(reason, notice) {
1176
1889
  + if (!daemon || terminating) return;
1177
1890
  + logger_1.serverLogger.log(`[Server] Daemon outdated: ${reason}`);
1178
1891
  + if (keptByLauncher) {
1179
1892
  + logger_1.serverLogger.log('[Server] Stopping: the process that started this daemon (--kept) starts its successor');
1180
- + stopDaemon(reason);
1893
+ + stopDaemon(reason, notice);
1181
1894
  + return;
1182
1895
  + }
1183
1896
  + // Do not spawn multiple replacements or answer queued requests while the
1184
1897
  + // asynchronous restart releases ownership and starts the successor.
1185
1898
  + terminating = true;
1186
1899
  + logger_1.serverLogger.log('[Server] Restarting daemon...');
1187
- + (0, shutdown_utils_1.handleServerProcessTerminationWithRestart)({ server: daemon.server, reason, sockets: daemon.sockets });
1900
+ + (0, shutdown_utils_1.handleServerProcessTerminationWithRestart)({ server: daemon.server, reason, sockets: daemon.sockets, notice });
1188
1901
  +}
1189
1902
  diff --git a/dist/src/daemon/server/server.js b/dist/src/daemon/server/server.js
1190
- index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79de1b47244 100644
1903
+ index b1c143e244efd435b28642ed86e999d6c86f7d94..cd24ff30639e538422cf813679e80966a83c3074 100644
1191
1904
  --- a/dist/src/daemon/server/server.js
1192
1905
  +++ b/dist/src/daemon/server/server.js
1193
1906
  @@ -7,17 +7,13 @@ const fs_1 = require("fs");
@@ -1232,7 +1945,7 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1232
1945
  let workspaceWatcherError;
1233
1946
  global.NX_DAEMON = true;
1234
1947
  process.env.NX_DAEMON_PROCESS = 'true';
1235
- @@ -97,6 +92,9 @@ const server = (0, net_1.createServer)(async (socket) => {
1948
+ @@ -97,11 +92,16 @@ const server = (0, net_1.createServer)(async (socket) => {
1236
1949
  numberOfOpenConnections -= 1;
1237
1950
  exports.openSockets.delete(socket);
1238
1951
  logger_1.serverLogger.log(`Closed a connection. Number of open connections: ${numberOfOpenConnections}`);
@@ -1242,7 +1955,14 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1242
1955
  (0, file_watcher_sockets_1.removeRegisteredFileWatcherSocket)(socket);
1243
1956
  (0, project_graph_listener_sockets_1.removeRegisteredProjectGraphListenerSocket)(socket);
1244
1957
  });
1245
- @@ -134,6 +132,11 @@ async function handleMessage(socket, data) {
1958
+ });
1959
+ registerProcessTerminationListeners();
1960
+ +/** Connections whose first file-state request found the watcher current. */
1961
+ +const provenConnections = new WeakSet();
1962
+ async function handleMessage(socket, data) {
1963
+ if (workspaceWatcherError) {
1964
+ await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, `File watcher error in the workspace '${workspace_root_1.workspaceRoot}'.`, workspaceWatcherError);
1965
+ @@ -134,9 +134,36 @@ async function handleMessage(socket, data) {
1246
1966
  return;
1247
1967
  }
1248
1968
  }
@@ -1254,7 +1974,32 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1254
1974
  if ((0, daemon_message_1.isDaemonMessage)(payload) && payload.env) {
1255
1975
  await (0, handle_client_env_1.handleClientEnv)(payload.env);
1256
1976
  }
1257
- @@ -185,15 +188,6 @@ async function handleMessage(socket, data) {
1977
+ + // The first request on a connection that is answered from the workspace's
1978
+ + // files waits until every write made before it has reached the daemon: an
1979
+ + // nx command opens its connection when it starts, so it sees every write
1980
+ + // made before it. Later requests on the connection are served as Nx
1981
+ + // always did, so a write another process makes while the command runs is
1982
+ + // as visible as it was. A watcher that stopped delivering stops the daemon
1983
+ + // instead; the client is told so first, and continues without a daemon at
1984
+ + // once.
1985
+ + if (answersFromFiles(payload) && !provenConnections.has(socket)) {
1986
+ + const stopped = await (0, watcher_1.proveWatcherCurrent)();
1987
+ + if (stopped !== undefined) {
1988
+ + const error = new Error(`The Nx daemon stopped: ${stopped}. This command continues without it.`);
1989
+ + // The client's tag for "this daemon is gone; continue without one in
1990
+ + // this process" (DaemonClient.orWithoutDaemon), as when a client that
1991
+ + // may not start a daemon loses its daemon.
1992
+ + error.daemonStartForbidden = true;
1993
+ + await (0, shutdown_utils_1.respondWithError)(socket, 'watcher stream stopped', error);
1994
+ + (0, restart_checks_1.stopDaemonForStoppedWatcher)(stopped);
1995
+ + return;
1996
+ + }
1997
+ + provenConnections.add(socket);
1998
+ + }
1999
+ if (payload.type === 'PING') {
2000
+ await handleResult(socket, 'PING', () => Promise.resolve({ response: true, description: 'ping' }), mode);
2001
+ }
2002
+ @@ -185,15 +212,6 @@ async function handleMessage(socket, data) {
1258
2003
  else if ((0, hash_glob_1.isHandleHashMultiGlobMessage)(payload)) {
1259
2004
  await handleResult(socket, hash_glob_1.HASH_GLOB, () => (0, handle_hash_glob_1.handleHashMultiGlob)(payload.globGroups), mode);
1260
2005
  }
@@ -1270,7 +2015,44 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1270
2015
  else if ((0, force_shutdown_1.isHandleForceShutdownMessage)(payload)) {
1271
2016
  await handleResult(socket, 'FORCE_SHUTDOWN', () => (0, handle_force_shutdown_1.handleForceShutdown)(server), mode);
1272
2017
  }
1273
- @@ -256,9 +250,10 @@ async function handleResult(socket, type, hrFn, mode) {
2018
+ @@ -231,6 +249,22 @@ async function handleMessage(socket, data) {
2019
+ await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, `Invalid payload from the client`, new Error(`Unsupported payload sent to daemon server: ${(0, consume_messages_from_socket_1.describeMessage)(unparsedPayload, { maxBytes: 200 })}`));
2020
+ }
2021
+ }
2022
+ +/**
2023
+ + * Whether the answer to `payload` depends on the workspace's files: every
2024
+ + * request except liveness, shutdown, subscriptions and the Nx Console and AI
2025
+ + * agent status, which read none.
2026
+ + */
2027
+ +function answersFromFiles(payload) {
2028
+ + return !(payload.type === 'PING' ||
2029
+ + payload.type === 'REQUEST_SHUTDOWN' ||
2030
+ + payload.type === 'REGISTER_FILE_WATCHER' ||
2031
+ + (0, register_project_graph_listener_1.isRegisterProjectGraphListenerMessage)(payload) ||
2032
+ + (0, force_shutdown_1.isHandleForceShutdownMessage)(payload) ||
2033
+ + (0, nx_console_1.isHandleGetNxConsoleStatusMessage)(payload) ||
2034
+ + (0, nx_console_1.isHandleSetNxConsolePreferenceAndInstallMessage)(payload) ||
2035
+ + (0, configure_ai_agents_1.isHandleGetConfigureAiAgentsStatusMessage)(payload) ||
2036
+ + (0, configure_ai_agents_1.isHandleResetConfigureAiAgentsStatusMessage)(payload));
2037
+ +}
2038
+ async function handleResult(socket, type, hrFn, mode) {
2039
+ let hr;
2040
+ const startMark = new Date();
2041
+ @@ -242,7 +276,12 @@ async function handleResult(socket, type, hrFn, mode) {
2042
+ }
2043
+ const doneHandlingMark = new Date();
2044
+ if (hr.error) {
2045
+ - await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, hr.description, hr.error);
2046
+ + if (type === 'REQUEST_PROJECT_GRAPH') {
2047
+ + await (0, shutdown_utils_1.respondWithError)(socket, hr.description, hr.error);
2048
+ + }
2049
+ + else {
2050
+ + await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, hr.description, hr.error);
2051
+ + }
2052
+ }
2053
+ else {
2054
+ logger_1.serverLogger.log(`Serializing response for ${type} message in ${mode} mode`);
2055
+ @@ -256,9 +295,10 @@ async function handleResult(socket, type, hrFn, mode) {
1274
2056
  logger_1.serverLogger.log(`Handled ${mode} message ${type}. Handling time: ${doneHandlingMark.getTime() - startMark.getTime()}. Response time: ${endMark.getTime() - doneHandlingMark.getTime()}.`);
1275
2057
  }
1276
2058
  function handleInactivityTimeout() {
@@ -1283,7 +2065,7 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1283
2065
  (0, shutdown_utils_1.resetInactivityTimeout)(handleInactivityTimeout);
1284
2066
  }
1285
2067
  else {
1286
- @@ -287,37 +282,6 @@ function registerProcessTerminationListeners() {
2068
+ @@ -287,37 +327,6 @@ function registerProcessTerminationListeners() {
1287
2069
  sockets: exports.openSockets,
1288
2070
  }));
1289
2071
  }
@@ -1321,7 +2103,7 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1321
2103
  /**
1322
2104
  * When applicable files in the workspaces are changed (created, updated, deleted),
1323
2105
  * we need to recompute the cached serialized project graph so that it is readily
1324
- @@ -339,6 +303,11 @@ const handleWorkspaceChanges = async (err, changeEvents) => {
2106
+ @@ -339,6 +348,11 @@ const handleWorkspaceChanges = async (err, changeEvents) => {
1325
2107
  return;
1326
2108
  }
1327
2109
  logger_1.serverLogger.watcherLog((0, watcher_1.convertChangeEventsToLogMessage)(changeEvents));
@@ -1333,7 +2115,7 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1333
2115
  (0, route_workspace_changes_1.routeWorkspaceChanges)(changeEvents);
1334
2116
  }
1335
2117
  catch (err) {
1336
- @@ -348,7 +317,43 @@ const handleWorkspaceChanges = async (err, changeEvents) => {
2118
+ @@ -348,65 +362,68 @@ const handleWorkspaceChanges = async (err, changeEvents) => {
1337
2119
  (0, file_watcher_sockets_1.notifyFileWatcherSocketsOfError)(err);
1338
2120
  }
1339
2121
  };
@@ -1373,11 +2155,15 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1373
2155
  + }
1374
2156
  + (0, restart_checks_1.registerDaemonForRestartChecks)(server, exports.openSockets, socketPath);
1375
2157
  + // Record before watch/scan so a lockfile or linked plugin changed during boot is not lost.
1376
- + (0, restart_checks_1.recordInstallationBaseline)();
2158
+ + await (0, restart_checks_1.recordInstallationBaseline)();
1377
2159
  // Watch before scan: a file written during boot must be visible to the
1378
2160
  // watcher or the scan below. Scan-first left a blind window where such
1379
2161
  // files stayed invisible to both until an unrelated change arrived.
1380
- @@ -359,54 +364,21 @@ async function startServer() {
2162
+ if (!(0, shutdown_utils_1.getWatcherInstance)()) {
2163
+ - (0, shutdown_utils_1.storeWatcherInstance)(await (0, watcher_1.watchWorkspace)(server, handleWorkspaceChanges));
2164
+ + (0, shutdown_utils_1.storeWatcherInstance)(await (0, watcher_1.watchWorkspace)(server, handleWorkspaceChanges, handle_outputs_changes_1.handleOutputsChanges));
2165
+ logger_1.serverLogger.watcherLog(`Subscribed to changes within: ${workspace_root_1.workspaceRoot} (native)`);
2166
+ }
1381
2167
  (0, workspace_context_1.setupWorkspaceContext)(workspace_root_1.workspaceRoot);
1382
2168
  // Initialize analytics for daemon process
1383
2169
  await (0, analytics_1.startAnalytics)();
@@ -1437,29 +2223,56 @@ index b1c143e244efd435b28642ed86e999d6c86f7d94..c68bcacb9bc46babaa86ea0ac212e79d
1437
2223
  return new Promise(async (resolve, reject) => {
1438
2224
  // `listen` reports a failed bind asynchronously on the server, which the
1439
2225
  // try below cannot catch — without this an EACCES became an uncaught
1440
- @@ -432,8 +404,7 @@ async function startServer() {
2226
+ @@ -432,25 +449,19 @@ async function startServer() {
1441
2227
  // Best effort; the 0700 socket directory is the primary control.
1442
2228
  }
1443
2229
  }
1444
2230
  - // this triggers the storage of the lock file hash
1445
2231
  - daemonIsOutdated();
2232
+ - if (!(0, shutdown_utils_1.getOutputWatcherInstance)()) {
2233
+ - (0, shutdown_utils_1.storeOutputWatcherInstance)(await (0, watcher_1.watchOutputFiles)(server, handle_outputs_changes_1.handleOutputsChanges));
2234
+ - }
1446
2235
  + (0, socket_utils_1.recordOwnSocket)(socketPath);
1447
- if (!(0, shutdown_utils_1.getOutputWatcherInstance)()) {
1448
- (0, shutdown_utils_1.storeOutputWatcherInstance)(await (0, watcher_1.watchOutputFiles)(server, handle_outputs_changes_1.handleOutputsChanges));
1449
- }
2236
+ + // The outputs are watched by the one watcher started
2237
+ + // above (watcher.js).
2238
+ // listen for project graph recomputation events to collect and schedule sync generators
2239
+ (0, project_graph_incremental_recomputation_1.registerProjectGraphRecomputationListener)(sync_generators_1.collectAndScheduleSyncGenerators);
2240
+ // register file change listener to invalidate sync generator cache
2241
+ (0, file_change_events_1.registerFileChangeListener)(sync_generators_1.clearSyncGeneratorsCache);
2242
+ // trigger an initial project graph recomputation
2243
+ (0, project_graph_incremental_recomputation_1.scheduleProjectGraphRecomputation)([], [], []);
2244
+ - // Kick off Nx Console check in background to prime the cache
2245
+ - (0, handle_nx_console_1.handleGetNxConsoleStatus)().catch(() => {
2246
+ - // Ignore errors, this is a background operation
2247
+ - });
2248
+ - // Kick off AI agents outdated check in background to prime the cache
2249
+ - (0, handle_configure_ai_agents_1.handleGetConfigureAiAgentsStatus)().catch(() => {
2250
+ - // Ignore errors, this is a background operation
2251
+ - });
2252
+ + // No Nx Console or AI-agent configuration check at start: both run each editor's CLI
2253
+ + // (`code --version`, `code --list-extensions`, ...), which outlives a daemon stopped meanwhile in
2254
+ + // the daemon's process group. A client on a terminal, which can show what they find, asks for
2255
+ + // them (init-local.js, run-command.js), and the checks run then.
2256
+ return resolve(server);
2257
+ }
2258
+ catch (err) {
1450
2259
  diff --git a/dist/src/daemon/server/shutdown-utils.d.ts b/dist/src/daemon/server/shutdown-utils.d.ts
1451
- index 698fcf9e061b8e0b37057a6318614dbe6d290b9e..5470c4935d0a5f230dde3ece13bfb6ba9a58e10b 100644
2260
+ index 698fcf9e061b8e0b37057a6318614dbe6d290b9e..832d848841cb8a0f761750e24ecfb84a85fc6ff9 100644
1452
2261
  --- a/dist/src/daemon/server/shutdown-utils.d.ts
1453
2262
  +++ b/dist/src/daemon/server/shutdown-utils.d.ts
1454
- @@ -1,6 +1,6 @@
2263
+ @@ -1,10 +1,8 @@
1455
2264
  import type { Server, Socket } from 'net';
1456
2265
  import type { Watcher } from '../../native';
1457
2266
  -export declare const SERVER_INACTIVITY_TIMEOUT_MS: 10800000;
1458
2267
  +export declare const SERVER_INACTIVITY_TIMEOUT_MS: 300000;
1459
2268
  export declare function storeWatcherInstance(instance: Watcher): void;
1460
2269
  export declare function getWatcherInstance(): Watcher;
1461
- export declare function storeOutputWatcherInstance(instance: Watcher): void;
1462
- @@ -12,6 +12,8 @@ interface HandleServerProcessTerminationParams {
2270
+ -export declare function storeOutputWatcherInstance(instance: Watcher): void;
2271
+ -export declare function getOutputWatcherInstance(): Watcher;
2272
+ interface HandleServerProcessTerminationParams {
2273
+ server: Server;
2274
+ reason: string;
2275
+ @@ -12,6 +10,8 @@ interface HandleServerProcessTerminationParams {
1463
2276
  }
1464
2277
  export declare function handleServerProcessTermination({ server, reason, sockets, }: HandleServerProcessTerminationParams): Promise<void>;
1465
2278
  export declare function handleServerProcessTerminationWithRestart({ server, reason, sockets, }: HandleServerProcessTerminationParams): Promise<void>;
@@ -1469,10 +2282,18 @@ index 698fcf9e061b8e0b37057a6318614dbe6d290b9e..5470c4935d0a5f230dde3ece13bfb6ba
1469
2282
  export declare function respondToClient(socket: Socket, response: Buffer, description: string): Promise<unknown>;
1470
2283
  /**
1471
2284
  diff --git a/dist/src/daemon/server/shutdown-utils.js b/dist/src/daemon/server/shutdown-utils.js
1472
- index a449f2fb3c864bc9d3d710078418c8a05879aa84..17d9f2a7569a1332105b8e1106a2b8fdfb8a3e96 100644
2285
+ index a449f2fb3c864bc9d3d710078418c8a05879aa84..83e23bb1aec2f5f3af3e886d8df3b24be678bf68 100644
1473
2286
  --- a/dist/src/daemon/server/shutdown-utils.js
1474
2287
  +++ b/dist/src/daemon/server/shutdown-utils.js
1475
- @@ -11,6 +11,7 @@ exports.resetInactivityTimeout = resetInactivityTimeout;
2288
+ @@ -3,14 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
2289
+ exports.SERVER_INACTIVITY_TIMEOUT_MS = void 0;
2290
+ exports.storeWatcherInstance = storeWatcherInstance;
2291
+ exports.getWatcherInstance = getWatcherInstance;
2292
+ -exports.storeOutputWatcherInstance = storeOutputWatcherInstance;
2293
+ -exports.getOutputWatcherInstance = getOutputWatcherInstance;
2294
+ exports.handleServerProcessTermination = handleServerProcessTermination;
2295
+ exports.handleServerProcessTerminationWithRestart = handleServerProcessTerminationWithRestart;
2296
+ exports.resetInactivityTimeout = resetInactivityTimeout;
1476
2297
  exports.respondToClient = respondToClient;
1477
2298
  exports.respondWithError = respondWithError;
1478
2299
  exports.respondWithErrorAndExit = respondWithErrorAndExit;
@@ -1480,7 +2301,7 @@ index a449f2fb3c864bc9d3d710078418c8a05879aa84..17d9f2a7569a1332105b8e1106a2b8fd
1480
2301
  const workspace_root_1 = require("../../utils/workspace-root");
1481
2302
  const logger_1 = require("../logger");
1482
2303
  const socket_utils_1 = require("../socket-utils");
1483
- @@ -23,16 +24,20 @@ const analytics_1 = require("../../analytics");
2304
+ @@ -23,16 +22,20 @@ const analytics_1 = require("../../analytics");
1484
2305
  const child_process_1 = require("child_process");
1485
2306
  const path_1 = require("path");
1486
2307
  const node_fs_1 = require("node:fs");
@@ -1505,7 +2326,7 @@ index a449f2fb3c864bc9d3d710078418c8a05879aa84..17d9f2a7569a1332105b8e1106a2b8fd
1505
2326
  // Use require.resolve to find the currently installed version's start.js
1506
2327
  // instead of using __dirname which points to the old running daemon's path
1507
2328
  // Resolve from workspace root to pick up the correct symlink target
1508
- @@ -50,14 +55,21 @@ async function startNewDaemonInBackground() {
2329
+ @@ -50,14 +53,21 @@ async function startNewDaemonInBackground() {
1509
2330
  logger_1.serverLogger.log(`Restarting daemon with script: ${startScriptPath}`);
1510
2331
  logger_1.serverLogger.log(`Old daemon __dirname: ${__dirname}`);
1511
2332
  logger_1.serverLogger.log(`Current process.execPath: ${process.execPath}`);
@@ -1535,19 +2356,63 @@ index a449f2fb3c864bc9d3d710078418c8a05879aa84..17d9f2a7569a1332105b8e1106a2b8fd
1535
2356
  backgroundProcess.unref();
1536
2357
  logger_1.serverLogger.log('Started new daemon process in background');
1537
2358
  }
1538
- @@ -85,15 +97,31 @@ async function handleServerProcessTerminationWithRestart({ server, reason, socke
1539
- await startNewDaemonInBackground();
1540
- await performShutdown(server, reason, sockets);
2359
+ @@ -68,40 +78,68 @@ function storeWatcherInstance(instance) {
2360
+ function getWatcherInstance() {
2361
+ return watcherInstance;
1541
2362
  }
1542
- -async function performShutdown(server, reason, sockets) {
2363
+ -let outputWatcherInstance;
2364
+ -function storeOutputWatcherInstance(instance) {
2365
+ - outputWatcherInstance = instance;
2366
+ -}
2367
+ -function getOutputWatcherInstance() {
2368
+ - return outputWatcherInstance;
2369
+ +async function handleServerProcessTermination({ server, reason, sockets, notice, }) {
2370
+ + await performShutdown(server, reason, sockets, notice);
2371
+ }
2372
+ -async function handleServerProcessTermination({ server, reason, sockets, }) {
2373
+ - await performShutdown(server, reason, sockets);
2374
+ -}
2375
+ -async function handleServerProcessTerminationWithRestart({ server, reason, sockets, }) {
2376
+ +async function handleServerProcessTerminationWithRestart({ server, reason, sockets, notice, }) {
2377
+ // Clean up old daemon cache before starting new instance
2378
+ (0, cache_1.deleteDaemonJsonProcessCache)();
2379
+ // Start new daemon before shutting down
2380
+ await startNewDaemonInBackground();
2381
+ - await performShutdown(server, reason, sockets);
2382
+ + await performShutdown(server, reason, sockets, notice);
2383
+ +}
1543
2384
  +/** The shutdown under way: each later reason to stop joins it. */
1544
2385
  +let shutdown;
1545
- +function performShutdown(server, reason, sockets) {
1546
- + shutdown ??= shutDown(server, reason, sockets);
2386
+ +function performShutdown(server, reason, sockets, notice) {
2387
+ + shutdown ??= shutDown(server, reason, sockets, notice);
1547
2388
  + return shutdown;
2389
+ }
2390
+ -async function performShutdown(server, reason, sockets) {
2391
+ +/**
2392
+ + * Answers every connection a command holds with `notice` before the daemon
2393
+ + * closes it, so the request waiting on it and every later one of that command
2394
+ + * fail with the reason. A file-watcher or project-graph-listener stream is left
2395
+ + * out: its client follows the successor and sees the change there. Both
2396
+ + * registries import server.js, which starts a daemon when imported anywhere
2397
+ + * else, so they are required here, in a stopping daemon.
2398
+ + */
2399
+ +async function noticeCommands(sockets, notice) {
2400
+ + const { registeredFileWatcherSockets } = require('./file-watching/file-watcher-sockets');
2401
+ + const { registeredProjectGraphListenerSockets } = require('./project-graph-listener-sockets');
2402
+ + const streams = new Set([
2403
+ + ...registeredFileWatcherSockets.map(({ socket }) => socket),
2404
+ + ...registeredProjectGraphListenerSockets,
2405
+ + ]);
2406
+ + const commands = [...sockets].filter((socket) => !streams.has(socket));
2407
+ + logger_1.serverLogger.log(`Telling ${commands.length} command connection(s): ${notice.message}`);
2408
+ + const response = Buffer.from((0, socket_utils_1.serializeResult)(notice, null, null), 'utf8');
2409
+ + await Promise.all(commands.map((socket) => respondToClient(socket, response, null)));
1548
2410
  +}
1549
- +async function shutDown(server, reason, sockets) {
2411
+ +async function shutDown(server, reason, sockets, notice) {
1550
2412
  try {
2413
+ + if (notice !== undefined) {
2414
+ + await noticeCommands(sockets, notice);
2415
+ + }
1551
2416
  + // Closing a listening Unix socket unlinks its path. Once another daemon
1552
2417
  + // has bound that path, the path is that daemon's socket, and closing this
1553
2418
  + // server would cut every client off from it. Such a server is left to end
@@ -1571,11 +2436,61 @@ index a449f2fb3c864bc9d3d710078418c8a05879aa84..17d9f2a7569a1332105b8e1106a2b8fd
1571
2436
  });
1572
2437
  if (watcherInstance) {
1573
2438
  await watcherInstance.stop();
2439
+ - logger_1.serverLogger.watcherLog(`Stopping the watcher for ${workspace_root_1.workspaceRoot} (sources)`);
2440
+ - }
2441
+ - if (outputWatcherInstance) {
2442
+ - await outputWatcherInstance.stop();
2443
+ - logger_1.serverLogger.watcherLog(`Stopping the watcher for ${workspace_root_1.workspaceRoot} (outputs)`);
2444
+ + logger_1.serverLogger.watcherLog(`Stopping the watcher for ${workspace_root_1.workspaceRoot}`);
2445
+ }
2446
+ (0, cache_1.deleteDaemonJsonProcessCache)();
2447
+ (0, get_plugins_1.cleanupPlugins)();
2448
+ diff --git a/dist/src/daemon/server/watcher.d.ts b/dist/src/daemon/server/watcher.d.ts
2449
+ index 05661e2b3b8e2e84e2f914479eda5d3f12446e83..fb3c1afb74c8fd40179f783971cd5aa94e621ace 100644
2450
+ --- a/dist/src/daemon/server/watcher.d.ts
2451
+ +++ b/dist/src/daemon/server/watcher.d.ts
2452
+ @@ -1,15 +1,24 @@
2453
+ import { Server } from 'net';
2454
+ import type { WatchEvent } from '../../native';
2455
+ export type FileWatcherCallback = (err: Error | string | null, changeEvents: WatchEvent[] | null) => Promise<void>;
2456
+ -export declare function watchWorkspace(server: Server, cb: FileWatcherCallback): Promise<any>;
2457
+ /**
2458
+ - * Synchronously drain anything the workspace watcher has buffered and feed
2459
+ - * it through the normal change-handling pipeline. Call this before serving
2460
+ - * a cached project graph so we never return data that the watcher has
2461
+ - * already seen invalidated but hasn't flushed yet.
2462
+ + * Starts the daemon's one file watcher. `cb` receives the changes to the
2463
+ + * workspace's files (its ignore files applied), `outputsCb` every change the
2464
+ + * watcher reports, gitignored outputs included.
2465
+ + */
2466
+ +export declare function watchWorkspace(server: Server, cb: FileWatcherCallback, outputsCb: FileWatcherCallback): Promise<any>;
2467
+ +/**
2468
+ + * Resolves `undefined` once everything written before the call has reached
2469
+ + * the daemon's change handling; on macOS through a probe written and seen to
2470
+ + * come back through the stream. Resolves the reason when the stream has
2471
+ + * stopped delivering: the caller tells its client and stops the daemon.
2472
+ + */
2473
+ +export declare function proveWatcherCurrent(): Promise<string | undefined>;
2474
+ +/**
2475
+ + * Synchronously drain anything the watcher has buffered and feed it through
2476
+ + * the normal change handling, before serving a cached project graph.
2477
+ */
2478
+ export declare function flushPendingWorkspaceChanges(): Promise<void>;
2479
+ -export declare function watchOutputFiles(server: Server, cb: FileWatcherCallback): Promise<any>;
2480
+ /**
2481
+ * NOTE: An event type of "create" will also apply to the case where the user has restored
2482
+ * an original version of a file after modifying/deleting it by using git, so we adjust
1574
2483
  diff --git a/dist/src/daemon/server/watcher.js b/dist/src/daemon/server/watcher.js
1575
- index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..11d56729bcd092605dd9ed64728232022e942384 100644
2484
+ index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..860117066547fd32188c35bea16bd16a9ee75445 100644
1576
2485
  --- a/dist/src/daemon/server/watcher.js
1577
2486
  +++ b/dist/src/daemon/server/watcher.js
1578
- @@ -6,20 +6,102 @@ exports.watchOutputFiles = watchOutputFiles;
2487
+ @@ -1,25 +1,116 @@
2488
+ "use strict";
2489
+ Object.defineProperty(exports, "__esModule", { value: true });
2490
+ exports.watchWorkspace = watchWorkspace;
2491
+ +exports.proveWatcherCurrent = proveWatcherCurrent;
2492
+ exports.flushPendingWorkspaceChanges = flushPendingWorkspaceChanges;
2493
+ -exports.watchOutputFiles = watchOutputFiles;
1579
2494
  exports.convertChangeEventsToLogMessage = convertChangeEventsToLogMessage;
1580
2495
  const workspace_root_1 = require("../../utils/workspace-root");
1581
2496
  const path_1 = require("path");
@@ -1585,23 +2500,33 @@ index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..11d56729bcd092605dd9ed6472823202
1585
2500
  -const cache_1 = require("../cache");
1586
2501
  const server_1 = require("./server");
1587
2502
  const handle_import_1 = require("../../utils/handle-import");
2503
+ -// Captured by watchWorkspace so flushPendingWorkspaceChanges can route
2504
+ -// force-flushed events through the same handling as the async callback.
1588
2505
  +const workspace_context_1 = require("../../utils/workspace-context");
1589
2506
  +const cache_directory_1 = require("../../utils/cache-directory");
1590
2507
  +const restart_checks_1 = require("./restart-checks");
1591
- // Captured by watchWorkspace so flushPendingWorkspaceChanges can route
1592
- // force-flushed events through the same handling as the async callback.
2508
+ +const ignore_1 = require("../../utils/ignore");
2509
+ +const fileutils_1 = require("../../utils/fileutils");
2510
+ +const path_2 = require("../../utils/path");
2511
+ +const logger_1 = require("../logger");
2512
+ +const ignore = require("ignore");
2513
+ +const local_plugin_source_1 = require("../../project-graph/plugins/local-plugin-source");
2514
+ +// Captured by watchWorkspace so proveWatcherCurrent can route force-flushed
2515
+ +// events through the same handling as the async callback.
1593
2516
  // Definite-assignment: dispatchWorkspaceChanges only runs after
1594
2517
  -// watchWorkspace has set both, so reading them as non-nullable is safe.
1595
2518
  +// watchWorkspace has set all four, so reading them as non-nullable is safe.
1596
2519
  let activeServer;
1597
2520
  let workspaceChangesCallback;
1598
2521
  -function dispatchWorkspaceChanges(events) {
2522
+ - for (const event of events) {
2523
+ - if (event.path.endsWith('.gitignore') || event.path === '.nxignore') {
1599
2524
  +let hashFile;
1600
2525
  +// The filesystem's own time, in nanoseconds, just before the watcher read
1601
2526
  +// the ignore files its filter is built from.
1602
2527
  +let filterReadFrom;
1603
2528
  +function isIgnoreFile(path) {
1604
- + return path.endsWith('.gitignore') || path === '.nxignore';
2529
+ + return (0, path_1.basename)(path) === '.gitignore' || path === '.nxignore';
1605
2530
  +}
1606
2531
  +/** The later of a path's ctime and mtime, in the filesystem's nanoseconds. */
1607
2532
  +function changedAt(path) {
@@ -1670,11 +2595,13 @@ index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..11d56729bcd092605dd9ed6472823202
1670
2595
  + return ignoreFilesAtStart;
1671
2596
  +}
1672
2597
  +// Every dispatch awaits the snapshot, so batches still reach the callback in
1673
- +// the order the watcher delivered them.
1674
- +async function dispatchWorkspaceChanges(events) {
2598
+ +// the order the watcher delivered them. `ignoreFileEvents` are the changed
2599
+ +// ignore files in directories the walker reads, whether or not the files
2600
+ +// themselves are workspace files: the workspace filter reads them all the
2601
+ +// same.
2602
+ +async function dispatchWorkspaceChanges(events, ignoreFileEvents = events) {
1675
2603
  + const filterRead = await ignoreFiles();
1676
- for (const event of events) {
1677
- - if (event.path.endsWith('.gitignore') || event.path === '.nxignore') {
2604
+ + for (const event of ignoreFileEvents) {
1678
2605
  + // hashFile answers null for a file that is gone, which no read hash is.
1679
2606
  + if (isIgnoreFile(event.path) &&
1680
2607
  + (!filterRead.has(event.path) ||
@@ -1683,40 +2610,292 @@ index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..11d56729bcd092605dd9ed6472823202
1683
2610
  // If the ignore files themselves have changed we need to dynamically
1684
2611
  // update our cached ignoreGlobs
1685
2612
  (0, shutdown_utils_1.handleServerProcessTermination)({
1686
- @@ -32,16 +114,20 @@ function dispatchWorkspaceChanges(events) {
1687
- return workspaceChangesCallback(null, events);
2613
+ @@ -29,59 +120,338 @@ function dispatchWorkspaceChanges(events) {
2614
+ });
2615
+ }
2616
+ }
2617
+ - return workspaceChangesCallback(null, events);
2618
+ + return events.length === 0 ? undefined : workspaceChangesCallback(null, events);
2619
+ +}
2620
+ +let outputsChangesCallback;
2621
+ +// One native watcher (one FSEventStream on macOS, one inotify set on Linux)
2622
+ +// carries every change the daemon acts on: the workspace's files, the
2623
+ +// gitignored outputs, the daemon record and the probes below. FSEvents orders
2624
+ +// events within one stream only, so a probe that came back through it proves
2625
+ +// every earlier write came back first; two streams prove nothing about each
2626
+ +// other.
2627
+ +//
2628
+ +// The native filter keeps Nx's hard-coded ignores only (node_modules, .git,
2629
+ +// the caches). The ignore files Nx's native workspace watcher read decide
2630
+ +// which events reach the workspace context, in its order
2631
+ +// (native/watch/watch_filterer.rs): the root .nxignore first, whose ignore or
2632
+ +// re-include decides; then the .gitignore files from the event's directory
2633
+ +// up to the workspace root, then those between a nested workspace and its
2634
+ +// git root, the nearest opinion deciding. A path under a directory those
2635
+ +// rules exclude is excluded, as the walker never enters that directory. A
2636
+ +// nested .nxignore, which only the walker reads, is not read here, as Nx's
2637
+ +// watcher never read it: nothing cached here can go stale behind a change
2638
+ +// the watcher cannot report. A change to any file read here stops the daemon
2639
+ +// (dispatchWorkspaceChanges), so the rules stay the ones the workspace
2640
+ +// context was built with.
2641
+ +let rootNxIgnore;
2642
+ +let parentIgnores;
2643
+ +const gitignores = new Map();
2644
+ +const excludedDirectories = new Map();
2645
+ +/**
2646
+ + * The .gitignore files between a workspace nested in a git repository and
2647
+ + * that repository's root, nearest first, each with the prefix that makes a
2648
+ + * workspace-relative path relative to its own directory.
2649
+ + */
2650
+ +function parentGitignores() {
2651
+ + const root = workspace_root_1.workspaceRoot;
2652
+ + const found = [];
2653
+ + if ((0, fs_1.existsSync)((0, path_1.join)(root, '.git'))) {
2654
+ + return found;
2655
+ + }
2656
+ + for (let directory = (0, path_1.dirname)(root);; directory = (0, path_1.dirname)(directory)) {
2657
+ + const content = (0, fileutils_1.readFileIfExisting)((0, path_1.join)(directory, '.gitignore'));
2658
+ + if (content) {
2659
+ + found.push({
2660
+ + prefix: `${(0, path_2.normalizePath)((0, path_1.relative)(directory, root))}/`,
2661
+ + matcher: ignore().add(content),
2662
+ + });
2663
+ + }
2664
+ + if ((0, fs_1.existsSync)((0, path_1.join)(directory, '.git'))) {
2665
+ + return found;
2666
+ + }
2667
+ + if ((0, path_1.dirname)(directory) === directory) {
2668
+ + // Outside any git repository the native filter reads no parent file.
2669
+ + return [];
2670
+ + }
2671
+ + }
2672
+ +}
2673
+ +function gitignoreIn(directory) {
2674
+ + let matcher = gitignores.get(directory);
2675
+ + if (matcher === undefined) {
2676
+ + const content = (0, fileutils_1.readFileIfExisting)((0, path_1.join)(workspace_root_1.workspaceRoot, directory, '.gitignore'));
2677
+ + matcher = content ? ignore().add(content) : null;
2678
+ + gitignores.set(directory, matcher);
2679
+ + }
2680
+ + return matcher;
2681
+ +}
2682
+ +/** `true` ignored, `false` re-included, `undefined` no opinion. */
2683
+ +function opinion(matcher, path) {
2684
+ + const { ignored, unignored } = matcher.test(path);
2685
+ + return ignored ? true : unignored ? false : undefined;
2686
+ +}
2687
+ +/** Whether the rules exclude `path` itself (a directory when `isDirectory`). */
2688
+ +function excludedByRules(path, isDirectory) {
2689
+ + const probe = isDirectory ? `${path}/` : path;
2690
+ + rootNxIgnore ??= ignore().add((0, fileutils_1.readFileIfExisting)((0, path_1.join)(workspace_root_1.workspaceRoot, '.nxignore')));
2691
+ + const nx = opinion(rootNxIgnore, probe);
2692
+ + if (nx !== undefined) {
2693
+ + return nx;
2694
+ + }
2695
+ + for (let directory = (0, ignore_1.posixDirname)(path);; directory = (0, ignore_1.posixDirname)(directory)) {
2696
+ + const matcher = gitignoreIn(directory);
2697
+ + if (matcher) {
2698
+ + const git = opinion(matcher, directory === '' ? probe : probe.slice(directory.length + 1));
2699
+ + if (git !== undefined) {
2700
+ + return git;
2701
+ + }
2702
+ + }
2703
+ + if (directory === '') {
2704
+ + break;
2705
+ + }
2706
+ + }
2707
+ + parentIgnores ??= parentGitignores();
2708
+ + for (const { prefix, matcher } of parentIgnores) {
2709
+ + const git = opinion(matcher, prefix + probe);
2710
+ + if (git !== undefined) {
2711
+ + return git;
2712
+ + }
2713
+ + }
2714
+ + return false;
2715
+ +}
2716
+ +function excludedDirectory(directory) {
2717
+ + if (directory === '') {
2718
+ + return false;
2719
+ + }
2720
+ + let excluded = excludedDirectories.get(directory);
2721
+ + if (excluded === undefined) {
2722
+ + excluded =
2723
+ + (0, ignore_1.isAlwaysIgnored)(`${directory}/`) ||
2724
+ + excludedDirectory((0, ignore_1.posixDirname)(directory)) ||
2725
+ + excludedByRules(directory, true);
2726
+ + excludedDirectories.set(directory, excluded);
2727
+ + }
2728
+ + return excluded;
2729
+ +}
2730
+ +function isWorkspaceFile(path) {
2731
+ + return (!(0, ignore_1.isAlwaysIgnored)(path) &&
2732
+ + !excludedDirectory((0, ignore_1.posixDirname)(path)) &&
2733
+ + !excludedByRules(path, false));
2734
+ +}
2735
+ +/** Whether the walker reads the directory holding `path`, and so its ignore files. */
2736
+ +function inWalkedDirectory(path) {
2737
+ + return !excludedDirectory((0, ignore_1.posixDirname)(path));
2738
+ +}
2739
+ +// macOS only: a probe is an empty file the daemon writes and waits to see come
2740
+ +// back through its stream. It lives where no walker and no `git status` look,
2741
+ +// in a directory physically inside the watched tree (a cowshed shed links its
2742
+ +// workspace data onto another volume, whose events no stream rooted at the
2743
+ +// workspace reports) whose probe path the root .nxignore does not ignore: the
2744
+ +// native watcher applies that file to every event, whatever else it admits,
2745
+ +// and a workspace that ignores every dot path (`.*`) hides `.nx` and `.git`.
2746
+ +// The first of the workspace data directory, `.git` and `node_modules` that
2747
+ +// qualifies holds them. With none, nothing can prove the stream current, so
2748
+ +// the daemon serves no file-state request: the first one is told why and
2749
+ +// continues without it, and the daemon stops, as for a stopped stream. On
2750
+ +// Linux the daemon only drains its watcher: inotify has no daemon between
2751
+ +// the kernel and the watcher that can stop serving it, and Nx registers no
2752
+ +// inotify watch under those directories.
2753
+ +const PROBE_NAME = 'nx-watch-probe-';
2754
+ +let probeDirectory;
2755
+ +let probeSequence = 0;
2756
+ +const probesAwaited = new Map();
2757
+ +function probeDirectoryInsideWorkspace() {
2758
+ + const root = (0, fs_1.realpathSync)(workspace_root_1.workspaceRoot);
2759
+ + const nxIgnore = ignore().add((0, fileutils_1.readFileIfExisting)((0, path_1.join)(workspace_root_1.workspaceRoot, '.nxignore')));
2760
+ + const refused = [];
2761
+ + for (const candidate of [
2762
+ + cache_directory_1.workspaceDataDirectory,
2763
+ + (0, path_1.join)(workspace_root_1.workspaceRoot, '.git'),
2764
+ + (0, path_1.join)(workspace_root_1.workspaceRoot, 'node_modules'),
2765
+ + ]) {
2766
+ + let real;
2767
+ + try {
2768
+ + if (!(0, fs_1.statSync)(candidate).isDirectory()) {
2769
+ + refused.push(`${candidate} is no directory`);
2770
+ + continue;
2771
+ + }
2772
+ + real = (0, fs_1.realpathSync)(candidate);
2773
+ + }
2774
+ + catch (error) {
2775
+ + if (error.code === 'ENOENT') {
2776
+ + refused.push(`${candidate} does not exist`);
2777
+ + continue;
2778
+ + }
2779
+ + throw error;
2780
+ + }
2781
+ + if (!real.startsWith(root + path_1.sep)) {
2782
+ + refused.push(`${candidate} lies outside it, at ${real}`);
2783
+ + continue;
2784
+ + }
2785
+ + const directory = (0, path_2.normalizePath)((0, path_1.relative)(root, real));
2786
+ + if (nxIgnore.ignores(`${directory}/${PROBE_NAME}0`)) {
2787
+ + refused.push(`${candidate} is ignored by its .nxignore`);
2788
+ + continue;
2789
+ + }
2790
+ + return { directory };
2791
+ + }
2792
+ + return { refused: `the file watcher of ${root} cannot be proven current: no directory takes a probe it reports (${refused.join('; ')})` };
2793
+ +}
2794
+ +// Routes one batch the stream delivered, in the order it delivered them.
2795
+ +function routeEvents(events) {
2796
+ + const changes = [];
2797
+ + for (const event of events) {
2798
+ + const arrived = probesAwaited.get(event.path);
2799
+ + if (arrived) {
2800
+ + arrived();
2801
+ + }
2802
+ + else if (!(probeDirectory !== undefined && event.path.startsWith(`${probeDirectory}/${PROBE_NAME}`))) {
2803
+ + changes.push(event);
2804
+ + }
2805
+ + }
2806
+ + if (changes.length === 0 || (0, restart_checks_1.stopDaemonIfReplaced)(changes)) {
2807
+ + return Promise.resolve();
2808
+ + }
2809
+ + outputsChangesCallback(null, changes);
2810
+ + const workspaceChanges = [];
2811
+ + const ignoredFiles = [];
2812
+ + for (const event of changes) {
2813
+ + if (isWorkspaceFile(event.path)) workspaceChanges.push(event);
2814
+ + else ignoredFiles.push(event.path);
2815
+ + }
2816
+ + const ignoreFileChanges = changes.filter((event) => isIgnoreFile(event.path) && inWalkedDirectory(event.path));
2817
+ + if ((0, local_plugin_source_1.invalidateIgnoredLocalPluginSources)(workspace_root_1.workspaceRoot, ignoredFiles)) {
2818
+ + const recomputation = require('./project-graph-incremental-recomputation');
2819
+ + recomputation.invalidateGraphCache();
2820
+ + if (workspaceChanges.length === 0 && ignoreFileChanges.length === 0) {
2821
+ + recomputation.scheduleProjectGraphRecomputation([], [], []);
2822
+ + }
2823
+ + }
2824
+ + return workspaceChanges.length === 0 && ignoreFileChanges.length === 0
2825
+ + ? Promise.resolve()
2826
+ + : dispatchWorkspaceChanges(workspaceChanges, ignoreFileChanges);
1688
2827
  }
1689
- async function watchWorkspace(server, cb) {
2828
+ -async function watchWorkspace(server, cb) {
1690
2829
  - const { Watcher } = await (0, handle_import_1.handleImport)('../../native/index.js', __dirname);
2830
+ +async function watchWorkspace(server, cb, outputsCb) {
1691
2831
  + const native = await (0, handle_import_1.handleImport)('../../native/index.js', __dirname);
1692
2832
  + const { Watcher } = native;
1693
2833
  activeServer = server;
1694
2834
  workspaceChangesCallback = cb;
2835
+ - const watcher = new Watcher(workspace_root_1.workspaceRoot);
2836
+ + outputsChangesCallback = outputsCb;
1695
2837
  + hashFile = native.hashFile;
2838
+ + const admitted = [`!${restart_checks_1.relativeServerProcess}`];
2839
+ + if (process.platform === 'darwin') {
2840
+ + const location = probeDirectoryInsideWorkspace();
2841
+ + if (location.directory !== undefined) {
2842
+ + probeDirectory = location.directory;
2843
+ + admitted.push(`!${probeDirectory}/${PROBE_NAME}*`);
2844
+ + }
2845
+ + else {
2846
+ + streamStoppedReason = location.refused;
2847
+ + }
2848
+ + }
1696
2849
  + filterReadFrom = filesystemNow();
1697
- const watcher = new Watcher(workspace_root_1.workspaceRoot);
2850
+ + const watcher = new Watcher(workspace_root_1.workspaceRoot, admitted, false);
1698
2851
  watcher.watch((err, events) => {
1699
2852
  if (err) {
2853
+ + outputsCb(err, null);
1700
2854
  return cb(err, null);
1701
2855
  }
1702
2856
  - dispatchWorkspaceChanges(events);
1703
- + dispatchWorkspaceChanges(events).catch((error) => cb(error, null));
2857
+ + routeEvents(events).catch((error) => cb(error, null));
1704
2858
  });
1705
2859
  + setImmediate(() => ignoreFiles().catch((error) => cb(error, null)));
1706
2860
  return watcher;
1707
2861
  }
2862
+ +/** Drain whatever the watcher has buffered through the normal routing. */
2863
+ +async function drainWatcher(watcher) {
2864
+ + const events = watcher.forceFlushPending();
2865
+ + if (events.length > 0) {
2866
+ + await routeEvents(events);
2867
+ + }
2868
+ + // Batches the native side already handed to the JS callback run before
2869
+ + // the caller looks again.
2870
+ + await new Promise(setImmediate);
2871
+ +}
1708
2872
  /**
1709
- @@ -61,21 +147,13 @@ async function flushPendingWorkspaceChanges() {
1710
- }
1711
- async function watchOutputFiles(server, cb) {
1712
- const { Watcher } = await (0, handle_import_1.handleImport)('../../native/index.js', __dirname);
2873
+ - * Synchronously drain anything the workspace watcher has buffered and feed
2874
+ - * it through the normal change-handling pipeline. Call this before serving
2875
+ - * a cached project graph so we never return data that the watcher has
2876
+ - * already seen invalidated but hasn't flushed yet.
2877
+ + * Synchronously drain anything the watcher has buffered and feed it through
2878
+ + * the normal change handling. Called before serving a cached project graph so
2879
+ + * we never return data that the watcher has already seen invalidated but
2880
+ + * hasn't flushed yet.
2881
+ */
2882
+ async function flushPendingWorkspaceChanges() {
2883
+ const watcher = (0, shutdown_utils_1.getWatcherInstance)();
2884
+ - if (!watcher)
2885
+ - return;
2886
+ - const events = watcher.forceFlushPending();
2887
+ - if (events.length === 0)
2888
+ - return;
2889
+ - await dispatchWorkspaceChanges(events);
2890
+ -}
2891
+ -async function watchOutputFiles(server, cb) {
2892
+ - const { Watcher } = await (0, handle_import_1.handleImport)('../../native/index.js', __dirname);
1713
2893
  - const relativeServerProcess = (0, path_2.normalizePath)((0, path_1.relative)(workspace_root_1.workspaceRoot, cache_1.serverProcessJsonPath));
1714
2894
  - const watcher = new Watcher(workspace_root_1.workspaceRoot, [`!${relativeServerProcess}`], false);
1715
- + const watcher = new Watcher(workspace_root_1.workspaceRoot, [`!${restart_checks_1.relativeServerProcess}`], false);
1716
- watcher.watch((err, events) => {
1717
- if (err) {
1718
- return cb(err, null);
1719
- }
2895
+ - watcher.watch((err, events) => {
2896
+ - if (err) {
2897
+ - return cb(err, null);
2898
+ - }
1720
2899
  - for (const event of events) {
1721
2900
  - if (event.path == relativeServerProcess &&
1722
2901
  - (0, cache_1.getDaemonProcessIdSync)() !== process.pid) {
@@ -1725,12 +2904,88 @@ index cbc34a1a9c7650ddd50ee7309a0b487f1837e88a..11d56729bcd092605dd9ed6472823202
1725
2904
  - reason: 'this process is no longer the current daemon (native)',
1726
2905
  - sockets: server_1.openSockets,
1727
2906
  - });
1728
- - }
1729
- + if ((0, restart_checks_1.stopDaemonIfReplaced)(events)) {
1730
- + return;
2907
+ + if (watcher) {
2908
+ + await drainWatcher(watcher);
2909
+ + }
2910
+ +}
2911
+ +/**
2912
+ + * Resolves `undefined` once everything written before the call has reached
2913
+ + * the daemon's change handling, so an answer given after it cannot come from
2914
+ + * file state older than the call. On macOS it writes a probe and drains the
2915
+ + * stream until that probe comes back: the stream delivers in order, so every
2916
+ + * earlier write came back before it. Calls made while a probe is out share
2917
+ + * the next one.
2918
+ + *
2919
+ + * A stream that stopped delivering (fseventsd restarted or refused the
2920
+ + * stream) raises no error: Nx's native watcher drops FSEvents' rescan and
2921
+ + * dropped-event flags. Its probe never comes back. The wait has no timer of
2922
+ + * its own; DAEMON_VALIDITY_SAFETY_INTERVAL_MS only decides that the probe is
2923
+ + * lost, and this resolves the reason, "watcher stream stopped: …", for this
2924
+ + * call and every later one. The caller tells its client and stops the
2925
+ + * daemon (stopDaemonForStoppedWatcher); the next daemon starts with a fresh
2926
+ + * stream.
2927
+ + *
2928
+ + * On macOS without a directory that takes a probe it resolves, from the
2929
+ + * start, the reason nothing can prove the stream current. On Linux it drains
2930
+ + * what the watcher holds, as Nx did before serving a graph.
2931
+ + */
2932
+ +let probing;
2933
+ +let nextProbe;
2934
+ +let streamStoppedReason;
2935
+ +function proveWatcherCurrent() {
2936
+ + if (!probing) {
2937
+ + probing = probeOnce().finally(() => {
2938
+ + probing = undefined;
2939
+ + });
2940
+ + return probing;
2941
+ + }
2942
+ + nextProbe ??= probing.then(() => {
2943
+ + nextProbe = undefined;
2944
+ + return proveWatcherCurrent();
2945
+ + });
2946
+ + return nextProbe;
2947
+ +}
2948
+ +async function probeOnce() {
2949
+ + const watcher = (0, shutdown_utils_1.getWatcherInstance)();
2950
+ + if (!watcher || streamStoppedReason !== undefined) {
2951
+ + return streamStoppedReason;
2952
+ + }
2953
+ + if (probeDirectory === undefined) {
2954
+ + await drainWatcher(watcher);
2955
+ + return undefined;
2956
+ + }
2957
+ + const probe = `${probeDirectory}/${PROBE_NAME}${process.pid}-${++probeSequence}`;
2958
+ + const probePath = (0, path_1.join)(workspace_root_1.workspaceRoot, probe);
2959
+ + let arrived = false;
2960
+ + probesAwaited.set(probe, () => {
2961
+ + arrived = true;
2962
+ + });
2963
+ + const writtenAt = performance.now();
2964
+ + (0, fs_1.writeFileSync)(probePath, '');
2965
+ + try {
2966
+ + while (!arrived) {
2967
+ + await drainWatcher(watcher);
2968
+ + if (!arrived &&
2969
+ + performance.now() - writtenAt > restart_checks_1.DAEMON_VALIDITY_SAFETY_INTERVAL_MS) {
2970
+ + streamStoppedReason = `watcher stream stopped: probe ${probe} did not come back through the file watcher of ${workspace_root_1.workspaceRoot} within ${restart_checks_1.DAEMON_VALIDITY_SAFETY_INTERVAL_MS} ms, so changes since its last event are unknown to this daemon`;
2971
+ + return streamStoppedReason;
2972
+ }
1731
2973
  }
1732
- if (events.length !== 0) {
1733
- cb(null, events);
2974
+ - if (events.length !== 0) {
2975
+ - cb(null, events);
2976
+ - }
2977
+ - });
2978
+ - return watcher;
2979
+ + logger_1.serverLogger.watcherLog(`watcher current: probe came back in ${(performance.now() - writtenAt).toFixed(1)} ms`);
2980
+ + return undefined;
2981
+ + }
2982
+ + finally {
2983
+ + probesAwaited.delete(probe);
2984
+ + (0, fs_1.rmSync)(probePath, { force: true });
2985
+ + }
2986
+ }
2987
+ /**
2988
+ * NOTE: An event type of "create" will also apply to the case where the user has restored
1734
2989
  diff --git a/dist/src/daemon/socket-utils.d.ts b/dist/src/daemon/socket-utils.d.ts
1735
2990
  index 52b621b433beccb8e6590316cfd5baa7c7ec3b80..1c13e49fb4e8e37520ecd6e7a1c00ce251d201e4 100644
1736
2991
  --- a/dist/src/daemon/socket-utils.d.ts
@@ -2299,19 +3554,177 @@ index a880a6620cd7ac46e4bd189bc2034ce58825769a..ef3c99a115e7114ea78da379316a0478
2299
3554
  return cached !== notCached;
2300
3555
  })) {
2301
3556
  return true;
3557
+ diff --git a/dist/src/project-graph/plugins/get-plugins.d.ts b/dist/src/project-graph/plugins/get-plugins.d.ts
3558
+ index ed7ae5d82d4c6981375ad71c3a57573376d76ab8..20ab7e18125d96d683b9198c74eb40b62870889c 100644
3559
+ --- a/dist/src/project-graph/plugins/get-plugins.d.ts
3560
+ +++ b/dist/src/project-graph/plugins/get-plugins.d.ts
3561
+ @@ -1,5 +1,5 @@
3562
+ import { NxJsonConfiguration, PluginConfiguration } from '../../config/nx-json';
3563
+ -import type { LoadedNxPlugin } from './loaded-nx-plugin';
3564
+ +import type { LoadedNxPlugin, PluginLoadResult } from './loaded-nx-plugin';
3565
+ export interface SeparatedPlugins {
3566
+ specifiedPlugins: LoadedNxPlugin[];
3567
+ defaultPlugins: LoadedNxPlugin[];
3568
+ @@ -7,11 +7,11 @@ export interface SeparatedPlugins {
3569
+ /** Exported for tests: the fallback latch is process-scoped by design. */
3570
+ export declare function resetIsolationFallbackForTesting(): void;
3571
+ /**
3572
+ - * Loads a plugin in a worker, falling back to this process when the worker's
3573
+ - * socket was refused.
3574
+ + * Loads local source in restartable workers. Installed plugins follow the
3575
+ + * isolation setting and may fall back to this process on worker socket refusal.
3576
+ *
3577
+ - * Isolation is preferred: it is what keeps two plugins with conflicting
3578
+ - * TypeScript versions or module-level state apart. But a sandbox that has not
3579
+ + * For installed plugins, isolation keeps conflicting TypeScript versions and
3580
+ + * module-level state apart. But a sandbox that has not
3581
+ * been told about the Nx socket root refuses the worker's socket, and failing
3582
+ * the whole command over that is worse than running the plugins here. The
3583
+ * fallback is narrow on purpose. It needs a failure to start or reach the
3584
+ @@ -21,12 +21,13 @@ export declare function resetIsolationFallbackForTesting(): void;
3585
+ * threw is rethrown, because rerunning it in-process would bury its actual
3586
+ * error.
3587
+ */
3588
+ -export declare const loadingMethod: (plugin: PluginConfiguration, root: string, index?: number) => Promise<readonly [Promise<LoadedNxPlugin>, () => void]>;
3589
+ +export declare const loadingMethod: (plugin: PluginConfiguration, root: string, index?: number) => Promise<PluginLoadResult>;
3590
+ /**
3591
+ * Returns all plugins (specified + default) as a flat list.
3592
+ * Specified plugins come first, followed by default plugins.
3593
+ */
3594
+ export declare function getPlugins(nxJson: NxJsonConfiguration, root?: string): Promise<LoadedNxPlugin[]>;
3595
+ +export declare function getPluginsConfigurationHash(nxJson: NxJsonConfiguration, root?: string): string;
3596
+ /**
3597
+ * Returns specified plugins (from nx.json) and default plugins (project.json,
3598
+ * package.json, etc.) as separate arrays. This separation is needed for
2302
3599
  diff --git a/dist/src/project-graph/plugins/get-plugins.js b/dist/src/project-graph/plugins/get-plugins.js
2303
- index df153050063fa81b915d3004d3a87cd397f73b19..ddc9b044fd4b74e7d25d95f6f636c1b721a301eb 100644
3600
+ index df153050063fa81b915d3004d3a87cd397f73b19..29c41c972bf12fc73423f5a66e6f5af6a1ecae02 100644
2304
3601
  --- a/dist/src/project-graph/plugins/get-plugins.js
2305
3602
  +++ b/dist/src/project-graph/plugins/get-plugins.js
2306
- @@ -22,6 +22,7 @@ const is_sandbox_1 = require("../../utils/is-sandbox");
3603
+ @@ -4,6 +4,7 @@ exports.loadingMethod = void 0;
3604
+ exports.resetIsolationFallbackForTesting = resetIsolationFallbackForTesting;
3605
+ exports.getPlugins = getPlugins;
3606
+ exports.getPluginsSeparated = getPluginsSeparated;
3607
+ +exports.getPluginsConfigurationHash = getPluginsConfigurationHash;
3608
+ exports.getOnlyDefaultPlugins = getOnlyDefaultPlugins;
3609
+ exports.getPluginsIfLoadedOrLoading = getPluginsIfLoadedOrLoading;
3610
+ exports.cleanupPlugins = cleanupPlugins;
3611
+ @@ -22,10 +23,14 @@ const is_sandbox_1 = require("../../utils/is-sandbox");
2307
3612
  const native_1 = require("../../native");
2308
3613
  const output_1 = require("../../utils/output");
2309
3614
  const transpiler_1 = require("./transpiler");
2310
3615
  +const error_types_1 = require("../error-types");
3616
+ +const local_plugin_source_1 = require("./local-plugin-source");
3617
+ +const installation_directory_1 = require("../../utils/installation-directory");
2311
3618
  /**
2312
3619
  * Stuff for specified NX Plugins.
2313
3620
  */
2314
- @@ -296,7 +297,7 @@ async function loadDefaultNxPlugins(root = workspace_root_1.workspaceRoot) {
3621
+ let currentPluginsConfigurationHash;
3622
+ +let specifiedPluginsConfigurationHash;
3623
+ let loadedPlugins;
3624
+ let cachedSeparatedPlugins;
3625
+ let pendingPluginsPromise;
3626
+ @@ -56,11 +61,11 @@ function resetIsolationFallbackForTesting() {
3627
+ isolationRefusedInThisProcess = false;
3628
+ }
3629
+ /**
3630
+ - * Loads a plugin in a worker, falling back to this process when the worker's
3631
+ - * socket was refused.
3632
+ + * Loads local source in restartable workers. Installed plugins follow the
3633
+ + * isolation setting and may fall back to this process on worker socket refusal.
3634
+ *
3635
+ - * Isolation is preferred: it is what keeps two plugins with conflicting
3636
+ - * TypeScript versions or module-level state apart. But a sandbox that has not
3637
+ + * For installed plugins, isolation keeps conflicting TypeScript versions and
3638
+ + * module-level state apart. But a sandbox that has not
3639
+ * been told about the Nx socket root refuses the worker's socket, and failing
3640
+ * the whole command over that is worse than running the plugins here. The
3641
+ * fallback is narrow on purpose. It needs a failure to start or reach the
3642
+ @@ -71,16 +76,31 @@ function resetIsolationFallbackForTesting() {
3643
+ * error.
3644
+ */
3645
+ const loadingMethod = async (plugin, root, index) => {
3646
+ - if (!(0, enabled_1.isIsolationEnabled)() || isolationRefusedInThisProcess) {
3647
+ + const moduleName = typeof plugin === 'string' ? plugin : plugin.plugin;
3648
+ + let resolved;
3649
+ + try {
3650
+ + resolved = await (0, resolve_plugin_1.resolveNxPlugin)(moduleName, root, (0, installation_directory_1.getNxRequirePaths)(root));
3651
+ + }
3652
+ + catch (error) {
3653
+ + throw new error_types_1.LoadPluginError(moduleName, error);
3654
+ + }
3655
+ + const local = (0, local_plugin_source_1.localPluginPath)(root, resolved.pluginPath) !== null;
3656
+ + // ESM modules cannot be evicted in-process. Local source therefore needs a
3657
+ + // restartable worker even when installed plugins run in this process.
3658
+ + if (!local && (!(0, enabled_1.isIsolationEnabled)() || isolationRefusedInThisProcess)) {
3659
+ return (0, in_process_loader_1.loadNxPlugin)(plugin, root, index);
3660
+ }
3661
+ - const [isolatedPlugin, cleanup] = await (0, isolation_1.loadIsolatedNxPlugin)(plugin, root, index);
3662
+ + const [isolatedPlugin, cleanup, created] = await (0, isolation_1.loadIsolatedNxPlugin)(plugin, root, index, resolved);
3663
+ // Awaited here rather than handed on, because the worker failure surfaces on
3664
+ // this promise and the fallback has to happen before the caller sees it.
3665
+ try {
3666
+ - return [Promise.resolve(await isolatedPlugin), cleanup];
3667
+ + return [Promise.resolve(await isolatedPlugin), cleanup, created];
3668
+ }
3669
+ catch (e) {
3670
+ + if (local) {
3671
+ + if (created) cleanup();
3672
+ + throw e;
3673
+ + }
3674
+ // Proof, kept separate from policy. The errno the worker saw is what makes
3675
+ // the message certain; whether that errno is also grounds for degrading is a
3676
+ // different question, and conflating them made the warning assert a sandbox
3677
+ @@ -92,7 +112,7 @@ const loadingMethod = async (plugin, root, index) => {
3678
+ !((provenRefusal && (0, native_1.isAiAgent)()) || (0, is_sandbox_1.isSandbox)())) {
3679
+ throw e;
3680
+ }
3681
+ - cleanup();
3682
+ + if (created) cleanup();
3683
+ // Read and set in one synchronous step. Concurrently loaded plugins each
3684
+ // arrive here with their own failure, so testing the latch after setting it
3685
+ // is what keeps the advice to one copy.
3686
+ @@ -120,6 +140,9 @@ const loadingMethod = async (plugin, root, index) => {
3687
+ }
3688
+ };
3689
+ exports.loadingMethod = loadingMethod;
3690
+ +function getPluginsConfigurationHash(nxJson, root = workspace_root_1.workspaceRoot) {
3691
+ + return (0, file_hasher_1.hashObject)({ root, plugins: nxJson.plugins ?? [], localSources: (0, local_plugin_source_1.getLocalPluginGeneration)() });
3692
+ +}
3693
+ /**
3694
+ * Returns all plugins (specified + default) as a flat list.
3695
+ * Specified plugins come first, followed by default plugins.
3696
+ @@ -140,8 +163,9 @@ async function getPlugins(nxJson, root = workspace_root_1.workspaceRoot) {
3697
+ */
3698
+ async function getPluginsSeparated(nxJson, root = workspace_root_1.workspaceRoot) {
3699
+ const pluginsConfiguration = nxJson.plugins ?? [];
3700
+ - const pluginsConfigurationHash = (0, file_hasher_1.hashObject)(pluginsConfiguration);
3701
+ - // If the plugins configuration has not changed, reuse the current plugins
3702
+ + const configurationHash = (0, file_hasher_1.hashObject)({ root, pluginsConfiguration });
3703
+ + const pluginsConfigurationHash = getPluginsConfigurationHash(nxJson, root);
3704
+ + // Configuration and loaded local source must both match the cached set.
3705
+ if (cachedSeparatedPlugins &&
3706
+ pluginsConfigurationHash === currentPluginsConfigurationHash) {
3707
+ return cachedSeparatedPlugins;
3708
+ @@ -152,12 +176,12 @@ async function getPluginsSeparated(nxJson, root = workspace_root_1.workspaceRoot
3709
+ if (pendingSeparatedPlugins?.hash === pluginsConfigurationHash) {
3710
+ return pendingSeparatedPlugins.promise;
3711
+ }
3712
+ - // Plugins config changed (e.g. `nx add @nx/maven` updated nx.json). The
3713
+ - // cached SeparatedPlugins is invalidated by the early-return above, but
3714
+ - // pendingPluginsPromise — the in-flight load — would otherwise be reused
3715
+ - // by the `??=` below and serve the previous plugin set forever. Tear
3716
+ - // down the old workers and force a fresh load.
3717
+ - cleanupSpecifiedPlugins?.();
3718
+ + // A local source change evicts only its worker. A configuration change
3719
+ + // tears down the previous set; unchanged workers otherwise stay resident.
3720
+ + if (specifiedPluginsConfigurationHash !== configurationHash) {
3721
+ + cleanupSpecifiedPlugins?.();
3722
+ + specifiedPluginsConfigurationHash = configurationHash;
3723
+ + }
3724
+ pendingPluginsPromise = undefined;
3725
+ const loadPromise = (async () => {
3726
+ const results = await Promise.allSettled([
3727
+ @@ -296,7 +320,7 @@ async function loadDefaultNxPlugins(root = workspace_root_1.workspaceRoot) {
2315
3728
  fn();
2316
3729
  }
2317
3730
  const errorMessage = errors
@@ -2320,7 +3733,39 @@ index df153050063fa81b915d3004d3a87cd397f73b19..ddc9b044fd4b74e7d25d95f6f636c1b7
2320
3733
  .join('\n');
2321
3734
  throw new AggregateError(errors.map((e) => e.error), `Failed to load ${errors.length} default Nx plugin(s):\n${errorMessage}`);
2322
3735
  }
2323
- @@ -361,7 +362,7 @@ async function loadSpecifiedNxPlugins(pluginsConfigurations, root = workspace_ro
3736
+ @@ -316,11 +340,6 @@ async function loadDefaultNxPlugins(root = workspace_root_1.workspaceRoot) {
3737
+ return ret;
3738
+ }
3739
+ async function loadSpecifiedNxPlugins(pluginsConfigurations, root = workspace_root_1.workspaceRoot) {
3740
+ - // Returning existing plugins is handled by getPlugins,
3741
+ - // so, if we are here and there are existing plugins, they are stale
3742
+ - if (cleanupSpecifiedPlugins) {
3743
+ - cleanupSpecifiedPlugins();
3744
+ - }
3745
+ performance.mark('loadSpecifiedNxPlugins:start');
3746
+ pluginsConfigurations ??= [];
3747
+ // Drop the cached workspace-layout snapshot local-plugin resolution uses:
3748
+ @@ -328,11 +347,13 @@ async function loadSpecifiedNxPlugins(pluginsConfigurations, root = workspace_ro
3749
+ // resolve it to the workspace root. Runs only when the plugin set changed.
3750
+ (0, resolve_plugin_1.resetResolvePluginCache)();
3751
+ const cleanupFunctions = [];
3752
+ + const createdCleanupFunctions = [];
3753
+ const results = await Promise.allSettled(pluginsConfigurations.map(async (plugin, index) => {
3754
+ const pluginPath = typeof plugin === 'string' ? plugin : plugin.plugin;
3755
+ performance.mark(`Load Nx Plugin: ${pluginPath} - start`);
3756
+ - const [loadedPluginPromise, cleanup] = await (0, exports.loadingMethod)(plugin, root, index);
3757
+ + const [loadedPluginPromise, cleanup, created] = await (0, exports.loadingMethod)(plugin, root, index);
3758
+ cleanupFunctions.push(cleanup);
3759
+ + if (created) createdCleanupFunctions.push(cleanup);
3760
+ const res = await loadedPluginPromise;
3761
+ performance.mark(`Load Nx Plugin: ${pluginPath} - end`);
3762
+ performance.measure(`Load Nx Plugin: ${pluginPath}`, `Load Nx Plugin: ${pluginPath} - start`, `Load Nx Plugin: ${pluginPath} - end`);
3763
+ @@ -357,11 +378,11 @@ async function loadSpecifiedNxPlugins(pluginsConfigurations, root = workspace_ro
3764
+ }
3765
+ }
3766
+ if (errors.length > 0) {
3767
+ - for (const fn of cleanupFunctions) {
3768
+ + for (const fn of createdCleanupFunctions) {
2324
3769
  fn();
2325
3770
  }
2326
3771
  const errorMessage = errors
@@ -2329,7 +3774,7 @@ index df153050063fa81b915d3004d3a87cd397f73b19..ddc9b044fd4b74e7d25d95f6f636c1b7
2329
3774
  .join('\n');
2330
3775
  throw new AggregateError(errors.map((e) => e.error), `Failed to load ${errors.length} Nx plugin(s):\n${errorMessage}`);
2331
3776
  }
2332
- @@ -385,6 +386,12 @@ function reasonToError(reason) {
3777
+ @@ -385,6 +406,12 @@ function reasonToError(reason) {
2333
3778
  if ('stack' in reason) {
2334
3779
  error.stack = String(reason.stack);
2335
3780
  }
@@ -2342,8 +3787,83 @@ index df153050063fa81b915d3004d3a87cd397f73b19..ddc9b044fd4b74e7d25d95f6f636c1b7
2342
3787
  return error;
2343
3788
  }
2344
3789
  return new Error(String(reason));
3790
+ diff --git a/dist/src/project-graph/plugins/in-process-loader.d.ts b/dist/src/project-graph/plugins/in-process-loader.d.ts
3791
+ index b0824c89a9e318a26d00b18b2ef71acbed4541ef..2237a7ed8b05b60b999f4862760633010ac0c532 100644
3792
+ --- a/dist/src/project-graph/plugins/in-process-loader.d.ts
3793
+ +++ b/dist/src/project-graph/plugins/in-process-loader.d.ts
3794
+ @@ -1,10 +1,10 @@
3795
+ import { ProjectConfiguration } from '../../config/workspace-json-project-json';
3796
+ import { PackageJson } from '../../utils/package-json';
3797
+ import type { PluginConfiguration } from '../../config/nx-json';
3798
+ -import type { LoadedNxPlugin } from './loaded-nx-plugin';
3799
+ +import type { LoadedNxPlugin, PluginLoadResult } from './loaded-nx-plugin';
3800
+ export declare function readPluginPackageJson(pluginName: string, projects: Record<string, ProjectConfiguration>, paths?: string[]): {
3801
+ path: string;
3802
+ json: PackageJson;
3803
+ };
3804
+ -export declare function loadNxPlugin(plugin: PluginConfiguration, root: string, index?: number): readonly [Promise<LoadedNxPlugin>, () => void];
3805
+ +export declare function loadNxPlugin(plugin: PluginConfiguration, root: string, index?: number): PluginLoadResult;
3806
+ export declare function loadNxPluginAsync(pluginConfiguration: PluginConfiguration, paths: string[], root: string, index?: number): Promise<LoadedNxPlugin>;
3807
+ diff --git a/dist/src/project-graph/plugins/in-process-loader.js b/dist/src/project-graph/plugins/in-process-loader.js
3808
+ index 2b80ddcfe7de5140519b427ac0b88c13798b6c8d..cd12b3771c8830261fc2c6ab17e840b0141d7844 100644
3809
+ --- a/dist/src/project-graph/plugins/in-process-loader.js
3810
+ +++ b/dist/src/project-graph/plugins/in-process-loader.js
3811
+ @@ -1,5 +1,4 @@
3812
+ "use strict";
3813
+ -// This file contains methods and utilities that should **only** be used by the plugin worker.
3814
+ Object.defineProperty(exports, "__esModule", { value: true });
3815
+ exports.readPluginPackageJson = readPluginPackageJson;
3816
+ exports.loadNxPlugin = loadNxPlugin;
3817
+ @@ -12,6 +11,7 @@ const path = require("node:path/posix");
3818
+ const resolve_plugin_1 = require("./resolve-plugin");
3819
+ const transpiler_1 = require("./transpiler");
3820
+ const handle_import_1 = require("../../utils/handle-import");
3821
+ +const NOOP = () => {};
3822
+ function readPluginPackageJson(pluginName, projects, paths = (0, installation_directory_1.getNxRequirePaths)()) {
3823
+ try {
3824
+ const result = (0, package_json_1.readModulePackageJsonWithoutFallbacks)(pluginName, paths);
3825
+ @@ -40,7 +40,8 @@ function readPluginPackageJson(pluginName, projects, paths = (0, installation_di
3826
+ function loadNxPlugin(plugin, root, index) {
3827
+ return [
3828
+ loadNxPluginAsync(plugin, (0, installation_directory_1.getNxRequirePaths)(root), root, index),
3829
+ - () => { },
3830
+ + NOOP,
3831
+ + false,
3832
+ ];
3833
+ }
3834
+ async function loadNxPluginAsync(pluginConfiguration, paths, root, index) {
3835
+ diff --git a/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts b/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts
3836
+ index 4e0a466144f0bbb35105041f5271e8d8de3ef2d6..de78990d19fd2d3209494e0228e09fb1fd44ec63 100644
3837
+ --- a/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts
3838
+ +++ b/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts
3839
+ @@ -7,6 +7,8 @@ import { LoadedNxPlugin } from '../loaded-nx-plugin';
3840
+ import type { CreateDependenciesContext, CreateMetadataContext, CreateNodesContext, CreateNodesResult, PostTasksExecutionContext, PreTasksExecutionContext, ProjectsMetadata } from '../public-api';
3841
+ import type { PluginWorkerLoadResult } from './messaging';
3842
+ import { Hook, Phase } from './plugin-lifecycle-manager';
3843
+ +import type { resolveNxPlugin } from '../resolve-plugin';
3844
+ +import type { LocalPluginSource } from '../local-plugin-source';
3845
+ export type LoadResultPayload = Extract<PluginWorkerLoadResult['payload'], {
3846
+ success: true;
3847
+ }>;
3848
+ @@ -39,7 +41,7 @@ export declare class IsolatedPlugin implements LoadedNxPlugin {
3849
+ /**
3850
+ * Creates and loads an isolated plugin worker.
3851
+ */
3852
+ - static load(plugin: PluginConfiguration, root: string, index?: number): Promise<IsolatedPlugin>;
3853
+ + static load(plugin: PluginConfiguration, root: string, index?: number, resolved?: Awaited<ReturnType<typeof resolveNxPlugin>>, localSource?: LocalPluginSource): Promise<IsolatedPlugin>;
3854
+ private constructor();
3855
+ private spawnAndConnect;
3856
+ /**
3857
+ @@ -67,6 +69,7 @@ export declare class IsolatedPlugin implements LoadedNxPlugin {
3858
+ setWorkerEnv(env: Record<string, string>): Promise<void>;
3859
+ notifyPhaseAborted(phase: Phase, lastCompletedHook: Hook): void;
3860
+ shutdown(): void;
3861
+ + retire(): void;
3862
+ private registerProcessMetrics;
3863
+ }
3864
+ export declare function getPluginWorkerSocketId(): string;
2345
3865
  diff --git a/dist/src/project-graph/plugins/isolation/isolated-plugin.js b/dist/src/project-graph/plugins/isolation/isolated-plugin.js
2346
- index 07648586a78fad6c986f537e042e94e65b0dcc61..d7079a756fda379a7c6898de2b08ff917bf214bb 100644
3866
+ index 07648586a78fad6c986f537e042e94e65b0dcc61..08ff3ea458ec16ab873c539079f0ad714267a7e4 100644
2347
3867
  --- a/dist/src/project-graph/plugins/isolation/isolated-plugin.js
2348
3868
  +++ b/dist/src/project-graph/plugins/isolation/isolated-plugin.js
2349
3869
  @@ -11,6 +11,7 @@ const crypto_1 = require("crypto");
@@ -2354,7 +3874,81 @@ index 07648586a78fad6c986f537e042e94e65b0dcc61..d7079a756fda379a7c6898de2b08ff91
2354
3874
  const sandbox_socket_hint_1 = require("../../../daemon/sandbox-socket-hint");
2355
3875
  const socket_refused_exit_code_1 = require("../../../utils/socket-refused-exit-code");
2356
3876
  const socket_utils_1 = require("../../../daemon/socket-utils");
2357
- @@ -210,7 +211,7 @@ class IsolatedPlugin {
3877
+ @@ -23,6 +24,7 @@ const progress_topics_1 = require("../../../utils/progress-topics");
3878
+ const wait_for_socket_connection_1 = require("../../../utils/wait-for-socket-connection");
3879
+ const workspace_root_1 = require("../../../utils/workspace-root");
3880
+ const resolve_plugin_1 = require("../resolve-plugin");
3881
+ +const local_plugin_source_1 = require("../local-plugin-source");
3882
+ const messaging_1 = require("./messaging");
3883
+ const plugin_lifecycle_manager_1 = require("./plugin-lifecycle-manager");
3884
+ const PLUGIN_TIMEOUT_HINT_TEXT = 'As a last resort, you can set NX_PLUGIN_NO_TIMEOUTS=true to bypass this timeout.';
3885
+ @@ -39,13 +41,20 @@ class IsolatedPlugin {
3886
+ /**
3887
+ * Creates and loads an isolated plugin worker.
3888
+ */
3889
+ - static async load(plugin, root, index) {
3890
+ + static async load(plugin, root, index, resolved, localSource) {
3891
+ const moduleName = typeof plugin === 'string' ? plugin : plugin.plugin;
3892
+ - const { name, pluginPath, shouldRegisterTSTranspiler } = await (0, resolve_plugin_1.resolveNxPlugin)(moduleName, root, (0, installation_directory_1.getNxRequirePaths)(root));
3893
+ + const { name, pluginPath, shouldRegisterTSTranspiler } = resolved ?? await (0, resolve_plugin_1.resolveNxPlugin)(moduleName, root, (0, installation_directory_1.getNxRequirePaths)(root));
3894
+ const instance = new IsolatedPlugin(plugin, root, name, pluginPath, shouldRegisterTSTranspiler, index);
3895
+ - const loadResult = await instance.spawnAndConnect();
3896
+ - instance.setupHooks(loadResult);
3897
+ - return instance;
3898
+ + instance.localSource = localSource;
3899
+ + try {
3900
+ + const loadResult = await instance.spawnAndConnect();
3901
+ + instance.setupHooks(loadResult);
3902
+ + return instance;
3903
+ + }
3904
+ + catch (error) {
3905
+ + instance.shutdown();
3906
+ + throw error;
3907
+ + }
3908
+ }
3909
+ constructor(plugin, root, name, pluginPath, shouldRegisterTSTranspiler, index) {
3910
+ this.index = index;
3911
+ @@ -80,6 +89,14 @@ class IsolatedPlugin {
3912
+ }
3913
+ const pending = this.responseHandlers.get(message.tx);
3914
+ if (pending) {
3915
+ + try {
3916
+ + (0, local_plugin_source_1.recordLocalPluginSources)(this.localSource, message.payload.localPluginSources);
3917
+ + }
3918
+ + catch (error) {
3919
+ + this.failPendingRequests(error);
3920
+ + this.retire();
3921
+ + return;
3922
+ + }
3923
+ this.responseHandlers.delete(message.tx);
3924
+ pending.onMessage(message);
3925
+ }
3926
+ @@ -91,7 +108,7 @@ class IsolatedPlugin {
3927
+ this.shouldRegisterTSTranspiler = shouldRegisterTSTranspiler;
3928
+ }
3929
+ async spawnAndConnect() {
3930
+ - const { worker, socket } = await startPluginWorker(this.name);
3931
+ + const { worker, socket } = await startPluginWorker(this.name, !!this.localSource);
3932
+ this.worker = worker;
3933
+ this.socket = socket;
3934
+ this.registerProcessMetrics();
3935
+ @@ -149,6 +166,7 @@ class IsolatedPlugin {
3936
+ * so that only one worker is ever spawned at a time.
3937
+ */
3938
+ async ensureAlive() {
3939
+ + if (this.disposed) throw new Error(`Local Nx plugin "${this.name}" was retired because its source changed.`);
3940
+ if (this._alive) {
3941
+ return;
3942
+ }
3943
+ @@ -199,6 +217,7 @@ class IsolatedPlugin {
3944
+ name: this.name,
3945
+ pluginPath: this.pluginPath,
3946
+ shouldRegisterTSTranspiler: this.shouldRegisterTSTranspiler,
3947
+ + trackLocalSources: !!this.localSource,
3948
+ },
3949
+ tx,
3950
+ });
3951
+ @@ -210,7 +229,7 @@ class IsolatedPlugin {
2358
3952
  this.include = loadResult.include;
2359
3953
  this.exclude = loadResult.exclude;
2360
3954
  const registeredHooks = hooks(loadResult.createNodesPattern && 'createNodes', loadResult.hasCreateDependencies && 'createDependencies', loadResult.hasCreateMetadata && 'createMetadata', loadResult.hasPreTasksExecution && 'preTasksExecution', loadResult.hasPostTasksExecution && 'postTasksExecution');
@@ -2363,6 +3957,179 @@ index 07648586a78fad6c986f537e042e94e65b0dcc61..d7079a756fda379a7c6898de2b08ff91
2363
3957
  const shutdown = (hookName) => this.shutdownIfInactive(hookName);
2364
3958
  const wrap = (hook, hookFn) => this.lifecycle.wrapHook(hook, async (...args) => {
2365
3959
  await this.ensureAlive();
3960
+ @@ -291,12 +310,14 @@ class IsolatedPlugin {
3961
+ const timeout = setTimeout(() => {
3962
+ this.responseHandlers.delete(tx);
3963
+ this.pendingCount--;
3964
+ + if (this.disposed && this.pendingCount === 0) this.shutdown();
3965
+ reject(new Error(`${this.name} timed out after ${MINUTES} minutes during ${type}. ${PLUGIN_TIMEOUT_HINT_TEXT}`));
3966
+ }, MAX_MESSAGE_WAIT);
3967
+ this.responseHandlers.set(tx, {
3968
+ onMessage: (msg) => {
3969
+ clearTimeout(timeout);
3970
+ this.pendingCount--;
3971
+ + if (this.disposed && this.pendingCount === 0) this.shutdown();
3972
+ if (msg.type !== expectedResultType) {
3973
+ reject(new Error(`Expected ${expectedResultType}, got ${msg.type}`));
3974
+ return;
3975
+ @@ -306,6 +327,7 @@ class IsolatedPlugin {
3976
+ onError: (error) => {
3977
+ clearTimeout(timeout);
3978
+ this.pendingCount--;
3979
+ + if (this.disposed && this.pendingCount === 0) this.shutdown();
3980
+ reject(error);
3981
+ },
3982
+ });
3983
+ @@ -338,8 +360,12 @@ class IsolatedPlugin {
3984
+ this.shutdownIfInactive(lastCompletedHook);
3985
+ }
3986
+ }
3987
+ + retire() {
3988
+ + this.disposed = true;
3989
+ + if (this.pendingCount === 0) this.shutdown();
3990
+ + }
3991
+ shutdown() {
3992
+ - if (!this._alive)
3993
+ + if (!this.worker && !this.socket)
3994
+ return;
3995
+ this._alive = false;
3996
+ this._connectPromise = null;
3997
+ @@ -385,7 +411,7 @@ function getPluginWorkerSocketId() {
3998
+ const counter = global.nxPluginWorkerCount++;
3999
+ return `${process.pid}-${counter.toString(36)}-${(0, crypto_1.randomBytes)(4).toString('hex')}`;
4000
+ }
4001
+ -async function startPluginWorker(name) {
4002
+ +async function startPluginWorker(name, local) {
4003
+ performance.mark(`start-plugin-worker:${name}`);
4004
+ const isWorkerTypescript = path.extname(__filename) === '.ts';
4005
+ const workerPath = path.join(__dirname, isWorkerTypescript ? 'plugin-worker.ts' : 'plugin-worker.js');
4006
+ @@ -405,7 +431,7 @@ async function startPluginWorker(name) {
4007
+ // keeps the socket path inside the 95-character budget `assertValidSocketPath`
4008
+ // enforces.
4009
+ const ipcPath = (0, socket_utils_1.getPluginOsSocketPath)(getPluginWorkerSocketId());
4010
+ - const worker = (0, child_process_1.spawn)(process.execPath, [
4011
+ + const worker = (0, child_process_1.spawn)(local ? 'node' : process.execPath, [
4012
+ // Spawn the worker with the same resolve conditions Nx uses for plugin
4013
+ // entries so the plugin's transitive workspace imports resolve to source.
4014
+ ...(0, typescript_1.getPluginResolveConditionNodeArgs)(),
4015
+ @@ -449,6 +475,12 @@ async function startPluginWorker(name) {
4016
+ async function connectToWorker(worker, ipcPath, name) {
4017
+ const abortController = new AbortController();
4018
+ let earlyExitError = null;
4019
+ + worker.once('error', (error) => {
4020
+ + if (!abortController.signal.aborted) {
4021
+ + earlyExitError = markWorkerStartupFailure(new Error(`Failed to start plugin worker for "${name}": ${error.message}`, { cause: error }));
4022
+ + abortController.abort();
4023
+ + }
4024
+ + });
4025
+ // If the worker exits before we connect, abort polling immediately
4026
+ // rather than burning through attempts against a dead socket.
4027
+ worker.once('exit', (code, signal) => {
4028
+ diff --git a/dist/src/project-graph/plugins/isolation/load-isolated-plugin.d.ts b/dist/src/project-graph/plugins/isolation/load-isolated-plugin.d.ts
4029
+ index 0856bb195da971ba2e9a83ded3b05aa6b9597d34..4679cc175a5a7078ea0b692356940307603935d3 100644
4030
+ --- a/dist/src/project-graph/plugins/isolation/load-isolated-plugin.d.ts
4031
+ +++ b/dist/src/project-graph/plugins/isolation/load-isolated-plugin.d.ts
4032
+ @@ -1,3 +1,4 @@
4033
+ import { PluginConfiguration } from '../../../config/nx-json';
4034
+ -import type { LoadedNxPlugin } from '../loaded-nx-plugin';
4035
+ -export declare function loadIsolatedNxPlugin(plugin: PluginConfiguration, root: string, index?: number): Promise<[Promise<LoadedNxPlugin>, () => void]>;
4036
+ +import type { PluginLoadResult } from '../loaded-nx-plugin';
4037
+ +import type { resolveNxPlugin } from '../resolve-plugin';
4038
+ +export declare function loadIsolatedNxPlugin(plugin: PluginConfiguration, root: string, index?: number, resolved?: Awaited<ReturnType<typeof resolveNxPlugin>>): Promise<PluginLoadResult>;
4039
+ diff --git a/dist/src/project-graph/plugins/isolation/load-isolated-plugin.js b/dist/src/project-graph/plugins/isolation/load-isolated-plugin.js
4040
+ index adb92a77fc7d0e0e930f00d5c496a8ae43f22bac..3ae7c22c59ad9d63af722864f024dbb6dfb4f023 100644
4041
+ --- a/dist/src/project-graph/plugins/isolation/load-isolated-plugin.js
4042
+ +++ b/dist/src/project-graph/plugins/isolation/load-isolated-plugin.js
4043
+ @@ -2,23 +2,33 @@
4044
+ Object.defineProperty(exports, "__esModule", { value: true });
4045
+ exports.loadIsolatedNxPlugin = loadIsolatedNxPlugin;
4046
+ const isolated_plugin_1 = require("./isolated-plugin");
4047
+ +const local_plugin_source_1 = require("../local-plugin-source");
4048
+ const isolatedPluginCache = (global['isolatedPluginCache'] ??= new Map());
4049
+ -async function loadIsolatedNxPlugin(plugin, root, index) {
4050
+ +async function loadIsolatedNxPlugin(plugin, root, index, resolved) {
4051
+ const cacheKey = JSON.stringify({ plugin, root });
4052
+ - if (isolatedPluginCache.has(cacheKey)) {
4053
+ - return [isolatedPluginCache.get(cacheKey), () => { }];
4054
+ + const cached = isolatedPluginCache.get(cacheKey);
4055
+ + if (cached) return [cached.promise, cached.cleanup, false];
4056
+ + let source;
4057
+ + const entry = {
4058
+ + retired: false,
4059
+ + cleanup() {
4060
+ + if (isolatedPluginCache.get(cacheKey) === entry) isolatedPluginCache.delete(cacheKey);
4061
+ + source?.dispose();
4062
+ + entry.retired = true;
4063
+ + entry.instance?.retire();
4064
+ + },
4065
+ + };
4066
+ + if (resolved && (0, local_plugin_source_1.localPluginPath)(root, resolved.pluginPath)) {
4067
+ + source = (0, local_plugin_source_1.createLocalPluginSource)(root, entry.cleanup);
4068
+ }
4069
+ - const pluginPromise = isolated_plugin_1.IsolatedPlugin.load(plugin, root, index).catch((err) => {
4070
+ - // Remove failed entries from cache so subsequent calls can retry
4071
+ - isolatedPluginCache.delete(cacheKey);
4072
+ - throw err;
4073
+ + entry.promise = isolated_plugin_1.IsolatedPlugin.load(plugin, root, index, resolved, source).then((instance) => {
4074
+ + entry.instance = instance;
4075
+ + if (entry.retired) instance.retire();
4076
+ + return instance;
4077
+ + }, (error) => {
4078
+ + entry.cleanup();
4079
+ + throw error;
4080
+ });
4081
+ - isolatedPluginCache.set(cacheKey, pluginPromise);
4082
+ - const cleanup = async () => {
4083
+ - const instancePromise = isolatedPluginCache.get(cacheKey);
4084
+ - isolatedPluginCache.delete(cacheKey);
4085
+ - const instance = await instancePromise;
4086
+ - instance?.shutdown();
4087
+ - };
4088
+ - return [pluginPromise, cleanup];
4089
+ + isolatedPluginCache.set(cacheKey, entry);
4090
+ + return [entry.promise, entry.cleanup, true];
4091
+ }
4092
+ diff --git a/dist/src/project-graph/plugins/isolation/messaging.d.ts b/dist/src/project-graph/plugins/isolation/messaging.d.ts
4093
+ index 08d65f5009598dd0ef6638cadd1d0c8225665624..869a09e33dee94c1e14208c9198425f2d975506d 100644
4094
+ --- a/dist/src/project-graph/plugins/isolation/messaging.d.ts
4095
+ +++ b/dist/src/project-graph/plugins/isolation/messaging.d.ts
4096
+ @@ -5,6 +5,10 @@ import type { ProjectGraph } from '../../../config/project-graph';
4097
+ import type { LoadedNxPlugin } from '../loaded-nx-plugin';
4098
+ import type { CreateDependenciesContext, CreateMetadataContext, CreateNodesContext, PostTasksExecutionContext, PreTasksExecutionContext } from '../public-api';
4099
+ import type { AllMessages, AllResults, DefineMessages, Handlers, ResultOf, WithResult } from './message-types';
4100
+ +import type { LocalPluginSourceReceipt } from '../local-plugin-source';
4101
+ +type LocalPluginSourceResult = {
4102
+ + payload: { localPluginSources?: LocalPluginSourceReceipt[] };
4103
+ +};
4104
+ /**
4105
+ * All plugin worker message definitions in one place.
4106
+ * Each entry defines:
4107
+ @@ -19,6 +23,7 @@ type PluginMessageDefs = DefineMessages<{
4108
+ name: string;
4109
+ pluginPath: string;
4110
+ shouldRegisterTSTranspiler: boolean;
4111
+ + trackLocalSources?: boolean;
4112
+ };
4113
+ result: {
4114
+ name: string;
4115
+ @@ -123,14 +128,14 @@ export type WorkspaceStampedMessage = {
4116
+ /** Union of all plugin worker message types */
4117
+ export type PluginWorkerMessage = AllMessages<PluginMessageDefs> & WorkspaceStampedMessage;
4118
+ /** Union of all plugin worker result types */
4119
+ -export type PluginWorkerResult = AllResults<PluginMessageDefs> & WorkspaceStampedMessage;
4120
+ +export type PluginWorkerResult = AllResults<PluginMessageDefs> & WorkspaceStampedMessage & LocalPluginSourceResult;
4121
+ /** Result type for the load message */
4122
+ -export type PluginWorkerLoadResult = ResultOf<PluginMessageDefs, 'load'>;
4123
+ +export type PluginWorkerLoadResult = ResultOf<PluginMessageDefs, 'load'> & LocalPluginSourceResult;
4124
+ /**
4125
+ * Maps a message type to its result type.
4126
+ * e.g., MessageResult<'createNodes'> gives the createNodes result type
4127
+ */
4128
+ -export type MessageResult<T extends PluginWorkerMessage['type']> = ResultOf<PluginMessageDefs, T & WithResult<PluginMessageDefs>>;
4129
+ +export type MessageResult<T extends PluginWorkerMessage['type']> = ResultOf<PluginMessageDefs, T & WithResult<PluginMessageDefs>> & LocalPluginSourceResult;
4130
+ export type PluginWorkerEmitLogNotification = {
4131
+ type: 'emitLog';
4132
+ level: 'log' | 'warn' | 'error';
2366
4133
  diff --git a/dist/src/project-graph/plugins/isolation/plugin-lifecycle-manager.d.ts b/dist/src/project-graph/plugins/isolation/plugin-lifecycle-manager.d.ts
2367
4134
  index 3a6df86646a11f371525fca978edf0fc318bf848..1e73ad21751964e3c6839e44eb43488677a9cec3 100644
2368
4135
  --- a/dist/src/project-graph/plugins/isolation/plugin-lifecycle-manager.d.ts
@@ -2437,6 +4204,276 @@ index 3659fdd1955d56e60cc0158d19ac55d59916d78e..92e6700adfc52a380b3e9e37b655fbc9
2437
4204
  }
2438
4205
  /**
2439
4206
  * Returns the current session count for a phase. Useful for testing.
4207
+ diff --git a/dist/src/project-graph/plugins/isolation/plugin-worker.js b/dist/src/project-graph/plugins/isolation/plugin-worker.js
4208
+ index f2d4d02f44a6e03d69b88d5ea5571fe4ddad6bc6..b8d745610eb53f84cebc1c29b2b34939498a679d 100644
4209
+ --- a/dist/src/project-graph/plugins/isolation/plugin-worker.js
4210
+ +++ b/dist/src/project-graph/plugins/isolation/plugin-worker.js
4211
+ @@ -26,6 +26,7 @@ node_perf_hooks_1.performance.measure(`plugin worker ${process.pid} code loading
4212
+ global.NX_GRAPH_CREATION = true;
4213
+ global.NX_PLUGIN_WORKER = true;
4214
+ let plugin;
4215
+ +let takeLocalPluginSources;
4216
+ const socketPath = process.argv[2];
4217
+ const expectedPluginName = process.argv[3];
4218
+ // The host's root, passed explicitly rather than re-resolved: a host that set
4219
+ @@ -74,10 +75,11 @@ const server = (0, net_1.createServer)((socket) => {
4220
+ return;
4221
+ }
4222
+ return (0, messaging_1.consumeMessage)(socket, message, {
4223
+ - load: async ({ plugin: pluginConfiguration, root, name, pluginPath, shouldRegisterTSTranspiler, }) => {
4224
+ + load: async ({ plugin: pluginConfiguration, root, name, pluginPath, shouldRegisterTSTranspiler, trackLocalSources, }) => {
4225
+ loadErrorTimeout?.clear();
4226
+ process.chdir(root);
4227
+ return withErrorHandling(async () => {
4228
+ + if (trackLocalSources) takeLocalPluginSources = require('../local-plugin-source').trackLocalPluginLoads(root);
4229
+ const { loadResolvedNxPluginAsync } = await Promise.resolve(require(require.resolve('../load-resolved-plugin')));
4230
+ // Register the ts-transpiler if we are pointing to a
4231
+ // plain ts file that's not part of a plugin project
4232
+ @@ -168,16 +170,18 @@ server.listen(socketPath, () => {
4233
+ logger_1.logger.verbose(`[plugin-worker] "${expectedPluginName}" (pid: ${process.pid}) listening on ${socketPath}`);
4234
+ });
4235
+ async function withErrorHandling(cb) {
4236
+ + let result;
4237
+ try {
4238
+ - const result = await cb();
4239
+ - return result ?? { success: true };
4240
+ + result = (await cb()) ?? { success: true };
4241
+ }
4242
+ catch (e) {
4243
+ - return {
4244
+ + result = {
4245
+ success: false,
4246
+ error: (0, serializable_error_1.createSerializableError)(e),
4247
+ };
4248
+ }
4249
+ + const localPluginSources = takeLocalPluginSources?.();
4250
+ + return localPluginSources?.length ? { ...result, localPluginSources } : result;
4251
+ }
4252
+ function setErrorTimeout(timeoutMs, errorMessage) {
4253
+ if (environment.NX_PLUGIN_NO_TIMEOUTS === 'true') {
4254
+ diff --git a/dist/src/project-graph/plugins/loaded-nx-plugin.d.ts b/dist/src/project-graph/plugins/loaded-nx-plugin.d.ts
4255
+ index f229abe26e7d23baaeff9949d648c8cf7906d50d..4ec12a7f8546f2fcd8d8d8bf06fd907ddbcfe730 100644
4256
+ --- a/dist/src/project-graph/plugins/loaded-nx-plugin.d.ts
4257
+ +++ b/dist/src/project-graph/plugins/loaded-nx-plugin.d.ts
4258
+ @@ -2,6 +2,7 @@ import type { ProjectGraph } from '../../config/project-graph';
4259
+ import { type PluginConfiguration } from '../../config/nx-json';
4260
+ import type { RawProjectGraphDependency } from '../project-graph-builder';
4261
+ import type { CreateDependenciesContext, CreateMetadataContext, CreateNodesContext, CreateNodesResult, NxPlugin, PostTasksExecutionContext, PreTasksExecutionContext, ProjectsMetadata } from './public-api';
4262
+ +export type PluginLoadResult = readonly [plugin: Promise<LoadedNxPlugin>, cleanup: () => void, created: boolean];
4263
+ /**
4264
+ * NOTE: Avoid using `import type` with this class. It causes issues with
4265
+ * jest's module resolution when running tests in projects that import
4266
+ diff --git a/dist/src/project-graph/plugins/local-plugin-source.d.ts b/dist/src/project-graph/plugins/local-plugin-source.d.ts
4267
+ new file mode 100644
4268
+ index 0000000000000000000000000000000000000000..c23bd14f2a28d660876423283d97fa833a057454
4269
+ --- /dev/null
4270
+ +++ b/dist/src/project-graph/plugins/local-plugin-source.d.ts
4271
+ @@ -0,0 +1,20 @@
4272
+ +export interface LocalPluginSourceReceipt {
4273
+ + readonly path: string;
4274
+ + readonly hash: string;
4275
+ +}
4276
+ +export interface LocalPluginSource {
4277
+ + readonly root: string;
4278
+ + readonly files: ReadonlyMap<string, string>;
4279
+ + readonly active: boolean;
4280
+ + invalidate(): void;
4281
+ + dispose(): void;
4282
+ +}
4283
+ +export declare function localPluginPath(root: string, file: string): string | null;
4284
+ +export declare function pluginsAffectedByChanges<Key>(closures: ReadonlyMap<Key, ReadonlySet<string> | ReadonlyMap<string, string>>, changedFiles: readonly string[]): Key[];
4285
+ +export declare function pluginsAffectedBySourceHashes<Key>(closures: ReadonlyMap<Key, ReadonlyMap<string, string>>, currentHashes: ReadonlyMap<string, string | null>): Key[];
4286
+ +export declare function createLocalPluginSource(root: string, onInvalidate: () => void): LocalPluginSource;
4287
+ +export declare function recordLocalPluginSources(source: LocalPluginSource | undefined, loaded: readonly LocalPluginSourceReceipt[] | undefined): void;
4288
+ +export declare function invalidateLocalPluginSources(root: string, changedFiles: readonly string[]): void;
4289
+ +export declare function invalidateIgnoredLocalPluginSources(root: string, changedFiles: readonly string[]): boolean;
4290
+ +export declare function getLocalPluginGeneration(): number;
4291
+ +export declare function trackLocalPluginLoads(root: string): () => LocalPluginSourceReceipt[];
4292
+ diff --git a/dist/src/project-graph/plugins/local-plugin-source.js b/dist/src/project-graph/plugins/local-plugin-source.js
4293
+ new file mode 100644
4294
+ index 0000000000000000000000000000000000000000..a6b40a520844b6e0ac46cc09c230cf805d586804
4295
+ --- /dev/null
4296
+ +++ b/dist/src/project-graph/plugins/local-plugin-source.js
4297
+ @@ -0,0 +1,158 @@
4298
+ +"use strict";
4299
+ +Object.defineProperty(exports, "__esModule", { value: true });
4300
+ +exports.localPluginPath = localPluginPath;
4301
+ +exports.pluginsAffectedByChanges = pluginsAffectedByChanges;
4302
+ +exports.createLocalPluginSource = createLocalPluginSource;
4303
+ +exports.recordLocalPluginSources = recordLocalPluginSources;
4304
+ +exports.invalidateLocalPluginSources = invalidateLocalPluginSources;
4305
+ +exports.getLocalPluginGeneration = () => generation;
4306
+ +exports.invalidateIgnoredLocalPluginSources = invalidateIgnoredLocalPluginSources;
4307
+ +exports.pluginsAffectedBySourceHashes = pluginsAffectedBySourceHashes;
4308
+ +exports.trackLocalPluginLoads = trackLocalPluginLoads;
4309
+ +const { createHash } = require("node:crypto");
4310
+ +const { readFileSync } = require("node:fs");
4311
+ +const { relative, resolve, sep, isAbsolute } = require("node:path");
4312
+ +const { fileURLToPath } = require("node:url");
4313
+ +const sourcesByRoot = new Map();
4314
+ +let generation = 0;
4315
+ +// A resolved package-manager link into workspace source is local; installed
4316
+ +// packages and links into another checkout retain their installation policy.
4317
+ +function localPluginPath(root, file) {
4318
+ + const path = relative(resolve(root), resolve(file));
4319
+ + return path && !isAbsolute(path) && path !== '..' && !path.startsWith('..' + sep) &&
4320
+ + !path.split(sep).includes('node_modules') ? path.split(sep).join('/') : null;
4321
+ +}
4322
+ +// Pure policy: watcher paths already exclude same-byte rewrites. No census,
4323
+ +// parsing, clock or filesystem operation belongs in this decision.
4324
+ +function pluginsAffectedByChanges(closures, changedFiles) {
4325
+ + const affected = [];
4326
+ + for (const [key, files] of closures) {
4327
+ + for (const file of changedFiles) {
4328
+ + if (files.has(file)) {
4329
+ + affected.push(key);
4330
+ + break;
4331
+ + }
4332
+ + }
4333
+ + }
4334
+ + return affected;
4335
+ +}
4336
+ +function createLocalPluginSource(root, onInvalidate) {
4337
+ + root = resolve(root);
4338
+ + const sources = sourcesByRoot.get(root) ?? new Map();
4339
+ + sourcesByRoot.set(root, sources);
4340
+ + const source = {
4341
+ + root,
4342
+ + files: new Map(),
4343
+ + active: true,
4344
+ + invalidate() {
4345
+ + if (!source.active) return;
4346
+ + source.dispose();
4347
+ + ++generation;
4348
+ + onInvalidate();
4349
+ + },
4350
+ + dispose() {
4351
+ + if (!source.active) return;
4352
+ + source.active = false;
4353
+ + sources.delete(source);
4354
+ + if (sources.size === 0) sourcesByRoot.delete(root);
4355
+ + },
4356
+ + };
4357
+ + sources.set(source, source.files);
4358
+ + return source;
4359
+ +}
4360
+ +function digest(bytes) {
4361
+ + return createHash('sha256').update(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).digest('hex');
4362
+ +}
4363
+ +function recordLocalPluginSources(source, loaded) {
4364
+ + if (!source?.active || !loaded) return;
4365
+ + for (const { path, hash } of loaded) {
4366
+ + // Publish membership before the check. Any subsequent watcher batch now
4367
+ + // sees this file; this first check closes the load-before-registration race.
4368
+ + source.files.set(path, hash);
4369
+ + let current;
4370
+ + try {
4371
+ + current = digest(readFileSync(resolve(source.root, path)));
4372
+ + }
4373
+ + catch (error) {
4374
+ + if (error.code !== 'ENOENT') {
4375
+ + source.invalidate();
4376
+ + throw error;
4377
+ + }
4378
+ + }
4379
+ + if (current !== hash) {
4380
+ + source.invalidate();
4381
+ + return;
4382
+ + }
4383
+ + }
4384
+ +}
4385
+ +function invalidateLocalPluginSources(root, changedFiles) {
4386
+ + const sources = sourcesByRoot.get(resolve(root));
4387
+ + if (!sources) return;
4388
+ + const affected = pluginsAffectedByChanges(sources, changedFiles);
4389
+ + for (const source of affected) source.invalidate();
4390
+ +}
4391
+ +// The native stream also sees gitignored modules, but they are not part of
4392
+ +// its workspace file index. Judge only those observed closure members by the
4393
+ +// loaded-byte receipts, never by a directory scan or a second repository hash.
4394
+ +function pluginsAffectedBySourceHashes(closures, currentHashes) {
4395
+ + const affected = [];
4396
+ + for (const [key, files] of closures) {
4397
+ + for (const [file, hash] of currentHashes) {
4398
+ + if (files.has(file) && files.get(file) !== hash) {
4399
+ + affected.push(key);
4400
+ + break;
4401
+ + }
4402
+ + }
4403
+ + }
4404
+ + return affected;
4405
+ +}
4406
+ +function invalidateIgnoredLocalPluginSources(root, changedFiles) {
4407
+ + const sources = sourcesByRoot.get(resolve(root));
4408
+ + if (!sources) return false;
4409
+ + const currentHashes = new Map();
4410
+ + for (const source of sources.keys()) {
4411
+ + for (const file of changedFiles) {
4412
+ + if (!source.files.has(file) || currentHashes.has(file)) continue;
4413
+ + let hash = null;
4414
+ + try {
4415
+ + hash = digest(readFileSync(resolve(source.root, file)));
4416
+ + }
4417
+ + catch (error) {
4418
+ + if (error.code !== 'ENOENT') {
4419
+ + require('../../daemon/logger').serverLogger.log(
4420
+ + `Cannot read loaded local Nx plugin source "${resolve(source.root, file)}"; invalidating its worker: ${error.message}`);
4421
+ + }
4422
+ + }
4423
+ + currentHashes.set(file, hash);
4424
+ + }
4425
+ + }
4426
+ + const affected = pluginsAffectedBySourceHashes(sources, currentHashes);
4427
+ + for (const source of affected) source.invalidate();
4428
+ + return affected.length > 0;
4429
+ +}
4430
+ +// The worker is the module-cache lifetime, including Node's non-evictable ESM
4431
+ +// cache. Observe actual loads (computed and lazy imports included), not guessed
4432
+ +// static imports or every file beside the entry. Each source receipt is sent once.
4433
+ +function trackLocalPluginLoads(root) {
4434
+ + const pending = [];
4435
+ + const seen = new Set();
4436
+ + const { registerHooks } = require('node:module');
4437
+ + if (typeof registerHooks !== 'function') {
4438
+ + throw new Error('Local Nx plugins require Node.js with node:module.registerHooks to reload their module closure.');
4439
+ + }
4440
+ + registerHooks({
4441
+ + load(url, context, nextLoad) {
4442
+ + const result = nextLoad(url, context);
4443
+ + if (url.startsWith('file:')) {
4444
+ + const file = fileURLToPath(url);
4445
+ + const path = localPluginPath(root, file);
4446
+ + if (path && !seen.has(path)) {
4447
+ + seen.add(path);
4448
+ + pending.push({ path, hash: digest(result.source ?? readFileSync(file)) });
4449
+ + }
4450
+ + }
4451
+ + return result;
4452
+ + },
4453
+ + });
4454
+ + return () => pending.splice(0);
4455
+ +}
4456
+ diff --git a/dist/src/project-graph/plugins/public-api.d.ts b/dist/src/project-graph/plugins/public-api.d.ts
4457
+ index 9014799cfb98f624d9d2b04891c3c9d876c64916..e29dd0e015e0e63ccdd27c2036b0395f2f2b99fa 100644
4458
+ --- a/dist/src/project-graph/plugins/public-api.d.ts
4459
+ +++ b/dist/src/project-graph/plugins/public-api.d.ts
4460
+ @@ -2,6 +2,7 @@ import type { FileMap, ProjectGraph, ProjectGraphExternalNode } from '../../conf
4461
+ import type { ProjectConfiguration } from '../../config/workspace-json-project-json';
4462
+ import type { NxJsonConfiguration } from '../../config/nx-json';
4463
+ import type { TaskResults } from '../../tasks-runner/life-cycle';
4464
+ +import type { TaskGraph } from '../../config/task-graph';
4465
+ import type { RawProjectGraphDependency } from '../project-graph-builder';
4466
+ export interface CreateNodesContext {
4467
+ readonly nxJsonConfiguration: NxJsonConfiguration;
4468
+ @@ -118,6 +119,8 @@ export type PreTasksExecutionContext = {
4469
+ readonly id: string;
4470
+ readonly workspaceRoot: string;
4471
+ readonly nxJsonConfiguration: NxJsonConfiguration;
4472
+ + readonly projectGraph: ProjectGraph;
4473
+ + readonly taskGraph: TaskGraph;
4474
+ readonly argv: string[];
4475
+ };
4476
+ export type PostTasksExecutionContext = {
2440
4477
  diff --git a/dist/src/project-graph/plugins/tasks-execution-hooks.js b/dist/src/project-graph/plugins/tasks-execution-hooks.js
2441
4478
  index 83c97768834e3f56083d5e5c2ff52806f55e95a3..eb2a4b563448079be79b58540290f41c53d0025d 100644
2442
4479
  --- a/dist/src/project-graph/plugins/tasks-execution-hooks.js
@@ -2509,10 +4546,18 @@ index 34aa44f81f1df63c5d33e58d8b55d15fcf1d7619..edf41406f465726828fa447bd10d71a4
2509
4546
  temporaryOutputPath(task: Task): string;
2510
4547
  private getRemoteCache;
2511
4548
  diff --git a/dist/src/tasks-runner/cache.js b/dist/src/tasks-runner/cache.js
2512
- index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..c7301119b99aa8d2a6dff507bee8f8652d2be0c4 100644
4549
+ index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..5f58606a9c57eb9b587cbe901201cfdb2b4c9105 100644
2513
4550
  --- a/dist/src/tasks-runner/cache.js
2514
4551
  +++ b/dist/src/tasks-runner/cache.js
2515
- @@ -149,7 +149,11 @@ class DbCache {
4552
+ @@ -26,6 +26,7 @@ const workspace_root_1 = require("../utils/workspace-root");
4553
+ const default_tasks_runner_1 = require("./default-tasks-runner");
4554
+ const task_io_service_1 = require("./task-io-service");
4555
+ const handle_import_1 = require("../utils/handle-import");
4556
+ +const remote_cache_outage_1 = require("./remote-cache-outage");
4557
+ // This function is called once during tasks runner initialization. It checks if the db cache is enabled and logs a warning if it is not.
4558
+ function dbCacheEnabled() {
4559
+ // ...but if on wasm and the the db cache isnt supported we shouldn't warn
4560
+ @@ -149,7 +150,11 @@ class DbCache {
2516
4561
  return this.cache.getCacheSize();
2517
4562
  }
2518
4563
  applyRemoteCacheResults(hash, res, outputs) {
@@ -2525,7 +4570,7 @@ index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..c7301119b99aa8d2a6dff507bee8f865
2525
4570
  }
2526
4571
  async put(task, terminalOutput, outputs, code) {
2527
4572
  return tryAndRetry(async () => {
2528
- @@ -162,7 +166,20 @@ class DbCache {
4573
+ @@ -162,7 +167,20 @@ class DbCache {
2529
4574
  });
2530
4575
  }
2531
4576
  copyFilesFromCache(_, cachedResult, outputs) {
@@ -2547,7 +4592,17 @@ index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..c7301119b99aa8d2a6dff507bee8f865
2547
4592
  }
2548
4593
  removeOldCacheRecords() {
2549
4594
  return this.cache.removeOldCacheRecords();
2550
- @@ -416,6 +433,7 @@ class Cache {
4595
+ @@ -257,7 +275,8 @@ class DbCache {
4596
+ if (process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0') {
4597
+ process.emitWarning("Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.");
4598
+ }
4599
+ - return new native_1.HttpRemoteCache();
4600
+ + const guard = new remote_cache_outage_1.RemoteCacheOutageGuard(new native_1.HttpRemoteCache(), process.env.NX_SELF_HOSTED_REMOTE_CACHE_SERVER);
4601
+ + return process.env.NX_SELF_HOSTED_REMOTE_CACHE_READ_ONLY === 'true' ? new remote_cache_outage_1.ReadOnlyRemoteCache(guard) : guard;
4602
+ }
4603
+ return null;
4604
+ }
4605
+ @@ -416,6 +435,7 @@ class Cache {
2551
4606
  await this.copy(cached, src);
2552
4607
  }
2553
4608
  }));
@@ -2555,7 +4610,7 @@ index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..c7301119b99aa8d2a6dff507bee8f865
2555
4610
  });
2556
4611
  }
2557
4612
  temporaryOutputPath(task) {
2558
- @@ -516,6 +534,35 @@ class Cache {
4613
+ @@ -516,6 +536,35 @@ class Cache {
2559
4614
  }
2560
4615
  }
2561
4616
  exports.Cache = Cache;
@@ -2591,7 +4646,7 @@ index 4a34ba245ca18dc4f4897fb45130e0d9dce94861..c7301119b99aa8d2a6dff507bee8f865
2591
4646
  function tryAndRetry(fn) {
2592
4647
  let attempts = 0;
2593
4648
  // Generate a random number between 2 and 4 to raise to the power of attempts
2594
- @@ -549,7 +596,29 @@ function resolveMaxCacheSize(nxJson) {
4649
+ @@ -549,7 +598,29 @@ function resolveMaxCacheSize(nxJson) {
2595
4650
  const rawMaxCacheSize = process.env.NX_MAX_CACHE_SIZE ?? nxJson.maxCacheSize;
2596
4651
  return rawMaxCacheSize !== undefined
2597
4652
  ? parseMaxCacheSize(rawMaxCacheSize)
@@ -3076,8 +5131,349 @@ index ab0d47a6d12a4ce8cb6b140364b1ada923fab95a..894bbfc883a7108a9fdd118691d6e2bc
3076
5131
  });
3077
5132
  console.log(JSON.stringify(timings, null, 2));
3078
5133
  }
5134
+ diff --git a/dist/src/tasks-runner/outputs-stamp.js b/dist/src/tasks-runner/outputs-stamp.js
5135
+ new file mode 100644
5136
+ index 0000000000000000000000000000000000000000..80e276f0681686cf4f47829bbdcfa7053c38051e
5137
+ --- /dev/null
5138
+ +++ b/dist/src/tasks-runner/outputs-stamp.js
5139
+ @@ -0,0 +1,71 @@
5140
+ +"use strict";
5141
+ +/**
5142
+ + * Outputs a cache hit can keep without a daemon. Nx keeps a hit's on-disk
5143
+ + * outputs only when its daemon recorded them; a client with none copied every
5144
+ + * cached output again. A cowshed job's client starts no daemon
5145
+ + * (NX_DAEMON_START=never), so each run restored, and re-dated, every output,
5146
+ + * including the tools other tasks' producers run, which no producer receipt
5147
+ + * could then vouch for.
5148
+ + *
5149
+ + * Each run or restore records, per task hash, the files its outputs hold:
5150
+ + * path, inode, size, mtime and ctime. A later hit keeps outputs that are still
5151
+ + * exactly those files. Any write since changes the ctime, which no tool can
5152
+ + * set back, a replaced file changes the inode, and an added or removed file
5153
+ + * changes the set; each restores. The record lives in the checkout's workspace
5154
+ + * data, beside the outputs it describes.
5155
+ + */
5156
+ +Object.defineProperty(exports, "__esModule", { value: true });
5157
+ +exports.recordOutputsStamps = recordOutputsStamps;
5158
+ +exports.outputsStampsMatch = outputsStampsMatch;
5159
+ +const node_fs_1 = require("node:fs");
5160
+ +const node_path_1 = require("node:path");
5161
+ +const native_1 = require("../native");
5162
+ +const cache_directory_1 = require("../utils/cache-directory");
5163
+ +const workspace_root_1 = require("../utils/workspace-root");
5164
+ +function stampPath(hash) {
5165
+ + return (0, node_path_1.join)(cache_directory_1.workspaceDataDirectory, 'outputs-stamps', `${hash}.json`);
5166
+ +}
5167
+ +/** The files of each entry's outputs, as the daemon's tracking expands them. */
5168
+ +function filesOf(entries) {
5169
+ + return (0, native_1.getFilesForOutputsBatch)(workspace_root_1.workspaceRoot, entries.map((entry) => entry.outputs));
5170
+ +}
5171
+ +/** What the files are now; throws when one is gone since the listing. */
5172
+ +function stampOf(entry, files) {
5173
+ + return JSON.stringify({
5174
+ + outputs: entry.outputs,
5175
+ + files: [...files].sort().map((file) => {
5176
+ + const stats = (0, node_fs_1.lstatSync)((0, node_path_1.join)(workspace_root_1.workspaceRoot, file), { bigint: true });
5177
+ + return [file, `${stats.ino}`, `${stats.size}`, `${stats.mtimeNs}`, `${stats.ctimeNs}`];
5178
+ + }),
5179
+ + });
5180
+ +}
5181
+ +/** Record what each entry's run or restore left. A record that cannot be written costs the next hit a copy. */
5182
+ +function recordOutputsStamps(entries) {
5183
+ + const files = filesOf(entries);
5184
+ + entries.forEach((entry, index) => {
5185
+ + const path = stampPath(entry.hash);
5186
+ + const written = `${path}.${process.pid}`;
5187
+ + try {
5188
+ + (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(path), { recursive: true });
5189
+ + (0, node_fs_1.writeFileSync)(written, stampOf(entry, files[index]));
5190
+ + (0, node_fs_1.renameSync)(written, path);
5191
+ + }
5192
+ + catch {
5193
+ + (0, node_fs_1.rmSync)(written, { force: true });
5194
+ + }
5195
+ + });
5196
+ +}
5197
+ +/** Whether each entry's outputs are still the files its record names; anything unreadable answers no. */
5198
+ +function outputsStampsMatch(entries) {
5199
+ + const files = filesOf(entries);
5200
+ + return entries.map((entry, index) => {
5201
+ + if (files[index].length === 0)
5202
+ + return false;
5203
+ + try {
5204
+ + return (0, node_fs_1.readFileSync)(stampPath(entry.hash), 'utf8') === stampOf(entry, files[index]);
5205
+ + }
5206
+ + catch {
5207
+ + return false;
5208
+ + }
5209
+ + });
5210
+ +}
5211
+ diff --git a/dist/src/tasks-runner/remote-cache-outage.d.ts b/dist/src/tasks-runner/remote-cache-outage.d.ts
5212
+ new file mode 100644
5213
+ index 0000000000000000000000000000000000000000..aecbe296f6bf80377fca38969c6efbf6d1b1a2b2
5214
+ --- /dev/null
5215
+ +++ b/dist/src/tasks-runner/remote-cache-outage.d.ts
5216
+ @@ -0,0 +1,46 @@
5217
+ +import type { CachedResult } from '../native';
5218
+ +import type { RemoteCacheV2 } from './default-tasks-runner';
5219
+ +export declare const REMOTE_CACHE_ANSWER_WITHIN_MS = 90000;
5220
+ +export type RemoteCacheSettlement<T> = {
5221
+ + kind: 'answered';
5222
+ + value: T;
5223
+ +} | {
5224
+ + kind: 'rejected';
5225
+ + error: unknown;
5226
+ +} | {
5227
+ + kind: 'unanswered';
5228
+ + withinMs: number;
5229
+ +};
5230
+ +export type RemoteCacheOutcome<T> = {
5231
+ + kind: 'answered';
5232
+ + value: T;
5233
+ +} | {
5234
+ + kind: 'outage';
5235
+ + cause: string;
5236
+ +} | {
5237
+ + kind: 'fatal';
5238
+ + error: unknown;
5239
+ +};
5240
+ +export declare function settleRemoteCacheRequest<T>(settlement: RemoteCacheSettlement<T>): RemoteCacheOutcome<T>;
5241
+ +export declare function describeRemoteCacheOutage(server: string, request: string, cause: string): string;
5242
+ +/** When a remote cache request stops waiting: `elapsed` settles at the deadline, `clear` releases it once the request settled first. */
5243
+ +export interface RemoteCacheDeadline {
5244
+ + readonly elapsed: Promise<void>;
5245
+ + clear(): void;
5246
+ +}
5247
+ +export declare class RemoteCacheOutageGuard implements RemoteCacheV2 {
5248
+ + private readonly remote;
5249
+ + private readonly server;
5250
+ + private readonly deadline;
5251
+ + private unavailable;
5252
+ + constructor(remote: RemoteCacheV2, server: string, deadline?: (withinMs: number) => RemoteCacheDeadline);
5253
+ + retrieve(hash: string, cacheDirectory: string): Promise<CachedResult | null>;
5254
+ + store(hash: string, cacheDirectory: string, terminalOutput: string, code: number): Promise<boolean>;
5255
+ + private request;
5256
+ +}
5257
+ +export declare class ReadOnlyRemoteCache implements RemoteCacheV2 {
5258
+ + private readonly remote;
5259
+ + constructor(remote: RemoteCacheV2);
5260
+ + retrieve(hash: string, cacheDirectory: string): Promise<CachedResult | null>;
5261
+ + store(hash: string, cacheDirectory: string, terminalOutput: string, code: number): Promise<boolean>;
5262
+ +}
5263
+ diff --git a/dist/src/tasks-runner/remote-cache-outage.js b/dist/src/tasks-runner/remote-cache-outage.js
5264
+ new file mode 100644
5265
+ index 0000000000000000000000000000000000000000..c8d00d0f94ed40dfc27b4ffaf001831125460cb0
5266
+ --- /dev/null
5267
+ +++ b/dist/src/tasks-runner/remote-cache-outage.js
5268
+ @@ -0,0 +1,206 @@
5269
+ +"use strict";
5270
+ +Object.defineProperty(exports, "__esModule", { value: true });
5271
+ +exports.ReadOnlyRemoteCache = exports.RemoteCacheOutageGuard = exports.REMOTE_CACHE_ANSWER_WITHIN_MS = void 0;
5272
+ +exports.settleRemoteCacheRequest = settleRemoteCacheRequest;
5273
+ +exports.describeRemoteCacheOutage = describeRemoteCacheOutage;
5274
+ +const node_fs_1 = require("node:fs");
5275
+ +const promises_1 = require("node:fs/promises");
5276
+ +const path_1 = require("node:path");
5277
+ +const logger_1 = require("../utils/logger");
5278
+ +/**
5279
+ + * How long one self-hosted remote cache request may go without a result
5280
+ + * before the run goes on without the cache. The native client sets no timeout
5281
+ + * of its own, so a server that accepts and never answers held the run forever.
5282
+ + * Cowshed's gateway answers a request whose backend does not answer within its
5283
+ + * connect (5 s) and response-head (60 s) bounds with its own audited miss
5284
+ + * (specs/cowshed/05_gateway.md, "Backend outage"); this bound is longer, so it
5285
+ + * decides only when the server itself cannot answer. It covers the whole
5286
+ + * native call: a store also builds its archive inside it.
5287
+ + */
5288
+ +exports.REMOTE_CACHE_ANSWER_WITHIN_MS = 90_000;
5289
+ +/**
5290
+ + * The native client's messages for a request no server answered or finished
5291
+ + * (`native/cache/errors.rs` RequestError, `http_remote_cache.rs` body read).
5292
+ + */
5293
+ +const UNREACHABLE = ['Failed to send request: ', 'Failed to read remote cache response body: '];
5294
+ +/** A 5xx status: the server answered that it cannot serve the cache now. */
5295
+ +const SERVER_ERROR = /^Misconfigured remote cache endpoint: Unexpected response status: 5\d\d\b/;
5296
+ +/**
5297
+ + * Decides what a settled remote cache request means for the run. An answer is
5298
+ + * used; a request the server never answered, or answered with a server error,
5299
+ + * is an outage, which the run reads as a miss or a skipped store; every other
5300
+ + * rejection (credentials, a status that is misconfiguration, a damaged
5301
+ + * archive, local disk) is returned unchanged to fail the run as before.
5302
+ + */
5303
+ +function settleRemoteCacheRequest(settlement) {
5304
+ + switch (settlement.kind) {
5305
+ + case 'answered':
5306
+ + return settlement;
5307
+ + case 'unanswered':
5308
+ + return { kind: 'outage', cause: 'no result within ' + settlement.withinMs + ' ms' };
5309
+ + case 'rejected': {
5310
+ + const { error } = settlement;
5311
+ + if (error instanceof Error &&
5312
+ + (UNREACHABLE.some((prefix) => error.message.startsWith(prefix)) || SERVER_ERROR.test(error.message))) {
5313
+ + return { kind: 'outage', cause: oneLine(error.message) };
5314
+ + }
5315
+ + return { kind: 'fatal', error };
5316
+ + }
5317
+ + default:
5318
+ + throw new Error('unknown remote cache settlement: ' + String(settlement.kind));
5319
+ + }
5320
+ +}
5321
+ +/** The one line a run prints when it goes on without its remote cache. */
5322
+ +function describeRemoteCacheOutage(server, request, cause) {
5323
+ + return oneLine('Remote cache ' + server + ' is unavailable (' + request + ': ' + cause +
5324
+ + '); this run continues without it: lookups miss and results are not stored.');
5325
+ +}
5326
+ +function oneLine(text) {
5327
+ + return text.split(/\s+/).filter((part) => part.length > 0).join(' ');
5328
+ +}
5329
+ +/** The run's clock: a deadline that elapses `withinMs` after it is made, unless cleared first. */
5330
+ +function timerDeadline(withinMs) {
5331
+ + let timer;
5332
+ + const elapsed = new Promise((resolve) => {
5333
+ + timer = setTimeout(resolve, withinMs);
5334
+ + });
5335
+ + return { elapsed, clear: () => clearTimeout(timer) };
5336
+ +}
5337
+ +/** Settles `request` as answered or rejected, or as unanswered once its `deadline` for `withinMs` elapses. */
5338
+ +async function settleWithin(request, withinMs, deadline) {
5339
+ + const { elapsed, clear } = deadline(withinMs);
5340
+ + try {
5341
+ + return await Promise.race([
5342
+ + request.then((value) => ({ kind: 'answered', value }), (error) => ({ kind: 'rejected', error })),
5343
+ + elapsed.then(() => ({ kind: 'unanswered', withinMs })),
5344
+ + ]);
5345
+ + }
5346
+ + finally {
5347
+ + clear();
5348
+ + }
5349
+ +}
5350
+ +/**
5351
+ + * The self-hosted remote cache as a run uses it: the first outage is said once,
5352
+ + * naming the server, the request and the cause, and from then on this process
5353
+ + * asks the server nothing more. Every lookup misses and every store is skipped,
5354
+ + * so the tasks run exactly as they would with no remote cache.
5355
+ + *
5356
+ + * A native call the deadline overtook keeps running, and nothing reads its
5357
+ + * result. A late lookup would still unpack its artifact into the directory it
5358
+ + * was given, so a lookup never gets the cache directory itself: it unpacks into
5359
+ + * a staging directory of its own, and only an artifact answered in time is
5360
+ + * moved to `<cacheDirectory>/<hash>`. A miss or an outage removes the staging
5361
+ + * directory once its native call settles; one still running when the process
5362
+ + * exits is removed then.
5363
+ + */
5364
+ +class RemoteCacheOutageGuard {
5365
+ + /** `deadline` is the guard's clock: a run keeps the timer, a test passes one it elapses itself. */
5366
+ + constructor(remote, server, deadline = timerDeadline) {
5367
+ + this.remote = remote;
5368
+ + this.server = server;
5369
+ + this.deadline = deadline;
5370
+ + this.unavailable = false;
5371
+ + }
5372
+ + async retrieve(hash, cacheDirectory) {
5373
+ + if (this.unavailable) {
5374
+ + return null;
5375
+ + }
5376
+ + const stagingRoot = (0, path_1.join)(cacheDirectory, 'remote-retrieve');
5377
+ + await (0, promises_1.mkdir)(stagingRoot, { recursive: true });
5378
+ + const staging = await (0, promises_1.mkdtemp)((0, path_1.join)(stagingRoot, process.pid + '-'));
5379
+ + unsettledStaging.add(staging);
5380
+ + if (this.unavailable) {
5381
+ + // Another request's outage arrived while this one prepared: ask nothing more.
5382
+ + await retire(staging);
5383
+ + return null;
5384
+ + }
5385
+ + const native = (async () => this.remote.retrieve(hash, staging))();
5386
+ + let result;
5387
+ + try {
5388
+ + result = await this.request('GET /v1/cache/' + hash, native, null);
5389
+ + }
5390
+ + catch (error) {
5391
+ + // A fatal rejection: the native call has settled.
5392
+ + await retire(staging);
5393
+ + throw error;
5394
+ + }
5395
+ + if (result === null) {
5396
+ + // A settled miss, or an outage whose native call may still unpack into the staging directory.
5397
+ + void native.then(() => retire(staging), () => retire(staging));
5398
+ + return null;
5399
+ + }
5400
+ + // Answered in time, so the native call has settled: publish its artifact under the hash.
5401
+ + const destination = (0, path_1.join)(cacheDirectory, hash);
5402
+ + await (0, promises_1.rm)(destination, { recursive: true, force: true });
5403
+ + await (0, promises_1.rename)(result.outputsPath, destination);
5404
+ + await retire(staging);
5405
+ + return { ...result, outputsPath: destination };
5406
+ + }
5407
+ + async store(hash, cacheDirectory, terminalOutput, code) {
5408
+ + if (this.unavailable) {
5409
+ + return false;
5410
+ + }
5411
+ + return this.request('PUT /v1/cache/' + hash, (async () => this.remote.store(hash, cacheDirectory, terminalOutput, code))(), false);
5412
+ + }
5413
+ + async request(request, native, skipped) {
5414
+ + if (this.unavailable) {
5415
+ + return skipped;
5416
+ + }
5417
+ + const outcome = settleRemoteCacheRequest(await settleWithin(native, exports.REMOTE_CACHE_ANSWER_WITHIN_MS, this.deadline));
5418
+ + switch (outcome.kind) {
5419
+ + case 'answered':
5420
+ + return outcome.value;
5421
+ + case 'fatal':
5422
+ + throw outcome.error;
5423
+ + case 'outage':
5424
+ + if (!this.unavailable) {
5425
+ + this.unavailable = true;
5426
+ + logger_1.logger.warn(describeRemoteCacheOutage(this.server, request, outcome.cause));
5427
+ + }
5428
+ + return skipped;
5429
+ + }
5430
+ + }
5431
+ +}
5432
+ +exports.RemoteCacheOutageGuard = RemoteCacheOutageGuard;
5433
+ +/**
5434
+ + * A shed reads the project cache and never stores to it: only a land publishes. store answers false before any
5435
+ + * archive is built, so a read-only run spends nothing on uploads (NX_SELF_HOSTED_REMOTE_CACHE_READ_ONLY=true).
5436
+ + */
5437
+ +class ReadOnlyRemoteCache {
5438
+ + constructor(remote) {
5439
+ + this.remote = remote;
5440
+ + }
5441
+ + retrieve(hash, cacheDirectory) {
5442
+ + return this.remote.retrieve(hash, cacheDirectory);
5443
+ + }
5444
+ + async store() {
5445
+ + return false;
5446
+ + }
5447
+ +}
5448
+ +exports.ReadOnlyRemoteCache = ReadOnlyRemoteCache;
5449
+ +/** Staging directories not yet removed; the process removes what is left as it exits. */
5450
+ +const unsettledStaging = new Set();
5451
+ +process.on('exit', () => {
5452
+ + for (const staging of unsettledStaging) {
5453
+ + try {
5454
+ + (0, node_fs_1.rmSync)(staging, { recursive: true, force: true });
5455
+ + }
5456
+ + catch (error) {
5457
+ + logger_1.logger.warn('Could not remove remote cache staging directory ' + staging + ': ' + String(error));
5458
+ + }
5459
+ + }
5460
+ +});
5461
+ +/**
5462
+ + * Removes a staging directory whose native lookup has settled; a removal that
5463
+ + * fails is said, never thrown. It stays listed until it is gone, so a process
5464
+ + * that exits mid-removal still removes it.
5465
+ + */
5466
+ +async function retire(staging) {
5467
+ + try {
5468
+ + await (0, promises_1.rm)(staging, { recursive: true, force: true });
5469
+ + unsettledStaging.delete(staging);
5470
+ + }
5471
+ + catch (error) {
5472
+ + logger_1.logger.warn('Could not remove remote cache staging directory ' + staging + ': ' + String(error));
5473
+ + }
5474
+ +}
3079
5475
  diff --git a/dist/src/tasks-runner/run-command.js b/dist/src/tasks-runner/run-command.js
3080
- index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd39fe9131 100644
5476
+ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..2e55ade6c0befcc6445808e6106b5b97dadc5657 100644
3081
5477
  --- a/dist/src/tasks-runner/run-command.js
3082
5478
  +++ b/dist/src/tasks-runner/run-command.js
3083
5479
  @@ -41,6 +41,7 @@ const task_profiling_life_cycle_1 = require("./life-cycles/task-profiling-life-c
@@ -3088,7 +5484,16 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3088
5484
  const task_telemetry_life_cycle_1 = require("./life-cycles/task-telemetry-life-cycle");
3089
5485
  const task_timings_life_cycle_1 = require("./life-cycles/task-timings-life-cycle");
3090
5486
  const tui_summary_life_cycle_1 = require("./life-cycles/tui-summary-life-cycle");
3091
- @@ -373,8 +374,9 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
5487
+ @@ -332,6 +333,8 @@ async function runCommand(projectsToRun, currentProjectGraph, { nxJson }, nxArgs
5488
+ id,
5489
+ workspaceRoot: workspace_root_1.workspaceRoot,
5490
+ nxJsonConfiguration: nxJson,
5491
+ + projectGraph: currentProjectGraph,
5492
+ + taskGraph: (0, create_task_graph_1.createTaskGraph)(currentProjectGraph, extraTargetDependencies ?? {}, projectsToRun.map((project) => project.name), nxArgs.targets, nxArgs.configuration, overrides, extraOptions.excludeTaskDependencies),
5493
+ argv: process.argv,
5494
+ });
5495
+ const startTime = Date.now();
5496
+ @@ -373,8 +376,9 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
3092
5497
  const initiatingTasks = tasks.filter((t) => projectNameSet.has(t.target.project) &&
3093
5498
  nxArgs.targets.includes(t.target.target));
3094
5499
  const { lifeCycle, renderIsDone, printSummary, restoreTerminal } = await getTerminalOutputLifeCycle(initiatingProject, initiatingTasks, projectNames, tasks, taskGraph, nxArgs, nxJson, overrides);
@@ -3099,7 +5504,7 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3099
5504
  tasks,
3100
5505
  projectGraph,
3101
5506
  taskGraph,
3102
- @@ -384,7 +386,7 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
5507
+ @@ -384,7 +388,7 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
3103
5508
  loadDotEnvFiles: extraOptions.loadDotEnvFiles,
3104
5509
  initiatingProject,
3105
5510
  initiatingTasks,
@@ -3108,7 +5513,7 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3108
5513
  await renderIsDone.finally(() => restoreTerminal?.());
3109
5514
  if (printSummary) {
3110
5515
  printSummary();
3111
- @@ -395,7 +397,7 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
5516
+ @@ -395,7 +399,7 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
3112
5517
  logger_1.logger.log((0, nx_key_1.createNxKeyLicenseeInformation)(nxKey));
3113
5518
  return {
3114
5519
  taskResults,
@@ -3117,7 +5522,19 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3117
5522
  };
3118
5523
  }
3119
5524
  catch (e) {
3120
- @@ -424,18 +426,22 @@ async function printConfigureAiAgentsDisclaimer() {
5525
+ @@ -411,7 +415,10 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
5526
+ }
5527
+ async function printConfigureAiAgentsDisclaimer() {
5528
+ try {
5529
+ - if (!client_1.daemonClient.enabled() || !(await client_1.daemonClient.isServerAvailable())) {
5530
+ + // The disclaimer is for a person at a terminal; the status behind it runs each editor's CLI in the daemon.
5531
+ + if (!process.stdout.isTTY ||
5532
+ + !client_1.daemonClient.enabled() ||
5533
+ + !(await client_1.daemonClient.isServerAvailable())) {
5534
+ return;
5535
+ }
5536
+ const { outdatedAgents } = await client_1.daemonClient.getConfigureAiAgentsStatus();
5537
+ @@ -424,18 +431,22 @@ async function printConfigureAiAgentsDisclaimer() {
3121
5538
  // Silently ignore errors
3122
5539
  }
3123
5540
  }
@@ -3149,7 +5566,7 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3149
5566
  }
3150
5567
  async function ensureWorkspaceIsInSyncAndGetGraphs(projectGraph, nxJson, projectNames, nxArgs, overrides, extraTargetDependencies, extraOptions) {
3151
5568
  let taskGraph = createTaskGraphAndRunValidations(projectGraph, extraTargetDependencies ?? {}, projectNames, nxArgs, overrides, extraOptions);
3152
- @@ -659,7 +665,16 @@ function setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles) {
5569
+ @@ -659,7 +670,16 @@ function setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles) {
3153
5570
  process.env.NX_LOAD_DOT_ENV_FILES = 'true';
3154
5571
  }
3155
5572
  }
@@ -3167,7 +5584,7 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3167
5584
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
3168
5585
  // this needs to be done before we start to run the tasks
3169
5586
  const taskDetails = (0, hash_task_1.getTaskDetails)();
3170
- @@ -674,10 +689,11 @@ async function invokeTasksRunner({ tasks, projectGraph, taskGraph, lifeCycle, nx
5587
+ @@ -674,10 +694,11 @@ async function invokeTasksRunner({ tasks, projectGraph, taskGraph, lifeCycle, nx
3171
5588
  ...constructLifeCycles(lifeCycle, taskGraph, nxJson, nxArgs.skipNxCache),
3172
5589
  taskResultsLifecycle,
3173
5590
  ]);
@@ -3181,7 +5598,7 @@ index 4e64afa4fb985191ac8bc0e31a1e380bcecf9427..14723620cb7d8094bff0eb70214442dd
3181
5598
  initiatingProject,
3182
5599
  initiatingTasks,
3183
5600
  projectGraph,
3184
- @@ -735,12 +751,24 @@ async function invokeTasksRunner({ tasks, projectGraph, taskGraph, lifeCycle, nx
5601
+ @@ -735,12 +756,24 @@ async function invokeTasksRunner({ tasks, projectGraph, taskGraph, lifeCycle, nx
3185
5602
  },
3186
5603
  },
3187
5604
  daemon: client_1.daemonClient,
@@ -3306,10 +5723,18 @@ index 42dc80a7a3551c9fe0737f39388670e9a7a475dd..99ac4a74f9cec802360f365d7ca8fc34
3306
5723
  }
3307
5724
  return cycles.size ? cycles : null;
3308
5725
  diff --git a/dist/src/tasks-runner/task-orchestrator.d.ts b/dist/src/tasks-runner/task-orchestrator.d.ts
3309
- index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5ee7228d57 100644
5726
+ index 6d3dd021b050be5c74803555b361b6502f9693c7..8545dea5e1f4221191de652435a6a635a2986f6e 100644
3310
5727
  --- a/dist/src/tasks-runner/task-orchestrator.d.ts
3311
5728
  +++ b/dist/src/tasks-runner/task-orchestrator.d.ts
3312
- @@ -57,11 +57,14 @@ export declare class TaskOrchestrator {
5729
+ @@ -50,6 +50,7 @@ export declare class TaskOrchestrator {
5730
+ private bailed;
5731
+ private resolveStopPromise;
5732
+ private stopRequested;
5733
+ + private stopSignal;
5734
+ private runningContinuousTasks;
5735
+ private runningRunCommandsTasks;
5736
+ private runningDiscreteTasks;
5737
+ @@ -57,11 +58,14 @@ export declare class TaskOrchestrator {
3313
5738
  private continuousTaskExitHandled;
3314
5739
  private cleanupPromise;
3315
5740
  private signalHandlers;
@@ -3324,7 +5749,7 @@ index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5e
3324
5749
  nextBatch(): Batch;
3325
5750
  /**
3326
5751
  * Coordinator loop. All batch operations (hashing, cache resolution)
3327
- @@ -105,12 +108,9 @@ export declare class TaskOrchestrator {
5752
+ @@ -105,12 +109,9 @@ export declare class TaskOrchestrator {
3328
5753
  * cache fetch + lifecycle to the public method. Returns true if any
3329
5754
  * tasks were resolved.
3330
5755
  *
@@ -3340,7 +5765,7 @@ index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5e
3340
5765
  */
3341
5766
  private resolveCachedTasksBulk;
3342
5767
  /**
3343
- @@ -124,6 +124,7 @@ export declare class TaskOrchestrator {
5768
+ @@ -124,6 +125,7 @@ export declare class TaskOrchestrator {
3344
5769
  private applyBatchCachedResults;
3345
5770
  private hashBatchTasks;
3346
5771
  applyFromCacheOrRunBatch(doNotSkipCache: boolean, batch: Batch, groupId: number): Promise<TaskResult[]>;
@@ -3348,7 +5773,7 @@ index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5e
3348
5773
  private runBatch;
3349
5774
  /**
3350
5775
  * Rendering a batch's output must never change the batch's results. A throw
3351
- @@ -202,12 +203,11 @@ export declare class TaskOrchestrator {
5776
+ @@ -202,12 +204,11 @@ export declare class TaskOrchestrator {
3352
5777
  * don't actually compete for parallelism.
3353
5778
  */
3354
5779
  resolveCachedTasks(doNotSkipCache: boolean, tasks: Task[], groupId: number): Promise<TaskResult[]>;
@@ -3364,7 +5789,7 @@ index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5e
3364
5789
  */
3365
5790
  private dispatchDiscreteWorker;
3366
5791
  /**
3367
- @@ -219,14 +219,16 @@ export declare class TaskOrchestrator {
5792
+ @@ -219,14 +220,16 @@ export declare class TaskOrchestrator {
3368
5793
  */
3369
5794
  private handleDiscreteWorkerFailure;
3370
5795
  /**
@@ -3385,18 +5810,19 @@ index 6d3dd021b050be5c74803555b361b6502f9693c7..81e7fa2f866eb22eeabcc8d2802aec5e
3385
5810
  private postRunSteps;
3386
5811
  private scheduleNextTasksAndReleaseThreads;
3387
5812
  diff --git a/dist/src/tasks-runner/task-orchestrator.js b/dist/src/tasks-runner/task-orchestrator.js
3388
- index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf092e3732b 100644
5813
+ index 0b446cbce811319a741165e42a0100e0570e0a2f..ef2bb51da2a360cff60a3b0a2e37ad444fd84df0 100644
3389
5814
  --- a/dist/src/tasks-runner/task-orchestrator.js
3390
5815
  +++ b/dist/src/tasks-runner/task-orchestrator.js
3391
- @@ -29,6 +29,7 @@ const shared_running_task_1 = require("./running-tasks/shared-running-task");
5816
+ @@ -29,6 +29,8 @@ const shared_running_task_1 = require("./running-tasks/shared-running-task");
3392
5817
  const task_env_1 = require("./task-env");
3393
5818
  const tasks_schedule_1 = require("./tasks-schedule");
3394
5819
  const utils_1 = require("./utils");
3395
5820
  +const { TaskOutputLeases } = require("./task-output-lease");
5821
+ +const outputs_stamp_1 = require("./outputs-stamp");
3396
5822
  /**
3397
5823
  * Resolve a batch executor's per-task result to a TaskStatus. Prefers an
3398
5824
  * explicit `status` from the executor; falls back to the `success` boolean
3399
- @@ -79,11 +80,9 @@ class TaskOrchestrator {
5825
+ @@ -79,11 +81,9 @@ class TaskOrchestrator {
3400
5826
  // underlying real tasks become the anchors.
3401
5827
  this.initializingTaskIds = (0, utils_1.expandInitiatingTasksThroughNoop)(this.initiatingTasks, this.taskGraph, this.projectGraph);
3402
5828
  this.processedTasks = new Map();
@@ -3411,7 +5837,15 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3411
5837
  this.cacheMissedHashes = new Set();
3412
5838
  this.completedTasks = new Map();
3413
5839
  this.waitingForTasks = [];
3414
- @@ -106,6 +105,9 @@ class TaskOrchestrator {
5840
+ @@ -99,6 +99,7 @@ class TaskOrchestrator {
5841
+ this.bailed = false;
5842
+ this.resolveStopPromise = null;
5843
+ this.stopRequested = false;
5844
+ + this.stopSignal = null;
5845
+ this.runningContinuousTasks = new Map();
5846
+ this.runningRunCommandsTasks = new Map();
5847
+ this.runningDiscreteTasks = new Map();
5848
+ @@ -106,6 +107,9 @@ class TaskOrchestrator {
3415
5849
  this.continuousTaskExitHandled = new Map();
3416
5850
  this.cleanupPromise = null;
3417
5851
  this.signalHandlers = [];
@@ -3421,7 +5855,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3421
5855
  }
3422
5856
  async init() {
3423
5857
  this.setupSignalHandlers();
3424
- @@ -125,6 +127,20 @@ class TaskOrchestrator {
5858
+ @@ -125,6 +129,20 @@ class TaskOrchestrator {
3425
5859
  }
3426
5860
  }
3427
5861
  async run() {
@@ -3442,7 +5876,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3442
5876
  await this.init();
3443
5877
  perf_hooks_1.performance.mark('task-execution:start');
3444
5878
  const { discrete, continuous, total } = getThreadPoolSize(this.options, this.taskGraph);
3445
- @@ -159,8 +175,6 @@ class TaskOrchestrator {
5879
+ @@ -159,8 +177,6 @@ class TaskOrchestrator {
3446
5880
  if (!this.stopRequested) {
3447
5881
  this.cache.removeOldCacheRecords();
3448
5882
  }
@@ -3451,7 +5885,26 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3451
5885
  // Public API (defaultTasksRunner) returns a plain object keyed by
3452
5886
  // task id. Internal state is a Map for faster lookup.
3453
5887
  return Object.fromEntries(this.completedTasks);
3454
- @@ -413,12 +427,9 @@ class TaskOrchestrator {
5888
+ @@ -227,6 +243,18 @@ class TaskOrchestrator {
5889
+ const groupId = this.closeGroup();
5890
+ this.dispatchDiscreteWorker(doNotSkipCache, task, groupId);
5891
+ }
5892
+ + // The jobserver is the CPU admission, not this count: cowshed gives every job a fifo with
5893
+ + // as many slots as the host has cores, served by its supervisor, and the kernel schedules
5894
+ + // what it admits. A slot here is a second admission, and a slot held by a test that runs at
5895
+ + // a tenth of a core holds back the gate's critical chain. So once every slot is held, the
5896
+ + // ready task heading the longest chain not yet started (TasksSchedule.nextCriticalTask)
5897
+ + // still starts, as one extra worker: never more than one beyond the parallelism.
5898
+ + if (this.pendingDiscreteWorkers.size === parallelism) {
5899
+ + const task = this.tasksSchedule.nextCriticalTask((t) => !t.continuous);
5900
+ + if (task) {
5901
+ + this.dispatchDiscreteWorker(doNotSkipCache, task, this.options.parallel + this.continuousTasksStarted++);
5902
+ + }
5903
+ + }
5904
+ // 6. Nothing left to dispatch and nothing in flight — done.
5905
+ if (!this.tasksSchedule.hasTasks() &&
5906
+ this.pendingDiscreteWorkers.size === 0) {
5907
+ @@ -413,12 +441,9 @@ class TaskOrchestrator {
3455
5908
  * cache fetch + lifecycle to the public method. Returns true if any
3456
5909
  * tasks were resolved.
3457
5910
  *
@@ -3467,7 +5920,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3467
5920
  */
3468
5921
  async resolveCachedTasksBulk() {
3469
5922
  const { scheduledTasks } = this.tasksSchedule.getAllScheduledTasks();
3470
- @@ -514,6 +525,13 @@ class TaskOrchestrator {
5923
+ @@ -514,6 +539,13 @@ class TaskOrchestrator {
3471
5924
  await (0, hash_task_1.hashTasks)(this.hasher, this.projectGraph, this.fullTaskGraph, perTaskEnvs, this.taskDetails, tasks);
3472
5925
  }
3473
5926
  async applyFromCacheOrRunBatch(doNotSkipCache, batch, groupId) {
@@ -3481,7 +5934,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3481
5934
  const applyFromCacheOrRunBatchStart = perf_hooks_1.performance.mark('TaskOrchestrator-apply-from-cache-or-run-batch:start');
3482
5935
  const taskEntries = Object.entries(batch.taskGraph.tasks);
3483
5936
  const tasks = taskEntries.map(([, task]) => task);
3484
- @@ -571,7 +589,7 @@ class TaskOrchestrator {
5937
+ @@ -571,7 +603,7 @@ class TaskOrchestrator {
3485
5938
  const tasksCompleted = taskEntries.filter(([taskId]) => this.completedTasks.has(taskId));
3486
5939
  // Batch is still not done, run it again
3487
5940
  if (tasksCompleted.length !== taskEntries.length) {
@@ -3490,7 +5943,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3490
5943
  id: batch.id,
3491
5944
  executorName: batch.executorName,
3492
5945
  taskGraph: (0, utils_1.removeTasksFromTaskGraph)(batch.taskGraph, tasksCompleted.map(([taskId]) => taskId)),
3493
- @@ -810,6 +828,20 @@ class TaskOrchestrator {
5946
+ @@ -810,6 +842,20 @@ class TaskOrchestrator {
3494
5947
  * don't actually compete for parallelism.
3495
5948
  */
3496
5949
  async resolveCachedTasks(doNotSkipCache, tasks, groupId) {
@@ -3511,7 +5964,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3511
5964
  if (!doNotSkipCache || tasks.length === 0)
3512
5965
  return [];
3513
5966
  const cacheableTasks = tasks.filter((t) => t.cache);
3514
- @@ -836,10 +868,8 @@ class TaskOrchestrator {
5967
+ @@ -836,10 +882,8 @@ class TaskOrchestrator {
3515
5968
  }
3516
5969
  /**
3517
5970
  * Fire a discrete-task worker and track it in pendingDiscreteWorkers until
@@ -3524,7 +5977,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3524
5977
  */
3525
5978
  dispatchDiscreteWorker(doNotSkipCache, task, groupId) {
3526
5979
  const worker = this.runTaskDirectly(doNotSkipCache, task, groupId)
3527
- @@ -870,11 +900,21 @@ class TaskOrchestrator {
5980
+ @@ -870,11 +914,21 @@ class TaskOrchestrator {
3528
5981
  await this.postRunSteps([{ task, status: 'failure', terminalOutput }], doNotSkipCache, groupId);
3529
5982
  }
3530
5983
  /**
@@ -3549,7 +6002,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3549
6002
  // Wait for task to be processed
3550
6003
  const taskSpecificEnv = await this.processedTasks.get(task.id);
3551
6004
  await this.preRunSteps([task], { groupId });
3552
- @@ -1063,6 +1103,10 @@ class TaskOrchestrator {
6005
+ @@ -1063,6 +1117,10 @@ class TaskOrchestrator {
3553
6006
  }
3554
6007
  }
3555
6008
  async startContinuousTask(task, groupId) {
@@ -3560,7 +6013,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3560
6013
  if (this.runningTasksService &&
3561
6014
  this.runningTasksService.getRunningTasks([task.id]).length) {
3562
6015
  await this.preRunSteps([task], { groupId });
3563
- @@ -1086,6 +1130,16 @@ class TaskOrchestrator {
6016
+ @@ -1086,6 +1144,16 @@ class TaskOrchestrator {
3564
6017
  await this.scheduleNextTasksAndReleaseThreads();
3565
6018
  return runningTask;
3566
6019
  }
@@ -3577,7 +6030,7 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3577
6030
  const taskSpecificEnv = await this.processedTasks.get(task.id);
3578
6031
  await this.preRunSteps([task], { groupId });
3579
6032
  const pipeOutput = await this.pipeOutputCapture(task);
3580
- @@ -1105,10 +1159,10 @@ class TaskOrchestrator {
6033
+ @@ -1105,10 +1173,10 @@ class TaskOrchestrator {
3581
6034
  groupId,
3582
6035
  ownsRunningTasksService: true,
3583
6036
  });
@@ -3592,40 +6045,63 @@ index 0b446cbce811319a741165e42a0100e0570e0a2f..e1989ccab6a0ddc3cea350ee60646bf0
3592
6045
  });
3593
6046
  }));
3594
6047
  await this.scheduleNextTasksAndReleaseThreads();
3595
- @@ -1300,7 +1354,9 @@ class TaskOrchestrator {
6048
+ @@ -1300,22 +1368,28 @@ class TaskOrchestrator {
3596
6049
  if (tasks.length === 0)
3597
6050
  return resultMap;
3598
6051
  if (this.daemon?.enabled()) {
3599
6052
  - const matches = await this.daemon.outputsHashesMatchBatch(tasks);
3600
- + // A daemon gone under a client that may never start one verifies
3601
- + // nothing: every output is copied, as below.
3602
- + const matches = await this.daemon.orWithoutDaemon(() => this.daemon.outputsHashesMatchBatch(tasks), () => tasks.map(() => false));
6053
+ + // A daemon gone under a client that may never start one leaves the
6054
+ + // recorded outputs stamps to verify them, as without one.
6055
+ + const matches = await this.daemon.orWithoutDaemon(() => this.daemon.outputsHashesMatchBatch(tasks), () => (0, outputs_stamp_1.outputsStampsMatch)(tasks));
3603
6056
  for (let i = 0; i < tasks.length; i++) {
3604
6057
  resultMap.set(tasks[i].hash, !matches[i]);
3605
6058
  }
3606
- @@ -1315,7 +1371,7 @@ class TaskOrchestrator {
6059
+ }
6060
+ else {
6061
+ - // No daemon → can't verify on-disk outputs, always copy.
6062
+ - for (const task of tasks) {
6063
+ - resultMap.set(task.hash, true);
6064
+ + // No daemon: the outputs stamps a run or restore recorded verify the
6065
+ + // on-disk outputs (outputs-stamp.js); anything else is copied.
6066
+ + const matches = (0, outputs_stamp_1.outputsStampsMatch)(tasks);
6067
+ + for (let i = 0; i < tasks.length; i++) {
6068
+ + resultMap.set(tasks[i].hash, !matches[i]);
6069
+ }
6070
+ }
6071
+ return resultMap;
3607
6072
  }
3608
6073
  async recordOutputsHashBatch(entries) {
6074
+ + // Recorded with or without a daemon: a later client may have none.
6075
+ + (0, outputs_stamp_1.recordOutputsStamps)(entries);
3609
6076
  if (this.daemon?.enabled()) {
3610
6077
  - return this.daemon.recordOutputsHashBatch(entries);
3611
6078
  + return this.daemon.orWithoutDaemon(() => this.daemon.recordOutputsHashBatch(entries), () => undefined);
3612
6079
  }
3613
6080
  }
3614
6081
  // endregion utils
3615
- @@ -1376,6 +1432,7 @@ class TaskOrchestrator {
6082
+ @@ -1376,6 +1450,8 @@ class TaskOrchestrator {
3616
6083
  return this.cleanupPromise;
3617
6084
  }
3618
6085
  async performCleanup() {
3619
- + await Promise.all([this.taskOutputLeases.cancelPending(), this.continuousStartupLeases.cancelPending()]);
6086
+ + const cause = this.stopSignal ? `the client received ${this.stopSignal}` : this.stopRequested ? 'the run was stopped' : 'the run ended';
6087
+ + await Promise.all([this.taskOutputLeases.cancelPending(cause), this.continuousStartupLeases.cancelPending(cause)]);
3620
6088
  // Mark all running tasks for intentional stop
3621
6089
  const reason = this.stopRequested ? 'interrupted' : 'fulfilled';
3622
6090
  for (const entry of this.runningContinuousTasks.values()) {
6091
+ @@ -1445,6 +1521,7 @@ class TaskOrchestrator {
6092
+ if (this.stopRequested)
6093
+ return;
6094
+ this.stopRequested = true;
6095
+ + this.stopSignal = signal;
6096
+ if (!this.tuiEnabled) {
6097
+ // Synchronously remove DB entries before async cleanup to prevent
6098
+ // new nx processes from seeing stale "Waiting for ..." messages.
3623
6099
  diff --git a/dist/src/tasks-runner/task-output-lease.d.ts b/dist/src/tasks-runner/task-output-lease.d.ts
3624
6100
  new file mode 100644
3625
- index 0000000000000000000000000000000000000000..ab141ad0de874122b363f2fd1894dcbde6ac52a4
6101
+ index 0000000000000000000000000000000000000000..94d8f145d8703356c00898c7dde7444a4654650d
3626
6102
  --- /dev/null
3627
6103
  +++ b/dist/src/tasks-runner/task-output-lease.d.ts
3628
- @@ -0,0 +1,10 @@
6104
+ @@ -0,0 +1,11 @@
3629
6105
  +import { Task } from '../config/task-graph';
3630
6106
  +import { TaskInvocationTracker } from '../native';
3631
6107
  +/** A target's cross-client ownership from cache lookup/restore through execution and cache capture. */
@@ -3633,18 +6109,19 @@ index 0000000000000000000000000000000000000000..ab141ad0de874122b363f2fd1894dcbd
3633
6109
  + constructor(parallelism: number, invocationTracker: TaskInvocationTracker | null, purpose: 'output' | 'continuous-startup');
3634
6110
  + acquire(tasks: readonly Task[]): Promise<() => Promise<void>>;
3635
6111
  + withTasks<T>(tasks: readonly Task[], action: () => Promise<T>): Promise<T>;
3636
- + cancelPending(): Promise<void>;
6112
+ + /** Ends every pending acquisition; `cause` says why the runner is cleaning up. */
6113
+ + cancelPending(cause: string): Promise<void>;
3637
6114
  + close(): Promise<void>;
3638
6115
  +}
3639
6116
  diff --git a/dist/src/tasks-runner/task-output-lease.js b/dist/src/tasks-runner/task-output-lease.js
3640
6117
  new file mode 100644
3641
- index 0000000000000000000000000000000000000000..df3e705647a8a38b28125df16ef0c73472b20df3
6118
+ index 0000000000000000000000000000000000000000..25a5647c236c3c1de0d638f6f50803ffb0c717c5
3642
6119
  --- /dev/null
3643
6120
  +++ b/dist/src/tasks-runner/task-output-lease.js
3644
- @@ -0,0 +1,233 @@
6121
+ @@ -0,0 +1,248 @@
3645
6122
  +"use strict";
3646
6123
  +const { channel } = require("node:diagnostics_channel");
3647
- +const { writeFileSync } = require("node:fs");
6124
+ +const { readFileSync, writeFileSync } = require("node:fs");
3648
6125
  +const { join } = require("node:path");
3649
6126
  +const { fork } = require("node:child_process");
3650
6127
  +const { FileLock, hashArray } = require("../native");
@@ -3696,6 +6173,18 @@ index 0000000000000000000000000000000000000000..df3e705647a8a38b28125df16ef0c734
3696
6173
  + });
3697
6174
  +}
3698
6175
  +else {
6176
+ + // A lock path and the record its holder wrote on taking it (helper and client
6177
+ + // pids, purpose, targets; empty once released): what a cancelled wait waited for.
6178
+ + function waitedFor(path) {
6179
+ + let record;
6180
+ + try {
6181
+ + record = readFileSync(path, "utf8");
6182
+ + }
6183
+ + catch (error) {
6184
+ + return `${path} (holder record unreadable: ${error.message})`;
6185
+ + }
6186
+ + return `${path} (holder record: ${record === "" ? "empty" : record})`;
6187
+ + }
3699
6188
  + // The pool's slots. A slot is held by a live helper in `workers`; an acquire
3700
6189
  + // that finds none free waits in `waiters` for one of exactly two events:
3701
6190
  + // a release hands it the released helper, or a helper leaves the pool
@@ -3849,22 +6338,25 @@ index 0000000000000000000000000000000000000000..df3e705647a8a38b28125df16ef0c734
3849
6338
  + await release();
3850
6339
  + }
3851
6340
  + }
3852
- + async cancelPending() {
6341
+ + // Ends every pending acquisition with `cause`, why the runner is cleaning up.
6342
+ + async cancelPending(cause) {
3853
6343
  + this.stopping = true;
3854
6344
  + for (const waiter of this.waiters.splice(0)) {
3855
- + waiter.reject(new Error("Task output lease acquisition cancelled during runner cleanup"));
6345
+ + waiter.reject(new Error(`Task output lease acquisition cancelled during runner cleanup (${cause})`));
3856
6346
  + }
3857
6347
  + await Promise.all([...this.workers]
3858
6348
  + .filter((entry) => entry.phase === "acquiring")
3859
6349
  + .map((entry) => {
3860
6350
  + // Interrupt the native wait unconditionally. This owned
3861
- + // helper holds only kernel leases, never task work.
3862
- + entry.worker.kill("SIGKILL");
6351
+ + // helper holds only kernel leases, never task work. Retired
6352
+ + // first, its acquire fails with the cancellation: the exit of
6353
+ + // a helper this client killed is not the reason.
6354
+ + this.retire(entry, new Error(`Task output lease acquisition for ${entry.targets.join(", ")} cancelled during runner cleanup (${cause}) while helper ${entry.worker.pid} waited for ${entry.paths.map(waitedFor).join(", ")}`));
3863
6355
  + return entry.exited;
3864
6356
  + }));
3865
6357
  + }
3866
6358
  + async close() {
3867
- + await this.cancelPending();
6359
+ + await this.cancelPending("the lease pool closed");
3868
6360
  + this.closed = true;
3869
6361
  + await Promise.all([...this.children].map((entry) => {
3870
6362
  + entry.worker.kill("SIGKILL");
@@ -3875,6 +6367,102 @@ index 0000000000000000000000000000000000000000..df3e705647a8a38b28125df16ef0c734
3875
6367
  + }
3876
6368
  + exports.TaskOutputLeases = TaskOutputLeases;
3877
6369
  +}
6370
+ diff --git a/dist/src/tasks-runner/tasks-schedule.d.ts b/dist/src/tasks-runner/tasks-schedule.d.ts
6371
+ index a4ba68a3174aec687ea74d223a77705f65763df4..966ba5c082f6b0bae1f1621a788293cd7731a503 100644
6372
+ --- a/dist/src/tasks-runner/tasks-schedule.d.ts
6373
+ +++ b/dist/src/tasks-runner/tasks-schedule.d.ts
6374
+ @@ -34,6 +34,7 @@ export declare class TasksSchedule {
6375
+ scheduledBatches: Batch[];
6376
+ };
6377
+ nextTask(filter?: (task: Task) => boolean): Task;
6378
+ + nextCriticalTask(filter: (task: Task) => boolean): Task | null;
6379
+ nextBatch(): Batch;
6380
+ getIncompleteTasks(): Task[];
6381
+ private scheduleTasks;
6382
+ diff --git a/dist/src/tasks-runner/tasks-schedule.js b/dist/src/tasks-runner/tasks-schedule.js
6383
+ index 1a9212cb4ba5446e94cf6cb1e5fb913ae3ff3325..df57029bbad70785d5acc365f8326de45d57f8c5 100644
6384
+ --- a/dist/src/tasks-runner/tasks-schedule.js
6385
+ +++ b/dist/src/tasks-runner/tasks-schedule.js
6386
+ @@ -29,6 +29,7 @@ class TasksSchedule {
6387
+ this.estimatedTaskTimings =
6388
+ await this.taskHistory.getEstimatedTaskTimings(Object.values(this.taskGraph.tasks).map((t) => t.target));
6389
+ }
6390
+ + this.criticalPaths = criticalPaths(this.taskGraph, this.estimatedTaskTimings);
6391
+ for (const project of Object.values(this.taskGraph.tasks).map((t) => t.target.project)) {
6392
+ this.projectDependencies[project] ??= (0, project_graph_utils_1.findAllProjectNodeDependencies)(project, this.reverseProjectGraph).length;
6393
+ }
6394
+ @@ -77,6 +78,26 @@ class TasksSchedule {
6395
+ const [taskId] = this.scheduledTasks.splice(idx, 1);
6396
+ return this.taskGraph.tasks[taskId];
6397
+ }
6398
+ + /**
6399
+ + * The ready task that heads the run's longest remaining chain, or null when a running task or
6400
+ + * one not yet ready heads a longer one: the one task the orchestrator dispatches past its
6401
+ + * parallelism while every slot is held (TaskOrchestrator.executeCoordinatorLoop). Scheduled
6402
+ + * tasks are sorted by critical path, so the first that passes `filter` is the longest ready one.
6403
+ + */
6404
+ + nextCriticalTask(filter) {
6405
+ + const first = this.scheduledTasks.find((id) => filter(this.taskGraph.tasks[id]));
6406
+ + if (first === undefined) {
6407
+ + return null;
6408
+ + }
6409
+ + const path = this.criticalPaths[first] ?? 0;
6410
+ + for (const id of [...this.runningTasks, ...Object.keys(this.notScheduledTaskGraph.tasks)]) {
6411
+ + if (id !== first && (this.criticalPaths[id] ?? 0) > path) {
6412
+ + return null;
6413
+ + }
6414
+ + }
6415
+ + this.scheduledTasks.splice(this.scheduledTasks.indexOf(first), 1);
6416
+ + return this.taskGraph.tasks[first];
6417
+ + }
6418
+ nextBatch() {
6419
+ return this.scheduledBatches.length > 0
6420
+ ? this.scheduledBatches.shift()
6421
+ @@ -120,8 +141,12 @@ class TasksSchedule {
6422
+ this.sortScheduledTasks();
6423
+ }
6424
+ sortScheduledTasks() {
6425
+ - // NOTE: sort task by most dependent on first
6426
+ + // The longest remaining chain first (criticalPaths); then Nx's order: most dependents first.
6427
+ this.scheduledTasks.sort((taskId1, taskId2) => {
6428
+ + const pathDifference = (this.criticalPaths?.[taskId2] ?? 0) - (this.criticalPaths?.[taskId1] ?? 0);
6429
+ + if (pathDifference !== 0) {
6430
+ + return pathDifference;
6431
+ + }
6432
+ // First compare the length of task dependencies.
6433
+ const taskDifference = this.reverseTaskDeps[taskId2].length -
6434
+ this.reverseTaskDeps[taskId1].length;
6435
+ @@ -256,3 +281,30 @@ class TasksSchedule {
6436
+ }
6437
+ }
6438
+ exports.TasksSchedule = TasksSchedule;
6439
+ +/**
6440
+ + * Each task's critical path: its estimated duration from task history (plus one, so a task with
6441
+ + * no history still counts as a step of its chain) and the longest critical path among the tasks
6442
+ + * that depend on it. A run ends when its longest chain does, so the ready task heading the longest
6443
+ + * remaining chain starts first.
6444
+ + */
6445
+ +function criticalPaths(taskGraph, timings) {
6446
+ + const dependents = (0, utils_1.calculateReverseDeps)(taskGraph);
6447
+ + const paths = {};
6448
+ + const pathOf = (taskId) => {
6449
+ + if (paths[taskId] !== undefined) {
6450
+ + return paths[taskId];
6451
+ + }
6452
+ + // Nx refuses a cyclic task graph before scheduling; this only keeps a cycle from recursing.
6453
+ + paths[taskId] = 0;
6454
+ + let longest = 0;
6455
+ + for (const dependent of dependents[taskId] ?? []) {
6456
+ + longest = Math.max(longest, pathOf(dependent));
6457
+ + }
6458
+ + paths[taskId] = (timings[taskId] || 0) + 1 + longest;
6459
+ + return paths[taskId];
6460
+ + };
6461
+ + for (const taskId of Object.keys(taskGraph.tasks)) {
6462
+ + pathOf(taskId);
6463
+ + }
6464
+ + return paths;
6465
+ +}
3878
6466
  diff --git a/dist/src/utils/handle-errors.js b/dist/src/utils/handle-errors.js
3879
6467
  index 41697164fa6d52a98ed5cc95be6b24f64f2ced5b..500aca4f73f366ba2910cdbd93963333310313dd 100644
3880
6468
  --- a/dist/src/utils/handle-errors.js
@@ -4038,6 +6626,59 @@ index 7adf7fe48dbde78fa770ad37c5714985d78a7b8c..13e1858e5487a7d308a84cba1f379cde
4038
6626
  if (!path) {
4039
6627
  return null;
4040
6628
  }
6629
+ diff --git a/dist/src/utils/serializable-error.js b/dist/src/utils/serializable-error.js
6630
+ index a1454a531c930ded0fe9de7e219bda61bfba09b2..121a38294d442426bfb165ac46bcc76ccfb1089e 100644
6631
+ --- a/dist/src/utils/serializable-error.js
6632
+ +++ b/dist/src/utils/serializable-error.js
6633
+ @@ -5,29 +5,24 @@ exports.createSerializableError = createSerializableError;
6634
+ * This function transforms an error into an object which can be properly serialized and deserialized to be sent between processes.
6635
+ */
6636
+ function createSerializableError(error) {
6637
+ - const res = {};
6638
+ - Object.getOwnPropertyNames(error).forEach((k) => {
6639
+ - let value = error[k];
6640
+ - // If an error has an error as a property such as cause, it will be transformed into a serializable error
6641
+ - if (typeof value === 'object' && value instanceof Error) {
6642
+ - value = createSerializableError(value);
6643
+ - }
6644
+ - // If an error has an array of errors as a property, they will be transformed into serializable errors
6645
+ - if (Array.isArray(value)) {
6646
+ - value = value.map((v) => {
6647
+ - if (typeof v === 'object' && v instanceof Error) {
6648
+ - return createSerializableError(v);
6649
+ - // Support for AggregateCreateNodesError
6650
+ - }
6651
+ - else if (Array.isArray(v) &&
6652
+ - v.length === 2 &&
6653
+ - v[1] instanceof Error) {
6654
+ - return [v[0], createSerializableError(v[1])];
6655
+ - }
6656
+ - return v;
6657
+ - });
6658
+ + return serializableValue(error, new Set());
6659
+ +}
6660
+ +function serializableValue(value, ancestors) {
6661
+ + if (value === null || typeof value !== 'object') return value;
6662
+ + if (ancestors.has(value)) return { message: '[Circular error]' };
6663
+ + // Keep JSON-native values (e.g. Date) on their existing wire path.
6664
+ + if (!(value instanceof Error) && typeof value.toJSON === 'function') return value;
6665
+ + ancestors.add(value);
6666
+ + try {
6667
+ + if (Array.isArray(value)) return value.map((item) => serializableValue(item, ancestors));
6668
+ + const result = {};
6669
+ + for (const key of Object.getOwnPropertyNames(value)) {
6670
+ + result[key] = serializableValue(value[key], ancestors);
6671
+ }
6672
+ - res[k] = value;
6673
+ - });
6674
+ - return res;
6675
+ + return result;
6676
+ + }
6677
+ + finally {
6678
+ + // Path-scoped, not global: the same child in two branches is not a cycle.
6679
+ + ancestors.delete(value);
6680
+ + }
6681
+ }
4041
6682
  diff --git a/dist/src/utils/sync-generators.js b/dist/src/utils/sync-generators.js
4042
6683
  index d753d65ac4e40380a4dda94a34bc60e0d233f452..796630a927c81afefc9a0ec8d4edb7a274cbe217 100644
4043
6684
  --- a/dist/src/utils/sync-generators.js