@smoothbricks/nx-plugin 0.4.20 → 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 +89 -18
- package/dist/bin/smoo-nx-cargo-hash.js +32 -4
- package/dist/cargo-source-hash.d.ts +10 -1
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +251 -41
- package/dist/cargo-workspace.d.ts +0 -1
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +9 -2
- package/dist/ensure-built.d.ts +6 -5
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +227 -30
- package/dist/index.js +18 -10
- package/dist/managed-files/ci-workflow.d.ts +20 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +42 -13
- package/dist/managed-files/files.d.ts +1 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +8 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +37 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +34 -6
- package/dist/workspace-manifest.d.ts +3 -1
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/managed/raw/tooling/direnv/setup-environment.ts +23 -91
- package/managed/raw/tooling/git-hooks/commit-msg.sh +6 -0
- package/managed/raw/tooling/git-hooks/pre-commit.sh +6 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +6 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +19 -7
- package/package.json +1 -1
- package/src/bin/smoo-nx-cargo-hash.ts +29 -5
- package/src/cargo-source-hash.test.ts +291 -1
- package/src/cargo-source-hash.ts +190 -28
- package/src/cargo-workspace.test.ts +17 -10
- package/src/cargo-workspace.ts +9 -3
- package/src/ensure-built.test.ts +87 -29
- package/src/ensure-built.ts +274 -29
- package/src/index.test.ts +117 -20
- package/src/index.ts +18 -11
- package/src/managed-files/ci-workflow.ts +45 -14
- package/src/managed-files/files.ts +9 -2
- package/src/managed-files/generator.test.ts +5 -1
- package/src/managed-files/github-host-cache-isolation.test.ts +32 -6
- package/src/managed-files/pr-preview-cleanup-workflow.ts +87 -6
- package/src/workspace-manifest.ts +3 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect, it } from 'bun:test';
|
|
2
2
|
import { execFileSync } from 'node:child_process';
|
|
3
|
-
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
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
6
|
import { hashCargoPathInputs } from './cargo-source-hash.js';
|
|
@@ -90,3 +90,293 @@ it('the runtime input is byte-identical under cargo lock contention: nothing on
|
|
|
90
90
|
await rm(root, { recursive: true, force: true });
|
|
91
91
|
}
|
|
92
92
|
});
|
|
93
|
+
|
|
94
|
+
async function workspaceCargoFixture(root: string) {
|
|
95
|
+
const workspace = join(root, 'workspace');
|
|
96
|
+
const manifest = join(workspace, 'Cargo.toml');
|
|
97
|
+
const sources = {
|
|
98
|
+
'workspace/Cargo.toml':
|
|
99
|
+
'[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
|
|
100
|
+
'workspace/packages/app/Cargo.toml':
|
|
101
|
+
'[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
|
|
102
|
+
'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
|
|
103
|
+
'workspace/crates/bridge/Cargo.toml':
|
|
104
|
+
'[package]\nname="bridge"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nleaf={path="../leaf"}\nexternal={path="../../../external"}\n',
|
|
105
|
+
'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
|
|
106
|
+
'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
|
|
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',
|
|
110
|
+
'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
|
|
111
|
+
'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
|
|
112
|
+
};
|
|
113
|
+
for (const [path, text] of Object.entries(sources)) {
|
|
114
|
+
const file = join(root, path);
|
|
115
|
+
await mkdir(dirname(file), { recursive: true });
|
|
116
|
+
await writeFile(file, text);
|
|
117
|
+
}
|
|
118
|
+
execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', manifest], { stdio: 'pipe' });
|
|
119
|
+
return { workspace, manifest, leafSource: join(workspace, 'crates/leaf/src/lib.rs') };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
it('include-workspace hashes in-workspace transitive source edits without changing the external-only contract', async () => {
|
|
123
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-hash-workspace-'));
|
|
124
|
+
try {
|
|
125
|
+
const { workspace, manifest, leafSource } = await workspaceCargoFixture(root);
|
|
126
|
+
const external = await hashCargoPathInputs(manifest, workspace);
|
|
127
|
+
const included = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
|
|
128
|
+
expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).toBe(included);
|
|
129
|
+
|
|
130
|
+
await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
|
|
131
|
+
expect(await hashCargoPathInputs(manifest, workspace)).toBe(external);
|
|
132
|
+
const changed = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
|
|
133
|
+
expect(changed).not.toBe(included);
|
|
134
|
+
expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).toBe(changed);
|
|
135
|
+
|
|
136
|
+
await writeFile(join(root, 'external/src/lib.rs'), 'pub fn answer() -> u8 { 20 }\n');
|
|
137
|
+
expect(await hashCargoPathInputs(manifest, workspace)).not.toBe(external);
|
|
138
|
+
expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).not.toBe(changed);
|
|
139
|
+
} finally {
|
|
140
|
+
await rm(root, { recursive: true, force: true });
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('include-workspace CLI accepts an optional manifest and refuses unsupported arguments', async () => {
|
|
145
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-hash-cli-'));
|
|
146
|
+
try {
|
|
147
|
+
const { workspace, leafSource } = await workspaceCargoFixture(root);
|
|
148
|
+
const bin = join(import.meta.dir, '../dist/bin/smoo-nx-cargo-hash.js');
|
|
149
|
+
const run = async (args: string[]) => {
|
|
150
|
+
const child = Bun.spawn(['bun', bin, ...args], { cwd: workspace, stdout: 'pipe', stderr: 'pipe' });
|
|
151
|
+
const [exitCode, stdout, stderr] = await Promise.all([
|
|
152
|
+
child.exited,
|
|
153
|
+
new Response(child.stdout).text(),
|
|
154
|
+
new Response(child.stderr).text(),
|
|
155
|
+
]);
|
|
156
|
+
return { exitCode, stdout, stderr };
|
|
157
|
+
};
|
|
158
|
+
const external = await run(['Cargo.toml']);
|
|
159
|
+
expect(external.exitCode, external.stderr).toBe(0);
|
|
160
|
+
const included = await run(['--include-workspace']);
|
|
161
|
+
expect(included.exitCode, included.stderr).toBe(0);
|
|
162
|
+
expect(included.stderr).toBe('');
|
|
163
|
+
expect(await run(['--include-workspace', 'Cargo.toml'])).toEqual(included);
|
|
164
|
+
|
|
165
|
+
await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
|
|
166
|
+
expect(await run([])).toEqual(external);
|
|
167
|
+
const changed = await run(['--include-workspace', 'Cargo.toml']);
|
|
168
|
+
expect(changed.exitCode, changed.stderr).toBe(0);
|
|
169
|
+
expect(changed.stderr).toBe('');
|
|
170
|
+
expect(changed.stdout).not.toBe(included.stdout);
|
|
171
|
+
expect(await run(['--include-workspace'])).toEqual(changed);
|
|
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
|
+
|
|
179
|
+
for (const args of [
|
|
180
|
+
['--unknown'],
|
|
181
|
+
['--include-workspace', '--unknown'],
|
|
182
|
+
['Cargo.toml', '--include-workspace'],
|
|
183
|
+
['--include-workspace', 'Cargo.toml', 'extra'],
|
|
184
|
+
['--include-workspace', '--include-workspace'],
|
|
185
|
+
['--closure'],
|
|
186
|
+
['--closure', '--include-workspace'],
|
|
187
|
+
['--closure', 'crates/leaf', '--closure', 'packages/app'],
|
|
188
|
+
]) {
|
|
189
|
+
const refused = await run(args);
|
|
190
|
+
expect(refused.exitCode, refused.stderr).toBe(2);
|
|
191
|
+
expect(refused.stdout).toBe('');
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
await rm(root, { recursive: true, force: true });
|
|
195
|
+
}
|
|
196
|
+
});
|
|
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
|
+
|
|
280
|
+
it.each(['helper', 'cli'])(
|
|
281
|
+
'include-workspace keeps a custom Nx cached Cargo consumer fresh via %s',
|
|
282
|
+
async (mode) => {
|
|
283
|
+
const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-hash-nx-')));
|
|
284
|
+
const repositoryRoot = join(import.meta.dir, '../../..');
|
|
285
|
+
const dist = join(import.meta.dir, '../dist');
|
|
286
|
+
try {
|
|
287
|
+
const { workspace, leafSource } = await workspaceCargoFixture(root);
|
|
288
|
+
execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
|
|
289
|
+
await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
|
|
290
|
+
await writeFile(join(workspace, '.gitignore'), 'node_modules/\ntarget/\ndist/\n.nx/\n');
|
|
291
|
+
await writeFile(join(workspace, 'package.json'), '{"name":"cargo-consumer-fixture","private":true}\n');
|
|
292
|
+
const hashProgram =
|
|
293
|
+
`import { hashCargoPathInputs } from ${JSON.stringify(join(dist, 'cargo-source-hash.js'))}; ` +
|
|
294
|
+
'console.log(await hashCargoPathInputs("Cargo.toml", process.cwd(), { includeWorkspace: true }));';
|
|
295
|
+
await writeFile(
|
|
296
|
+
join(workspace, 'nx.json'),
|
|
297
|
+
JSON.stringify({
|
|
298
|
+
namedInputs: {
|
|
299
|
+
cargoSources: [
|
|
300
|
+
'{workspaceRoot}/Cargo.lock',
|
|
301
|
+
{
|
|
302
|
+
runtime:
|
|
303
|
+
mode === 'helper'
|
|
304
|
+
? `bun -e '${hashProgram}'`
|
|
305
|
+
: `bun ${JSON.stringify(join(dist, 'bin/smoo-nx-cargo-hash.js'))} --include-workspace`,
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
},
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
await writeFile(
|
|
312
|
+
join(workspace, 'packages/app/project.json'),
|
|
313
|
+
JSON.stringify({
|
|
314
|
+
name: 'app',
|
|
315
|
+
targets: {
|
|
316
|
+
build: {
|
|
317
|
+
executor: 'nx:run-commands',
|
|
318
|
+
cache: true,
|
|
319
|
+
inputs: ['{projectRoot}/**/*', '!{projectRoot}/dist/**/*', 'cargoSources'],
|
|
320
|
+
outputs: ['{projectRoot}/dist'],
|
|
321
|
+
options: { command: 'bun packages/app/build.ts' },
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
}),
|
|
325
|
+
);
|
|
326
|
+
const executions = join(root, 'executions.log');
|
|
327
|
+
const outputDirectory = join(workspace, 'packages/app/dist');
|
|
328
|
+
const output = join(outputDirectory, 'result.txt');
|
|
329
|
+
await writeFile(
|
|
330
|
+
join(workspace, 'packages/app/build.ts'),
|
|
331
|
+
`import { execFileSync } from 'node:child_process';
|
|
332
|
+
import { appendFile, mkdir, writeFile } from 'node:fs/promises';
|
|
333
|
+
const result = execFileSync('cargo', ['run', '--quiet', '--locked', '--offline', '--manifest-path', 'packages/app/Cargo.toml'], { encoding: 'utf8' });
|
|
334
|
+
await mkdir(${JSON.stringify(outputDirectory)}, { recursive: true });
|
|
335
|
+
await writeFile(${JSON.stringify(output)}, result);
|
|
336
|
+
await appendFile(${JSON.stringify(executions)}, result);
|
|
337
|
+
`,
|
|
338
|
+
);
|
|
339
|
+
const run = async () => {
|
|
340
|
+
const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
|
|
341
|
+
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
|
+
},
|
|
350
|
+
stdout: 'pipe',
|
|
351
|
+
stderr: 'pipe',
|
|
352
|
+
});
|
|
353
|
+
const [exitCode, stdout, stderr] = await Promise.all([
|
|
354
|
+
child.exited,
|
|
355
|
+
new Response(child.stdout).text(),
|
|
356
|
+
new Response(child.stderr).text(),
|
|
357
|
+
]);
|
|
358
|
+
expect(exitCode, stdout + stderr).toBe(0);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
await run();
|
|
362
|
+
expect(await readFile(output, 'utf8')).toBe('11\n');
|
|
363
|
+
expect(await readFile(executions, 'utf8')).toBe('11\n');
|
|
364
|
+
await rm(outputDirectory, { recursive: true });
|
|
365
|
+
await run();
|
|
366
|
+
expect(await readFile(output, 'utf8')).toBe('11\n');
|
|
367
|
+
expect(await readFile(executions, 'utf8')).toBe('11\n');
|
|
368
|
+
|
|
369
|
+
await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
|
|
370
|
+
await run();
|
|
371
|
+
expect(await readFile(output, 'utf8')).toBe('12\n');
|
|
372
|
+
expect(await readFile(executions, 'utf8')).toBe('11\n12\n');
|
|
373
|
+
await rm(outputDirectory, { recursive: true });
|
|
374
|
+
await run();
|
|
375
|
+
expect(await readFile(output, 'utf8')).toBe('12\n');
|
|
376
|
+
expect(await readFile(executions, 'utf8')).toBe('11\n12\n');
|
|
377
|
+
} finally {
|
|
378
|
+
await rm(root, { recursive: true, force: true });
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
120_000,
|
|
382
|
+
);
|
package/src/cargo-source-hash.ts
CHANGED
|
@@ -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',
|
|
@@ -34,61 +46,87 @@ const ancestorInputs = ['Cargo.toml', '.cargo/config', '.cargo/config.toml'];
|
|
|
34
46
|
* patches, and transitive path dependencies. A locked offline query observes
|
|
35
47
|
* that graph without fetching dependencies or changing the lockfile. Git and
|
|
36
48
|
* registry packages are immutable inputs already identified by Cargo.lock.
|
|
49
|
+
* By default, Nx's in-workspace inputs own workspace packages; includeWorkspace
|
|
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.
|
|
37
54
|
*
|
|
38
55
|
* This covers Rust sources and Cargo configuration, matching the plugin's
|
|
39
56
|
* in-workspace Rust inputs. Build scripts' other data/environment inputs remain
|
|
40
|
-
* 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.
|
|
41
62
|
*/
|
|
42
63
|
// Nx hashes a runtime input's stdout AND stderr. Cargo reports lock contention
|
|
43
64
|
// on stderr ("Blocking waiting for file lock on package cache") a
|
|
44
65
|
// timing-dependent number of times when Nx hashes many tasks at once, which
|
|
45
66
|
// made one unchanged tree produce four different task hashes per run. Cargo's
|
|
46
67
|
// stderr is kept for the failure path only, where execFileSync attaches it to
|
|
47
|
-
// the thrown error.
|
|
48
|
-
const
|
|
68
|
+
// the thrown error. Git runs under the same rule.
|
|
69
|
+
const CHILD_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
|
|
49
70
|
|
|
50
|
-
export async function hashCargoPathInputs(
|
|
71
|
+
export async function hashCargoPathInputs(
|
|
72
|
+
manifestPath: string,
|
|
73
|
+
workspaceRoot: string,
|
|
74
|
+
options?: CargoPathInputsOptions,
|
|
75
|
+
): Promise<string> {
|
|
51
76
|
const root = await realpath(workspaceRoot);
|
|
52
77
|
const metadata = parseMetadata(
|
|
53
78
|
execFileSync(
|
|
54
79
|
'cargo',
|
|
55
80
|
['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
|
|
56
|
-
{ cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio:
|
|
81
|
+
{ cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: CHILD_STDIO },
|
|
57
82
|
),
|
|
58
83
|
);
|
|
59
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
|
+
|
|
60
96
|
const directories = new Set<string>();
|
|
61
97
|
const files = new Set<string>();
|
|
62
98
|
const ancestors = new Set<string>();
|
|
63
99
|
const links = new Map<string, string>();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const directory =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
100
|
+
const packageSources: string[] = [];
|
|
101
|
+
for (const pkg of local) {
|
|
102
|
+
if (members !== undefined && !members.has(pkg.id)) continue;
|
|
103
|
+
const directory = pkg.directory;
|
|
104
|
+
if (!options?.includeWorkspace) {
|
|
105
|
+
const fromRoot = relative(root, directory);
|
|
106
|
+
const fromCargo = relative(cargoRoot, directory);
|
|
107
|
+
if (
|
|
108
|
+
fromRoot !== '..' &&
|
|
109
|
+
!fromRoot.startsWith(`..${sep}`) &&
|
|
110
|
+
!isAbsolute(fromRoot) &&
|
|
111
|
+
fromCargo !== '..' &&
|
|
112
|
+
!fromCargo.startsWith(`..${sep}`) &&
|
|
113
|
+
!isAbsolute(fromCargo) &&
|
|
114
|
+
!fromRoot.split(sep).includes('node_modules')
|
|
115
|
+
)
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
80
118
|
directories.add(directory);
|
|
81
|
-
files.add(manifest);
|
|
119
|
+
files.add(pkg.manifest);
|
|
82
120
|
// Cargo resolves explicit package.workspace ownership as well as ancestor
|
|
83
121
|
// workspaces. Metadata's workspace_root describes only the invoking root.
|
|
84
122
|
const governingManifest = execFileSync(
|
|
85
123
|
'cargo',
|
|
86
|
-
['locate-project', '--workspace', '--manifest-path', manifest, '--message-format', 'plain'],
|
|
87
|
-
{ cwd: root, encoding: 'utf8', stdio:
|
|
124
|
+
['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
|
|
125
|
+
{ cwd: root, encoding: 'utf8', stdio: CHILD_STDIO },
|
|
88
126
|
).trim();
|
|
89
127
|
files.add(await realpath(governingManifest));
|
|
90
|
-
|
|
91
|
-
//
|
|
128
|
+
packageSources.push(...pkg.sources);
|
|
129
|
+
// A member can inherit edition, lint policy, dependencies and
|
|
92
130
|
// profiles from a workspace above its package directory. Those manifests
|
|
93
131
|
// are not descendants of the source roots returned by Cargo metadata.
|
|
94
132
|
for (let ancestor = dirname(directory); !ancestors.has(ancestor); ancestor = dirname(ancestor)) {
|
|
@@ -100,6 +138,8 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
|
|
|
100
138
|
}
|
|
101
139
|
}
|
|
102
140
|
|
|
141
|
+
const ignored = gitIgnoredBeneath([...directories]);
|
|
142
|
+
for (const source of packageSources) if (!ignored(source)) files.add(source);
|
|
103
143
|
// Overlapping packages (a facade root plus its child crates) share one walk.
|
|
104
144
|
const visited = new Set<string>();
|
|
105
145
|
async function walk(directory: string): Promise<void> {
|
|
@@ -109,6 +149,7 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
|
|
|
109
149
|
for (const entry of await readdir(canonical, { withFileTypes: true })) {
|
|
110
150
|
if (ignoredDirectories.has(entry.name)) continue;
|
|
111
151
|
const path = join(canonical, entry.name);
|
|
152
|
+
if (ignored(path)) continue;
|
|
112
153
|
if (entry.isSymbolicLink()) links.set(path, await readlink(path));
|
|
113
154
|
const info = entry.isSymbolicLink() ? await lstat(await realpath(path)) : entry;
|
|
114
155
|
if (info.isDirectory()) {
|
|
@@ -144,3 +185,124 @@ export async function hashCargoPathInputs(manifestPath: string, workspaceRoot: s
|
|
|
144
185
|
}
|
|
145
186
|
return hash.digest('hex');
|
|
146
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
|
+
}
|
|
@@ -136,10 +136,11 @@ describe('Cargo workspace layouts', () => {
|
|
|
136
136
|
absoluteProjectRoot: join(root, 'packages/app'),
|
|
137
137
|
memberDir: 'crates/app',
|
|
138
138
|
});
|
|
139
|
-
expect(matches(inputs, 'packages/
|
|
140
|
-
expect(matches(inputs, '
|
|
141
|
-
expect(matches(inputs, '
|
|
142
|
-
expect(matches(inputs, 'packages/
|
|
139
|
+
expect(matches(inputs, 'packages/app/crates/app/src/lib.rs')).toBe(true);
|
|
140
|
+
expect(matches(inputs, 'packages/base/src/lib.rs')).toBe(true);
|
|
141
|
+
expect(matches(inputs, '.cargo/config.toml')).toBe(true);
|
|
142
|
+
expect(matches(inputs, 'packages/rust-toolchain.toml')).toBe(true);
|
|
143
|
+
expect(matches(inputs, 'packages/base/target/debug/output')).toBe(false);
|
|
143
144
|
expect(inputs).not.toContain('externalRustCrates');
|
|
144
145
|
expect(inputs.some((input) => input.includes('../'))).toBe(false);
|
|
145
146
|
} finally {
|
|
@@ -269,12 +270,18 @@ describe('Cargo workspace layouts', () => {
|
|
|
269
270
|
});
|
|
270
271
|
});
|
|
271
272
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Whether Nx would hash `path` (workspace-relative) under `inputs`.
|
|
275
|
+
*
|
|
276
|
+
* Only `{workspaceRoot}` filesets resolve here. Nx resolves a `{projectRoot}`
|
|
277
|
+
* fileset against the files its project owns, which a directory prefix cannot
|
|
278
|
+
* model: resolving it as one would pass the very inputs that hash nothing once
|
|
279
|
+
* a nested project owns the crate, so this harness refuses them instead.
|
|
280
|
+
*/
|
|
281
|
+
function matches(inputs: readonly string[], path: string): boolean {
|
|
282
|
+
expect(inputs.filter((input) => input.includes('{projectRoot}'))).toEqual([]);
|
|
283
|
+
const filesets = inputs.filter((input) => input.includes('{workspaceRoot}'));
|
|
284
|
+
const match = (input: string): boolean => new Glob(input.replace('{workspaceRoot}/', '')).match(path);
|
|
278
285
|
return (
|
|
279
286
|
filesets.some((input) => !input.startsWith('!') && match(input)) &&
|
|
280
287
|
!filesets.some((input) => input.startsWith('!') && match(input.slice(1)))
|