@smoothbricks/nx-plugin 0.4.22 → 0.4.24

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 (72) hide show
  1. package/README.md +93 -33
  2. package/dist/bin/smoo-nx-exec.js +35 -11
  3. package/dist/bin/smoo-nx-nextest-extract.d.ts +3 -0
  4. package/dist/bin/smoo-nx-nextest-extract.d.ts.map +1 -0
  5. package/dist/bin/smoo-nx-nextest-extract.js +41 -0
  6. package/dist/cargo-closure-input.d.ts +63 -0
  7. package/dist/cargo-closure-input.d.ts.map +1 -0
  8. package/dist/cargo-closure-input.js +197 -0
  9. package/dist/cargo-source-hash.d.ts +37 -0
  10. package/dist/cargo-source-hash.d.ts.map +1 -1
  11. package/dist/cargo-source-hash.js +108 -190
  12. package/dist/cross-check-policy.d.ts +14 -0
  13. package/dist/cross-check-policy.d.ts.map +1 -1
  14. package/dist/cross-check-policy.js +15 -1
  15. package/dist/ensure-built.d.ts +3 -2
  16. package/dist/ensure-built.d.ts.map +1 -1
  17. package/dist/ensure-built.js +33 -56
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +152 -91
  20. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  21. package/dist/managed-files/ci-workflow.js +12 -15
  22. package/dist/managed-files/files.d.ts.map +1 -1
  23. package/dist/managed-files/files.js +10 -0
  24. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  25. package/dist/managed-files/publish-workflow.js +12 -6
  26. package/dist/manifest-hash.d.ts.map +1 -1
  27. package/dist/manifest-hash.js +24 -30
  28. package/dist/nextest-extraction.d.ts +101 -0
  29. package/dist/nextest-extraction.d.ts.map +1 -0
  30. package/dist/nextest-extraction.js +268 -0
  31. package/dist/package-target-policy.js +1 -1
  32. package/dist/platform-targets.d.ts +7 -0
  33. package/dist/platform-targets.d.ts.map +1 -1
  34. package/dist/platform-targets.js +7 -0
  35. package/dist/workspace-config-policy.d.ts +1 -1
  36. package/dist/workspace-config-policy.d.ts.map +1 -1
  37. package/dist/workspace-config-policy.js +1 -1
  38. package/managed/raw/envrc +38 -1
  39. package/managed/raw/tooling/direnv/developer-links.ts +201 -0
  40. package/managed/raw/tooling/direnv/devenv.smoo.nix +75 -51
  41. package/managed/raw/tooling/direnv/secret-references.ts +3 -1
  42. package/managed/raw/tooling/direnv/setup-environment.ts +562 -78
  43. package/managed/raw/tooling/direnv/shared-caches.sh +53 -0
  44. package/managed/raw/tooling/git-hooks/pre-push.sh +22 -1
  45. package/managed/templates/github/workflows/managed-files.yml +4 -3
  46. package/nextest.toml +5 -5
  47. package/package.json +4 -3
  48. package/src/__tests__/fixture-nx-env.ts +31 -0
  49. package/src/__tests__/in-process-nx-index.ts +9 -0
  50. package/src/bin/smoo-nx-exec.ts +40 -11
  51. package/src/bin/smoo-nx-nextest-extract.ts +43 -0
  52. package/src/bounded-test-policy.test.ts +4 -1
  53. package/src/cargo-closure-input.test.ts +184 -0
  54. package/src/cargo-closure-input.ts +247 -0
  55. package/src/cargo-source-hash.test.ts +8 -9
  56. package/src/cargo-source-hash.ts +146 -47
  57. package/src/cross-check-policy.ts +15 -3
  58. package/src/ensure-built.test.ts +65 -5
  59. package/src/ensure-built.ts +40 -85
  60. package/src/index.test.ts +222 -75
  61. package/src/index.ts +213 -101
  62. package/src/managed-files/ci-workflow.ts +12 -15
  63. package/src/managed-files/files.ts +10 -0
  64. package/src/managed-files/generator.test.ts +29 -0
  65. package/src/managed-files/publish-workflow.ts +17 -6
  66. package/src/manifest-hash.test.ts +21 -1
  67. package/src/manifest-hash.ts +26 -29
  68. package/src/nextest-extraction.test.ts +74 -0
  69. package/src/nextest-extraction.ts +329 -0
  70. package/src/package-target-policy.ts +1 -1
  71. package/src/platform-targets.ts +8 -0
  72. package/src/workspace-config-policy.ts +6 -1
package/README.md CHANGED
@@ -50,10 +50,14 @@ The plugin discovers Cargo workspaces beside Nx project manifests at any depth:
50
50
  Projects can own multiple crates. A nested independent workspace has its own prerequisites and does not inherit those
51
51
  of an outer workspace.
52
52
  - **Commands:** validation runs from the governing Cargo workspace directory. One `cargo fmt --all --check` and one
53
- `cargo --frozen clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings` cover every member.
54
- Clippy of a crate is a check build of its whole closure, so a target directory per crate rebuilt every shared
55
- dependency once per crate — 49 closures and 11 GiB on one repository — and no per-crate cache hit repaid it. Cargo's
56
- own lock serializes the invocations that share the directory.
53
+ `cargo --frozen clippy --workspace --all-targets -- -D warnings` cover every member. Clippy of a crate is a check
54
+ build of its whole closure, so a target directory per crate rebuilt every shared dependency once per crate — 49
55
+ closures and 11 GiB on one repository — and no per-crate cache hit repaid it. Clippy writes the workspace's own
56
+ `target/`, like every dev build: its check units carry their mode in the unit hash and never collide with build units,
57
+ and the host units a dev build compiled (build scripts and their host-only dependencies) are reused instead of
58
+ compiled a second time. Cargo's own lock serializes the invocations that share the directory. `cargo-lint-cross` keeps
59
+ `target/cargo-lint-cross`: a foreign triple's units are distinct anyway, and its own lock keeps the cross check from
60
+ queueing behind host builds.
57
61
  - **Overrides:** normal Nx merging applies. Explicit `nx.targets` fields replace inferred fields, while omitted fields
58
62
  retain their inferred base. Use `"dependsOn": ["...", "extra"]` to preserve inferred prerequisites when adding an
59
63
  edge; replacing the array makes its author responsible for fetching before frozen Cargo commands.
@@ -61,6 +65,19 @@ The plugin discovers Cargo workspaces beside Nx project manifests at any depth:
61
65
  Member and exclude patterns may use `*` and `?` in any path segment (for example, `packages/*/crates/*`). Unsupported
62
66
  patterns and member patterns that match no directory fail graph construction instead of silently omitting crates.
63
67
 
68
+ ### Cargo profiles
69
+
70
+ The default configuration of every cargo target compiles cargo's `dev` profile: every local build and test, gates, CI
71
+ validation and `nx build` share one `target/` and one set of units, so cargo recompiles only what changed. The
72
+ `production` configuration adds `--release` to the targets that build shipped artifacts (`cargo-napi`,
73
+ `napi-<arch>-<os>`, `cargo-wasm`) and is selected only where an artifact ships: the generated publish workflow's build
74
+ and platform-leg steps, the `smoo release` commands it runs, and the build and deploy runs of a production
75
+ `smoo github-ci nx-deploy`. Test targets have no `production` configuration, so a release build never compiles release
76
+ test binaries. A package-local target that builds a shipped artifact follows the same rule: its default options compile
77
+ `dev` and its `production` configuration compiles a shipping profile. A shipping profile other than `release` is only
78
+ for an artifact family no other build compiles, such as a size-optimised wasm32 lane; a profile that compiles the same
79
+ crates for the same target as `dev` or `release` compiles every dependency a second time.
80
+
64
81
  ### Cargo cache boundaries
65
82
 
66
83
  `cargo-fetch` is uncached because the registry lives outside Nx outputs. `cargo-test-compile` is also uncached: it warms
@@ -73,8 +90,7 @@ declared toolchain pin, and the toolchain's own reported versions. The pin is `d
73
90
  input at both `{workspaceRoot}/devenv.lock` and `{workspaceRoot}/tooling/direnv/devenv.lock` — devenv resolves it into
74
91
  the rustc, cargo, linker, C toolchain and SDK every cargo command inherits, so bumping it invalidates every cached cargo
75
92
  target. Workspace test runners additionally hash all workspace member manifests because those affect unified features,
76
- and the nextest executable/configuration. Production test configurations use release compilation and release runners
77
- together.
93
+ and the nextest executable/configuration.
78
94
 
79
95
  Nothing machine-local takes part. The ambient cargo environment and the global `$CARGO_HOME/config.toml` are
80
96
  deliberately not hashed: their values carry absolute store paths and per-checkout state directories, which would make
@@ -113,23 +129,52 @@ both the Nx and Cargo workspaces are left to the plugin's inferred file inputs.
113
129
  require maintaining a second list of source roots. The command refuses missing dependencies or an unavailable locked
114
130
  dependency cache instead of emitting a partial digest.
115
131
 
116
- Custom Cargo targets that replace inferred inputs need the in-workspace source closure too. Use
117
- `smoo-nx-cargo-hash --include-workspace --closure <directory> [Cargo.toml]` to hash the locked, offline resolve closure
118
- of the local packages under `<directory>`: those packages and every mutable local package they reach through normal,
119
- build or dev dependencies, inside or outside the Nx workspace. An edit to a workspace member outside that closure leaves
120
- the digest unchanged. Nx does not substitute `{projectRoot}` into runtime inputs, so each project that owns custom Cargo
121
- targets declares its own `cargoSources` and names its directory. For a repository-root Cargo workspace, `nx.json`
122
- declares:
132
+ ### Cargo closure input
133
+
134
+ Custom Cargo targets that replace inferred inputs need the source closure of the crates they build. The plugin infers it
135
+ as the `cargoClosure` named input of every `package.json` project with a member of a Cargo workspace under its root,
136
+ where that Cargo workspace's root is itself a project (the root `package.json`, for a repository-root workspace). Name
137
+ it in each custom Cargo target's `inputs`, alongside `cargoToolchain`, the target's own scripts and non-Rust inputs:
123
138
 
124
139
  ```json
125
140
  {
126
- "namedInputs": {
127
- "externalRustCrates": [{ "runtime": "smoo-nx-cargo-hash" }]
141
+ "nx": {
142
+ "targets": {
143
+ "build": {
144
+ "inputs": ["{projectRoot}/scripts/build.sh", "cargoToolchain", "cargoClosure"]
145
+ }
146
+ }
128
147
  }
129
148
  }
130
149
  ```
131
150
 
132
- and `packages/example/project.json` (or the `nx` key of its `package.json`) declares:
151
+ Cargo decides the closure. Once per project-graph computation, `cargo metadata --locked --offline` resolves the local
152
+ packages under the project's root and every mutable local package they reach through normal, build or dev dependencies;
153
+ an edit to a workspace member outside that closure leaves the key alone. The members Nx's file index holds become
154
+ filesets: each package's `.rs` files, `Cargo.toml` files and `.cargo/config[.toml]` below its directory (skipping
155
+ `target/` and the other directories the hash command skips), target sources outside it, its governing workspace
156
+ manifest, the `Cargo.toml` and `.cargo/config[.toml]` of every directory from the package up to the Nx root, and the
157
+ workspace's `Cargo.lock`. Nx hashes those from the file hashes it already keeps, so hashing a task over an unchanged
158
+ tree starts no process; the graph computation pays one `cargo metadata` instead.
159
+
160
+ Members the file index cannot hold, outside the Nx workspace or under `node_modules`, are hashed by one runtime
161
+ `smoo-nx-cargo-hash --closure <projectRoot> <Cargo.toml>` entry, present only while such members exist. It resolves the
162
+ closure again when it runs, so a member that only an edit outside the workspace brings in is still covered.
163
+
164
+ When the closure cannot be expressed that way, `cargoClosure` is the complete runtime hash,
165
+ `smoo-nx-cargo-hash --include-workspace --closure <projectRoot> <Cargo.toml>`, and the plugin says why on stderr: Cargo
166
+ could not resolve the workspace during graph computation (no `cargo` on PATH, a stale `Cargo.lock`, an unfetched
167
+ dependency), or a member inside the workspace is missing from the file index (an ignored directory). The command reports
168
+ the same Cargo failure wherever a task needs it.
169
+
170
+ In-workspace members follow Nx's file semantics, which differ from the command's own walk in two places: a symlinked
171
+ directory inside a package is not followed, and manifests and Cargo configuration above the Nx root (a
172
+ `~/.cargo/config.toml` above a checkout in the home directory) are machine-local and not hashed, as for every other
173
+ cargo input the plugin infers.
174
+
175
+ A project the plugin does not infer (a `project.json` without a `package.json`), or one in a Cargo workspace whose root
176
+ is not a project, names the command directly. Nx does not substitute `{projectRoot}` into runtime inputs, so each such
177
+ project declares its own named input and names its directory:
133
178
 
134
179
  ```json
135
180
  {
@@ -144,11 +189,10 @@ and `packages/example/project.json` (or the `nx` key of its `package.json`) decl
144
189
  }
145
190
  ```
146
191
 
147
- Name `cargoSources` in each custom Cargo target's `inputs`, alongside its own scripts and non-Rust build inputs. Keep
148
- `externalRustCrates` for the plugin's external-source inference contract; unrelated TypeScript targets do not need
149
- `cargoSources`. Without `--closure`, `--include-workspace` hashes every mutable local package in the Cargo workspace.
150
- For a package-root workspace, pass its manifest last and name that workspace's `Cargo.lock`. An Nx `^production` input
151
- only follows existing project graph edges; it cannot replace a missing Cargo dependency closure.
192
+ Without `--closure`, `--include-workspace` hashes every mutable local package in the Cargo workspace. For a package-root
193
+ workspace, pass its manifest last and name that workspace's `Cargo.lock`. Keep `externalRustCrates` for the plugin's
194
+ external-source inference contract; unrelated TypeScript targets need neither input. An Nx `^production` input only
195
+ follows existing project graph edges; it cannot replace a missing Cargo dependency closure.
152
196
 
153
197
  Files git ignores beneath a package directory are not hashed, matching Nx's own file inputs. A generated source is its
154
198
  producer's output: the consuming target depends on the producer and hashes the source through
@@ -229,18 +273,22 @@ Concrete targets come from concrete files:
229
273
  `{workspaceRoot}`: Nx resolves a `{projectRoot}` glob against only the files its project owns, and a nested package
230
274
  owns the crates under it, so a root workspace's archive spelled that way hashes none of its members and replays stale
231
275
  binaries against edited crates. Each member crate gets a cached `cargo-test-<package>` run (30s per-test timeout like
232
- `bun test --timeout=30000`) whose inputs include that crate and its path dependencies, and whose command is
233
- `cargo --frozen nextest run --archive-file <archive> --workspace-remap . -E 'package(<crate>)'`. Runners therefore
234
- execute rather than build: they extract binaries into their own temporary directory, write nothing to cargo's flocked
235
- `target/`, and fan out instead of chaining. `--workspace-remap` is required — an archive records the producing tree's
236
- absolute paths, and without it a restored archive hands tests another checkout's `CARGO_MANIFEST_DIR`. With it the
237
- archive is relocatable, so one cache entry serves every checkout of the same commit; nextest re-points
238
- `CARGO_BIN_EXE_<name>`/`NEXTEST_BIN_EXE_<name>` at the extracted binaries at runtime, but a test that reads them
239
- through the compile-time `env!` macro keeps the producing tree's path. Per-crate runners accept an empty nextest
240
- selection because a valid workspace member may have no tests and a hash partition may legitimately be empty.
241
- `napi-debug` stays behind `cargo-test-compile`, and a crate in the project that builds the debug cdylib runs after it.
242
- A crate declaring `[package.metadata.smoothbricks.wasm-bindgen]` also receives the cacheable `cargo-wasm` output
243
- target in its owning project.
276
+ `bun test --timeout=30000`) whose inputs include that crate and its path dependencies, and whose command runs
277
+ `cargo --frozen nextest run --binaries-metadata … --cargo-metadata … --target-dir-remap … --workspace-remap . -E 'package(<crate>)'`
278
+ from the archive's one extraction. Runners therefore execute rather than build: they write nothing to cargo's build
279
+ tree, and fan out instead of chaining. They do not unpack the archive each: before the run, `smoo-nx-nextest-extract`
280
+ hashes it and extracts it into `target/nextest/archive.tar.zst.extracted/<sha256>` unless a runner already did — one
281
+ extracts, the others wait — so a workspace with ~200 test binaries and ~25 crates unpacks them once per archive
282
+ instead of once per crate. The directory is keyed by the archive's bytes, so a rebuilt or cache-restored archive never
283
+ runs an older extraction's binaries, and publishing one removes the others; it is never an Nx output.
284
+ `--workspace-remap` is required — an archive records the producing tree's absolute paths, and without it a restored
285
+ archive hands tests another checkout's `CARGO_MANIFEST_DIR`. With it the archive is relocatable, so one cache entry
286
+ serves every checkout of the same commit; nextest re-points `CARGO_BIN_EXE_<name>`/`NEXTEST_BIN_EXE_<name>` at the
287
+ extracted binaries at runtime, but a test that reads them through the compile-time `env!` macro keeps the producing
288
+ tree's path. Per-crate runners accept an empty nextest selection because a valid workspace member may have no tests
289
+ and a hash partition may legitimately be empty. `napi-debug` stays behind `cargo-test-compile`, and a crate in the
290
+ project that builds the debug cdylib runs after it. A crate declaring `[package.metadata.smoothbricks.wasm-bindgen]`
291
+ also receives the cacheable `cargo-wasm` output target in its owning project.
244
292
  - The plugin's own `nextest.toml` is passed as `--tool-config-file "smoo:$PWD/<path>"`, so it is a layer BENEATH the
245
293
  repository's `<cargo-workspace>/.config/nextest.toml` rather than a replacement for it: a repository can raise a
246
294
  timeout, add a test group, or declare `archive.include` for a cdylib or fixture the archived test binaries need, and
@@ -370,10 +418,22 @@ It is deliberately not `nx run <target> && exec`:
370
418
  print.
371
419
  - It execs the binary, preserving its argv, exit status, and signals. `nx run` has no notion of handing the process over
372
420
  to another binary.
421
+ - A miss runs the target through the workspace's `nx` CLI with the child's stdout on stderr, so stdout carries only the
422
+ binary's output (`wrapper | jq` works). Nx's pseudo-terminal writes task output straight to file descriptor 1, which
423
+ only a child process can redirect.
373
424
 
374
425
  Pass `--workspace-root <dir>` before `--` when invoking the wrapper outside the workspace. The binary path resolves
375
426
  against the caller's current directory.
376
427
 
428
+ Without `--` and a binary, the wrapper only makes the target current and exits 0, or with the failed run's status:
429
+
430
+ ```bash
431
+ smoo-nx-exec my-cli:build --workspace-root "$root"
432
+ ```
433
+
434
+ A hit prints nothing, where `nx run` would replay every task's cached log. A miss prints Nx's run of the target on
435
+ stderr.
436
+
377
437
  ## Bun Test Tracing Generator
378
438
 
379
439
  Configure a package for the Bun test tracing + no-emit test typechecking pattern used in this repo.
@@ -3,7 +3,7 @@ import { existsSync } from 'node:fs';
3
3
  import { dirname, isAbsolute, join, resolve } from 'node:path';
4
4
  import { inspect } from 'node:util';
5
5
  import { describeMiss, ensureBuilt, parseTargetSelector } from '../ensure-built.js';
6
- const USAGE = 'usage: smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>] -- <binary> [args...]';
6
+ const USAGE = 'usage: smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>] [-- <binary> [args...]]';
7
7
  /** Argument and environment problems all exit 2, the shell's "usage" code. */
8
8
  function usageError(message) {
9
9
  process.stderr.write(`smoo-nx-exec: ${message}\n${USAGE}\n`);
@@ -52,16 +52,16 @@ function findWorkspaceRoot(from) {
52
52
  }
53
53
  const argv = process.argv.slice(2);
54
54
  const separator = argv.indexOf('--');
55
- if (separator === -1) {
56
- usageError('missing `--` before the binary to exec');
57
- }
58
- const command = argv.slice(separator + 1);
59
- if (command.length === 0) {
55
+ // Without `--` there is nothing to exec: build what is stale and exit, so a
56
+ // caller that only needs the target current pays the probe, not an Nx CLI
57
+ // start that replays every cached task's log.
58
+ const command = separator === -1 ? [] : argv.slice(separator + 1);
59
+ if (separator !== -1 && command.length === 0) {
60
60
  usageError('no binary given after `--`');
61
61
  }
62
62
  let target;
63
63
  let workspaceRootArg;
64
- const tokens = tokenize(argv.slice(0, separator));
64
+ const tokens = tokenize(separator === -1 ? argv : argv.slice(0, separator));
65
65
  for (let index = 0; index < tokens.length; index += 1) {
66
66
  const token = tokens[index];
67
67
  if (token === '--workspace-root') {
@@ -86,12 +86,9 @@ if (parseTargetSelector(target) === null) {
86
86
  }
87
87
  // `execve` does no PATH lookup, so a bare name would fail as a missing file in
88
88
  // the current directory. Say which mistake was made instead.
89
- if (!command[0].includes('/')) {
89
+ if (command.length > 0 && !command[0].includes('/')) {
90
90
  usageError(`'${command[0]}' must be a path to the binary, not a name to look up on PATH`);
91
91
  }
92
- // Resolved against the directory the user is standing in, not the workspace
93
- // root: this is their command line, and the exec'd process inherits their cwd.
94
- const binary = isAbsolute(command[0]) ? command[0] : resolve(process.cwd(), command[0]);
95
92
  const workspaceRoot = workspaceRootArg === undefined ? findWorkspaceRoot(process.cwd()) : resolve(process.cwd(), workspaceRootArg);
96
93
  const result = await ensureBuilt({ target, cwd: workspaceRoot }).catch((error) => {
97
94
  process.stderr.write(`smoo-nx-exec: ${describeError(error)}\n`);
@@ -100,6 +97,13 @@ const result = await ensureBuilt({ target, cwd: workspaceRoot }).catch((error) =
100
97
  if (result.disposition !== 'hit' && process.env.NX_VERBOSE_LOGGING === 'true') {
101
98
  process.stderr.write(`smoo-nx-exec: ran ${target} because ${describeMiss(result.reason)}\n`);
102
99
  }
100
+ if (result.disposition === 'failed' || command.length === 0) {
101
+ // This process ends here rather than becoming the binary, and `process.exit`
102
+ // drops whatever a pipe has not taken yet: stdio pipes are asynchronous on
103
+ // macOS, and the tail of a build log is the part that explains it. Under
104
+ // Node an empty write completes once everything queued before it has.
105
+ await Promise.all([process.stdout, process.stderr].map((stream) => new Promise((settle) => stream.write('', () => settle()))));
106
+ }
103
107
  if (result.disposition === 'failed') {
104
108
  if (result.signal !== null) {
105
109
  // Re-raise rather than translate, so a build killed by SIGINT leaves this
@@ -108,9 +112,29 @@ if (result.disposition === 'failed') {
108
112
  }
109
113
  process.exit(result.exitCode);
110
114
  }
115
+ if (command.length === 0) {
116
+ process.exit(0);
117
+ }
118
+ // Resolved against the directory the user is standing in, not the workspace
119
+ // root: this is their command line, and the exec'd process inherits their cwd.
120
+ const binary = isAbsolute(command[0]) ? command[0] : resolve(process.cwd(), command[0]);
111
121
  if (process.execve === undefined) {
112
122
  throw new Error('smoo-nx-exec needs process.execve, which requires a POSIX host on Node 24+ or Bun');
113
123
  }
124
+ // Node puts a stdio pipe into O_NONBLOCK on macOS, and that flag belongs to the
125
+ // open file description, so it outlives execve. A binary that writes as if its
126
+ // stdout blocked would then lose everything past the first full pipe buffer
127
+ // (EAGAIN) whenever its reader falls behind: a `<cli> list | jq` read
128
+ // exactly 65536 bytes. Hand the binary the blocking stdio every program expects.
129
+ for (const stream of [process.stdin, process.stdout, process.stderr]) {
130
+ const handle = Reflect.get(stream, '_handle');
131
+ if (typeof handle === 'object' &&
132
+ handle !== null &&
133
+ 'setBlocking' in handle &&
134
+ typeof handle.setBlocking === 'function') {
135
+ handle.setBlocking(true);
136
+ }
137
+ }
114
138
  // `execve`, not spawn-and-wait: the built binary replaces this process, so it
115
139
  // owns the terminal, the signals and the exit status directly, with no wrapper
116
140
  // left behind to forward them.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=smoo-nx-nextest-extract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"smoo-nx-nextest-extract.d.ts","sourceRoot":"","sources":["../../src/bin/smoo-nx-nextest-extract.ts"],"names":[],"mappings":""}
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env node
2
+ import { ensureNextestArchiveExtracted, extractWithNextest } from '../nextest-extraction.js';
3
+ // Prints the directory holding the archive's extraction on stdout, the only
4
+ // thing a runner's `$(...)` captures. Everything said to a person goes to stderr.
5
+ const args = process.argv.slice(2);
6
+ const archive = args[0];
7
+ if (archive === undefined || args.length !== 1 || archive.startsWith('-')) {
8
+ process.stderr.write('usage: smoo-nx-nextest-extract <archive.tar.zst>\n');
9
+ process.exit(2);
10
+ }
11
+ const say = (line) => process.stderr.write(`smoo-nx-nextest-extract: ${line}\n`);
12
+ const seconds = (ms) => `${(ms / 1000).toFixed(1)}s`;
13
+ try {
14
+ const extraction = await ensureNextestArchiveExtracted(archive, extractWithNextest, (event) => {
15
+ switch (event.kind) {
16
+ case 'waiting':
17
+ say(`${event.holder} is extracting ${archive}; waiting for it`);
18
+ break;
19
+ case 'stale-lock':
20
+ say(`${event.holder} stopped refreshing its extraction lock ${seconds(event.ageMs)} ago; extracting instead`);
21
+ break;
22
+ }
23
+ });
24
+ const identity = `${archive} (sha256 ${extraction.key.slice(0, 12)})`;
25
+ switch (extraction.kind) {
26
+ case 'reused':
27
+ say(`running ${identity} from ${extraction.directory}`);
28
+ break;
29
+ case 'extracted':
30
+ say(`extracted ${identity} to ${extraction.directory} in ${seconds(extraction.elapsedMs)}`);
31
+ break;
32
+ case 'awaited':
33
+ say(`running ${identity} from ${extraction.directory} after ${seconds(extraction.elapsedMs)} waiting on ${extraction.holder}`);
34
+ break;
35
+ }
36
+ process.stdout.write(`${extraction.directory}\n`);
37
+ }
38
+ catch (error) {
39
+ say(error instanceof Error ? error.message : String(error));
40
+ process.exit(1);
41
+ }
@@ -0,0 +1,63 @@
1
+ import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
2
+ import { type CargoResolve } from './cargo-source-hash.js';
3
+ /**
4
+ * The named input the plugin infers for every project with crates of a Cargo
5
+ * workspace under its root: the mutable sources of the Cargo-resolved
6
+ * dependency closure of those crates, and the lockfile that pins the rest.
7
+ */
8
+ export declare const CARGO_CLOSURE_INPUT = "cargoClosure";
9
+ type Input = NonNullable<TargetConfiguration['inputs']>[number];
10
+ /**
11
+ * One Cargo workspace as one project-graph computation sees it. `manifest` is
12
+ * the workspace's root manifest relative to the Nx workspace root.
13
+ */
14
+ export type CargoClosureSource = {
15
+ readonly kind: 'resolved';
16
+ readonly manifest: string;
17
+ /** The canonical Nx workspace root. */
18
+ readonly root: string;
19
+ readonly cargo: CargoResolve;
20
+ /** Every `Cargo.toml` in Nx's file index, relative to the workspace root. */
21
+ readonly indexed: ReadonlySet<string>;
22
+ } | {
23
+ readonly kind: 'unresolved';
24
+ readonly manifest: string;
25
+ };
26
+ /**
27
+ * Every `Cargo.toml` Nx's workspace file index holds: .gitignore and .nxignore
28
+ * applied, `node_modules` never entered. A package whose manifest is missing
29
+ * here has no files a fileset input can hash.
30
+ */
31
+ export declare function indexedCargoManifests(workspaceRoot: string): Promise<ReadonlySet<string>>;
32
+ /**
33
+ * Locked, offline `cargo metadata` for one Cargo workspace, once per graph
34
+ * computation. Never rejects: a workspace Cargo cannot resolve right now (no
35
+ * cargo on PATH, a stale lockfile, an unfetched dependency) still gets its
36
+ * closure inputs, hashed at run time by the command that reports the same
37
+ * failure wherever a task needs it. Said once here, not once per project.
38
+ */
39
+ export declare function resolveCargoClosureSource(manifest: string, workspaceRoot: string, indexed: Promise<ReadonlySet<string>>): Promise<CargoClosureSource>;
40
+ /**
41
+ * The `cargoClosure` definition for the project at `projectRoot`: for each
42
+ * Cargo workspace with crates under it, the workspace's `Cargo.lock` plus the
43
+ * resolve closure of those crates.
44
+ *
45
+ * Closure members in Nx's file index are hashed by Nx itself, as filesets:
46
+ * each package's Rust sources, manifests and Cargo configuration below its
47
+ * directory, target sources outside it, its governing manifest, and the
48
+ * manifests and Cargo configuration of every directory from the package up
49
+ * to the workspace root. Nx keeps those file hashes current, so an unchanged
50
+ * tree costs no process at all. Members Nx cannot see — outside the
51
+ * workspace, or installed under `node_modules` — are hashed by one runtime
52
+ * `smoo-nx-cargo-hash --closure` entry, present only while such members
53
+ * exist. That command resolves the closure again when it runs, so a member
54
+ * that only an edit outside the workspace brings in is still covered.
55
+ *
56
+ * When the closure cannot be expressed that way — Cargo could not resolve it
57
+ * here, or a member inside the workspace is missing from Nx's file index —
58
+ * the definition is the complete runtime hash instead: correct, slower, and
59
+ * reported on stderr.
60
+ */
61
+ export declare function cargoClosureInputs(projectRoot: string, sources: readonly CargoClosureSource[]): Promise<Input[]>;
62
+ export {};
63
+ //# sourceMappingURL=cargo-closure-input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cargo-closure-input.d.ts","sourceRoot":"","sources":["../src/cargo-closure-input.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAGpE,OAAO,EAEL,KAAK,YAAY,EAKlB,MAAM,wBAAwB,CAAC;AAEhC;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAElD,KAAK,KAAK,GAAG,WAAW,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAgBhE;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAC1B;IACE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,uCAAuC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,6EAA6E;IAC7E,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CACvC,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/D;;;;GAIG;AACH,wBAAsB,qBAAqB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAE/F;AAED;;;;;;GAMG;AACH,wBAAsB,yBAAyB,CAC7C,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,GACpC,OAAO,CAAC,kBAAkB,CAAC,CAoB7B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,SAAS,kBAAkB,EAAE,GACrC,OAAO,CAAC,KAAK,EAAE,CAAC,CAelB"}
@@ -0,0 +1,197 @@
1
+ import { realpath } from 'node:fs/promises';
2
+ import { isAbsolute, join, posix, relative, sep } from 'node:path';
3
+ import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context.js';
4
+ import { CARGO_ANCESTOR_INPUTS, dependencyClosure, governingManifest, HASH_SKIPPED_DIRECTORIES, readCargoResolve, } from './cargo-source-hash.js';
5
+ /**
6
+ * The named input the plugin infers for every project with crates of a Cargo
7
+ * workspace under its root: the mutable sources of the Cargo-resolved
8
+ * dependency closure of those crates, and the lockfile that pins the rest.
9
+ */
10
+ export const CARGO_CLOSURE_INPUT = 'cargoClosure';
11
+ /**
12
+ * How a runtime input reaches the hash command. Nx runs it with `sh -c` from
13
+ * the workspace root and puts no `node_modules/.bin` on its PATH; a plugin Nx
14
+ * loads by name is installed at exactly this path.
15
+ */
16
+ const CARGO_HASH_COMMAND = 'node node_modules/@smoothbricks/nx-plugin/dist/bin/smoo-nx-cargo-hash.js';
17
+ /** A package's own Rust sources and Cargo configuration, exactly what the hash command's walk selects. */
18
+ const PACKAGE_FILES = '**/{*.rs,Cargo.toml,.cargo/config,.cargo/config.toml}';
19
+ const ANCESTOR_FILES = `{${CARGO_ANCESTOR_INPUTS.join(',')}}`;
20
+ const SKIPPED_TREES = `**/{${HASH_SKIPPED_DIRECTORIES.join(',')}}/**`;
21
+ /** A path holding any of these would be read as a pattern, not as itself. */
22
+ const GLOB_SYNTAX = /[*?[\]{}()!,]/;
23
+ /**
24
+ * Every `Cargo.toml` Nx's workspace file index holds: .gitignore and .nxignore
25
+ * applied, `node_modules` never entered. A package whose manifest is missing
26
+ * here has no files a fileset input can hash.
27
+ */
28
+ export async function indexedCargoManifests(workspaceRoot) {
29
+ return new Set(await globWithWorkspaceContext(workspaceRoot, ['**/Cargo.toml']));
30
+ }
31
+ /**
32
+ * Locked, offline `cargo metadata` for one Cargo workspace, once per graph
33
+ * computation. Never rejects: a workspace Cargo cannot resolve right now (no
34
+ * cargo on PATH, a stale lockfile, an unfetched dependency) still gets its
35
+ * closure inputs, hashed at run time by the command that reports the same
36
+ * failure wherever a task needs it. Said once here, not once per project.
37
+ */
38
+ export async function resolveCargoClosureSource(manifest, workspaceRoot, indexed) {
39
+ try {
40
+ // One Promise.all, entered before any await, so a failed index listing is
41
+ // handled here even when it settles before Cargo does.
42
+ const [{ root, cargo }, index] = await Promise.all([
43
+ realpath(workspaceRoot).then(async (root) => ({
44
+ root,
45
+ cargo: await readCargoResolve(join(root, manifest), root),
46
+ })),
47
+ indexed,
48
+ ]);
49
+ return { kind: 'resolved', manifest, root, cargo, indexed: index };
50
+ }
51
+ catch (error) {
52
+ const message = error instanceof Error ? error.message : String(error);
53
+ process.stderr.write(`@smoothbricks/nx-plugin: resolving ${manifest} failed (${message.trim().split('\n').slice(0, 3).join(' ')}); ` +
54
+ `every ${CARGO_CLOSURE_INPUT} under it runs smoo-nx-cargo-hash on each hash instead of reading Nx's file index.\n`);
55
+ return { kind: 'unresolved', manifest };
56
+ }
57
+ }
58
+ /**
59
+ * The `cargoClosure` definition for the project at `projectRoot`: for each
60
+ * Cargo workspace with crates under it, the workspace's `Cargo.lock` plus the
61
+ * resolve closure of those crates.
62
+ *
63
+ * Closure members in Nx's file index are hashed by Nx itself, as filesets:
64
+ * each package's Rust sources, manifests and Cargo configuration below its
65
+ * directory, target sources outside it, its governing manifest, and the
66
+ * manifests and Cargo configuration of every directory from the package up
67
+ * to the workspace root. Nx keeps those file hashes current, so an unchanged
68
+ * tree costs no process at all. Members Nx cannot see — outside the
69
+ * workspace, or installed under `node_modules` — are hashed by one runtime
70
+ * `smoo-nx-cargo-hash --closure` entry, present only while such members
71
+ * exist. That command resolves the closure again when it runs, so a member
72
+ * that only an edit outside the workspace brings in is still covered.
73
+ *
74
+ * When the closure cannot be expressed that way — Cargo could not resolve it
75
+ * here, or a member inside the workspace is missing from Nx's file index —
76
+ * the definition is the complete runtime hash instead: correct, slower, and
77
+ * reported on stderr.
78
+ */
79
+ export async function cargoClosureInputs(projectRoot, sources) {
80
+ const inputs = [];
81
+ const seen = new Set();
82
+ const ordered = [...sources].sort((left, right) => left.manifest < right.manifest ? -1 : left.manifest > right.manifest ? 1 : 0);
83
+ for (const source of ordered) {
84
+ for (const input of await workspaceClosureInputs(projectRoot, source)) {
85
+ const key = typeof input === 'string' ? input : JSON.stringify(input);
86
+ if (seen.has(key))
87
+ continue;
88
+ seen.add(key);
89
+ inputs.push(input);
90
+ }
91
+ }
92
+ return inputs;
93
+ }
94
+ async function workspaceClosureInputs(projectRoot, source) {
95
+ const lock = posix.join('{workspaceRoot}', posix.dirname(source.manifest), 'Cargo.lock');
96
+ const closureArguments = `--closure ${shellWord(projectRoot)} ${shellWord(source.manifest)}`;
97
+ const runtimeHash = [lock, { runtime: `${CARGO_HASH_COMMAND} --include-workspace ${closureArguments}` }];
98
+ if (source.kind === 'unresolved')
99
+ return runtimeHash;
100
+ const { root, cargo, indexed } = source;
101
+ const packageDirectories = [];
102
+ const files = [];
103
+ let external = false;
104
+ try {
105
+ const members = await dependencyClosure(cargo, join(root, projectRoot));
106
+ for (const pkg of cargo.local) {
107
+ if (!members.has(pkg.id))
108
+ continue;
109
+ const directory = indexablePath(root, pkg.directory);
110
+ if (directory === null) {
111
+ external = true;
112
+ continue;
113
+ }
114
+ if (!indexed.has(posix.join(directory, 'Cargo.toml'))) {
115
+ throw new Error(`the Cargo package ${directory} is not in Nx's file index (an ignored directory)`);
116
+ }
117
+ packageDirectories.push(directory);
118
+ for (const file of [...pkg.sources, await governingManifest(cargo, pkg, root)]) {
119
+ const path = indexablePath(root, file);
120
+ if (path === null)
121
+ throw new Error(`${directory} compiles ${file}, which Nx's file index cannot hold`);
122
+ files.push(path);
123
+ }
124
+ }
125
+ const unglobbable = [...packageDirectories, ...files].find((path) => GLOB_SYNTAX.test(path));
126
+ if (unglobbable !== undefined)
127
+ throw new Error(`${unglobbable} contains glob syntax, so no fileset can name it`);
128
+ }
129
+ catch (error) {
130
+ process.stderr.write(`@smoothbricks/nx-plugin: ${projectRoot}'s ${CARGO_CLOSURE_INPUT} runs smoo-nx-cargo-hash on each hash ` +
131
+ `instead of reading Nx's file index: ${error instanceof Error ? error.message : String(error)}\n`);
132
+ return runtimeHash;
133
+ }
134
+ // A package nested in another member's directory is already covered by it.
135
+ const directories = [...new Set(packageDirectories)]
136
+ .sort()
137
+ .filter((directory, index, sorted) => !sorted.slice(0, index).some((outer) => contains(outer, directory)));
138
+ const ancestors = new Set();
139
+ for (const directory of directories) {
140
+ for (let ancestor = parent(directory); ancestor !== null; ancestor = parent(ancestor)) {
141
+ const candidate = ancestor;
142
+ if (!directories.some((outer) => contains(outer, candidate)))
143
+ ancestors.add(candidate);
144
+ }
145
+ }
146
+ // Target sources outside every package directory, and governing manifests
147
+ // that are not already one of the ancestors' manifests.
148
+ const loose = new Set(files.filter((file) => !directories.some((directory) => contains(directory, file)) &&
149
+ !(posix.basename(file) === 'Cargo.toml' && ancestors.has(parent(file) ?? ''))));
150
+ // Sibling packages share one pattern, `crates/{a,b}/…`, so a closure of
151
+ // dozens of crates reads as a few lines in `nx show project`.
152
+ const siblings = new Map();
153
+ for (const directory of directories) {
154
+ const key = parent(directory);
155
+ if (key === null)
156
+ continue;
157
+ siblings.set(key, [...(siblings.get(key) ?? []), posix.basename(directory)]);
158
+ }
159
+ const packageGlobs = directories.includes('')
160
+ ? ['']
161
+ : [...siblings].map(([under, names]) => posix.join(under, names.length === 1 ? names.join('') : `{${names.join(',')}}`));
162
+ const anchored = (path, pattern = '') => posix.join('{workspaceRoot}', path, pattern);
163
+ return [
164
+ lock,
165
+ ...packageGlobs.map((glob) => anchored(glob, PACKAGE_FILES)),
166
+ ...packageGlobs.map((glob) => `!${anchored(glob, SKIPPED_TREES)}`),
167
+ ...[...ancestors].sort().map((ancestor) => anchored(ancestor, ANCESTOR_FILES)),
168
+ ...[...loose].sort().map((file) => anchored(file)),
169
+ ...(external ? [{ runtime: `${CARGO_HASH_COMMAND} ${closureArguments}` }] : []),
170
+ ];
171
+ }
172
+ /**
173
+ * `path` relative to the workspace root, `/`-separated, when it is somewhere
174
+ * Nx's file index can hold: inside the workspace and not under `node_modules`.
175
+ * The root itself is `''`.
176
+ */
177
+ function indexablePath(root, path) {
178
+ const fromRoot = relative(root, path);
179
+ if (fromRoot === '..' || fromRoot.startsWith(`..${sep}`) || isAbsolute(fromRoot))
180
+ return null;
181
+ const segments = fromRoot === '' ? [] : fromRoot.split(sep);
182
+ return segments.includes('node_modules') ? null : segments.join('/');
183
+ }
184
+ /** The directory holding `path`, `''` being the workspace root, which has none. */
185
+ function parent(path) {
186
+ if (path === '')
187
+ return null;
188
+ const directory = posix.dirname(path);
189
+ return directory === '.' ? '' : directory;
190
+ }
191
+ function contains(directory, path) {
192
+ return directory === '' || path === directory || path.startsWith(`${directory}/`);
193
+ }
194
+ /** One shell word: bare when nothing in it is special to `sh`, single-quoted otherwise. */
195
+ function shellWord(word) {
196
+ return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'"'"'`)}'`;
197
+ }