@smoothbricks/nx-plugin 0.4.25 → 0.4.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/README.md +1078 -98
  2. package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
  3. package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-bound-failures.js +31 -0
  5. package/dist/bin/smoo-nx-cargo-hash.js +28 -3
  6. package/dist/bin/smoo-nx-exec.js +16 -85
  7. package/dist/bin/smoo-nx-failures.d.ts +3 -0
  8. package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
  9. package/dist/bin/smoo-nx-failures.js +3 -0
  10. package/dist/bin/smoo-nx-nextest-extract.js +3 -2
  11. package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
  12. package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
  13. package/dist/bin/smoo-nx-test-walks.js +34 -0
  14. package/dist/bounded-test-policy.d.ts +7 -1
  15. package/dist/bounded-test-policy.d.ts.map +1 -1
  16. package/dist/bounded-test-policy.js +101 -40
  17. package/dist/build-state-output-policy.d.ts +25 -0
  18. package/dist/build-state-output-policy.d.ts.map +1 -0
  19. package/dist/build-state-output-policy.js +64 -0
  20. package/dist/cargo-closure-input.d.ts +16 -17
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +37 -44
  23. package/dist/cargo-dev-profile.d.ts +37 -0
  24. package/dist/cargo-dev-profile.d.ts.map +1 -0
  25. package/dist/cargo-dev-profile.js +108 -0
  26. package/dist/cargo-source-hash.d.ts +50 -8
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +691 -71
  29. package/dist/cargo-toolchain-policy.d.ts +4 -0
  30. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  31. package/dist/cargo-toolchain-policy.js +5 -0
  32. package/dist/cargo-workspace.d.ts +108 -12
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +299 -18
  35. package/dist/cross-check-policy.d.ts +21 -3
  36. package/dist/cross-check-policy.d.ts.map +1 -1
  37. package/dist/cross-check-policy.js +24 -6
  38. package/dist/disk-command-policy.d.ts +7 -0
  39. package/dist/disk-command-policy.d.ts.map +1 -0
  40. package/dist/disk-command-policy.js +98 -0
  41. package/dist/ensure-built-receipt.d.ts +25 -0
  42. package/dist/ensure-built-receipt.d.ts.map +1 -0
  43. package/dist/ensure-built-receipt.js +261 -0
  44. package/dist/ensure-built-witness.d.ts +57 -0
  45. package/dist/ensure-built-witness.d.ts.map +1 -0
  46. package/dist/ensure-built-witness.js +412 -0
  47. package/dist/ensure-built.d.ts +213 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +624 -177
  50. package/dist/eslint-file-set.d.ts +7 -0
  51. package/dist/eslint-file-set.d.ts.map +1 -0
  52. package/dist/eslint-file-set.js +12 -0
  53. package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
  54. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
  55. package/dist/executors/bounded-exec/cpu-pool.js +379 -0
  56. package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
  57. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
  58. package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
  59. package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
  60. package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/disk-lease.js +91 -0
  62. package/dist/executors/bounded-exec/executor.d.ts +45 -2
  63. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  64. package/dist/executors/bounded-exec/executor.js +447 -52
  65. package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
  66. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
  67. package/dist/executors/bounded-exec/gateway-lease.js +143 -0
  68. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
  69. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
  70. package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
  71. package/dist/executors/bounded-exec/progress.d.ts +35 -0
  72. package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
  73. package/dist/executors/bounded-exec/progress.js +79 -0
  74. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
  75. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
  76. package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
  77. package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
  78. package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
  79. package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
  80. package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
  81. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
  82. package/dist/executors/bounded-exec/ram-temp.js +1528 -0
  83. package/dist/executors/bounded-exec/schema.d.ts +29 -11
  84. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  85. package/dist/executors/bounded-exec/verdict.d.ts +162 -0
  86. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/verdict.js +318 -0
  88. package/dist/executors/shard-timings/executor.d.ts +14 -0
  89. package/dist/executors/shard-timings/executor.d.ts.map +1 -0
  90. package/dist/executors/shard-timings/executor.js +25 -0
  91. package/dist/executors/shard-timings/schema.d.ts +4 -0
  92. package/dist/executors/shard-timings/schema.d.ts.map +1 -0
  93. package/dist/executors/shard-timings/schema.js +1 -0
  94. package/dist/failure-summary.d.ts +5 -0
  95. package/dist/failure-summary.d.ts.map +1 -0
  96. package/dist/failure-summary.js +118 -0
  97. package/dist/generators/create-package/generator.d.ts.map +1 -1
  98. package/dist/generators/create-package/generator.js +6 -2
  99. package/dist/index.d.ts +14 -2
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +435 -230
  102. package/dist/managed-assets.d.ts +6 -0
  103. package/dist/managed-assets.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.d.ts +13 -0
  105. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  106. package/dist/managed-files/ci-workflow.js +44 -13
  107. package/dist/managed-files/files.d.ts +7 -1
  108. package/dist/managed-files/files.d.ts.map +1 -1
  109. package/dist/managed-files/files.js +62 -8
  110. package/dist/managed-files/json-edit.d.ts +21 -0
  111. package/dist/managed-files/json-edit.d.ts.map +1 -0
  112. package/dist/managed-files/json-edit.js +295 -0
  113. package/dist/managed-files/managed-format.d.ts +33 -1
  114. package/dist/managed-files/managed-format.d.ts.map +1 -1
  115. package/dist/managed-files/managed-format.js +82 -2
  116. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  117. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  119. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  120. package/dist/managed-files/publish-workflow.js +231 -48
  121. package/dist/managed-files/tree.d.ts +5 -0
  122. package/dist/managed-files/tree.d.ts.map +1 -1
  123. package/dist/managed-files/tree.js +5 -3
  124. package/dist/manifest-hash.js +3 -2
  125. package/dist/nextest-code-bound.d.ts +62 -0
  126. package/dist/nextest-code-bound.d.ts.map +1 -0
  127. package/dist/nextest-code-bound.js +154 -0
  128. package/dist/nextest-extraction.d.ts +24 -8
  129. package/dist/nextest-extraction.d.ts.map +1 -1
  130. package/dist/nextest-extraction.js +42 -7
  131. package/dist/package-target-policy.d.ts.map +1 -1
  132. package/dist/package-target-policy.js +3 -5
  133. package/dist/release-config-policy.d.ts.map +1 -1
  134. package/dist/release-config-policy.js +3 -5
  135. package/dist/runtime-input.d.ts +14 -0
  136. package/dist/runtime-input.d.ts.map +1 -0
  137. package/dist/runtime-input.js +15 -0
  138. package/dist/rust-tokens.d.ts +11 -0
  139. package/dist/rust-tokens.d.ts.map +1 -0
  140. package/dist/rust-tokens.js +81 -0
  141. package/dist/task-results.d.ts +6 -0
  142. package/dist/task-results.d.ts.map +1 -0
  143. package/dist/task-results.js +61 -0
  144. package/dist/test-program.d.ts +54 -0
  145. package/dist/test-program.d.ts.map +1 -0
  146. package/dist/test-program.js +292 -0
  147. package/dist/test-timings.d.ts +71 -0
  148. package/dist/test-timings.d.ts.map +1 -0
  149. package/dist/test-timings.js +393 -0
  150. package/dist/test-walk-policy.d.ts +103 -0
  151. package/dist/test-walk-policy.d.ts.map +1 -0
  152. package/dist/test-walk-policy.js +657 -0
  153. package/dist/testing.d.ts +177 -0
  154. package/dist/testing.d.ts.map +1 -0
  155. package/dist/testing.js +824 -0
  156. package/dist/type-guards.d.ts +3 -0
  157. package/dist/type-guards.d.ts.map +1 -0
  158. package/dist/type-guards.js +4 -0
  159. package/dist/typecheck-test-policy.d.ts +8 -5
  160. package/dist/typecheck-test-policy.d.ts.map +1 -1
  161. package/dist/typecheck-test-policy.js +113 -100
  162. package/dist/workspace-config-policy.d.ts +8 -0
  163. package/dist/workspace-config-policy.d.ts.map +1 -1
  164. package/dist/workspace-config-policy.js +175 -63
  165. package/dist/workspace-index.d.ts +12 -0
  166. package/dist/workspace-index.d.ts.map +1 -0
  167. package/dist/workspace-index.js +17 -0
  168. package/dist/workspace-manifest.d.ts +37 -1
  169. package/dist/workspace-manifest.d.ts.map +1 -1
  170. package/executors.json +5 -0
  171. package/loader.js +13 -11
  172. package/managed/raw/envrc +33 -62
  173. package/managed/raw/git-format-staged.yml +30 -35
  174. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  175. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  176. package/managed/raw/tooling/direnv/developer-links.ts +607 -17
  177. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -92
  178. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  179. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  180. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  181. package/managed/raw/tooling/direnv/repo-path +14 -2
  182. package/managed/raw/tooling/direnv/setup-environment.ts +1009 -118
  183. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  184. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  185. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  186. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  187. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  188. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  189. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  190. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  191. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  192. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  194. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  196. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  197. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  198. package/managed/templates/github/workflows/managed-files.yml +1 -2
  199. package/nextest.toml +8 -75
  200. package/package.json +75 -13
  201. package/runtime-input.sh +81 -0
  202. package/src/__tests__/counted-cargo-process.ts +286 -0
  203. package/src/__tests__/counted-cargo.ts +730 -0
  204. package/src/__tests__/fixture-cargo-home.ts +36 -0
  205. package/src/__tests__/fixture-nx-env.ts +157 -2
  206. package/src/__tests__/progress-clock.ts +43 -0
  207. package/src/__tests__/runtime-input.ts +23 -0
  208. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  209. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  210. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  211. package/src/bin/smoo-nx-exec.ts +17 -86
  212. package/src/bin/smoo-nx-failures.ts +4 -0
  213. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  214. package/src/bin/smoo-nx-test-walks.ts +42 -0
  215. package/src/bounded-test-policy.test.ts +268 -22
  216. package/src/bounded-test-policy.ts +114 -42
  217. package/src/build-state-output-policy.test.ts +115 -0
  218. package/src/build-state-output-policy.ts +90 -0
  219. package/src/bun-bootstrap.test.ts +57 -3
  220. package/src/bun-file-packages.test.ts +47 -0
  221. package/src/cargo-closure-input.test.ts +432 -62
  222. package/src/cargo-closure-input.ts +51 -57
  223. package/src/cargo-dev-profile.ts +137 -0
  224. package/src/cargo-resolve-cache.test.ts +1044 -0
  225. package/src/cargo-source-hash.test.ts +203 -93
  226. package/src/cargo-source-hash.ts +762 -75
  227. package/src/cargo-toolchain-policy.ts +6 -0
  228. package/src/cargo-workspace.test.ts +320 -0
  229. package/src/cargo-workspace.ts +344 -18
  230. package/src/cross-check-policy.test.ts +59 -1
  231. package/src/cross-check-policy.ts +25 -6
  232. package/src/disk-command-policy.test.ts +61 -0
  233. package/src/disk-command-policy.ts +107 -0
  234. package/src/ensure-built-receipt.test.ts +184 -0
  235. package/src/ensure-built-receipt.ts +263 -0
  236. package/src/ensure-built-witness.test.ts +344 -0
  237. package/src/ensure-built-witness.ts +252 -0
  238. package/src/ensure-built.test.ts +1043 -483
  239. package/src/ensure-built.ts +870 -197
  240. package/src/eslint-file-set.ts +9 -0
  241. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  242. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  243. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  244. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  245. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  246. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  247. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  248. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  249. package/src/executors/bounded-exec/executor.ts +565 -67
  250. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  251. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  252. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  253. package/src/executors/bounded-exec/progress.test.ts +79 -0
  254. package/src/executors/bounded-exec/progress.ts +118 -0
  255. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  256. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  257. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  258. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  259. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  260. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  261. package/src/executors/bounded-exec/schema.json +11 -5
  262. package/src/executors/bounded-exec/schema.ts +27 -11
  263. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  264. package/src/executors/bounded-exec/verdict.ts +433 -0
  265. package/src/executors/shard-timings/executor.ts +36 -0
  266. package/src/executors/shard-timings/schema.json +21 -0
  267. package/src/executors/shard-timings/schema.ts +4 -0
  268. package/src/failure-summary.ts +119 -0
  269. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  270. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  271. package/src/generators/create-package/generator.test.ts +6 -0
  272. package/src/generators/create-package/generator.ts +6 -2
  273. package/src/host-controller-wiring.test.ts +31 -0
  274. package/src/index.test.ts +1070 -360
  275. package/src/index.ts +524 -255
  276. package/src/linked-snapshots.nx.test.ts +172 -0
  277. package/src/managed-assets.ts +12 -0
  278. package/src/managed-files/ci-workflow.ts +54 -13
  279. package/src/managed-files/files.ts +65 -9
  280. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  281. package/src/managed-files/generator.test.ts +60 -1
  282. package/src/managed-files/json-edit.test.ts +216 -0
  283. package/src/managed-files/json-edit.ts +290 -0
  284. package/src/managed-files/managed-format-json.test.ts +308 -0
  285. package/src/managed-files/managed-format.ts +90 -2
  286. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  287. package/src/managed-files/publish-workflow.ts +285 -67
  288. package/src/managed-files/repo-path.test.ts +129 -0
  289. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  290. package/src/managed-files/tree.ts +11 -3
  291. package/src/manifest-hash.ts +3 -2
  292. package/src/nextest-code-bound.test.ts +155 -0
  293. package/src/nextest-code-bound.ts +195 -0
  294. package/src/nextest-extraction.test.ts +37 -5
  295. package/src/nextest-extraction.ts +51 -9
  296. package/src/nextest-partitions.test.ts +495 -0
  297. package/src/nx-cache-bound.test.ts +109 -0
  298. package/src/nx-cache-restore-mtime.test.ts +170 -0
  299. package/src/nx-daemon-claim.test.ts +277 -0
  300. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  301. package/src/nx-daemon-idle-policy.test.ts +51 -0
  302. package/src/nx-daemon-ignore-files.test.ts +210 -0
  303. package/src/nx-daemon-root-normalization.test.ts +105 -0
  304. package/src/nx-daemon-validity.test.ts +925 -0
  305. package/src/nx-dot-input-policy.test.ts +353 -0
  306. package/src/nx-failure-life-cycles.test.ts +207 -0
  307. package/src/nx-files-archive-racy.test.ts +97 -0
  308. package/src/nx-foreign-workspace-env.test.ts +329 -0
  309. package/src/nx-incomplete-run-summary.test.ts +70 -0
  310. package/src/nx-plugin-load-errors.test.ts +120 -0
  311. package/src/nx-project-resolution-cache.test.ts +176 -0
  312. package/src/nx-raw-inputs.test.ts +166 -0
  313. package/src/nx-resident-graph-workers.test.ts +112 -0
  314. package/src/nx-run-information.test.ts +118 -0
  315. package/src/nx-skipped-task-exit.test.ts +180 -0
  316. package/src/nx-state-location.test.ts +63 -0
  317. package/src/nx-store-resolution.test.ts +99 -0
  318. package/src/nx-task-graph-cycles.test.ts +148 -0
  319. package/src/nx-task-output-lease.test.ts +414 -0
  320. package/src/nx-use-local.test.ts +103 -0
  321. package/src/package-target-policy.ts +3 -6
  322. package/src/release-config-policy.ts +3 -6
  323. package/src/runtime-input.test.ts +215 -0
  324. package/src/runtime-input.ts +17 -0
  325. package/src/rust-tokens.ts +78 -0
  326. package/src/serial-group-wiring.test.ts +326 -0
  327. package/src/task-history-namespace.test.ts +181 -0
  328. package/src/task-results.test.ts +226 -0
  329. package/src/task-results.ts +75 -0
  330. package/src/test-program.test.ts +303 -0
  331. package/src/test-program.ts +341 -0
  332. package/src/test-timings.test.ts +240 -0
  333. package/src/test-timings.ts +311 -0
  334. package/src/test-walk-policy.test.ts +585 -0
  335. package/src/test-walk-policy.ts +731 -0
  336. package/src/testing.test.ts +509 -0
  337. package/src/testing.ts +922 -0
  338. package/src/type-guards.ts +4 -0
  339. package/src/typecheck-test-policy.test.ts +496 -11
  340. package/src/typecheck-test-policy.ts +137 -103
  341. package/src/workspace-config-policy.test.ts +212 -13
  342. package/src/workspace-config-policy.ts +101 -73
  343. package/src/workspace-index.ts +18 -0
  344. package/src/workspace-manifest.ts +41 -1
  345. package/test-timings.json +85 -28
  346. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -615
  347. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
  348. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -1,16 +1,44 @@
1
- import { execFile, execFileSync, spawnSync } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
3
- import { realpathSync, statSync } from 'node:fs';
4
- import { lstat, readdir, readFile, readlink, realpath } from 'node:fs/promises';
5
- import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
1
+ import { type ChildProcess, type ChildProcessByStdio, execFileSync, spawn, spawnSync } from 'node:child_process';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
+ import { channel } from 'node:diagnostics_channel';
4
+ import { constants, existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
5
+ import {
6
+ access,
7
+ lstat,
8
+ mkdir,
9
+ readdir,
10
+ readFile,
11
+ readlink,
12
+ realpath,
13
+ rename,
14
+ rm,
15
+ stat,
16
+ writeFile,
17
+ } from 'node:fs/promises';
18
+ import { createRequire } from 'node:module';
19
+ import { homedir } from 'node:os';
20
+ import { delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
21
+ import type { Readable } from 'node:stream';
22
+ import native from 'nx/src/native/index.js';
23
+ import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
24
+ import { CARGO_TOOLCHAIN_PIN_INPUTS } from './cargo-toolchain-policy.js';
25
+ import { rustTokens } from './rust-tokens.js';
26
+
27
+ const { IS_WASM, WorkspaceContext } = native;
28
+
29
+ const cargoResolveEvents = channel('@smoothbricks/nx-plugin:cargo-resolve');
6
30
 
7
31
  /** The fields of `cargo metadata --format-version 1` this module reads. */
8
32
  interface CargoMetadata {
9
33
  packages: {
10
34
  id: string;
35
+ name: string;
36
+ version: string;
11
37
  source: string | null;
12
38
  manifest_path: string;
13
39
  targets: { src_path: string }[];
40
+ /** Added by the cache after Cargo identifies each local package's governing workspace. */
41
+ governing_manifest_path?: string;
14
42
  }[];
15
43
  resolve: { nodes: { id: string; deps: { pkg: string }[] }[] } | null;
16
44
  workspace_members: string[];
@@ -38,11 +66,16 @@ function parseCargoMetadata(json: string): CargoMetadata {
38
66
  return {
39
67
  packages: list(member(document, 'packages'), 'packages').map((pkg) => ({
40
68
  id: text(member(pkg, 'id'), 'package id'),
69
+ name: text(member(pkg, 'name'), 'package name'),
70
+ version: text(member(pkg, 'version'), 'package version'),
41
71
  source: member(pkg, 'source') === null ? null : text(member(pkg, 'source'), 'package source'),
42
72
  manifest_path: text(member(pkg, 'manifest_path'), 'package manifest_path'),
43
73
  targets: list(member(pkg, 'targets'), 'package targets').map((target) => ({
44
74
  src_path: text(member(target, 'src_path'), 'target src_path'),
45
75
  })),
76
+ ...(member(pkg, 'governing_manifest_path') === undefined
77
+ ? {}
78
+ : { governing_manifest_path: text(member(pkg, 'governing_manifest_path'), 'governing manifest') }),
46
79
  })),
47
80
  resolve:
48
81
  resolveGraph === null
@@ -65,10 +98,13 @@ function parseCargoMetadata(json: string): CargoMetadata {
65
98
  /** A mutable (path) package in Cargo's locked resolve. Every path is canonical. */
66
99
  export interface LocalCargoPackage {
67
100
  readonly id: string;
101
+ readonly name: string;
102
+ readonly version: string;
68
103
  readonly directory: string;
69
104
  readonly manifest: string;
70
105
  /** Every target's source file: library, binaries, tests, build script. A target may live outside `directory`. */
71
106
  readonly sources: readonly string[];
107
+ readonly governingManifest: string;
72
108
  }
73
109
 
74
110
  /** What locked, offline `cargo metadata` reports about one Cargo workspace's mutable packages. */
@@ -92,6 +128,67 @@ export interface CargoPathInputsOptions {
92
128
  closure?: string;
93
129
  }
94
130
 
131
+ /**
132
+ * Nx workers handle termination by exiting, so their exit hook kills owned
133
+ * Cargo groups. Standalone callers need signal forwarding after detaching
134
+ * Cargo from the terminal; handlers are installed only where the caller has
135
+ * not already chosen a signal disposition, and removed after the last child.
136
+ * The hooks are in place before the first child is spawned: a child runs as
137
+ * soon as it is spawned, and a signal that reached this process before its
138
+ * hooks would end it on the default action with Cargo's group left running.
139
+ * Uncatchable SIGKILL cannot execute either hook.
140
+ */
141
+ const cargoChildren = new Set<ChildProcess>();
142
+
143
+ function killCargoChild(child: ChildProcess): void {
144
+ if (process.platform === 'win32' || child.pid === undefined) child.kill('SIGKILL');
145
+ else {
146
+ try {
147
+ process.kill(-child.pid, 'SIGKILL');
148
+ } catch (error) {
149
+ if (!(error instanceof Error && 'code' in error && error.code === 'ESRCH')) throw error;
150
+ }
151
+ }
152
+ }
153
+
154
+ function killCargoChildren(): void {
155
+ for (const child of cargoChildren) killCargoChild(child);
156
+ }
157
+
158
+ type CargoSignal = 'SIGINT' | 'SIGTERM' | 'SIGHUP';
159
+ const forwardedSignals = new Set<CargoSignal>();
160
+ const cargoSignalHandlers: Record<CargoSignal, () => void> = {
161
+ SIGINT: () => forwardCargoSignal('SIGINT'),
162
+ SIGTERM: () => forwardCargoSignal('SIGTERM'),
163
+ SIGHUP: () => forwardCargoSignal('SIGHUP'),
164
+ };
165
+
166
+ function removeCargoSignalHandlers(): void {
167
+ for (const signal of forwardedSignals) process.removeListener(signal, cargoSignalHandlers[signal]);
168
+ forwardedSignals.clear();
169
+ }
170
+
171
+ function forwardCargoSignal(signal: CargoSignal): void {
172
+ killCargoChildren();
173
+ removeCargoSignalHandlers();
174
+ process.kill(process.pid, signal);
175
+ }
176
+
177
+ function ownCargoChildren(): void {
178
+ process.on('exit', killCargoChildren);
179
+ for (const signal of Object.keys(cargoSignalHandlers)) {
180
+ if (signal !== 'SIGINT' && signal !== 'SIGTERM' && signal !== 'SIGHUP') continue;
181
+ if (process.listenerCount(signal) !== 0) continue;
182
+ process.on(signal, cargoSignalHandlers[signal]);
183
+ forwardedSignals.add(signal);
184
+ }
185
+ }
186
+
187
+ function disownCargoChildren(): void {
188
+ process.removeListener('exit', killCargoChildren);
189
+ removeCargoSignalHandlers();
190
+ }
191
+
95
192
  /**
96
193
  * Cargo's stdout, or its failure with Cargo's stderr in the message. Not
97
194
  * `promisify` from `node:util`: importing that module makes Node probe color
@@ -100,12 +197,78 @@ export interface CargoPathInputsOptions {
100
197
  * under its own pid, which a runtime input would hash as a new digest on
101
198
  * every run.
102
199
  */
103
- function runCargo(args: readonly string[], cwd: string): Promise<string> {
104
- // The executor form: the workspace compiles against es2022, which has no `Promise.withResolvers`.
200
+ function runCargo(args: readonly string[], cwd: string, boundMs: number = CARGO_RESOLVE_BOUND_MS): Promise<string> {
201
+ // spawn forwards detached groups; execFile does not. The public ES2022
202
+ // library API requires the constructor form, not Promise.withResolvers.
105
203
  return new Promise((settle, reject) => {
106
- execFile('cargo', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }, (error, stdout) =>
107
- error === null ? settle(stdout) : reject(error),
204
+ // Hooks first: a signal caught from here on runs its listener after this
205
+ // synchronous body, by which time the child is in `cargoChildren`.
206
+ if (cargoChildren.size === 0) ownCargoChildren();
207
+ let child: ChildProcessByStdio<null, Readable, Readable>;
208
+ try {
209
+ child = spawn('cargo', args, {
210
+ cwd,
211
+ detached: process.platform !== 'win32',
212
+ stdio: ['ignore', 'pipe', 'pipe'],
213
+ });
214
+ } catch (error) {
215
+ if (cargoChildren.size === 0) disownCargoChildren();
216
+ throw error;
217
+ }
218
+ cargoChildren.add(child);
219
+ let stdout = '';
220
+ let stderr = '';
221
+ let stdoutBytes = 0;
222
+ let stderrBytes = 0;
223
+ let failure: Error | undefined;
224
+ const fail = (error: Error): void => {
225
+ failure ??= error;
226
+ killCargoChild(child);
227
+ };
228
+ const timer = setTimeout(
229
+ () =>
230
+ fail(
231
+ new Error(
232
+ `cargo ${args.join(' ')} in ${cwd} did not finish within ${boundMs}ms; killed it, ` +
233
+ 'because an unbounded Cargo wait hangs every project-graph construction with no output',
234
+ ),
235
+ ),
236
+ boundMs,
108
237
  );
238
+ child.stdout.setEncoding('utf8');
239
+ child.stderr.setEncoding('utf8');
240
+ child.stdout.on('data', (chunk: string) => {
241
+ if (failure !== undefined) return;
242
+ stdoutBytes += Buffer.byteLength(chunk);
243
+ if (stdoutBytes > 64 * 1024 * 1024) {
244
+ fail(new Error('Cargo stdout exceeded 64 MiB'));
245
+ return;
246
+ }
247
+ stdout += chunk;
248
+ });
249
+ child.stderr.on('data', (chunk: string) => {
250
+ if (failure !== undefined) return;
251
+ stderrBytes += Buffer.byteLength(chunk);
252
+ if (stderrBytes > 64 * 1024 * 1024) {
253
+ fail(new Error('Cargo stderr exceeded 64 MiB'));
254
+ return;
255
+ }
256
+ stderr += chunk;
257
+ });
258
+ child.on('error', (error) => {
259
+ failure ??= error;
260
+ });
261
+ child.stdout.on('error', fail);
262
+ child.stderr.on('error', fail);
263
+ child.on('close', (code, signal) => {
264
+ clearTimeout(timer);
265
+ cargoChildren.delete(child);
266
+ if (cargoChildren.size === 0) disownCargoChildren();
267
+ if (failure !== undefined) reject(failure);
268
+ else if (code !== 0) {
269
+ reject(new Error(`cargo ${args.join(' ')} failed (${signal ?? code}): ${stderr.trim()}`));
270
+ } else settle(stdout);
271
+ });
109
272
  });
110
273
  }
111
274
 
@@ -114,6 +277,7 @@ export const HASH_SKIPPED_DIRECTORIES = [
114
277
  'target',
115
278
  '.git',
116
279
  'node_modules',
280
+ '.nx',
117
281
  '.cache',
118
282
  '.cowshed',
119
283
  '.fseventsd',
@@ -136,8 +300,8 @@ export const CARGO_ANCESTOR_INPUTS = ['Cargo.toml', '.cargo/config', '.cargo/con
136
300
  * directory actually depend on, so an edit to an unrelated workspace member
137
301
  * leaves the digest alone.
138
302
  *
139
- * This covers Rust sources and Cargo configuration, matching the plugin's
140
- * in-workspace Rust inputs. Build scripts' other data/environment inputs remain
303
+ * This covers Rust sources, literal compiled-in files and Cargo configuration,
304
+ * matching the plugin's in-workspace Rust inputs. Build scripts' other data/environment inputs remain
141
305
  * explicit Nx inputs, just as they are for in-workspace crates. Files git
142
306
  * ignores beneath a package are left out, like Nx's own file inputs: a
143
307
  * generated source is its producer's output, which the consuming target hashes
@@ -148,50 +312,474 @@ export const CARGO_ANCESTOR_INPUTS = ['Cargo.toml', '.cargo/config', '.cargo/con
148
312
  // on stderr ("Blocking waiting for file lock on package cache") a
149
313
  // timing-dependent number of times when Nx hashes many tasks at once, which
150
314
  // made one unchanged tree produce four different task hashes per run. Cargo's
151
- // stderr is kept for the failure path only, where execFile attaches it to the
315
+ // stderr is kept for the failure path only, where runCargo includes it in the
152
316
  // rejected error. Git runs under the same rule.
153
317
  const CHILD_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
154
318
 
155
- /** Locked, offline `cargo metadata` for the workspace `manifestPath` names, run from `cwd`. */
156
- export async function readCargoResolve(manifestPath: string, cwd: string): Promise<CargoResolve> {
157
- const stdout = await runCargo(
158
- ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
159
- cwd,
160
- );
161
- const metadata = parseCargoMetadata(stdout);
162
- const local = await Promise.all(
163
- metadata.packages
164
- .filter((pkg) => pkg.source === null)
165
- .map(async (pkg): Promise<LocalCargoPackage> => {
166
- const manifest = await realpath(pkg.manifest_path);
167
- const sources = await Promise.all(pkg.targets.map((target) => realpath(target.src_path)));
168
- return { id: pkg.id, directory: dirname(manifest), manifest, sources };
169
- }),
319
+ /**
320
+ * Git reads only the repository, never the operator's home (cowshed
321
+ * `04_sandbox.md`, runtime inputs). A host client's runtime input runs in a
322
+ * workspace's sandboxed daemon with the host's HOME: git dies on the global
323
+ * configuration the sandbox denies, and its global ignore file
324
+ * (`~/.config/git/ignore`, read even without a global configuration) would
325
+ * decide which sources the key covers. Neither belongs to the tree.
326
+ */
327
+ const ISOLATED_GIT_ARGS = ['-c', 'core.excludesFile=/dev/null'] as const;
328
+
329
+ function isolatedGitEnv(): NodeJS.ProcessEnv {
330
+ return { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
331
+ }
332
+
333
+ /** A Cargo resolution refusal: no stale closure or runtime whole-workspace substitute is safe. */
334
+ export class CargoMetadataError extends Error {
335
+ readonly manifestPath: string;
336
+
337
+ constructor(manifestPath: string, cause: unknown) {
338
+ const detail =
339
+ cause instanceof Error
340
+ ? `${cause.name}: ${cause.message}`
341
+ : typeof cause === 'object' && cause !== null && 'message' in cause
342
+ ? String(cause.message)
343
+ : String(cause);
344
+ super(`Locked offline Cargo resolution failed for ${manifestPath}: ${detail}`, { cause });
345
+ this.name = 'CargoMetadataError';
346
+ this.manifestPath = manifestPath;
347
+ }
348
+ }
349
+
350
+ interface ResolveCache {
351
+ readonly key: string;
352
+ readonly cargo: CargoResolve;
353
+ }
354
+ interface ResolveState {
355
+ ready: Promise<void>;
356
+ cached: ResolveCache | null;
357
+ pending?: { readonly key: string; readonly promise: Promise<CargoResolve> };
358
+ }
359
+ const resolves = new Map<string, ResolveState>();
360
+ // v2 retains package name/version for checkout-independent artifact input identities.
361
+ const RESOLVE_CACHE_SCHEMA = 'cargo-resolve-v2';
362
+
363
+ /**
364
+ * The longest one Cargo child, or one wait for a foreign resolve flight's lock, may take.
365
+ * Both waits sit on every project-graph construction, so an unbounded one is a silent,
366
+ * output-free hang of every gate that resolves the graph (a stopped lock holder, a Cargo
367
+ * process convoy): at the bound the wait fails, naming what it waited on and for how long.
368
+ */
369
+ export const CARGO_RESOLVE_BOUND_MS = 300_000;
370
+
371
+ /**
372
+ * The full manifest SET, not merely the last closure: adding a glob workspace
373
+ * member must invalidate resolution even when Cargo.lock did not change.
374
+ * Graph callers supply Nx's already-current index; standalone hash callers
375
+ * take a fresh native snapshot. File contents below are read directly so an
376
+ * edit within Nx's mtime granularity cannot reuse a stale resolution.
377
+ */
378
+ function resolutionFiles(
379
+ root: string,
380
+ manifest: string,
381
+ cargo: CargoResolve | null,
382
+ indexed: ReadonlySet<string>,
383
+ ): ReadonlySet<string> {
384
+ const paths = new Set<string>([manifest]);
385
+ for (const file of indexed) paths.add(resolve(root, file));
386
+ for (const pkg of cargo?.local ?? []) {
387
+ paths.add(pkg.manifest);
388
+ paths.add(pkg.governingManifest);
389
+ }
390
+ if (cargo !== null) paths.add(join(cargo.root, 'Cargo.toml'));
391
+ const directories = new Set<string>([root]);
392
+ for (const file of paths) directories.add(dirname(file));
393
+ for (const directory of directories) {
394
+ paths.add(join(directory, 'Cargo.lock'));
395
+ for (let ancestor = directory; ; ancestor = dirname(ancestor)) {
396
+ paths.add(join(ancestor, '.cargo/config'));
397
+ paths.add(join(ancestor, '.cargo/config.toml'));
398
+ paths.add(join(ancestor, 'rust-toolchain'));
399
+ paths.add(join(ancestor, 'rust-toolchain.toml'));
400
+ paths.add(join(ancestor, 'Cargo.toml'));
401
+ if (dirname(ancestor) === ancestor) break;
402
+ }
403
+ }
404
+ for (const pin of CARGO_TOOLCHAIN_PIN_INPUTS) paths.add(pin.replace('{workspaceRoot}', root));
405
+ const cargoHome = resolve(process.env.CARGO_HOME ?? join(homedir(), '.cargo'));
406
+ paths.add(join(cargoHome, 'config'));
407
+ paths.add(join(cargoHome, 'config.toml'));
408
+ return paths;
409
+ }
410
+
411
+ /** Toolchain selection can change even while the declared pin's bytes do not. */
412
+ async function cargoExecutable(): Promise<string> {
413
+ for (const directory of (process.env.PATH ?? '').split(delimiter)) {
414
+ const executable = resolve(directory, process.platform === 'win32' ? 'cargo.exe' : 'cargo');
415
+ try {
416
+ await access(executable, constants.X_OK);
417
+ return await realpath(executable);
418
+ } catch (error) {
419
+ if (
420
+ !(
421
+ error instanceof Error &&
422
+ 'code' in error &&
423
+ (error.code === 'ENOENT' || error.code === 'EACCES' || error.code === 'ENOTDIR')
424
+ )
425
+ )
426
+ throw error;
427
+ }
428
+ }
429
+ return '<cargo-not-on-PATH>';
430
+ }
431
+
432
+ async function resolutionKey(
433
+ root: string,
434
+ manifest: string,
435
+ cargo: CargoResolve | null,
436
+ indexed: ReadonlySet<string>,
437
+ ): Promise<string> {
438
+ const cargoHome = resolve(process.env.CARGO_HOME ?? join(homedir(), '.cargo'));
439
+ const executable = await cargoExecutable();
440
+ const hash = createHash('sha256').update(
441
+ `${RESOLVE_CACHE_SCHEMA}\0${root}\0${manifest}\0${cargoHome}\0${executable}\0${process.env.RUSTUP_TOOLCHAIN ?? ''}\0${process.env.RUSTUP_HOME ?? ''}\0`,
170
442
  );
443
+ for (const file of [...resolutionFiles(root, manifest, cargo, indexed)].sort()) {
444
+ hash.update(file).update('\0');
445
+ try {
446
+ const [bytes, canonical] = await Promise.all([readFile(file), realpath(file)]);
447
+ hash.update(`${canonical}\0${bytes.length}\0`).update(bytes);
448
+ } catch (error) {
449
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error;
450
+ hash.update('missing');
451
+ }
452
+ hash.update('\0');
453
+ }
454
+ return hash.digest('hex');
455
+ }
456
+
457
+ function cargoFromMetadata(metadata: CargoMetadata): CargoResolve {
171
458
  return {
172
- root: await realpath(metadata.workspace_root),
459
+ root: metadata.workspace_root,
173
460
  members: new Set(metadata.workspace_members),
174
- local,
461
+ local: metadata.packages
462
+ .filter((pkg) => pkg.source === null)
463
+ .map((pkg) => {
464
+ if (pkg.governing_manifest_path === undefined) throw new Error('Cargo cache omitted a governing manifest');
465
+ return {
466
+ id: pkg.id,
467
+ name: pkg.name,
468
+ version: pkg.version,
469
+ directory: dirname(pkg.manifest_path),
470
+ manifest: pkg.manifest_path,
471
+ sources: pkg.targets.map((target) => target.src_path),
472
+ governingManifest: pkg.governing_manifest_path,
473
+ };
474
+ }),
175
475
  edges:
176
476
  metadata.resolve === null
177
477
  ? null
178
- : new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps.map((dependency) => dependency.pkg)])),
478
+ : new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps.map((dep) => dep.pkg)])),
179
479
  };
180
480
  }
181
481
 
482
+ async function loadResolveCache(file: string): Promise<ResolveCache | null> {
483
+ let content: string;
484
+ try {
485
+ content = await readFile(file, 'utf8');
486
+ } catch (error) {
487
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return null;
488
+ throw error;
489
+ }
490
+ try {
491
+ const newline = content.indexOf('\n');
492
+ const key = content.slice(0, newline);
493
+ if (newline === -1 || !/^[a-f0-9]{64}$/.test(key)) throw new Error('invalid content-key header');
494
+ const metadata = parseCargoMetadata(content.slice(newline + 1));
495
+ return { key, cargo: cargoFromMetadata(metadata) };
496
+ } catch (error) {
497
+ process.stderr.write(
498
+ `@smoothbricks/nx-plugin: discarding corrupt Cargo resolution cache ${file}: ${error instanceof Error ? error.message : String(error)}\n`,
499
+ );
500
+ return null;
501
+ }
502
+ }
503
+
182
504
  /**
183
- * The canonical root manifest of the Cargo workspace that governs `pkg`.
184
- * Metadata's `workspace_root` describes only the invoking workspace, which is
185
- * the answer for its own members. Any other path package asks Cargo, which
186
- * resolves an explicit `package.workspace` as well as the ancestor search.
505
+ * One content-keyed resolution per Cargo workspace, across concurrent graph
506
+ * calls and workers. A bounded child owns Nx's kernel FileLock while the
507
+ * resolution runs; in-process callers join before asking for that lease.
508
+ * A changed key queues behind the old child instead of receiving its stale answer.
187
509
  */
188
- export async function governingManifest(cargo: CargoResolve, pkg: LocalCargoPackage, cwd: string): Promise<string> {
189
- if (cargo.members.has(pkg.id)) return realpath(join(cargo.root, 'Cargo.toml'));
510
+ export async function readCargoResolve(
511
+ manifestPath: string,
512
+ cwd: string,
513
+ indexed?: ReadonlySet<string>,
514
+ boundMs: number = CARGO_RESOLVE_BOUND_MS,
515
+ ): Promise<CargoResolve> {
516
+ let manifest = resolve(manifestPath);
517
+ try {
518
+ const root = await realpath(cwd);
519
+ manifest = await realpath(manifest);
520
+ const index =
521
+ indexed ?? new Set(new WorkspaceContext(root, workspaceDataDirectoryForWorkspace(root)).glob(['**/Cargo.toml']));
522
+ const identity = createHash('sha256').update(`${RESOLVE_CACHE_SCHEMA}\0${root}\0${manifest}`).digest('hex');
523
+ const directory = join(workspaceDataDirectoryForWorkspace(root), 'cargo-resolve', identity);
524
+ const file = join(directory, 'entry.cache');
525
+ let state = resolves.get(identity);
526
+ if (state === undefined) {
527
+ const created: ResolveState = { ready: Promise.resolve(), cached: null };
528
+ created.ready = loadResolveCache(file).then((cached) => {
529
+ created.cached = cached;
530
+ });
531
+ resolves.set(identity, created);
532
+ state = created;
533
+ try {
534
+ await created.ready;
535
+ } catch (error) {
536
+ resolves.delete(identity);
537
+ throw error;
538
+ }
539
+ } else await state.ready;
540
+ for (let joined = 0; joined <= 3; joined += 1) {
541
+ const key = await resolutionKey(root, manifest, state.cached?.cargo ?? null, index);
542
+ const pending = state.pending;
543
+ if (pending !== undefined) {
544
+ if (joined === 3) break;
545
+ if (cargoResolveEvents.hasSubscribers) cargoResolveEvents.publish({ phase: 'join', manifest });
546
+ try {
547
+ await pending.promise;
548
+ } catch (error) {
549
+ // Sharers of a refused key receive its refusal; a changed key still
550
+ // needs its own resolution after the old flight releases the lock.
551
+ if (pending.key === key) throw error;
552
+ }
553
+ // Re-key every joiner with the discovered paths, including an external
554
+ // manifest edited after the flight's last validation, before publication.
555
+ continue;
556
+ }
557
+ if (state.cached?.key === key) return state.cached.cargo;
558
+ if (joined === 3) break;
559
+ const active = state;
560
+ const promise = resolveAndCache(root, manifest, index, directory, file, active, boundMs);
561
+ active.pending = { key, promise };
562
+ try {
563
+ return await promise;
564
+ } finally {
565
+ if (active.pending?.promise === promise) delete active.pending;
566
+ }
567
+ }
568
+ throw new Error('Cargo resolution inputs kept changing across three consecutive in-flight queries');
569
+ } catch (error) {
570
+ if (error instanceof CargoMetadataError) throw error;
571
+ throw new CargoMetadataError(manifest, error);
572
+ }
573
+ }
574
+
575
+ async function resolveAndCache(
576
+ root: string,
577
+ manifest: string,
578
+ indexed: ReadonlySet<string>,
579
+ directory: string,
580
+ file: string,
581
+ state: ResolveState,
582
+ boundMs: number = CARGO_RESOLVE_BOUND_MS,
583
+ ): Promise<CargoResolve> {
584
+ if (IS_WASM)
585
+ throw new Error('Cargo resolution requires the native Nx file lock; a WASM Nx host cannot own Cargo children');
586
+ await mkdir(directory, { recursive: true });
587
+ // A warm cache answers with no lock: reading it and keying it spawns nothing and
588
+ // waits for nobody, so an unchanged workspace never queues behind a foreign flight.
589
+ state.cached = await loadResolveCache(file);
590
+ {
591
+ const key = await resolutionKey(root, manifest, state.cached?.cargo ?? null, indexed);
592
+ if (state.cached?.key === key) return state.cached.cargo;
593
+ }
594
+ const lockFile = join(directory, 'resolve.lock');
595
+ const lock = await acquireCargoResolveLock(lockFile, boundMs);
596
+ try {
597
+ // Another worker may have populated it while this worker waited.
598
+ state.cached = await loadResolveCache(file);
599
+ let inputs = state.cached?.cargo ?? null;
600
+ for (let attempt = 0; attempt < 3; attempt += 1) {
601
+ const before = await resolutionKey(root, manifest, inputs, indexed);
602
+ if (state.cached?.key === before) return state.cached.cargo;
603
+ const knownFiles = resolutionFiles(root, manifest, inputs, indexed);
604
+ // A filesystem timestamp, not a sandbox's wall clock: a newly
605
+ // discovered external input edited during this flight requires replay.
606
+ await writeFile(lockFile, '');
607
+ const started = (await stat(lockFile)).mtimeMs;
608
+ const metadata = await resolveCargoMetadata(manifest, root, boundMs);
609
+ const cargo = cargoFromMetadata(metadata);
610
+ // Capture the published key before validation. Reading it afterwards
611
+ // could pair a post-edit key with Cargo's pre-edit answer.
612
+ const key = await resolutionKey(root, manifest, cargo, indexed);
613
+ let changed = before !== (await resolutionKey(root, manifest, inputs, indexed));
614
+ for (const added of resolutionFiles(root, manifest, cargo, indexed)) {
615
+ if (knownFiles.has(added)) continue;
616
+ try {
617
+ if ((await stat(added)).mtimeMs >= started) changed = true;
618
+ } catch (error) {
619
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error;
620
+ }
621
+ }
622
+ if (changed) {
623
+ inputs = cargo;
624
+ continue;
625
+ }
626
+ const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
627
+ try {
628
+ await writeFile(temporary, `${key}\n${JSON.stringify(metadata)}\n`);
629
+ await rename(temporary, file);
630
+ } finally {
631
+ await rm(temporary, { force: true });
632
+ }
633
+ state.cached = { key, cargo };
634
+ return cargo;
635
+ }
636
+ throw new Error('Cargo resolution inputs kept changing during three consecutive locked offline queries');
637
+ } finally {
638
+ await lock.release();
639
+ }
640
+ }
641
+
642
+ /** A failed acquisition names the lock, elapsed wait, and the reaped waiter. */
643
+ export class CargoResolveLockError extends Error {
644
+ constructor(
645
+ readonly lockFile: string,
646
+ readonly boundMs: number,
647
+ readonly waitedMs: number,
648
+ readonly waiterPid: number | undefined,
649
+ ) {
650
+ super(
651
+ `waited ${waitedMs}ms for the Cargo resolve lock ${lockFile} (bound ${boundMs}ms; holder pid unknown); ` +
652
+ `killed and reaped lock waiter pid ${waiterPid ?? 'unavailable'} because an unbounded wait hangs project-graph construction`,
653
+ );
654
+ this.name = 'CargoResolveLockError';
655
+ }
656
+ }
657
+
658
+ /**
659
+ * The pinned Nx binding has only blocking acquisition. Isolate that syscall in
660
+ * one child per cold flight so the caller's deadline can kill and reap it.
661
+ * Closing stdin releases a successful lease; warm cache reads spawn no child.
662
+ */
663
+ export function acquireCargoResolveLock(
664
+ lockFile: string,
665
+ boundMs: number,
666
+ ): Promise<{ readonly pid: number; release(): Promise<void> }> {
667
+ const nativeModule = createRequire(import.meta.url).resolve('nx/src/native/index.js');
668
+ const program = [
669
+ `const { FileLock } = require(${JSON.stringify(nativeModule)});`,
670
+ `const lock = new FileLock(${JSON.stringify(lockFile)});`,
671
+ 'lock.lock();',
672
+ "process.stdout.write('locked\\n');",
673
+ 'process.stdin.resume();',
674
+ "process.stdin.on('end', () => { lock.unlock(); process.exit(0); });",
675
+ ].join('\n');
676
+ // The public ES2022 library API requires the constructor form, not Promise.withResolvers.
677
+ return new Promise((settle, reject) => {
678
+ const started = performance.now();
679
+ const child = spawn(process.execPath, ['--eval', program], {
680
+ detached: process.platform !== 'win32',
681
+ stdio: ['pipe', 'pipe', 'pipe'],
682
+ });
683
+ let acquired = false;
684
+ let releasing = false;
685
+ let failure: Error | undefined;
686
+ let stdout = '';
687
+ let stderr = '';
688
+ const fail = (error: Error): void => {
689
+ failure ??= error;
690
+ killCargoChild(child);
691
+ };
692
+ const timer = setTimeout(
693
+ () => fail(new CargoResolveLockError(lockFile, boundMs, Math.ceil(performance.now() - started), child.pid)),
694
+ boundMs,
695
+ );
696
+ const closed = new Promise<void>((finish) => {
697
+ child.on('close', (code, signal) => {
698
+ clearTimeout(timer);
699
+ cargoChildren.delete(child);
700
+ if (cargoChildren.size === 0) {
701
+ process.removeListener('exit', killCargoChildren);
702
+ removeCargoSignalHandlers();
703
+ }
704
+ if (failure === undefined && (!releasing || code !== 0)) {
705
+ failure = new Error(
706
+ `Cargo resolve lock ${lockFile} waiter pid ${child.pid} exited (${signal ?? code}): ${stderr.trim()}`,
707
+ );
708
+ }
709
+ if (!acquired) reject(failure);
710
+ finish();
711
+ });
712
+ });
713
+ child.stdout.setEncoding('utf8');
714
+ child.stderr.setEncoding('utf8');
715
+ child.stdout.on('data', (chunk: string) => {
716
+ stdout += chunk;
717
+ if (failure !== undefined || acquired || stdout !== 'locked\n') return;
718
+ const waitedMs = Math.ceil(performance.now() - started);
719
+ if (waitedMs >= boundMs) {
720
+ fail(new CargoResolveLockError(lockFile, boundMs, waitedMs, child.pid));
721
+ return;
722
+ }
723
+ if (child.pid === undefined) {
724
+ fail(new Error(`Cargo resolve lock ${lockFile} acquired without a child pid`));
725
+ return;
726
+ }
727
+ acquired = true;
728
+ clearTimeout(timer);
729
+ settle({
730
+ pid: child.pid,
731
+ release: async () => {
732
+ releasing = true;
733
+ child.stdin.end();
734
+ await closed;
735
+ if (failure !== undefined) throw failure;
736
+ },
737
+ });
738
+ });
739
+ child.stderr.on('data', (chunk: string) => {
740
+ stderr += chunk;
741
+ });
742
+ child.on('error', fail);
743
+ child.stdin.on('error', fail);
744
+ child.stdout.on('error', fail);
745
+ child.stderr.on('error', fail);
746
+ if (cargoChildren.size === 0) ownCargoChildren();
747
+ cargoChildren.add(child);
748
+ });
749
+ }
750
+
751
+ async function resolveCargoMetadata(
752
+ manifestPath: string,
753
+ cwd: string,
754
+ boundMs: number = CARGO_RESOLVE_BOUND_MS,
755
+ ): Promise<CargoMetadata> {
190
756
  const stdout = await runCargo(
191
- ['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
757
+ ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', manifestPath],
192
758
  cwd,
759
+ boundMs,
193
760
  );
194
- return realpath(stdout.trim());
761
+ const metadata = parseCargoMetadata(stdout);
762
+ metadata.workspace_root = await realpath(metadata.workspace_root);
763
+ const members = new Set(metadata.workspace_members);
764
+ // Sequential: the metadata flight owns every Cargo process it starts,
765
+ // including the one-time governing-workspace lookups for path dependencies.
766
+ for (const pkg of metadata.packages) {
767
+ if (pkg.source !== null) continue;
768
+ pkg.manifest_path = await realpath(pkg.manifest_path);
769
+ for (const target of pkg.targets) target.src_path = await realpath(target.src_path);
770
+ pkg.governing_manifest_path = members.has(pkg.id)
771
+ ? join(metadata.workspace_root, 'Cargo.toml')
772
+ : await realpath(
773
+ (
774
+ await runCargo(
775
+ ['locate-project', '--workspace', '--manifest-path', pkg.manifest_path, '--message-format', 'plain'],
776
+ cwd,
777
+ boundMs,
778
+ )
779
+ ).trim(),
780
+ );
781
+ }
782
+ return metadata;
195
783
  }
196
784
 
197
785
  export async function hashCargoPathInputs(
@@ -202,13 +790,32 @@ export async function hashCargoPathInputs(
202
790
  const root = await realpath(workspaceRoot);
203
791
  const cargo = await readCargoResolve(manifestPath, root);
204
792
  const members = options?.closure === undefined ? undefined : await dependencyClosure(cargo, resolve(options.closure));
205
-
206
- const directories = new Set<string>();
207
- const files = new Set<string>();
793
+ type InputRole = 'manifest' | 'workspace' | 'ancestor' | 'target' | 'file' | 'link' | 'include';
794
+ const inputKey = (pkg: LocalCargoPackage, role: InputRole, path: string) =>
795
+ JSON.stringify([pkg.name, pkg.version, role, path.split(sep).join('/')]);
796
+ const directories = new Map<string, LocalCargoPackage>();
797
+ const files = new Map<string, string>();
208
798
  const ancestors = new Set<string>();
209
- const links = new Map<string, string>();
210
- const packageSources: string[] = [];
211
- for (const pkg of cargo.local) {
799
+ const links = new Map<string, { key: string; target: string }>();
800
+ const packageSources: { file: string; key: string }[] = [];
801
+ // Read shared bytes once. The smallest declared identity makes multiple owners
802
+ // deterministic without using a consuming checkout's mount path as an input.
803
+ const includeFile = (file: string, key: string) => {
804
+ const prior = files.get(file);
805
+ if (prior === undefined || key < prior) files.set(file, key);
806
+ };
807
+ const packages = [...cargo.local].sort((left, right) =>
808
+ left.name < right.name
809
+ ? -1
810
+ : left.name > right.name
811
+ ? 1
812
+ : left.version < right.version
813
+ ? -1
814
+ : left.version > right.version
815
+ ? 1
816
+ : 0,
817
+ );
818
+ for (const pkg of packages) {
212
819
  if (members !== undefined && !members.has(pkg.id)) continue;
213
820
  const directory = pkg.directory;
214
821
  if (!options?.includeWorkspace) {
@@ -216,37 +823,39 @@ export async function hashCargoPathInputs(
216
823
  const fromCargo = relative(cargo.root, directory);
217
824
  if (
218
825
  fromRoot !== '..' &&
219
- !fromRoot.startsWith(`..${sep}`) &&
826
+ !fromRoot.startsWith('..' + sep) &&
220
827
  !isAbsolute(fromRoot) &&
221
828
  fromCargo !== '..' &&
222
- !fromCargo.startsWith(`..${sep}`) &&
829
+ !fromCargo.startsWith('..' + sep) &&
223
830
  !isAbsolute(fromCargo) &&
224
831
  !fromRoot.split(sep).includes('node_modules')
225
832
  )
226
833
  continue;
227
834
  }
228
- directories.add(directory);
229
- files.add(pkg.manifest);
230
- files.add(await governingManifest(cargo, pkg, root));
231
- packageSources.push(...pkg.sources);
232
- // A member can inherit edition, lint policy, dependencies and
233
- // profiles from a workspace above its package directory. Those manifests
234
- // are not descendants of the source roots returned by Cargo metadata.
235
- for (let ancestor = dirname(directory); !ancestors.has(ancestor); ancestor = dirname(ancestor)) {
835
+ directories.set(directory, pkg);
836
+ includeFile(pkg.manifest, inputKey(pkg, 'manifest', 'Cargo.toml'));
837
+ includeFile(pkg.governingManifest, inputKey(pkg, 'workspace', 'Cargo.toml'));
838
+ for (const [index, file] of pkg.sources.entries())
839
+ packageSources.push({ file, key: inputKey(pkg, 'target', String(index)) });
840
+ for (
841
+ let ancestor = dirname(directory), depth = 1;
842
+ !ancestors.has(ancestor);
843
+ ancestor = dirname(ancestor), depth += 1
844
+ ) {
236
845
  ancestors.add(ancestor);
237
846
  for (const name of CARGO_ANCESTOR_INPUTS) {
238
847
  const file = join(ancestor, name);
239
- if (statSync(file, { throwIfNoEntry: false })?.isFile()) files.add(await realpath(file));
848
+ if (statSync(file, { throwIfNoEntry: false })?.isFile()) {
849
+ includeFile(await realpath(file), inputKey(pkg, 'ancestor', depth + '/' + name));
850
+ }
240
851
  }
241
852
  }
242
853
  }
243
-
244
- const ignored = gitIgnoredBeneath([...directories]);
245
- for (const source of packageSources) if (!ignored(source)) files.add(source);
854
+ const ignored = gitIgnoredBeneath([...directories.keys()]);
855
+ for (const { file, key } of packageSources) if (!ignored(file)) includeFile(file, key);
246
856
  const skipped: ReadonlySet<string> = new Set(HASH_SKIPPED_DIRECTORIES);
247
- // Overlapping packages (a facade root plus its child crates) share one walk.
248
857
  const visited = new Set<string>();
249
- async function walk(directory: string): Promise<void> {
858
+ async function walk(directory: string, owner: LocalCargoPackage, within: string): Promise<void> {
250
859
  const canonical = await realpath(directory);
251
860
  if (visited.has(canonical)) return;
252
861
  visited.add(canonical);
@@ -254,34 +863,48 @@ export async function hashCargoPathInputs(
254
863
  if (skipped.has(entry.name)) continue;
255
864
  const path = join(canonical, entry.name);
256
865
  if (ignored(path)) continue;
257
- if (entry.isSymbolicLink()) links.set(path, await readlink(path));
866
+ const localPath = within === '' ? entry.name : within + '/' + entry.name;
867
+ if (entry.isSymbolicLink())
868
+ links.set(path, { key: inputKey(owner, 'link', localPath), target: await readlink(path) });
258
869
  const info = entry.isSymbolicLink() ? await lstat(await realpath(path)) : entry;
259
870
  if (info.isDirectory()) {
260
- await walk(path);
871
+ await walk(path, owner, localPath);
261
872
  } else if (
262
873
  info.isFile() &&
263
874
  (entry.name.endsWith('.rs') ||
264
875
  entry.name === 'Cargo.toml' ||
265
- (canonical.endsWith(`${sep}.cargo`) && (entry.name === 'config' || entry.name === 'config.toml')))
876
+ ((within === '.cargo' || within.endsWith('/.cargo')) &&
877
+ (entry.name === 'config' || entry.name === 'config.toml')))
266
878
  ) {
267
- files.add(await realpath(path));
879
+ includeFile(await realpath(path), inputKey(owner, 'file', localPath));
880
+ }
881
+ }
882
+ }
883
+ for (const [directory, owner] of directories) await walk(directory, owner, '');
884
+ const compiledCache = new Map<string, CompiledCargoInput[]>();
885
+ for (const [directory, owner] of directories) {
886
+ for (const input of compiledInFiles(directory, compiledCache)) {
887
+ if (!ignored(input.file)) {
888
+ includeFile(
889
+ await realpath(input.file),
890
+ inputKey(owner, 'include', relative(directory, input.source) + '\0' + input.literal),
891
+ );
268
892
  }
269
893
  }
270
894
  }
271
- for (const directory of directories) await walk(directory);
272
895
  const hash = createHash('sha256');
273
- hash.update('cargo-path-inputs-v1\0');
274
- for (const [path, target] of [...links].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))) {
275
- hash.update(relative(root, path));
896
+ hash.update('cargo-path-inputs-v2\0');
897
+ for (const { key, target } of [...links.values()].sort((left, right) =>
898
+ left.key < right.key ? -1 : left.key > right.key ? 1 : 0,
899
+ )) {
900
+ hash.update(key);
276
901
  hash.update('\0symlink\0');
277
902
  hash.update(target);
278
903
  hash.update('\0');
279
904
  }
280
- for (const file of [...files].sort()) {
905
+ for (const [file, key] of [...files].sort((left, right) => (left[1] < right[1] ? -1 : left[1] > right[1] ? 1 : 0))) {
281
906
  const bytes = await readFile(file);
282
- // Both names and bytes matter: moving a module can change resolution even
283
- // when its contents are unchanged. Length framing makes boundaries unique.
284
- hash.update(relative(root, file));
907
+ hash.update(key);
285
908
  hash.update('\0');
286
909
  hash.update(String(bytes.length));
287
910
  hash.update('\0');
@@ -339,6 +962,7 @@ function gitIgnoredBeneath(directories: readonly string[]): (path: string) => bo
339
962
  const listing = execFileSync(
340
963
  'git',
341
964
  [
965
+ ...ISOLATED_GIT_ARGS,
342
966
  '--literal-pathspecs',
343
967
  '-C',
344
968
  repository,
@@ -351,7 +975,7 @@ function gitIgnoredBeneath(directories: readonly string[]): (path: string) => bo
351
975
  '--',
352
976
  ...sourceRoots,
353
977
  ],
354
- { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO },
978
+ { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO, env: isolatedGitEnv() },
355
979
  );
356
980
  for (const entry of listing.split('\0')) {
357
981
  if (entry.length === 0) continue;
@@ -369,10 +993,11 @@ function gitIgnoredBeneath(directories: readonly string[]): (path: string) => bo
369
993
 
370
994
  /** The members of `paths` (relative to `repository`) that git ignores, own patterns or an ancestor's. */
371
995
  function gitCheckIgnore(repository: string, paths: readonly string[]): string[] {
372
- const result = spawnSync('git', ['-C', repository, 'check-ignore', '-z', '--stdin'], {
996
+ const result = spawnSync('git', [...ISOLATED_GIT_ARGS, '-C', repository, 'check-ignore', '-z', '--stdin'], {
373
997
  input: `${paths.join('\0')}\0`,
374
998
  encoding: 'utf8',
375
999
  maxBuffer: 64 * 1024 * 1024,
1000
+ env: isolatedGitEnv(),
376
1001
  });
377
1002
  // check-ignore answers 1 when nothing is ignored; only 0 carries a listing.
378
1003
  if (result.status === 1) return [];
@@ -387,9 +1012,10 @@ function gitRepository(directory: string): string | undefined {
387
1012
  try {
388
1013
  // The walk compares realpaths, so git's answer is canonicalized the same way.
389
1014
  return realpathSync(
390
- execFileSync('git', ['-C', directory, 'rev-parse', '--show-toplevel'], {
1015
+ execFileSync('git', [...ISOLATED_GIT_ARGS, '-C', directory, 'rev-parse', '--show-toplevel'], {
391
1016
  encoding: 'utf8',
392
1017
  stdio: CHILD_STDIO,
1018
+ env: isolatedGitEnv(),
393
1019
  }).trim(),
394
1020
  );
395
1021
  } catch (error) {
@@ -405,3 +1031,64 @@ function gitRepository(directory: string): string | undefined {
405
1031
  throw error;
406
1032
  }
407
1033
  }
1034
+
1035
+ /** A compiled-in value, with the source spelling that identifies it across checkouts. */
1036
+ export interface CompiledCargoInput {
1037
+ readonly file: string;
1038
+ readonly source: string;
1039
+ readonly literal: string;
1040
+ }
1041
+
1042
+ // A tokenized literal macro, never text inside a comment or another string.
1043
+ const INCLUDE_CLOSERS: Readonly<Record<string, string>> = { '(': ')', '[': ']', '{': '}' };
1044
+ const INCLUDE_STRING = /^"([^"\\\r\n]*)"$/u;
1045
+ const INCLUDE_RAW_STRING = /^r(#*)"([\s\S]*)"\1$/u;
1046
+
1047
+ /**
1048
+ * Existing literal include_str!/include_bytes! files read by this package's Rust sources.
1049
+ * One graph owns the memo; runtime hashes use a fresh memo. Nested packages own their
1050
+ * sources separately. Computed paths and arbitrary build-script reads remain explicit inputs.
1051
+ */
1052
+ export function compiledInFiles(root: string, cache: Map<string, CompiledCargoInput[]>): CompiledCargoInput[] {
1053
+ const cached = cache.get(root);
1054
+ if (cached !== undefined) return cached;
1055
+ const found = new Map<string, CompiledCargoInput>();
1056
+ const skipped: readonly string[] = HASH_SKIPPED_DIRECTORIES;
1057
+ const scan = [root];
1058
+ while (scan.length > 0) {
1059
+ const current = scan.pop();
1060
+ if (current === undefined || !existsSync(current)) continue;
1061
+ for (const entry of readdirSync(current, { withFileTypes: true })) {
1062
+ const child = join(current, entry.name);
1063
+ if (entry.isDirectory()) {
1064
+ if (!skipped.includes(entry.name) && !existsSync(join(child, 'Cargo.toml'))) scan.push(child);
1065
+ } else if (entry.isFile() && entry.name.endsWith('.rs')) {
1066
+ const text = readFileSync(child, 'utf8');
1067
+ if (!text.includes('include_')) continue;
1068
+ const tokens = rustTokens(text);
1069
+ for (let index = 0; index < tokens.length; index += 1) {
1070
+ if (tokens[index]?.text !== 'include_str' && tokens[index]?.text !== 'include_bytes') continue;
1071
+ if (tokens[index + 1]?.text !== '!') continue;
1072
+ const closing = INCLUDE_CLOSERS[tokens[index + 2]?.text ?? ''];
1073
+ const end = tokens[index + 4]?.text === ',' ? index + 5 : index + 4;
1074
+ if (closing === undefined || tokens[end]?.text !== closing) continue;
1075
+ const literal = tokens[index + 3]?.text ?? '';
1076
+ const path = INCLUDE_STRING.exec(literal)?.[1] ?? INCLUDE_RAW_STRING.exec(literal)?.[2];
1077
+ if (path === undefined || path === '') continue;
1078
+ const file = resolve(current, path);
1079
+ if (statSync(file, { throwIfNoEntry: false })?.isFile()) {
1080
+ const prior = found.get(file);
1081
+ if (prior === undefined || child < prior.source || (child === prior.source && path < prior.literal)) {
1082
+ found.set(file, { file, source: child, literal: path });
1083
+ }
1084
+ }
1085
+ }
1086
+ }
1087
+ }
1088
+ }
1089
+ const files = [...found.values()].sort((left, right) =>
1090
+ left.file < right.file ? -1 : left.file > right.file ? 1 : 0,
1091
+ );
1092
+ cache.set(root, files);
1093
+ return files;
1094
+ }