@smoothbricks/nx-plugin 0.4.21 → 0.4.22

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.
package/README.md CHANGED
@@ -114,15 +114,28 @@ require maintaining a second list of source roots. The command refuses missing d
114
114
  dependency cache instead of emitting a partial digest.
115
115
 
116
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:
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:
123
+
124
+ ```json
125
+ {
126
+ "namedInputs": {
127
+ "externalRustCrates": [{ "runtime": "smoo-nx-cargo-hash" }]
128
+ }
129
+ }
130
+ ```
131
+
132
+ and `packages/example/project.json` (or the `nx` key of its `package.json`) declares:
119
133
 
120
134
  ```json
121
135
  {
122
136
  "namedInputs": {
123
- "externalRustCrates": [{ "runtime": "smoo-nx-cargo-hash" }],
124
137
  "cargoSources": [
125
- { "runtime": "smoo-nx-cargo-hash --include-workspace" },
138
+ { "runtime": "smoo-nx-cargo-hash --include-workspace --closure packages/example" },
126
139
  "{workspaceRoot}/Cargo.lock",
127
140
  "{workspaceRoot}/devenv.lock",
128
141
  "{workspaceRoot}/tooling/direnv/devenv.lock"
@@ -133,12 +146,20 @@ offline Cargo metadata result. For a repository-root Cargo workspace:
133
146
 
134
147
  Name `cargoSources` in each custom Cargo target's `inputs`, alongside its own scripts and non-Rust build inputs. Keep
135
148
  `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.
139
-
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.
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.
152
+
153
+ Files git ignores beneath a package directory are not hashed, matching Nx's own file inputs. A generated source is its
154
+ producer's output: the consuming target depends on the producer and hashes the source through
155
+ `dependentTasksOutputFiles`, so a tree that has not generated it yet hashes like one that has. A package whose own
156
+ directory git ignores, such as one installed under `node_modules`, keeps all of its sources, and a directory outside any
157
+ Git repository ignores nothing.
158
+
159
+ The helper exposes the same choices as
160
+ `hashCargoPathInputs(manifestPath, workspaceRoot, { includeWorkspace: true, closure: 'packages/example' })`, resolving
161
+ `closure` like `manifestPath`. Omitting the options, or setting `includeWorkspace: false`, retains the external-only
162
+ behavior.
142
163
 
143
164
  Git and registry dependencies are identified by `Cargo.lock`; uncommitted changes in a Git repository are not changes to
144
165
  a pinned Git dependency. Local `path` dependencies stay live and their source edits change the digest. Build-script data
@@ -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`);
@@ -1,4 +1,11 @@
1
- export declare function hashCargoPathInputs(manifestPath: string, workspaceRoot: string, options?: {
1
+ export interface CargoPathInputsOptions {
2
+ /** Also hash packages inside both the Nx and Cargo workspaces, for custom targets without inferred Cargo inputs. */
2
3
  includeWorkspace?: boolean;
3
- }): Promise<string>;
4
+ /**
5
+ * Hash only the resolved dependency closure of the local packages whose manifests lie under this directory
6
+ * (resolved like `manifestPath`), instead of every package in the Cargo workspace.
7
+ */
8
+ closure?: string;
9
+ }
10
+ export declare function hashCargoPathInputs(manifestPath: string, workspaceRoot: string, options?: CargoPathInputsOptions): Promise<string>;
4
11
  //# 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":"AAkBA,MAAM,WAAW,sBAAsB;IACrC,oHAAoH;IACpH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AA4CD,wBAAsB,mBAAmB,CACvC,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAgHjB"}
@@ -1,38 +1,55 @@
1
1
  import * as _assertGuard_1 from "typia/lib/internal/_assertGuard";
2
- import { execFileSync } from 'node:child_process';
2
+ import { execFileSync, spawnSync } from 'node:child_process';
3
3
  import { createHash } from 'node:crypto';
4
- import { statSync } from 'node:fs';
4
+ import { realpathSync, statSync } from 'node:fs';
5
5
  import { lstat, readdir, readFile, readlink, realpath } from 'node:fs/promises';
6
6
  import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
7
7
  import typia from 'typia';
8
8
  const parseMetadata = (() => {
9
+ const _ip0 = input => "string" === typeof input["id"];
10
+ const _ap0 = (input, _path, _exceptionable = true) => "string" === typeof input["id"] || _assertGuard_1._assertGuard(_exceptionable, {
11
+ method: "typia.json.createAssertParse",
12
+ path: _path + ".id",
13
+ expected: "string",
14
+ value: input["id"]
15
+ }, _errorFactory);
9
16
  const _ao0 = (input, _path, _exceptionable = true) => ((Array.isArray(input.packages) || _assertGuard_1._assertGuard(_exceptionable, {
10
17
  method: "typia.json.createAssertParse",
11
18
  path: _path + ".packages",
12
- expected: "{ source: string | null; manifest_path: string; targets: { src_path: string; }[]; }[]",
19
+ expected: "{ id: string; source: string | null; manifest_path: string; targets: { src_path: string; }[]; }[]",
13
20
  value: input.packages
14
- }, _errorFactory)) && input.packages.every((elem, _index3) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
21
+ }, _errorFactory)) && input.packages.every((elem, _index5) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
15
22
  method: "typia.json.createAssertParse",
16
- path: _path + ".packages[" + _index3 + "]",
17
- expected: "{ source: string | null; manifest_path: string; targets: { src_path: string; }[]; }",
23
+ path: _path + ".packages[" + _index5 + "]",
24
+ expected: "{ id: string; source: string | null; manifest_path: string; targets: { src_path: string; }[]; }",
18
25
  value: elem
19
- }, _errorFactory)) && _ao1(elem, _path + ".packages[" + _index3 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
26
+ }, _errorFactory)) && _ao1(elem, _path + ".packages[" + _index5 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
20
27
  method: "typia.json.createAssertParse",
21
- path: _path + ".packages[" + _index3 + "]",
22
- expected: "{ source: string | null; manifest_path: string; targets: { src_path: string; }[]; }",
28
+ path: _path + ".packages[" + _index5 + "]",
29
+ expected: "{ id: string; source: string | null; manifest_path: string; targets: { src_path: string; }[]; }",
23
30
  value: elem
24
31
  }, _errorFactory)) || _assertGuard_1._assertGuard(_exceptionable, {
25
32
  method: "typia.json.createAssertParse",
26
33
  path: _path + ".packages",
27
- expected: "{ source: string | null; manifest_path: string; targets: { src_path: string; }[]; }[]",
34
+ expected: "{ id: string; source: string | null; manifest_path: string; targets: { src_path: string; }[]; }[]",
28
35
  value: input.packages
36
+ }, _errorFactory)) && (null === input.resolve || ("object" === typeof input.resolve && null !== input.resolve || _assertGuard_1._assertGuard(_exceptionable, {
37
+ method: "typia.json.createAssertParse",
38
+ path: _path + ".resolve",
39
+ expected: "(null | { nodes: { id: string; deps: { pkg: string; }[]; }[]; })",
40
+ value: input.resolve
41
+ }, _errorFactory)) && _ao3(input.resolve, _path + ".resolve", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
42
+ method: "typia.json.createAssertParse",
43
+ path: _path + ".resolve",
44
+ expected: "(null | { nodes: { id: string; deps: { pkg: string; }[]; }[]; })",
45
+ value: input.resolve
29
46
  }, _errorFactory)) && ("string" === typeof input.workspace_root || _assertGuard_1._assertGuard(_exceptionable, {
30
47
  method: "typia.json.createAssertParse",
31
48
  path: _path + ".workspace_root",
32
49
  expected: "string",
33
50
  value: input.workspace_root
34
51
  }, _errorFactory));
35
- const _ao1 = (input, _path, _exceptionable = true) => (null === input.source || "string" === typeof input.source || _assertGuard_1._assertGuard(_exceptionable, {
52
+ const _ao1 = (input, _path, _exceptionable = true) => _ap0(input, _path, true && _exceptionable) && (null === input.source || "string" === typeof input.source || _assertGuard_1._assertGuard(_exceptionable, {
36
53
  method: "typia.json.createAssertParse",
37
54
  path: _path + ".source",
38
55
  expected: "(null | string)",
@@ -47,14 +64,14 @@ const parseMetadata = (() => {
47
64
  path: _path + ".targets",
48
65
  expected: "{ src_path: string; }[]",
49
66
  value: input.targets
50
- }, _errorFactory)) && input.targets.every((elem, _index4) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
67
+ }, _errorFactory)) && input.targets.every((elem, _index6) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
51
68
  method: "typia.json.createAssertParse",
52
- path: _path + ".targets[" + _index4 + "]",
69
+ path: _path + ".targets[" + _index6 + "]",
53
70
  expected: "{ src_path: string; }",
54
71
  value: elem
55
- }, _errorFactory)) && _ao2(elem, _path + ".targets[" + _index4 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
72
+ }, _errorFactory)) && _ao2(elem, _path + ".targets[" + _index6 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
56
73
  method: "typia.json.createAssertParse",
57
- path: _path + ".targets[" + _index4 + "]",
74
+ path: _path + ".targets[" + _index6 + "]",
58
75
  expected: "{ src_path: string; }",
59
76
  value: elem
60
77
  }, _errorFactory)) || _assertGuard_1._assertGuard(_exceptionable, {
@@ -69,9 +86,60 @@ const parseMetadata = (() => {
69
86
  expected: "string",
70
87
  value: input.src_path
71
88
  }, _errorFactory);
72
- const _io0 = input => Array.isArray(input.packages) && input.packages.every(elem => "object" === typeof elem && null !== elem && _io1(elem)) && "string" === typeof input.workspace_root;
73
- const _io1 = input => (null === input.source || "string" === typeof input.source) && "string" === typeof input.manifest_path && (Array.isArray(input.targets) && input.targets.every(elem => "object" === typeof elem && null !== elem && _io2(elem)));
89
+ const _ao3 = (input, _path, _exceptionable = true) => (Array.isArray(input.nodes) || _assertGuard_1._assertGuard(_exceptionable, {
90
+ method: "typia.json.createAssertParse",
91
+ path: _path + ".nodes",
92
+ expected: "{ id: string; deps: { pkg: string; }[]; }[]",
93
+ value: input.nodes
94
+ }, _errorFactory)) && input.nodes.every((elem, _index7) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
95
+ method: "typia.json.createAssertParse",
96
+ path: _path + ".nodes[" + _index7 + "]",
97
+ expected: "{ id: string; deps: { pkg: string; }[]; }",
98
+ value: elem
99
+ }, _errorFactory)) && _ao4(elem, _path + ".nodes[" + _index7 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
100
+ method: "typia.json.createAssertParse",
101
+ path: _path + ".nodes[" + _index7 + "]",
102
+ expected: "{ id: string; deps: { pkg: string; }[]; }",
103
+ value: elem
104
+ }, _errorFactory)) || _assertGuard_1._assertGuard(_exceptionable, {
105
+ method: "typia.json.createAssertParse",
106
+ path: _path + ".nodes",
107
+ expected: "{ id: string; deps: { pkg: string; }[]; }[]",
108
+ value: input.nodes
109
+ }, _errorFactory);
110
+ const _ao4 = (input, _path, _exceptionable = true) => _ap0(input, _path, true && _exceptionable) && ((Array.isArray(input.deps) || _assertGuard_1._assertGuard(_exceptionable, {
111
+ method: "typia.json.createAssertParse",
112
+ path: _path + ".deps",
113
+ expected: "{ pkg: string; }[]",
114
+ value: input.deps
115
+ }, _errorFactory)) && input.deps.every((elem, _index8) => ("object" === typeof elem && null !== elem || _assertGuard_1._assertGuard(_exceptionable, {
116
+ method: "typia.json.createAssertParse",
117
+ path: _path + ".deps[" + _index8 + "]",
118
+ expected: "{ pkg: string; }",
119
+ value: elem
120
+ }, _errorFactory)) && _ao5(elem, _path + ".deps[" + _index8 + "]", true && _exceptionable) || _assertGuard_1._assertGuard(_exceptionable, {
121
+ method: "typia.json.createAssertParse",
122
+ path: _path + ".deps[" + _index8 + "]",
123
+ expected: "{ pkg: string; }",
124
+ value: elem
125
+ }, _errorFactory)) || _assertGuard_1._assertGuard(_exceptionable, {
126
+ method: "typia.json.createAssertParse",
127
+ path: _path + ".deps",
128
+ expected: "{ pkg: string; }[]",
129
+ value: input.deps
130
+ }, _errorFactory));
131
+ const _ao5 = (input, _path, _exceptionable = true) => "string" === typeof input.pkg || _assertGuard_1._assertGuard(_exceptionable, {
132
+ method: "typia.json.createAssertParse",
133
+ path: _path + ".pkg",
134
+ expected: "string",
135
+ value: input.pkg
136
+ }, _errorFactory);
137
+ const _io0 = input => Array.isArray(input.packages) && input.packages.every(elem => "object" === typeof elem && null !== elem && _io1(elem)) && (null === input.resolve || "object" === typeof input.resolve && null !== input.resolve && _io3(input.resolve)) && "string" === typeof input.workspace_root;
138
+ const _io1 = input => _ip0(input) && (null === input.source || "string" === typeof input.source) && "string" === typeof input.manifest_path && (Array.isArray(input.targets) && input.targets.every(elem => "object" === typeof elem && null !== elem && _io2(elem)));
74
139
  const _io2 = input => "string" === typeof input.src_path;
140
+ const _io3 = input => Array.isArray(input.nodes) && input.nodes.every(elem => "object" === typeof elem && null !== elem && _io4(elem));
141
+ const _io4 = input => _ip0(input) && (Array.isArray(input.deps) && input.deps.every(elem => "object" === typeof elem && null !== elem && _io5(elem)));
142
+ const _io5 = input => "string" === typeof input.pkg;
75
143
  const __is = input => "object" === typeof input && null !== input && _io0(input);
76
144
  let _errorFactory;
77
145
  const __assert = (input, errorFactory) => {
@@ -113,31 +181,49 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
113
181
  * registry packages are immutable inputs already identified by Cargo.lock.
114
182
  * By default, Nx's in-workspace inputs own workspace packages; includeWorkspace
115
183
  * also covers those packages for custom targets without inferred Cargo inputs.
184
+ * `closure` narrows the packages to what the local packages under one
185
+ * directory actually depend on, so an edit to an unrelated workspace member
186
+ * leaves the digest alone.
116
187
  *
117
188
  * This covers Rust sources and Cargo configuration, matching the plugin's
118
189
  * in-workspace Rust inputs. Build scripts' other data/environment inputs remain
119
- * explicit Nx inputs, just as they are for in-workspace crates.
190
+ * explicit Nx inputs, just as they are for in-workspace crates. Files git
191
+ * ignores beneath a package are left out, like Nx's own file inputs: a
192
+ * generated source is its producer's output, which the consuming target hashes
193
+ * through `dependentTasksOutputFiles`, and a tree that has not generated it yet
194
+ * must hash like one that has.
120
195
  */
121
196
  // Nx hashes a runtime input's stdout AND stderr. Cargo reports lock contention
122
197
  // on stderr ("Blocking waiting for file lock on package cache") a
123
198
  // timing-dependent number of times when Nx hashes many tasks at once, which
124
199
  // made one unchanged tree produce four different task hashes per run. Cargo's
125
200
  // stderr is kept for the failure path only, where execFileSync attaches it to
126
- // the thrown error.
127
- const CARGO_STDIO = ['ignore', 'pipe', 'pipe'];
201
+ // the thrown error. Git runs under the same rule.
202
+ const CHILD_STDIO = ['ignore', 'pipe', 'pipe'];
128
203
  export async function hashCargoPathInputs(manifestPath, workspaceRoot, options) {
129
204
  const root = await realpath(workspaceRoot);
130
- const metadata = parseMetadata(execFileSync('cargo', ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CARGO_STDIO }));
205
+ const metadata = parseMetadata(execFileSync('cargo', ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO }));
131
206
  const cargoRoot = await realpath(metadata.workspace_root);
207
+ const local = [];
208
+ for (const pkg of metadata.packages) {
209
+ if (pkg.source !== null)
210
+ continue;
211
+ const manifest = await realpath(pkg.manifest_path);
212
+ const sources = [];
213
+ for (const target of pkg.targets)
214
+ sources.push(await realpath(target.src_path));
215
+ local.push({ id: pkg.id, directory: dirname(manifest), manifest, sources });
216
+ }
217
+ const members = options?.closure === undefined ? undefined : await dependencyClosure(metadata, local, resolve(options.closure));
132
218
  const directories = new Set();
133
219
  const files = new Set();
134
220
  const ancestors = new Set();
135
221
  const links = new Map();
136
- for (const pkg of metadata.packages) {
137
- if (pkg.source !== null)
222
+ const packageSources = [];
223
+ for (const pkg of local) {
224
+ if (members !== undefined && !members.has(pkg.id))
138
225
  continue;
139
- const manifest = await realpath(pkg.manifest_path);
140
- const directory = dirname(manifest);
226
+ const directory = pkg.directory;
141
227
  if (!options?.includeWorkspace) {
142
228
  const fromRoot = relative(root, directory);
143
229
  const fromCargo = relative(cargoRoot, directory);
@@ -151,13 +237,12 @@ export async function hashCargoPathInputs(manifestPath, workspaceRoot, options)
151
237
  continue;
152
238
  }
153
239
  directories.add(directory);
154
- files.add(manifest);
240
+ files.add(pkg.manifest);
155
241
  // Cargo resolves explicit package.workspace ownership as well as ancestor
156
242
  // workspaces. Metadata's workspace_root describes only the invoking root.
157
- const governingManifest = execFileSync('cargo', ['locate-project', '--workspace', '--manifest-path', manifest, '--message-format', 'plain'], { cwd: root, encoding: 'utf8', stdio: CARGO_STDIO }).trim();
243
+ const governingManifest = execFileSync('cargo', ['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'], { cwd: root, encoding: 'utf8', stdio: CHILD_STDIO }).trim();
158
244
  files.add(await realpath(governingManifest));
159
- for (const target of pkg.targets)
160
- files.add(await realpath(target.src_path));
245
+ packageSources.push(...pkg.sources);
161
246
  // A member can inherit edition, lint policy, dependencies and
162
247
  // profiles from a workspace above its package directory. Those manifests
163
248
  // are not descendants of the source roots returned by Cargo metadata.
@@ -170,6 +255,10 @@ export async function hashCargoPathInputs(manifestPath, workspaceRoot, options)
170
255
  }
171
256
  }
172
257
  }
258
+ const ignored = gitIgnoredBeneath([...directories]);
259
+ for (const source of packageSources)
260
+ if (!ignored(source))
261
+ files.add(source);
173
262
  // Overlapping packages (a facade root plus its child crates) share one walk.
174
263
  const visited = new Set();
175
264
  async function walk(directory) {
@@ -181,6 +270,8 @@ export async function hashCargoPathInputs(manifestPath, workspaceRoot, options)
181
270
  if (ignoredDirectories.has(entry.name))
182
271
  continue;
183
272
  const path = join(canonical, entry.name);
273
+ if (ignored(path))
274
+ continue;
184
275
  if (entry.isSymbolicLink())
185
276
  links.set(path, await readlink(path));
186
277
  const info = entry.isSymbolicLink() ? await lstat(await realpath(path)) : entry;
@@ -217,3 +308,118 @@ export async function hashCargoPathInputs(manifestPath, workspaceRoot, options)
217
308
  }
218
309
  return hash.digest('hex');
219
310
  }
311
+ /**
312
+ * The ids Cargo's resolve graph reaches from the local packages under
313
+ * `directory`, over every dependency kind: a target may build, test or run
314
+ * build scripts, and any of those compiles the edge's source.
315
+ */
316
+ async function dependencyClosure(metadata, local, directory) {
317
+ const base = await realpath(directory);
318
+ const pending = local
319
+ .filter((pkg) => pkg.directory === base || pkg.directory.startsWith(`${base}${sep}`))
320
+ .map((pkg) => pkg.id);
321
+ if (pending.length === 0)
322
+ throw new Error(`no local Cargo package lies under ${directory}`);
323
+ if (metadata.resolve === null)
324
+ throw new Error('cargo metadata reported no dependency resolution');
325
+ const edges = new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps]));
326
+ const reached = new Set();
327
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
328
+ if (reached.has(id))
329
+ continue;
330
+ reached.add(id);
331
+ for (const dependency of edges.get(id) ?? [])
332
+ pending.push(dependency.pkg);
333
+ }
334
+ return reached;
335
+ }
336
+ /**
337
+ * Whether git ignores a path beneath one of the package directories. A
338
+ * package whose own directory is ignored (an installed dependency, say) keeps
339
+ * every file, and a directory outside any git repository ignores nothing.
340
+ */
341
+ function gitIgnoredBeneath(directories) {
342
+ const byRepository = new Map();
343
+ for (const directory of directories) {
344
+ const repository = gitRepository(directory);
345
+ if (repository === undefined)
346
+ continue;
347
+ const members = byRepository.get(repository) ?? [];
348
+ members.push(directory);
349
+ byRepository.set(repository, members);
350
+ }
351
+ const files = new Set();
352
+ const trees = [];
353
+ for (const [repository, members] of byRepository) {
354
+ const relativeMembers = members.map((member) => relative(repository, member) || '.');
355
+ // A package whose own directory git ignores is an installed tree, not a source tree with
356
+ // generated files in it. It also stays out of ls-files, which fails outright on a pathspec
357
+ // inside an ignored directory ("directory entry not superset of prefix").
358
+ const ignoredRoots = new Set(gitCheckIgnore(repository, relativeMembers));
359
+ const sourceRoots = relativeMembers.filter((member) => !ignoredRoots.has(member));
360
+ if (sourceRoots.length === 0)
361
+ continue;
362
+ const installed = [...ignoredRoots].map((member) => join(repository, member));
363
+ const listing = execFileSync('git', [
364
+ '--literal-pathspecs',
365
+ '-C',
366
+ repository,
367
+ 'ls-files',
368
+ '-z',
369
+ '--others',
370
+ '--ignored',
371
+ '--exclude-standard',
372
+ '--directory',
373
+ '--',
374
+ ...sourceRoots,
375
+ ], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO });
376
+ for (const entry of listing.split('\0')) {
377
+ if (entry.length === 0)
378
+ continue;
379
+ const path = join(repository, entry);
380
+ if (!entry.endsWith('/')) {
381
+ files.add(path);
382
+ }
383
+ else if (!installed.some((member) => member === path || member.startsWith(`${path}${sep}`))) {
384
+ // An ignored directory holding a nested installed package leaves that package whole.
385
+ trees.push(path);
386
+ }
387
+ }
388
+ }
389
+ return (path) => files.has(path) || trees.some((tree) => path === tree || path.startsWith(`${tree}${sep}`));
390
+ }
391
+ /** The members of `paths` (relative to `repository`) that git ignores, own patterns or an ancestor's. */
392
+ function gitCheckIgnore(repository, paths) {
393
+ const result = spawnSync('git', ['-C', repository, 'check-ignore', '-z', '--stdin'], {
394
+ input: `${paths.join('\0')}\0`,
395
+ encoding: 'utf8',
396
+ maxBuffer: 64 * 1024 * 1024,
397
+ });
398
+ // check-ignore answers 1 when nothing is ignored; only 0 carries a listing.
399
+ if (result.status === 1)
400
+ return [];
401
+ if (result.status !== 0) {
402
+ throw new Error(`git check-ignore in ${repository} failed (${result.status ?? result.signal}): ${result.stderr}`);
403
+ }
404
+ return result.stdout.split('\0').filter((path) => path.length > 0);
405
+ }
406
+ /** The canonical top level of the git work tree holding `directory`, or undefined outside any repository. */
407
+ function gitRepository(directory) {
408
+ try {
409
+ // The walk compares realpaths, so git's answer is canonicalized the same way.
410
+ return realpathSync(execFileSync('git', ['-C', directory, 'rev-parse', '--show-toplevel'], {
411
+ encoding: 'utf8',
412
+ stdio: CHILD_STDIO,
413
+ }).trim());
414
+ }
415
+ catch (error) {
416
+ if (typeof error === 'object' &&
417
+ error !== null &&
418
+ 'status' in error &&
419
+ error.status === 128 &&
420
+ 'stderr' in error &&
421
+ String(error.stderr).includes('not a git repository'))
422
+ return undefined;
423
+ throw error;
424
+ }
425
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.21",
3
+ "version": "0.4.22",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -1,18 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  import { resolve } from 'node:path';
3
- import { hashCargoPathInputs } from '../cargo-source-hash.js';
3
+ import { type CargoPathInputsOptions, hashCargoPathInputs } from '../cargo-source-hash.js';
4
4
 
5
- const args = process.argv.slice(2);
6
- const includeWorkspace = args[0] === '--include-workspace';
7
- const manifest = args[includeWorkspace ? 1 : 0];
8
- if (args.length > (includeWorkspace ? 2 : 1) || manifest?.startsWith('-')) {
9
- process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [Cargo.toml]\n');
5
+ /** Flags first, in any order and at most once each, then at most one manifest; anything else is refused. */
6
+ function parseArguments(args: readonly string[]): { manifest: string; options: CargoPathInputsOptions } | undefined {
7
+ const options: CargoPathInputsOptions = {};
8
+ let index = 0;
9
+ for (; index < args.length; index += 1) {
10
+ const arg = args[index];
11
+ if (arg === '--include-workspace' && options.includeWorkspace === undefined) {
12
+ options.includeWorkspace = true;
13
+ } else if (arg === '--closure' && options.closure === undefined) {
14
+ const directory = args[index + 1];
15
+ if (directory === undefined || directory.startsWith('-')) return undefined;
16
+ options.closure = directory;
17
+ index += 1;
18
+ } else if (arg?.startsWith('-')) {
19
+ return undefined;
20
+ } else {
21
+ break;
22
+ }
23
+ }
24
+ const rest = args.slice(index);
25
+ if (rest.length > 1) return undefined;
26
+ return { manifest: rest[0] ?? 'Cargo.toml', options };
27
+ }
28
+
29
+ const parsed = parseArguments(process.argv.slice(2));
30
+ if (parsed === undefined) {
31
+ process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [--closure <directory>] [Cargo.toml]\n');
10
32
  process.exit(2);
11
33
  }
12
34
  try {
13
- process.stdout.write(
14
- `${await hashCargoPathInputs(resolve(manifest ?? 'Cargo.toml'), process.cwd(), { includeWorkspace })}\n`,
15
- );
35
+ process.stdout.write(`${await hashCargoPathInputs(resolve(parsed.manifest), process.cwd(), parsed.options)}\n`);
16
36
  } catch (error) {
17
37
  process.stderr.write(`smoo-nx-cargo-hash: ${error instanceof Error ? error.message : String(error)}\n`);
18
38
  process.stderr.write(
@@ -95,7 +95,8 @@ async function workspaceCargoFixture(root: string) {
95
95
  const workspace = join(root, 'workspace');
96
96
  const manifest = join(workspace, 'Cargo.toml');
97
97
  const sources = {
98
- 'workspace/Cargo.toml': '[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf"]\nresolver="2"\n',
98
+ 'workspace/Cargo.toml':
99
+ '[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
99
100
  'workspace/packages/app/Cargo.toml':
100
101
  '[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
101
102
  'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
@@ -104,6 +105,8 @@ async function workspaceCargoFixture(root: string) {
104
105
  'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
105
106
  'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
106
107
  'workspace/crates/leaf/src/lib.rs': 'pub fn answer() -> u8 { 1 }\n',
108
+ 'workspace/crates/unrelated/Cargo.toml': '[package]\nname="unrelated"\nversion="0.1.0"\nedition="2021"\n',
109
+ 'workspace/crates/unrelated/src/lib.rs': 'pub fn unrelated() -> u8 { 3 }\n',
107
110
  'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
108
111
  'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
109
112
  };
@@ -167,11 +170,21 @@ it('include-workspace CLI accepts an optional manifest and refuses unsupported a
167
170
  expect(changed.stdout).not.toBe(included.stdout);
168
171
  expect(await run(['--include-workspace'])).toEqual(changed);
169
172
 
173
+ const closure = await run(['--include-workspace', '--closure', 'crates/leaf', 'Cargo.toml']);
174
+ expect(closure.exitCode, closure.stderr).toBe(0);
175
+ expect(closure.stderr).toBe('');
176
+ expect(closure.stdout).not.toBe(changed.stdout);
177
+ expect(await run(['--closure', 'crates/leaf', '--include-workspace'])).toEqual(closure);
178
+
170
179
  for (const args of [
171
180
  ['--unknown'],
172
181
  ['--include-workspace', '--unknown'],
173
182
  ['Cargo.toml', '--include-workspace'],
174
183
  ['--include-workspace', 'Cargo.toml', 'extra'],
184
+ ['--include-workspace', '--include-workspace'],
185
+ ['--closure'],
186
+ ['--closure', '--include-workspace'],
187
+ ['--closure', 'crates/leaf', '--closure', 'packages/app'],
175
188
  ]) {
176
189
  const refused = await run(args);
177
190
  expect(refused.exitCode, refused.stderr).toBe(2);
@@ -182,6 +195,88 @@ it('include-workspace CLI accepts an optional manifest and refuses unsupported a
182
195
  }
183
196
  });
184
197
 
198
+ it('closure hashes the dependency closure of one directory and ignores unrelated workspace members', async () => {
199
+ const root = await mkdtemp(join(tmpdir(), 'cargo-hash-closure-'));
200
+ try {
201
+ const { workspace, manifest, leafSource } = await workspaceCargoFixture(root);
202
+ const app = { includeWorkspace: true, closure: join(workspace, 'packages/app') };
203
+ const leaf = { includeWorkspace: true, closure: join(workspace, 'crates/leaf') };
204
+ const initial = await hashCargoPathInputs(manifest, workspace, app);
205
+ const leafOnly = await hashCargoPathInputs(manifest, workspace, leaf);
206
+ const whole = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
207
+
208
+ await writeFile(join(workspace, 'crates/unrelated/src/lib.rs'), 'pub fn unrelated() -> u8 { 4 }\n');
209
+ expect(await hashCargoPathInputs(manifest, workspace, app)).toBe(initial);
210
+ expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).not.toBe(whole);
211
+
212
+ await writeFile(join(workspace, 'crates/bridge/src/lib.rs'), 'pub fn answer() -> u8 { leaf::answer() }\n');
213
+ const bridgeChanged = await hashCargoPathInputs(manifest, workspace, app);
214
+ expect(bridgeChanged).not.toBe(initial);
215
+ expect(await hashCargoPathInputs(manifest, workspace, leaf)).toBe(leafOnly);
216
+
217
+ await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
218
+ expect(await hashCargoPathInputs(manifest, workspace, app)).not.toBe(bridgeChanged);
219
+ expect(await hashCargoPathInputs(manifest, workspace, leaf)).not.toBe(leafOnly);
220
+
221
+ // The external package is reached through bridge, so the app closure sees it and the leaf closure does not.
222
+ const appBefore = await hashCargoPathInputs(manifest, workspace, app);
223
+ const leafBefore = await hashCargoPathInputs(manifest, workspace, leaf);
224
+ await writeFile(join(root, 'external/src/lib.rs'), 'pub fn answer() -> u8 { 20 }\n');
225
+ expect(await hashCargoPathInputs(manifest, workspace, app)).not.toBe(appBefore);
226
+ expect(await hashCargoPathInputs(manifest, workspace, leaf)).toBe(leafBefore);
227
+
228
+ await mkdir(join(workspace, 'docs'), { recursive: true });
229
+ await expect(
230
+ hashCargoPathInputs(manifest, workspace, { includeWorkspace: true, closure: join(workspace, 'docs') }),
231
+ ).rejects.toThrow('no local Cargo package lies under');
232
+ } finally {
233
+ await rm(root, { recursive: true, force: true });
234
+ }
235
+ });
236
+
237
+ it('leaves gitignored sources beneath a package out of the digest but keeps an ignored package root', async () => {
238
+ const root = await mkdtemp(join(tmpdir(), 'cargo-hash-ignored-'));
239
+ try {
240
+ const { workspace, manifest } = await workspaceCargoFixture(root);
241
+ execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
242
+ await writeFile(join(workspace, '.gitignore'), 'target/\ngenerated.rs\ncrates/leaf/\n');
243
+ const options = { includeWorkspace: true };
244
+ const initial = await hashCargoPathInputs(manifest, workspace, options);
245
+
246
+ // A source a producer generates later: a fresh tree and a built one hash alike.
247
+ await writeFile(join(workspace, 'crates/bridge/src/generated.rs'), 'pub const GENERATED: u8 = 1;\n');
248
+ expect(await hashCargoPathInputs(manifest, workspace, options)).toBe(initial);
249
+
250
+ // An untracked source nobody ignores is an edit like any other.
251
+ await writeFile(join(workspace, 'crates/bridge/src/extra.rs'), 'pub const EXTRA: u8 = 1;\n');
252
+ const untracked = await hashCargoPathInputs(manifest, workspace, options);
253
+ expect(untracked).not.toBe(initial);
254
+
255
+ // A package whose own directory is ignored still hashes its sources.
256
+ await writeFile(join(workspace, 'crates/leaf/src/lib.rs'), 'pub fn answer() -> u8 { 3 }\n');
257
+ expect(await hashCargoPathInputs(manifest, workspace, options)).not.toBe(untracked);
258
+ } finally {
259
+ await rm(root, { recursive: true, force: true });
260
+ }
261
+ });
262
+
263
+ it('hashes a Cargo workspace that lives inside a directory its enclosing repository ignores', async () => {
264
+ // A sandbox's TMPDIR, or a checkout under another repository's ignored scratch directory: every
265
+ // package sits beneath one ignored ancestor, where `git ls-files` refuses the pathspec outright.
266
+ const repository = await mkdtemp(join(tmpdir(), 'cargo-hash-enclosed-'));
267
+ try {
268
+ execFileSync('git', ['init', '--quiet', repository], { stdio: 'pipe' });
269
+ await writeFile(join(repository, '.gitignore'), 'scratch/\n');
270
+ const { workspace, manifest, leafSource } = await workspaceCargoFixture(join(repository, 'scratch/deep'));
271
+ const options = { includeWorkspace: true };
272
+ const initial = await hashCargoPathInputs(manifest, workspace, options);
273
+ await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
274
+ expect(await hashCargoPathInputs(manifest, workspace, options)).not.toBe(initial);
275
+ } finally {
276
+ await rm(repository, { recursive: true, force: true });
277
+ }
278
+ });
279
+
185
280
  it.each(['helper', 'cli'])(
186
281
  'include-workspace keeps a custom Nx cached Cargo consumer fresh via %s',
187
282
  async (mode) => {
@@ -1,19 +1,31 @@
1
- import { execFileSync } from 'node:child_process';
1
+ import { execFileSync, spawnSync } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import { statSync } from 'node:fs';
3
+ import { realpathSync, statSync } from 'node:fs';
4
4
  import { lstat, readdir, readFile, readlink, realpath } from 'node:fs/promises';
5
5
  import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
6
6
  import typia from 'typia';
7
7
 
8
8
  interface CargoMetadata {
9
9
  packages: {
10
+ id: string;
10
11
  source: string | null;
11
12
  manifest_path: string;
12
13
  targets: { src_path: string }[];
13
14
  }[];
15
+ resolve: { nodes: { id: string; deps: { pkg: string }[] }[] } | null;
14
16
  workspace_root: string;
15
17
  }
16
18
 
19
+ export interface CargoPathInputsOptions {
20
+ /** Also hash packages inside both the Nx and Cargo workspaces, for custom targets without inferred Cargo inputs. */
21
+ includeWorkspace?: boolean;
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
+
17
29
  const parseMetadata = typia.json.createAssertParse<CargoMetadata>();
18
30
  const ignoredDirectories = new Set([
19
31
  'target',
@@ -36,41 +48,59 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
36
48
  * registry packages are immutable inputs already identified by Cargo.lock.
37
49
  * By default, Nx's in-workspace inputs own workspace packages; includeWorkspace
38
50
  * also covers those packages for custom targets without inferred Cargo inputs.
51
+ * `closure` narrows the packages to what the local packages under one
52
+ * directory actually depend on, so an edit to an unrelated workspace member
53
+ * leaves the digest alone.
39
54
  *
40
55
  * This covers Rust sources and Cargo configuration, matching the plugin's
41
56
  * in-workspace Rust inputs. Build scripts' other data/environment inputs remain
42
- * explicit Nx inputs, just as they are for in-workspace crates.
57
+ * explicit Nx inputs, just as they are for in-workspace crates. Files git
58
+ * ignores beneath a package are left out, like Nx's own file inputs: a
59
+ * generated source is its producer's output, which the consuming target hashes
60
+ * through `dependentTasksOutputFiles`, and a tree that has not generated it yet
61
+ * must hash like one that has.
43
62
  */
44
63
  // Nx hashes a runtime input's stdout AND stderr. Cargo reports lock contention
45
64
  // on stderr ("Blocking waiting for file lock on package cache") a
46
65
  // timing-dependent number of times when Nx hashes many tasks at once, which
47
66
  // made one unchanged tree produce four different task hashes per run. Cargo's
48
67
  // stderr is kept for the failure path only, where execFileSync attaches it to
49
- // the thrown error.
50
- const CARGO_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
68
+ // the thrown error. Git runs under the same rule.
69
+ const CHILD_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
51
70
 
52
71
  export async function hashCargoPathInputs(
53
72
  manifestPath: string,
54
73
  workspaceRoot: string,
55
- options?: { includeWorkspace?: boolean },
74
+ options?: CargoPathInputsOptions,
56
75
  ): Promise<string> {
57
76
  const root = await realpath(workspaceRoot);
58
77
  const metadata = parseMetadata(
59
78
  execFileSync(
60
79
  'cargo',
61
80
  ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
62
- { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CARGO_STDIO },
81
+ { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO },
63
82
  ),
64
83
  );
65
84
  const cargoRoot = await realpath(metadata.workspace_root);
85
+ const local: { id: string; directory: string; manifest: string; sources: string[] }[] = [];
86
+ for (const pkg of metadata.packages) {
87
+ if (pkg.source !== null) continue;
88
+ const manifest = await realpath(pkg.manifest_path);
89
+ const sources: string[] = [];
90
+ for (const target of pkg.targets) sources.push(await realpath(target.src_path));
91
+ local.push({ id: pkg.id, directory: dirname(manifest), manifest, sources });
92
+ }
93
+ const members =
94
+ options?.closure === undefined ? undefined : await dependencyClosure(metadata, local, resolve(options.closure));
95
+
66
96
  const directories = new Set<string>();
67
97
  const files = new Set<string>();
68
98
  const ancestors = new Set<string>();
69
99
  const links = new Map<string, string>();
70
- for (const pkg of metadata.packages) {
71
- if (pkg.source !== null) continue;
72
- const manifest = await realpath(pkg.manifest_path);
73
- const directory = dirname(manifest);
100
+ const packageSources: string[] = [];
101
+ for (const pkg of local) {
102
+ if (members !== undefined && !members.has(pkg.id)) continue;
103
+ const directory = pkg.directory;
74
104
  if (!options?.includeWorkspace) {
75
105
  const fromRoot = relative(root, directory);
76
106
  const fromCargo = relative(cargoRoot, directory);
@@ -86,16 +116,16 @@ export async function hashCargoPathInputs(
86
116
  continue;
87
117
  }
88
118
  directories.add(directory);
89
- files.add(manifest);
119
+ files.add(pkg.manifest);
90
120
  // Cargo resolves explicit package.workspace ownership as well as ancestor
91
121
  // workspaces. Metadata's workspace_root describes only the invoking root.
92
122
  const governingManifest = execFileSync(
93
123
  'cargo',
94
- ['locate-project', '--workspace', '--manifest-path', manifest, '--message-format', 'plain'],
95
- { cwd: root, encoding: 'utf8', stdio: CARGO_STDIO },
124
+ ['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
125
+ { cwd: root, encoding: 'utf8', stdio: CHILD_STDIO },
96
126
  ).trim();
97
127
  files.add(await realpath(governingManifest));
98
- for (const target of pkg.targets) files.add(await realpath(target.src_path));
128
+ packageSources.push(...pkg.sources);
99
129
  // A member can inherit edition, lint policy, dependencies and
100
130
  // profiles from a workspace above its package directory. Those manifests
101
131
  // are not descendants of the source roots returned by Cargo metadata.
@@ -108,6 +138,8 @@ export async function hashCargoPathInputs(
108
138
  }
109
139
  }
110
140
 
141
+ const ignored = gitIgnoredBeneath([...directories]);
142
+ for (const source of packageSources) if (!ignored(source)) files.add(source);
111
143
  // Overlapping packages (a facade root plus its child crates) share one walk.
112
144
  const visited = new Set<string>();
113
145
  async function walk(directory: string): Promise<void> {
@@ -117,6 +149,7 @@ export async function hashCargoPathInputs(
117
149
  for (const entry of await readdir(canonical, { withFileTypes: true })) {
118
150
  if (ignoredDirectories.has(entry.name)) continue;
119
151
  const path = join(canonical, entry.name);
152
+ if (ignored(path)) continue;
120
153
  if (entry.isSymbolicLink()) links.set(path, await readlink(path));
121
154
  const info = entry.isSymbolicLink() ? await lstat(await realpath(path)) : entry;
122
155
  if (info.isDirectory()) {
@@ -152,3 +185,124 @@ export async function hashCargoPathInputs(
152
185
  }
153
186
  return hash.digest('hex');
154
187
  }
188
+
189
+ /**
190
+ * The ids Cargo's resolve graph reaches from the local packages under
191
+ * `directory`, over every dependency kind: a target may build, test or run
192
+ * build scripts, and any of those compiles the edge's source.
193
+ */
194
+ async function dependencyClosure(
195
+ metadata: CargoMetadata,
196
+ local: readonly { id: string; directory: string }[],
197
+ directory: string,
198
+ ): Promise<ReadonlySet<string>> {
199
+ const base = await realpath(directory);
200
+ const pending = local
201
+ .filter((pkg) => pkg.directory === base || pkg.directory.startsWith(`${base}${sep}`))
202
+ .map((pkg) => pkg.id);
203
+ if (pending.length === 0) throw new Error(`no local Cargo package lies under ${directory}`);
204
+ if (metadata.resolve === null) throw new Error('cargo metadata reported no dependency resolution');
205
+ const edges = new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps]));
206
+ const reached = new Set<string>();
207
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
208
+ if (reached.has(id)) continue;
209
+ reached.add(id);
210
+ for (const dependency of edges.get(id) ?? []) pending.push(dependency.pkg);
211
+ }
212
+ return reached;
213
+ }
214
+
215
+ /**
216
+ * Whether git ignores a path beneath one of the package directories. A
217
+ * package whose own directory is ignored (an installed dependency, say) keeps
218
+ * every file, and a directory outside any git repository ignores nothing.
219
+ */
220
+ function gitIgnoredBeneath(directories: readonly string[]): (path: string) => boolean {
221
+ const byRepository = new Map<string, string[]>();
222
+ for (const directory of directories) {
223
+ const repository = gitRepository(directory);
224
+ if (repository === undefined) continue;
225
+ const members = byRepository.get(repository) ?? [];
226
+ members.push(directory);
227
+ byRepository.set(repository, members);
228
+ }
229
+ const files = new Set<string>();
230
+ const trees: string[] = [];
231
+ for (const [repository, members] of byRepository) {
232
+ const relativeMembers = members.map((member) => relative(repository, member) || '.');
233
+ // A package whose own directory git ignores is an installed tree, not a source tree with
234
+ // generated files in it. It also stays out of ls-files, which fails outright on a pathspec
235
+ // inside an ignored directory ("directory entry not superset of prefix").
236
+ const ignoredRoots = new Set(gitCheckIgnore(repository, relativeMembers));
237
+ const sourceRoots = relativeMembers.filter((member) => !ignoredRoots.has(member));
238
+ if (sourceRoots.length === 0) continue;
239
+ const installed = [...ignoredRoots].map((member) => join(repository, member));
240
+ const listing = execFileSync(
241
+ 'git',
242
+ [
243
+ '--literal-pathspecs',
244
+ '-C',
245
+ repository,
246
+ 'ls-files',
247
+ '-z',
248
+ '--others',
249
+ '--ignored',
250
+ '--exclude-standard',
251
+ '--directory',
252
+ '--',
253
+ ...sourceRoots,
254
+ ],
255
+ { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO },
256
+ );
257
+ for (const entry of listing.split('\0')) {
258
+ if (entry.length === 0) continue;
259
+ const path = join(repository, entry);
260
+ if (!entry.endsWith('/')) {
261
+ files.add(path);
262
+ } else if (!installed.some((member) => member === path || member.startsWith(`${path}${sep}`))) {
263
+ // An ignored directory holding a nested installed package leaves that package whole.
264
+ trees.push(path);
265
+ }
266
+ }
267
+ }
268
+ return (path) => files.has(path) || trees.some((tree) => path === tree || path.startsWith(`${tree}${sep}`));
269
+ }
270
+
271
+ /** The members of `paths` (relative to `repository`) that git ignores, own patterns or an ancestor's. */
272
+ function gitCheckIgnore(repository: string, paths: readonly string[]): string[] {
273
+ const result = spawnSync('git', ['-C', repository, 'check-ignore', '-z', '--stdin'], {
274
+ input: `${paths.join('\0')}\0`,
275
+ encoding: 'utf8',
276
+ maxBuffer: 64 * 1024 * 1024,
277
+ });
278
+ // check-ignore answers 1 when nothing is ignored; only 0 carries a listing.
279
+ if (result.status === 1) return [];
280
+ if (result.status !== 0) {
281
+ throw new Error(`git check-ignore in ${repository} failed (${result.status ?? result.signal}): ${result.stderr}`);
282
+ }
283
+ return result.stdout.split('\0').filter((path) => path.length > 0);
284
+ }
285
+
286
+ /** The canonical top level of the git work tree holding `directory`, or undefined outside any repository. */
287
+ function gitRepository(directory: string): string | undefined {
288
+ try {
289
+ // The walk compares realpaths, so git's answer is canonicalized the same way.
290
+ return realpathSync(
291
+ execFileSync('git', ['-C', directory, 'rev-parse', '--show-toplevel'], {
292
+ encoding: 'utf8',
293
+ stdio: CHILD_STDIO,
294
+ }).trim(),
295
+ );
296
+ } catch (error) {
297
+ if (
298
+ typeof error === 'object' &&
299
+ error !== null &&
300
+ 'status' in error &&
301
+ error.status === 128 &&
302
+ 'stderr' in error &&
303
+ String(error.stderr).includes('not a git repository')
304
+ )
305
+ return undefined;
306
+ throw error;
307
+ }
308
+ }