@smoothbricks/nx-plugin 0.4.22 → 0.4.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -33
- package/dist/bin/smoo-nx-exec.js +35 -11
- package/dist/bin/smoo-nx-nextest-extract.d.ts +3 -0
- package/dist/bin/smoo-nx-nextest-extract.d.ts.map +1 -0
- package/dist/bin/smoo-nx-nextest-extract.js +41 -0
- package/dist/cargo-closure-input.d.ts +63 -0
- package/dist/cargo-closure-input.d.ts.map +1 -0
- package/dist/cargo-closure-input.js +197 -0
- package/dist/cargo-source-hash.d.ts +37 -0
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +108 -190
- package/dist/cross-check-policy.d.ts +14 -0
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +15 -1
- package/dist/ensure-built.d.ts +3 -2
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +33 -56
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +152 -91
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +12 -15
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +10 -0
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +12 -6
- package/dist/manifest-hash.d.ts.map +1 -1
- package/dist/manifest-hash.js +24 -30
- package/dist/nextest-extraction.d.ts +101 -0
- package/dist/nextest-extraction.d.ts.map +1 -0
- package/dist/nextest-extraction.js +268 -0
- package/dist/package-target-policy.js +1 -1
- package/dist/platform-targets.d.ts +7 -0
- package/dist/platform-targets.d.ts.map +1 -1
- package/dist/platform-targets.js +7 -0
- package/dist/workspace-config-policy.d.ts +1 -1
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +1 -1
- package/managed/raw/envrc +38 -1
- package/managed/raw/tooling/direnv/developer-links.ts +201 -0
- package/managed/raw/tooling/direnv/devenv.smoo.nix +75 -51
- package/managed/raw/tooling/direnv/secret-references.ts +3 -1
- package/managed/raw/tooling/direnv/setup-environment.ts +562 -78
- package/managed/raw/tooling/direnv/shared-caches.sh +53 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +22 -1
- package/managed/templates/github/workflows/managed-files.yml +4 -3
- package/nextest.toml +5 -5
- package/package.json +4 -3
- package/src/__tests__/fixture-nx-env.ts +31 -0
- package/src/__tests__/in-process-nx-index.ts +9 -0
- package/src/bin/smoo-nx-exec.ts +40 -11
- package/src/bin/smoo-nx-nextest-extract.ts +43 -0
- package/src/bounded-test-policy.test.ts +4 -1
- package/src/cargo-closure-input.test.ts +184 -0
- package/src/cargo-closure-input.ts +247 -0
- package/src/cargo-source-hash.test.ts +8 -9
- package/src/cargo-source-hash.ts +146 -47
- package/src/cross-check-policy.ts +15 -3
- package/src/ensure-built.test.ts +65 -5
- package/src/ensure-built.ts +40 -85
- package/src/index.test.ts +222 -75
- package/src/index.ts +213 -101
- package/src/managed-files/ci-workflow.ts +12 -15
- package/src/managed-files/files.ts +10 -0
- package/src/managed-files/generator.test.ts +29 -0
- package/src/managed-files/publish-workflow.ts +17 -6
- package/src/manifest-hash.test.ts +21 -1
- package/src/manifest-hash.ts +26 -29
- package/src/nextest-extraction.test.ts +74 -0
- package/src/nextest-extraction.ts +329 -0
- package/src/package-target-policy.ts +1 -1
- package/src/platform-targets.ts +8 -0
- package/src/workspace-config-policy.ts +6 -1
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { realpath } from 'node:fs/promises';
|
|
2
|
+
import { isAbsolute, join, posix, relative, sep } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
|
|
5
|
+
import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context.js';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
CARGO_ANCESTOR_INPUTS,
|
|
9
|
+
type CargoResolve,
|
|
10
|
+
dependencyClosure,
|
|
11
|
+
governingManifest,
|
|
12
|
+
HASH_SKIPPED_DIRECTORIES,
|
|
13
|
+
readCargoResolve,
|
|
14
|
+
} from './cargo-source-hash.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The named input the plugin infers for every project with crates of a Cargo
|
|
18
|
+
* workspace under its root: the mutable sources of the Cargo-resolved
|
|
19
|
+
* dependency closure of those crates, and the lockfile that pins the rest.
|
|
20
|
+
*/
|
|
21
|
+
export const CARGO_CLOSURE_INPUT = 'cargoClosure';
|
|
22
|
+
|
|
23
|
+
type Input = NonNullable<TargetConfiguration['inputs']>[number];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How a runtime input reaches the hash command. Nx runs it with `sh -c` from
|
|
27
|
+
* the workspace root and puts no `node_modules/.bin` on its PATH; a plugin Nx
|
|
28
|
+
* loads by name is installed at exactly this path.
|
|
29
|
+
*/
|
|
30
|
+
const CARGO_HASH_COMMAND = 'node node_modules/@smoothbricks/nx-plugin/dist/bin/smoo-nx-cargo-hash.js';
|
|
31
|
+
|
|
32
|
+
/** A package's own Rust sources and Cargo configuration, exactly what the hash command's walk selects. */
|
|
33
|
+
const PACKAGE_FILES = '**/{*.rs,Cargo.toml,.cargo/config,.cargo/config.toml}';
|
|
34
|
+
const ANCESTOR_FILES = `{${CARGO_ANCESTOR_INPUTS.join(',')}}`;
|
|
35
|
+
const SKIPPED_TREES = `**/{${HASH_SKIPPED_DIRECTORIES.join(',')}}/**`;
|
|
36
|
+
/** A path holding any of these would be read as a pattern, not as itself. */
|
|
37
|
+
const GLOB_SYNTAX = /[*?[\]{}()!,]/;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One Cargo workspace as one project-graph computation sees it. `manifest` is
|
|
41
|
+
* the workspace's root manifest relative to the Nx workspace root.
|
|
42
|
+
*/
|
|
43
|
+
export type CargoClosureSource =
|
|
44
|
+
| {
|
|
45
|
+
readonly kind: 'resolved';
|
|
46
|
+
readonly manifest: string;
|
|
47
|
+
/** The canonical Nx workspace root. */
|
|
48
|
+
readonly root: string;
|
|
49
|
+
readonly cargo: CargoResolve;
|
|
50
|
+
/** Every `Cargo.toml` in Nx's file index, relative to the workspace root. */
|
|
51
|
+
readonly indexed: ReadonlySet<string>;
|
|
52
|
+
}
|
|
53
|
+
| { readonly kind: 'unresolved'; readonly manifest: string };
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Every `Cargo.toml` Nx's workspace file index holds: .gitignore and .nxignore
|
|
57
|
+
* applied, `node_modules` never entered. A package whose manifest is missing
|
|
58
|
+
* here has no files a fileset input can hash.
|
|
59
|
+
*/
|
|
60
|
+
export async function indexedCargoManifests(workspaceRoot: string): Promise<ReadonlySet<string>> {
|
|
61
|
+
return new Set(await globWithWorkspaceContext(workspaceRoot, ['**/Cargo.toml']));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Locked, offline `cargo metadata` for one Cargo workspace, once per graph
|
|
66
|
+
* computation. Never rejects: a workspace Cargo cannot resolve right now (no
|
|
67
|
+
* cargo on PATH, a stale lockfile, an unfetched dependency) still gets its
|
|
68
|
+
* closure inputs, hashed at run time by the command that reports the same
|
|
69
|
+
* failure wherever a task needs it. Said once here, not once per project.
|
|
70
|
+
*/
|
|
71
|
+
export async function resolveCargoClosureSource(
|
|
72
|
+
manifest: string,
|
|
73
|
+
workspaceRoot: string,
|
|
74
|
+
indexed: Promise<ReadonlySet<string>>,
|
|
75
|
+
): Promise<CargoClosureSource> {
|
|
76
|
+
try {
|
|
77
|
+
// One Promise.all, entered before any await, so a failed index listing is
|
|
78
|
+
// handled here even when it settles before Cargo does.
|
|
79
|
+
const [{ root, cargo }, index] = await Promise.all([
|
|
80
|
+
realpath(workspaceRoot).then(async (root) => ({
|
|
81
|
+
root,
|
|
82
|
+
cargo: await readCargoResolve(join(root, manifest), root),
|
|
83
|
+
})),
|
|
84
|
+
indexed,
|
|
85
|
+
]);
|
|
86
|
+
return { kind: 'resolved', manifest, root, cargo, indexed: index };
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
89
|
+
process.stderr.write(
|
|
90
|
+
`@smoothbricks/nx-plugin: resolving ${manifest} failed (${message.trim().split('\n').slice(0, 3).join(' ')}); ` +
|
|
91
|
+
`every ${CARGO_CLOSURE_INPUT} under it runs smoo-nx-cargo-hash on each hash instead of reading Nx's file index.\n`,
|
|
92
|
+
);
|
|
93
|
+
return { kind: 'unresolved', manifest };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The `cargoClosure` definition for the project at `projectRoot`: for each
|
|
99
|
+
* Cargo workspace with crates under it, the workspace's `Cargo.lock` plus the
|
|
100
|
+
* resolve closure of those crates.
|
|
101
|
+
*
|
|
102
|
+
* Closure members in Nx's file index are hashed by Nx itself, as filesets:
|
|
103
|
+
* each package's Rust sources, manifests and Cargo configuration below its
|
|
104
|
+
* directory, target sources outside it, its governing manifest, and the
|
|
105
|
+
* manifests and Cargo configuration of every directory from the package up
|
|
106
|
+
* to the workspace root. Nx keeps those file hashes current, so an unchanged
|
|
107
|
+
* tree costs no process at all. Members Nx cannot see — outside the
|
|
108
|
+
* workspace, or installed under `node_modules` — are hashed by one runtime
|
|
109
|
+
* `smoo-nx-cargo-hash --closure` entry, present only while such members
|
|
110
|
+
* exist. That command resolves the closure again when it runs, so a member
|
|
111
|
+
* that only an edit outside the workspace brings in is still covered.
|
|
112
|
+
*
|
|
113
|
+
* When the closure cannot be expressed that way — Cargo could not resolve it
|
|
114
|
+
* here, or a member inside the workspace is missing from Nx's file index —
|
|
115
|
+
* the definition is the complete runtime hash instead: correct, slower, and
|
|
116
|
+
* reported on stderr.
|
|
117
|
+
*/
|
|
118
|
+
export async function cargoClosureInputs(
|
|
119
|
+
projectRoot: string,
|
|
120
|
+
sources: readonly CargoClosureSource[],
|
|
121
|
+
): Promise<Input[]> {
|
|
122
|
+
const inputs: Input[] = [];
|
|
123
|
+
const seen = new Set<string>();
|
|
124
|
+
const ordered = [...sources].sort((left, right) =>
|
|
125
|
+
left.manifest < right.manifest ? -1 : left.manifest > right.manifest ? 1 : 0,
|
|
126
|
+
);
|
|
127
|
+
for (const source of ordered) {
|
|
128
|
+
for (const input of await workspaceClosureInputs(projectRoot, source)) {
|
|
129
|
+
const key = typeof input === 'string' ? input : JSON.stringify(input);
|
|
130
|
+
if (seen.has(key)) continue;
|
|
131
|
+
seen.add(key);
|
|
132
|
+
inputs.push(input);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return inputs;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function workspaceClosureInputs(projectRoot: string, source: CargoClosureSource): Promise<Input[]> {
|
|
139
|
+
const lock = posix.join('{workspaceRoot}', posix.dirname(source.manifest), 'Cargo.lock');
|
|
140
|
+
const closureArguments = `--closure ${shellWord(projectRoot)} ${shellWord(source.manifest)}`;
|
|
141
|
+
const runtimeHash: Input[] = [lock, { runtime: `${CARGO_HASH_COMMAND} --include-workspace ${closureArguments}` }];
|
|
142
|
+
if (source.kind === 'unresolved') return runtimeHash;
|
|
143
|
+
|
|
144
|
+
const { root, cargo, indexed } = source;
|
|
145
|
+
const packageDirectories: string[] = [];
|
|
146
|
+
const files: string[] = [];
|
|
147
|
+
let external = false;
|
|
148
|
+
try {
|
|
149
|
+
const members = await dependencyClosure(cargo, join(root, projectRoot));
|
|
150
|
+
for (const pkg of cargo.local) {
|
|
151
|
+
if (!members.has(pkg.id)) continue;
|
|
152
|
+
const directory = indexablePath(root, pkg.directory);
|
|
153
|
+
if (directory === null) {
|
|
154
|
+
external = true;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (!indexed.has(posix.join(directory, 'Cargo.toml'))) {
|
|
158
|
+
throw new Error(`the Cargo package ${directory} is not in Nx's file index (an ignored directory)`);
|
|
159
|
+
}
|
|
160
|
+
packageDirectories.push(directory);
|
|
161
|
+
for (const file of [...pkg.sources, await governingManifest(cargo, pkg, root)]) {
|
|
162
|
+
const path = indexablePath(root, file);
|
|
163
|
+
if (path === null) throw new Error(`${directory} compiles ${file}, which Nx's file index cannot hold`);
|
|
164
|
+
files.push(path);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const unglobbable = [...packageDirectories, ...files].find((path) => GLOB_SYNTAX.test(path));
|
|
168
|
+
if (unglobbable !== undefined) throw new Error(`${unglobbable} contains glob syntax, so no fileset can name it`);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
process.stderr.write(
|
|
171
|
+
`@smoothbricks/nx-plugin: ${projectRoot}'s ${CARGO_CLOSURE_INPUT} runs smoo-nx-cargo-hash on each hash ` +
|
|
172
|
+
`instead of reading Nx's file index: ${error instanceof Error ? error.message : String(error)}\n`,
|
|
173
|
+
);
|
|
174
|
+
return runtimeHash;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// A package nested in another member's directory is already covered by it.
|
|
178
|
+
const directories = [...new Set(packageDirectories)]
|
|
179
|
+
.sort()
|
|
180
|
+
.filter((directory, index, sorted) => !sorted.slice(0, index).some((outer) => contains(outer, directory)));
|
|
181
|
+
const ancestors = new Set<string>();
|
|
182
|
+
for (const directory of directories) {
|
|
183
|
+
for (let ancestor = parent(directory); ancestor !== null; ancestor = parent(ancestor)) {
|
|
184
|
+
const candidate = ancestor;
|
|
185
|
+
if (!directories.some((outer) => contains(outer, candidate))) ancestors.add(candidate);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// Target sources outside every package directory, and governing manifests
|
|
189
|
+
// that are not already one of the ancestors' manifests.
|
|
190
|
+
const loose = new Set(
|
|
191
|
+
files.filter(
|
|
192
|
+
(file) =>
|
|
193
|
+
!directories.some((directory) => contains(directory, file)) &&
|
|
194
|
+
!(posix.basename(file) === 'Cargo.toml' && ancestors.has(parent(file) ?? '')),
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
// Sibling packages share one pattern, `crates/{a,b}/…`, so a closure of
|
|
198
|
+
// dozens of crates reads as a few lines in `nx show project`.
|
|
199
|
+
const siblings = new Map<string, string[]>();
|
|
200
|
+
for (const directory of directories) {
|
|
201
|
+
const key = parent(directory);
|
|
202
|
+
if (key === null) continue;
|
|
203
|
+
siblings.set(key, [...(siblings.get(key) ?? []), posix.basename(directory)]);
|
|
204
|
+
}
|
|
205
|
+
const packageGlobs = directories.includes('')
|
|
206
|
+
? ['']
|
|
207
|
+
: [...siblings].map(([under, names]) =>
|
|
208
|
+
posix.join(under, names.length === 1 ? names.join('') : `{${names.join(',')}}`),
|
|
209
|
+
);
|
|
210
|
+
const anchored = (path: string, pattern = '') => posix.join('{workspaceRoot}', path, pattern);
|
|
211
|
+
return [
|
|
212
|
+
lock,
|
|
213
|
+
...packageGlobs.map((glob) => anchored(glob, PACKAGE_FILES)),
|
|
214
|
+
...packageGlobs.map((glob) => `!${anchored(glob, SKIPPED_TREES)}`),
|
|
215
|
+
...[...ancestors].sort().map((ancestor) => anchored(ancestor, ANCESTOR_FILES)),
|
|
216
|
+
...[...loose].sort().map((file) => anchored(file)),
|
|
217
|
+
...(external ? [{ runtime: `${CARGO_HASH_COMMAND} ${closureArguments}` }] : []),
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* `path` relative to the workspace root, `/`-separated, when it is somewhere
|
|
223
|
+
* Nx's file index can hold: inside the workspace and not under `node_modules`.
|
|
224
|
+
* The root itself is `''`.
|
|
225
|
+
*/
|
|
226
|
+
function indexablePath(root: string, path: string): string | null {
|
|
227
|
+
const fromRoot = relative(root, path);
|
|
228
|
+
if (fromRoot === '..' || fromRoot.startsWith(`..${sep}`) || isAbsolute(fromRoot)) return null;
|
|
229
|
+
const segments = fromRoot === '' ? [] : fromRoot.split(sep);
|
|
230
|
+
return segments.includes('node_modules') ? null : segments.join('/');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The directory holding `path`, `''` being the workspace root, which has none. */
|
|
234
|
+
function parent(path: string): string | null {
|
|
235
|
+
if (path === '') return null;
|
|
236
|
+
const directory = posix.dirname(path);
|
|
237
|
+
return directory === '.' ? '' : directory;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function contains(directory: string, path: string): boolean {
|
|
241
|
+
return directory === '' || path === directory || path.startsWith(`${directory}/`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** One shell word: bare when nothing in it is special to `sh`, single-quoted otherwise. */
|
|
245
|
+
function shellWord(word: string): string {
|
|
246
|
+
return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'"'"'`)}'`;
|
|
247
|
+
}
|
|
@@ -3,6 +3,7 @@ import { execFileSync } from 'node:child_process';
|
|
|
3
3
|
import { mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { dirname, join } from 'node:path';
|
|
6
|
+
import { fixtureNxEnv } from './__tests__/fixture-nx-env.js';
|
|
6
7
|
import { hashCargoPathInputs } from './cargo-source-hash.js';
|
|
7
8
|
|
|
8
9
|
it.each(['app', '.'])('invalidates transitive and inherited Cargo inputs with Nx rooted at %s', async (nxDirectory) => {
|
|
@@ -61,7 +62,12 @@ it('the runtime input is byte-identical under cargo lock contention: nothing on
|
|
|
61
62
|
await mkdir(join(root, 'src'), { recursive: true });
|
|
62
63
|
await writeFile(manifest, '[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[workspace]\n');
|
|
63
64
|
await writeFile(join(root, 'src', 'lib.rs'), 'pub fn run() {}\n');
|
|
64
|
-
|
|
65
|
+
// A runtime input inherits the Nx process's environment untouched. An Nx
|
|
66
|
+
// running inside another Nx task carries that task's FORCE_COLOR, and a
|
|
67
|
+
// shell may export NO_COLOR; with both set, Node warns on stderr under its
|
|
68
|
+
// own pid the moment anything imports `node:util`, so each run would
|
|
69
|
+
// hash differently.
|
|
70
|
+
const env = { ...process.env, CARGO_HOME: join(root, '.cargo-home'), FORCE_COLOR: 'true', NO_COLOR: '1' };
|
|
65
71
|
execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', manifest], { stdio: 'pipe', env });
|
|
66
72
|
// The built bin under node, exactly as nx.json's runtime input invokes it (test depends on build).
|
|
67
73
|
const bin = join(import.meta.dir, '..', 'dist', 'bin', 'smoo-nx-cargo-hash.js');
|
|
@@ -339,14 +345,7 @@ await appendFile(${JSON.stringify(executions)}, result);
|
|
|
339
345
|
const run = async () => {
|
|
340
346
|
const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
|
|
341
347
|
cwd: workspace,
|
|
342
|
-
env:
|
|
343
|
-
...process.env,
|
|
344
|
-
NX_WORKSPACE_ROOT_PATH: workspace,
|
|
345
|
-
NX_DAEMON: 'false',
|
|
346
|
-
NX_ISOLATE_PLUGINS: 'false',
|
|
347
|
-
NX_WORKSPACE_DATA_DIRECTORY: join(workspace, '.nx/workspace-data'),
|
|
348
|
-
NX_CACHE_DIRECTORY: join(workspace, '.nx/cache'),
|
|
349
|
-
},
|
|
348
|
+
env: fixtureNxEnv(workspace),
|
|
350
349
|
stdout: 'pipe',
|
|
351
350
|
stderr: 'pipe',
|
|
352
351
|
});
|
package/src/cargo-source-hash.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { execFileSync, spawnSync } from 'node:child_process';
|
|
1
|
+
import { execFile, execFileSync, spawnSync } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
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
|
-
import typia from 'typia';
|
|
7
6
|
|
|
7
|
+
/** The fields of `cargo metadata --format-version 1` this module reads. */
|
|
8
8
|
interface CargoMetadata {
|
|
9
9
|
packages: {
|
|
10
10
|
id: string;
|
|
@@ -13,9 +13,75 @@ interface CargoMetadata {
|
|
|
13
13
|
targets: { src_path: string }[];
|
|
14
14
|
}[];
|
|
15
15
|
resolve: { nodes: { id: string; deps: { pkg: string }[] }[] } | null;
|
|
16
|
+
workspace_members: string[];
|
|
16
17
|
workspace_root: string;
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Narrowed field by field, not by typia: graph inference imports this module,
|
|
22
|
+
* and a workspace that loads the plugin from its sources runs them without
|
|
23
|
+
* typia's compile-time transform.
|
|
24
|
+
*/
|
|
25
|
+
function parseCargoMetadata(json: string): CargoMetadata {
|
|
26
|
+
const member = (value: unknown, key: string): unknown =>
|
|
27
|
+
typeof value === 'object' && value !== null && key in value ? Reflect.get(value, key) : undefined;
|
|
28
|
+
const text = (value: unknown, field: string): string => {
|
|
29
|
+
if (typeof value !== 'string') throw new Error(`cargo metadata reported no valid ${field}`);
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
const list = (value: unknown, field: string): unknown[] => {
|
|
33
|
+
if (!Array.isArray(value)) throw new Error(`cargo metadata reported no valid ${field}`);
|
|
34
|
+
return value;
|
|
35
|
+
};
|
|
36
|
+
const document: unknown = JSON.parse(json);
|
|
37
|
+
const resolveGraph = member(document, 'resolve');
|
|
38
|
+
return {
|
|
39
|
+
packages: list(member(document, 'packages'), 'packages').map((pkg) => ({
|
|
40
|
+
id: text(member(pkg, 'id'), 'package id'),
|
|
41
|
+
source: member(pkg, 'source') === null ? null : text(member(pkg, 'source'), 'package source'),
|
|
42
|
+
manifest_path: text(member(pkg, 'manifest_path'), 'package manifest_path'),
|
|
43
|
+
targets: list(member(pkg, 'targets'), 'package targets').map((target) => ({
|
|
44
|
+
src_path: text(member(target, 'src_path'), 'target src_path'),
|
|
45
|
+
})),
|
|
46
|
+
})),
|
|
47
|
+
resolve:
|
|
48
|
+
resolveGraph === null
|
|
49
|
+
? null
|
|
50
|
+
: {
|
|
51
|
+
nodes: list(member(resolveGraph, 'nodes'), 'resolve').map((node) => ({
|
|
52
|
+
id: text(member(node, 'id'), 'resolve node id'),
|
|
53
|
+
deps: list(member(node, 'deps'), 'resolve node deps').map((dependency) => ({
|
|
54
|
+
pkg: text(member(dependency, 'pkg'), 'resolve edge'),
|
|
55
|
+
})),
|
|
56
|
+
})),
|
|
57
|
+
},
|
|
58
|
+
workspace_members: list(member(document, 'workspace_members'), 'workspace_members').map((id) =>
|
|
59
|
+
text(id, 'workspace member'),
|
|
60
|
+
),
|
|
61
|
+
workspace_root: text(member(document, 'workspace_root'), 'workspace_root'),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A mutable (path) package in Cargo's locked resolve. Every path is canonical. */
|
|
66
|
+
export interface LocalCargoPackage {
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly directory: string;
|
|
69
|
+
readonly manifest: string;
|
|
70
|
+
/** Every target's source file: library, binaries, tests, build script. A target may live outside `directory`. */
|
|
71
|
+
readonly sources: readonly string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What locked, offline `cargo metadata` reports about one Cargo workspace's mutable packages. */
|
|
75
|
+
export interface CargoResolve {
|
|
76
|
+
/** The canonical Cargo workspace root. */
|
|
77
|
+
readonly root: string;
|
|
78
|
+
/** The workspace's own members: `root`'s manifest governs each of them. */
|
|
79
|
+
readonly members: ReadonlySet<string>;
|
|
80
|
+
readonly local: readonly LocalCargoPackage[];
|
|
81
|
+
/** Every dependency edge by package id, or null when Cargo reported no resolution. */
|
|
82
|
+
readonly edges: ReadonlyMap<string, readonly string[]> | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
19
85
|
export interface CargoPathInputsOptions {
|
|
20
86
|
/** Also hash packages inside both the Nx and Cargo workspaces, for custom targets without inferred Cargo inputs. */
|
|
21
87
|
includeWorkspace?: boolean;
|
|
@@ -26,8 +92,25 @@ export interface CargoPathInputsOptions {
|
|
|
26
92
|
closure?: string;
|
|
27
93
|
}
|
|
28
94
|
|
|
29
|
-
|
|
30
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Cargo's stdout, or its failure with Cargo's stderr in the message. Not
|
|
97
|
+
* `promisify` from `node:util`: importing that module makes Node probe color
|
|
98
|
+
* support at startup, and with FORCE_COLOR and NO_COLOR both set (an Nx
|
|
99
|
+
* inside an Nx task, in a shell that exports NO_COLOR) it warns on stderr
|
|
100
|
+
* under its own pid, which a runtime input would hash as a new digest on
|
|
101
|
+
* every run.
|
|
102
|
+
*/
|
|
103
|
+
function runCargo(args: readonly string[], cwd: string): Promise<string> {
|
|
104
|
+
// The executor form: the workspace compiles against es2022, which has no `Promise.withResolvers`.
|
|
105
|
+
return new Promise((settle, reject) => {
|
|
106
|
+
execFile('cargo', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }, (error, stdout) =>
|
|
107
|
+
error === null ? settle(stdout) : reject(error),
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Directories the hash never descends into: build output, VCS and volume metadata, installed packages. */
|
|
113
|
+
export const HASH_SKIPPED_DIRECTORIES = [
|
|
31
114
|
'target',
|
|
32
115
|
'.git',
|
|
33
116
|
'node_modules',
|
|
@@ -38,8 +121,9 @@ const ignoredDirectories = new Set([
|
|
|
38
121
|
'.Trashes',
|
|
39
122
|
'.Spotlight-V100',
|
|
40
123
|
'.DocumentRevisions-V100',
|
|
41
|
-
]
|
|
42
|
-
|
|
124
|
+
] as const;
|
|
125
|
+
/** Files in every ancestor of a package directory that Cargo reads while building it. */
|
|
126
|
+
export const CARGO_ANCESTOR_INPUTS = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'] as const;
|
|
43
127
|
|
|
44
128
|
/**
|
|
45
129
|
* Cargo owns resolution, including workspace inheritance, target dependencies,
|
|
@@ -64,46 +148,72 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
|
|
|
64
148
|
// on stderr ("Blocking waiting for file lock on package cache") a
|
|
65
149
|
// timing-dependent number of times when Nx hashes many tasks at once, which
|
|
66
150
|
// made one unchanged tree produce four different task hashes per run. Cargo's
|
|
67
|
-
// stderr is kept for the failure path only, where
|
|
68
|
-
//
|
|
151
|
+
// stderr is kept for the failure path only, where execFile attaches it to the
|
|
152
|
+
// rejected error. Git runs under the same rule.
|
|
69
153
|
const CHILD_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
|
|
70
154
|
|
|
155
|
+
/** Locked, offline `cargo metadata` for the workspace `manifestPath` names, run from `cwd`. */
|
|
156
|
+
export async function readCargoResolve(manifestPath: string, cwd: string): Promise<CargoResolve> {
|
|
157
|
+
const stdout = await runCargo(
|
|
158
|
+
['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
|
|
159
|
+
cwd,
|
|
160
|
+
);
|
|
161
|
+
const metadata = parseCargoMetadata(stdout);
|
|
162
|
+
const local = await Promise.all(
|
|
163
|
+
metadata.packages
|
|
164
|
+
.filter((pkg) => pkg.source === null)
|
|
165
|
+
.map(async (pkg): Promise<LocalCargoPackage> => {
|
|
166
|
+
const manifest = await realpath(pkg.manifest_path);
|
|
167
|
+
const sources = await Promise.all(pkg.targets.map((target) => realpath(target.src_path)));
|
|
168
|
+
return { id: pkg.id, directory: dirname(manifest), manifest, sources };
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
return {
|
|
172
|
+
root: await realpath(metadata.workspace_root),
|
|
173
|
+
members: new Set(metadata.workspace_members),
|
|
174
|
+
local,
|
|
175
|
+
edges:
|
|
176
|
+
metadata.resolve === null
|
|
177
|
+
? null
|
|
178
|
+
: new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps.map((dependency) => dependency.pkg)])),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The canonical root manifest of the Cargo workspace that governs `pkg`.
|
|
184
|
+
* Metadata's `workspace_root` describes only the invoking workspace, which is
|
|
185
|
+
* the answer for its own members. Any other path package asks Cargo, which
|
|
186
|
+
* resolves an explicit `package.workspace` as well as the ancestor search.
|
|
187
|
+
*/
|
|
188
|
+
export async function governingManifest(cargo: CargoResolve, pkg: LocalCargoPackage, cwd: string): Promise<string> {
|
|
189
|
+
if (cargo.members.has(pkg.id)) return realpath(join(cargo.root, 'Cargo.toml'));
|
|
190
|
+
const stdout = await runCargo(
|
|
191
|
+
['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
|
|
192
|
+
cwd,
|
|
193
|
+
);
|
|
194
|
+
return realpath(stdout.trim());
|
|
195
|
+
}
|
|
196
|
+
|
|
71
197
|
export async function hashCargoPathInputs(
|
|
72
198
|
manifestPath: string,
|
|
73
199
|
workspaceRoot: string,
|
|
74
200
|
options?: CargoPathInputsOptions,
|
|
75
201
|
): Promise<string> {
|
|
76
202
|
const root = await realpath(workspaceRoot);
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
'cargo',
|
|
80
|
-
['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
|
|
81
|
-
{ cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO },
|
|
82
|
-
),
|
|
83
|
-
);
|
|
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));
|
|
203
|
+
const cargo = await readCargoResolve(manifestPath, root);
|
|
204
|
+
const members = options?.closure === undefined ? undefined : await dependencyClosure(cargo, resolve(options.closure));
|
|
95
205
|
|
|
96
206
|
const directories = new Set<string>();
|
|
97
207
|
const files = new Set<string>();
|
|
98
208
|
const ancestors = new Set<string>();
|
|
99
209
|
const links = new Map<string, string>();
|
|
100
210
|
const packageSources: string[] = [];
|
|
101
|
-
for (const pkg of local) {
|
|
211
|
+
for (const pkg of cargo.local) {
|
|
102
212
|
if (members !== undefined && !members.has(pkg.id)) continue;
|
|
103
213
|
const directory = pkg.directory;
|
|
104
214
|
if (!options?.includeWorkspace) {
|
|
105
215
|
const fromRoot = relative(root, directory);
|
|
106
|
-
const fromCargo = relative(
|
|
216
|
+
const fromCargo = relative(cargo.root, directory);
|
|
107
217
|
if (
|
|
108
218
|
fromRoot !== '..' &&
|
|
109
219
|
!fromRoot.startsWith(`..${sep}`) &&
|
|
@@ -117,21 +227,14 @@ export async function hashCargoPathInputs(
|
|
|
117
227
|
}
|
|
118
228
|
directories.add(directory);
|
|
119
229
|
files.add(pkg.manifest);
|
|
120
|
-
|
|
121
|
-
// workspaces. Metadata's workspace_root describes only the invoking root.
|
|
122
|
-
const governingManifest = execFileSync(
|
|
123
|
-
'cargo',
|
|
124
|
-
['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
|
|
125
|
-
{ cwd: root, encoding: 'utf8', stdio: CHILD_STDIO },
|
|
126
|
-
).trim();
|
|
127
|
-
files.add(await realpath(governingManifest));
|
|
230
|
+
files.add(await governingManifest(cargo, pkg, root));
|
|
128
231
|
packageSources.push(...pkg.sources);
|
|
129
232
|
// A member can inherit edition, lint policy, dependencies and
|
|
130
233
|
// profiles from a workspace above its package directory. Those manifests
|
|
131
234
|
// are not descendants of the source roots returned by Cargo metadata.
|
|
132
235
|
for (let ancestor = dirname(directory); !ancestors.has(ancestor); ancestor = dirname(ancestor)) {
|
|
133
236
|
ancestors.add(ancestor);
|
|
134
|
-
for (const name of
|
|
237
|
+
for (const name of CARGO_ANCESTOR_INPUTS) {
|
|
135
238
|
const file = join(ancestor, name);
|
|
136
239
|
if (statSync(file, { throwIfNoEntry: false })?.isFile()) files.add(await realpath(file));
|
|
137
240
|
}
|
|
@@ -140,6 +243,7 @@ export async function hashCargoPathInputs(
|
|
|
140
243
|
|
|
141
244
|
const ignored = gitIgnoredBeneath([...directories]);
|
|
142
245
|
for (const source of packageSources) if (!ignored(source)) files.add(source);
|
|
246
|
+
const skipped: ReadonlySet<string> = new Set(HASH_SKIPPED_DIRECTORIES);
|
|
143
247
|
// Overlapping packages (a facade root plus its child crates) share one walk.
|
|
144
248
|
const visited = new Set<string>();
|
|
145
249
|
async function walk(directory: string): Promise<void> {
|
|
@@ -147,7 +251,7 @@ export async function hashCargoPathInputs(
|
|
|
147
251
|
if (visited.has(canonical)) return;
|
|
148
252
|
visited.add(canonical);
|
|
149
253
|
for (const entry of await readdir(canonical, { withFileTypes: true })) {
|
|
150
|
-
if (
|
|
254
|
+
if (skipped.has(entry.name)) continue;
|
|
151
255
|
const path = join(canonical, entry.name);
|
|
152
256
|
if (ignored(path)) continue;
|
|
153
257
|
if (entry.isSymbolicLink()) links.set(path, await readlink(path));
|
|
@@ -191,23 +295,18 @@ export async function hashCargoPathInputs(
|
|
|
191
295
|
* `directory`, over every dependency kind: a target may build, test or run
|
|
192
296
|
* build scripts, and any of those compiles the edge's source.
|
|
193
297
|
*/
|
|
194
|
-
async function dependencyClosure(
|
|
195
|
-
metadata: CargoMetadata,
|
|
196
|
-
local: readonly { id: string; directory: string }[],
|
|
197
|
-
directory: string,
|
|
198
|
-
): Promise<ReadonlySet<string>> {
|
|
298
|
+
export async function dependencyClosure(cargo: CargoResolve, directory: string): Promise<ReadonlySet<string>> {
|
|
199
299
|
const base = await realpath(directory);
|
|
200
|
-
const pending = local
|
|
300
|
+
const pending = cargo.local
|
|
201
301
|
.filter((pkg) => pkg.directory === base || pkg.directory.startsWith(`${base}${sep}`))
|
|
202
302
|
.map((pkg) => pkg.id);
|
|
203
303
|
if (pending.length === 0) throw new Error(`no local Cargo package lies under ${directory}`);
|
|
204
|
-
if (
|
|
205
|
-
const edges = new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps]));
|
|
304
|
+
if (cargo.edges === null) throw new Error('cargo metadata reported no dependency resolution');
|
|
206
305
|
const reached = new Set<string>();
|
|
207
306
|
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
208
307
|
if (reached.has(id)) continue;
|
|
209
308
|
reached.add(id);
|
|
210
|
-
for (const dependency of edges.get(id) ?? []) pending.push(dependency
|
|
309
|
+
for (const dependency of cargo.edges.get(id) ?? []) pending.push(dependency);
|
|
211
310
|
}
|
|
212
311
|
return reached;
|
|
213
312
|
}
|
|
@@ -116,14 +116,26 @@ export const CARGO_FETCH_COMMAND = 'cargo fetch --locked';
|
|
|
116
116
|
* or replaying it through --config changes path resolution or precedence.
|
|
117
117
|
* Cargo may serialize registry access on its package-cache lock; these checks
|
|
118
118
|
* are unbounded, and correctness takes priority over overlapping that access.
|
|
119
|
+
*
|
|
120
|
+
* `target/cargo-lint-cross` stays its own directory: a foreign triple's units
|
|
121
|
+
* are distinct anyway, and its own lock keeps the cross check from queueing
|
|
122
|
+
* behind host builds.
|
|
119
123
|
*/
|
|
120
124
|
export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${cargoFrozen(
|
|
121
125
|
`clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross -- -D warnings`,
|
|
122
126
|
)}`;
|
|
123
127
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
128
|
+
/**
|
|
129
|
+
* No `--target-dir`: clippy writes the workspace's own `target/`, beside every
|
|
130
|
+
* dev build. Check units carry their mode in the unit hash, so they never
|
|
131
|
+
* collide with build units, and the host units a dev build already compiled —
|
|
132
|
+
* build scripts and their host-only dependencies — are reused rather than
|
|
133
|
+
* compiled a second time in a directory of their own. A dependency that both
|
|
134
|
+
* a proc-macro and ordinary code use stays a distinct unit: a dev build
|
|
135
|
+
* compiles it once at the target profile, while a check build needs a separate
|
|
136
|
+
* host copy. Distinct hashes coexist, so neither command dirties the other.
|
|
137
|
+
*/
|
|
138
|
+
export const CARGO_LINT_CLIPPY_COMMAND = cargoFrozen('clippy --workspace --all-targets -- -D warnings');
|
|
127
139
|
|
|
128
140
|
/** Root `package.json` script name, in the repo's `verb:qualifier` style. */
|
|
129
141
|
export const CROSS_CHECK_SCRIPT_NAME = 'check:linux';
|