@smoothbricks/nx-plugin 0.4.21 → 0.4.23

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 (64) hide show
  1. package/README.md +95 -18
  2. package/dist/bin/smoo-nx-cargo-hash.js +32 -6
  3. package/dist/bin/smoo-nx-exec.js +35 -11
  4. package/dist/cargo-closure-input.d.ts +63 -0
  5. package/dist/cargo-closure-input.d.ts.map +1 -0
  6. package/dist/cargo-closure-input.js +197 -0
  7. package/dist/cargo-source-hash.d.ts +46 -2
  8. package/dist/cargo-source-hash.d.ts.map +1 -1
  9. package/dist/cargo-source-hash.js +238 -114
  10. package/dist/cross-check-policy.d.ts +14 -0
  11. package/dist/cross-check-policy.d.ts.map +1 -1
  12. package/dist/cross-check-policy.js +15 -1
  13. package/dist/ensure-built.d.ts +3 -2
  14. package/dist/ensure-built.d.ts.map +1 -1
  15. package/dist/ensure-built.js +33 -56
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +112 -76
  18. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  19. package/dist/managed-files/ci-workflow.js +12 -15
  20. package/dist/managed-files/files.d.ts.map +1 -1
  21. package/dist/managed-files/files.js +10 -0
  22. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  23. package/dist/managed-files/publish-workflow.js +12 -6
  24. package/dist/manifest-hash.d.ts.map +1 -1
  25. package/dist/manifest-hash.js +24 -30
  26. package/dist/package-target-policy.js +1 -1
  27. package/dist/platform-targets.d.ts +7 -0
  28. package/dist/platform-targets.d.ts.map +1 -1
  29. package/dist/platform-targets.js +7 -0
  30. package/dist/workspace-config-policy.d.ts +1 -1
  31. package/dist/workspace-config-policy.d.ts.map +1 -1
  32. package/dist/workspace-config-policy.js +1 -1
  33. package/managed/raw/envrc +38 -1
  34. package/managed/raw/tooling/direnv/developer-links.ts +201 -0
  35. package/managed/raw/tooling/direnv/devenv.smoo.nix +75 -51
  36. package/managed/raw/tooling/direnv/secret-references.ts +3 -1
  37. package/managed/raw/tooling/direnv/setup-environment.ts +562 -78
  38. package/managed/raw/tooling/direnv/shared-caches.sh +53 -0
  39. package/managed/raw/tooling/git-hooks/pre-push.sh +22 -1
  40. package/managed/templates/github/workflows/managed-files.yml +4 -3
  41. package/nextest.toml +5 -5
  42. package/package.json +2 -2
  43. package/src/__tests__/fixture-nx-env.ts +31 -0
  44. package/src/__tests__/in-process-nx-index.ts +9 -0
  45. package/src/bin/smoo-nx-cargo-hash.ts +29 -9
  46. package/src/bin/smoo-nx-exec.ts +40 -11
  47. package/src/cargo-closure-input.test.ts +184 -0
  48. package/src/cargo-closure-input.ts +247 -0
  49. package/src/cargo-source-hash.test.ts +104 -10
  50. package/src/cargo-source-hash.ts +290 -37
  51. package/src/cross-check-policy.ts +15 -3
  52. package/src/ensure-built.test.ts +65 -5
  53. package/src/ensure-built.ts +40 -85
  54. package/src/index.test.ts +94 -53
  55. package/src/index.ts +164 -84
  56. package/src/managed-files/ci-workflow.ts +12 -15
  57. package/src/managed-files/files.ts +10 -0
  58. package/src/managed-files/generator.test.ts +29 -0
  59. package/src/managed-files/publish-workflow.ts +17 -6
  60. package/src/manifest-hash.test.ts +21 -1
  61. package/src/manifest-hash.ts +26 -29
  62. package/src/package-target-policy.ts +1 -1
  63. package/src/platform-targets.ts +8 -0
  64. 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,16 +129,58 @@ 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 [Cargo.toml]` to include every mutable local package from the same locked,
118
- offline Cargo metadata result. For a repository-root Cargo workspace:
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:
138
+
139
+ ```json
140
+ {
141
+ "nx": {
142
+ "targets": {
143
+ "build": {
144
+ "inputs": ["{projectRoot}/scripts/build.sh", "cargoToolchain", "cargoClosure"]
145
+ }
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
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:
119
178
 
120
179
  ```json
121
180
  {
122
181
  "namedInputs": {
123
- "externalRustCrates": [{ "runtime": "smoo-nx-cargo-hash" }],
124
182
  "cargoSources": [
125
- { "runtime": "smoo-nx-cargo-hash --include-workspace" },
183
+ { "runtime": "smoo-nx-cargo-hash --include-workspace --closure packages/example" },
126
184
  "{workspaceRoot}/Cargo.lock",
127
185
  "{workspaceRoot}/devenv.lock",
128
186
  "{workspaceRoot}/tooling/direnv/devenv.lock"
@@ -131,14 +189,21 @@ offline Cargo metadata result. For a repository-root Cargo workspace:
131
189
  }
132
190
  ```
133
191
 
134
- Name `cargoSources` in each custom Cargo target's `inputs`, alongside its own scripts and non-Rust build inputs. Keep
135
- `externalRustCrates` for the plugin's external-source inference contract; unrelated TypeScript targets do not need
136
- `cargoSources`. For a package-root workspace, pass `--include-workspace packages/example/Cargo.toml` and name that
137
- workspace's `Cargo.lock`. An Nx `^production` input only follows existing project graph edges; it cannot replace a
138
- 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.
139
196
 
140
- The helper exposes the same choice as `hashCargoPathInputs(manifestPath, workspaceRoot, { includeWorkspace: true })`.
141
- Omitting the options, or setting `includeWorkspace: false`, retains the external-only behavior.
197
+ Files git ignores beneath a package directory are not hashed, matching Nx's own file inputs. A generated source is its
198
+ producer's output: the consuming target depends on the producer and hashes the source through
199
+ `dependentTasksOutputFiles`, so a tree that has not generated it yet hashes like one that has. A package whose own
200
+ directory git ignores, such as one installed under `node_modules`, keeps all of its sources, and a directory outside any
201
+ Git repository ignores nothing.
202
+
203
+ The helper exposes the same choices as
204
+ `hashCargoPathInputs(manifestPath, workspaceRoot, { includeWorkspace: true, closure: 'packages/example' })`, resolving
205
+ `closure` like `manifestPath`. Omitting the options, or setting `includeWorkspace: false`, retains the external-only
206
+ behavior.
142
207
 
143
208
  Git and registry dependencies are identified by `Cargo.lock`; uncommitted changes in a Git repository are not changes to
144
209
  a pinned Git dependency. Local `path` dependencies stay live and their source edits change the digest. Build-script data
@@ -349,10 +414,22 @@ It is deliberately not `nx run <target> && exec`:
349
414
  print.
350
415
  - It execs the binary, preserving its argv, exit status, and signals. `nx run` has no notion of handing the process over
351
416
  to another binary.
417
+ - A miss runs the target through the workspace's `nx` CLI with the child's stdout on stderr, so stdout carries only the
418
+ binary's output (`wrapper | jq` works). Nx's pseudo-terminal writes task output straight to file descriptor 1, which
419
+ only a child process can redirect.
352
420
 
353
421
  Pass `--workspace-root <dir>` before `--` when invoking the wrapper outside the workspace. The binary path resolves
354
422
  against the caller's current directory.
355
423
 
424
+ Without `--` and a binary, the wrapper only makes the target current and exits 0, or with the failed run's status:
425
+
426
+ ```bash
427
+ smoo-nx-exec my-cli:build --workspace-root "$root"
428
+ ```
429
+
430
+ A hit prints nothing, where `nx run` would replay every task's cached log. A miss prints Nx's run of the target on
431
+ stderr.
432
+
356
433
  ## Bun Test Tracing Generator
357
434
 
358
435
  Configure a package for the Bun test tracing + no-emit test typechecking pattern used in this repo.
@@ -1,15 +1,41 @@
1
1
  #!/usr/bin/env node
2
2
  import { resolve } from 'node:path';
3
3
  import { hashCargoPathInputs } from '../cargo-source-hash.js';
4
- const args = process.argv.slice(2);
5
- const includeWorkspace = args[0] === '--include-workspace';
6
- const manifest = args[includeWorkspace ? 1 : 0];
7
- if (args.length > (includeWorkspace ? 2 : 1) || manifest?.startsWith('-')) {
8
- process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [Cargo.toml]\n');
4
+ /** Flags first, in any order and at most once each, then at most one manifest; anything else is refused. */
5
+ function parseArguments(args) {
6
+ const options = {};
7
+ let index = 0;
8
+ for (; index < args.length; index += 1) {
9
+ const arg = args[index];
10
+ if (arg === '--include-workspace' && options.includeWorkspace === undefined) {
11
+ options.includeWorkspace = true;
12
+ }
13
+ else if (arg === '--closure' && options.closure === undefined) {
14
+ const directory = args[index + 1];
15
+ if (directory === undefined || directory.startsWith('-'))
16
+ return undefined;
17
+ options.closure = directory;
18
+ index += 1;
19
+ }
20
+ else if (arg?.startsWith('-')) {
21
+ return undefined;
22
+ }
23
+ else {
24
+ break;
25
+ }
26
+ }
27
+ const rest = args.slice(index);
28
+ if (rest.length > 1)
29
+ return undefined;
30
+ return { manifest: rest[0] ?? 'Cargo.toml', options };
31
+ }
32
+ const parsed = parseArguments(process.argv.slice(2));
33
+ if (parsed === undefined) {
34
+ process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [--closure <directory>] [Cargo.toml]\n');
9
35
  process.exit(2);
10
36
  }
11
37
  try {
12
- process.stdout.write(`${await hashCargoPathInputs(resolve(manifest ?? 'Cargo.toml'), process.cwd(), { includeWorkspace })}\n`);
38
+ process.stdout.write(`${await hashCargoPathInputs(resolve(parsed.manifest), process.cwd(), parsed.options)}\n`);
13
39
  }
14
40
  catch (error) {
15
41
  process.stderr.write(`smoo-nx-cargo-hash: ${error instanceof Error ? error.message : String(error)}\n`);
@@ -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,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
+ }
@@ -1,4 +1,48 @@
1
- export declare function hashCargoPathInputs(manifestPath: string, workspaceRoot: string, options?: {
1
+ /** A mutable (path) package in Cargo's locked resolve. Every path is canonical. */
2
+ export interface LocalCargoPackage {
3
+ readonly id: string;
4
+ readonly directory: string;
5
+ readonly manifest: string;
6
+ /** Every target's source file: library, binaries, tests, build script. A target may live outside `directory`. */
7
+ readonly sources: readonly string[];
8
+ }
9
+ /** What locked, offline `cargo metadata` reports about one Cargo workspace's mutable packages. */
10
+ export interface CargoResolve {
11
+ /** The canonical Cargo workspace root. */
12
+ readonly root: string;
13
+ /** The workspace's own members: `root`'s manifest governs each of them. */
14
+ readonly members: ReadonlySet<string>;
15
+ readonly local: readonly LocalCargoPackage[];
16
+ /** Every dependency edge by package id, or null when Cargo reported no resolution. */
17
+ readonly edges: ReadonlyMap<string, readonly string[]> | null;
18
+ }
19
+ export interface CargoPathInputsOptions {
20
+ /** Also hash packages inside both the Nx and Cargo workspaces, for custom targets without inferred Cargo inputs. */
2
21
  includeWorkspace?: boolean;
3
- }): Promise<string>;
22
+ /**
23
+ * Hash only the resolved dependency closure of the local packages whose manifests lie under this directory
24
+ * (resolved like `manifestPath`), instead of every package in the Cargo workspace.
25
+ */
26
+ closure?: string;
27
+ }
28
+ /** Directories the hash never descends into: build output, VCS and volume metadata, installed packages. */
29
+ export declare const HASH_SKIPPED_DIRECTORIES: readonly ['target', '.git', 'node_modules', '.cache', '.cowshed', '.fseventsd', '.TemporaryItems', '.Trashes', '.Spotlight-V100', '.DocumentRevisions-V100'];
30
+ /** Files in every ancestor of a package directory that Cargo reads while building it. */
31
+ export declare const CARGO_ANCESTOR_INPUTS: readonly ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
32
+ /** Locked, offline `cargo metadata` for the workspace `manifestPath` names, run from `cwd`. */
33
+ export declare function readCargoResolve(manifestPath: string, cwd: string): Promise<CargoResolve>;
34
+ /**
35
+ * The canonical root manifest of the Cargo workspace that governs `pkg`.
36
+ * Metadata's `workspace_root` describes only the invoking workspace, which is
37
+ * the answer for its own members. Any other path package asks Cargo, which
38
+ * resolves an explicit `package.workspace` as well as the ancestor search.
39
+ */
40
+ export declare function governingManifest(cargo: CargoResolve, pkg: LocalCargoPackage, cwd: string): Promise<string>;
41
+ export declare function hashCargoPathInputs(manifestPath: string, workspaceRoot: string, options?: CargoPathInputsOptions): Promise<string>;
42
+ /**
43
+ * The ids Cargo's resolve graph reaches from the local packages under
44
+ * `directory`, over every dependency kind: a target may build, test or run
45
+ * build scripts, and any of those compiles the edge's source.
46
+ */
47
+ export declare function dependencyClosure(cargo: CargoResolve, directory: string): Promise<ReadonlySet<string>>;
4
48
  //# sourceMappingURL=cargo-source-hash.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cargo-source-hash.d.ts","sourceRoot":"","sources":["../src/cargo-source-hash.ts"],"names":[],"mappings":"AAmDA,wBAAsB,mBAAmB,CACvC,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE;IAAE,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAAE,GACvC,OAAO,CAAC,MAAM,CAAC,CAkGjB"}
1
+ {"version":3,"file":"cargo-source-hash.d.ts","sourceRoot":"","sources":["../src/cargo-source-hash.ts"],"names":[],"mappings":"AAgEA,mFAAmF;AACnF,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,iHAAiH;IACjH,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;CACrC;AAED,kGAAkG;AAClG,MAAM,WAAW,YAAY;IAC3B,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,QAAQ,CAAC,KAAK,EAAE,SAAS,iBAAiB,EAAE,CAAC;IAC7C,sFAAsF;IACtF,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;CAC/D;AAED,MAAM,WAAW,sBAAsB;IACrC,oHAAoH;IACpH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAmBD,2GAA2G;AAC3G,eAAO,MAAM,wBAAwB,YACnC,QAAQ,EACR,MAAM,EACN,cAAc,EACd,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,UAAU,EACV,iBAAiB,EACjB,yBAAyB,CACjB,CAAC;AACX,yFAAyF;AACzF,eAAO,MAAM,qBAAqB,YAAI,YAAY,EAAE,eAAe,EAAE,oBAAoB,CAAU,CAAC;AA6BpG,+FAA+F;AAC/F,wBAAsB,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAwB/F;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAOjH;AAED,wBAAsB,mBAAmB,CACvC,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,MAAM,CAAC,CA0FjB;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAc5G"}