@smoothbricks/nx-plugin 0.4.21 → 0.4.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -18
- package/dist/bin/smoo-nx-cargo-hash.js +32 -6
- package/dist/bin/smoo-nx-exec.js +35 -11
- 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 +46 -2
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +238 -114
- 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 +112 -76
- 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/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 +2 -2
- package/src/__tests__/fixture-nx-env.ts +31 -0
- package/src/__tests__/in-process-nx-index.ts +9 -0
- package/src/bin/smoo-nx-cargo-hash.ts +29 -9
- package/src/bin/smoo-nx-exec.ts +40 -11
- package/src/cargo-closure-input.test.ts +184 -0
- package/src/cargo-closure-input.ts +247 -0
- package/src/cargo-source-hash.test.ts +104 -10
- package/src/cargo-source-hash.ts +290 -37
- 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 +94 -53
- package/src/index.ts +164 -84
- 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/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');
|
|
@@ -95,7 +101,8 @@ async function workspaceCargoFixture(root: string) {
|
|
|
95
101
|
const workspace = join(root, 'workspace');
|
|
96
102
|
const manifest = join(workspace, 'Cargo.toml');
|
|
97
103
|
const sources = {
|
|
98
|
-
'workspace/Cargo.toml':
|
|
104
|
+
'workspace/Cargo.toml':
|
|
105
|
+
'[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
|
|
99
106
|
'workspace/packages/app/Cargo.toml':
|
|
100
107
|
'[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
|
|
101
108
|
'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
|
|
@@ -104,6 +111,8 @@ async function workspaceCargoFixture(root: string) {
|
|
|
104
111
|
'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
|
|
105
112
|
'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
|
|
106
113
|
'workspace/crates/leaf/src/lib.rs': 'pub fn answer() -> u8 { 1 }\n',
|
|
114
|
+
'workspace/crates/unrelated/Cargo.toml': '[package]\nname="unrelated"\nversion="0.1.0"\nedition="2021"\n',
|
|
115
|
+
'workspace/crates/unrelated/src/lib.rs': 'pub fn unrelated() -> u8 { 3 }\n',
|
|
107
116
|
'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
|
|
108
117
|
'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
|
|
109
118
|
};
|
|
@@ -167,11 +176,21 @@ it('include-workspace CLI accepts an optional manifest and refuses unsupported a
|
|
|
167
176
|
expect(changed.stdout).not.toBe(included.stdout);
|
|
168
177
|
expect(await run(['--include-workspace'])).toEqual(changed);
|
|
169
178
|
|
|
179
|
+
const closure = await run(['--include-workspace', '--closure', 'crates/leaf', 'Cargo.toml']);
|
|
180
|
+
expect(closure.exitCode, closure.stderr).toBe(0);
|
|
181
|
+
expect(closure.stderr).toBe('');
|
|
182
|
+
expect(closure.stdout).not.toBe(changed.stdout);
|
|
183
|
+
expect(await run(['--closure', 'crates/leaf', '--include-workspace'])).toEqual(closure);
|
|
184
|
+
|
|
170
185
|
for (const args of [
|
|
171
186
|
['--unknown'],
|
|
172
187
|
['--include-workspace', '--unknown'],
|
|
173
188
|
['Cargo.toml', '--include-workspace'],
|
|
174
189
|
['--include-workspace', 'Cargo.toml', 'extra'],
|
|
190
|
+
['--include-workspace', '--include-workspace'],
|
|
191
|
+
['--closure'],
|
|
192
|
+
['--closure', '--include-workspace'],
|
|
193
|
+
['--closure', 'crates/leaf', '--closure', 'packages/app'],
|
|
175
194
|
]) {
|
|
176
195
|
const refused = await run(args);
|
|
177
196
|
expect(refused.exitCode, refused.stderr).toBe(2);
|
|
@@ -182,6 +201,88 @@ it('include-workspace CLI accepts an optional manifest and refuses unsupported a
|
|
|
182
201
|
}
|
|
183
202
|
});
|
|
184
203
|
|
|
204
|
+
it('closure hashes the dependency closure of one directory and ignores unrelated workspace members', async () => {
|
|
205
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-hash-closure-'));
|
|
206
|
+
try {
|
|
207
|
+
const { workspace, manifest, leafSource } = await workspaceCargoFixture(root);
|
|
208
|
+
const app = { includeWorkspace: true, closure: join(workspace, 'packages/app') };
|
|
209
|
+
const leaf = { includeWorkspace: true, closure: join(workspace, 'crates/leaf') };
|
|
210
|
+
const initial = await hashCargoPathInputs(manifest, workspace, app);
|
|
211
|
+
const leafOnly = await hashCargoPathInputs(manifest, workspace, leaf);
|
|
212
|
+
const whole = await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true });
|
|
213
|
+
|
|
214
|
+
await writeFile(join(workspace, 'crates/unrelated/src/lib.rs'), 'pub fn unrelated() -> u8 { 4 }\n');
|
|
215
|
+
expect(await hashCargoPathInputs(manifest, workspace, app)).toBe(initial);
|
|
216
|
+
expect(await hashCargoPathInputs(manifest, workspace, { includeWorkspace: true })).not.toBe(whole);
|
|
217
|
+
|
|
218
|
+
await writeFile(join(workspace, 'crates/bridge/src/lib.rs'), 'pub fn answer() -> u8 { leaf::answer() }\n');
|
|
219
|
+
const bridgeChanged = await hashCargoPathInputs(manifest, workspace, app);
|
|
220
|
+
expect(bridgeChanged).not.toBe(initial);
|
|
221
|
+
expect(await hashCargoPathInputs(manifest, workspace, leaf)).toBe(leafOnly);
|
|
222
|
+
|
|
223
|
+
await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
|
|
224
|
+
expect(await hashCargoPathInputs(manifest, workspace, app)).not.toBe(bridgeChanged);
|
|
225
|
+
expect(await hashCargoPathInputs(manifest, workspace, leaf)).not.toBe(leafOnly);
|
|
226
|
+
|
|
227
|
+
// The external package is reached through bridge, so the app closure sees it and the leaf closure does not.
|
|
228
|
+
const appBefore = await hashCargoPathInputs(manifest, workspace, app);
|
|
229
|
+
const leafBefore = await hashCargoPathInputs(manifest, workspace, leaf);
|
|
230
|
+
await writeFile(join(root, 'external/src/lib.rs'), 'pub fn answer() -> u8 { 20 }\n');
|
|
231
|
+
expect(await hashCargoPathInputs(manifest, workspace, app)).not.toBe(appBefore);
|
|
232
|
+
expect(await hashCargoPathInputs(manifest, workspace, leaf)).toBe(leafBefore);
|
|
233
|
+
|
|
234
|
+
await mkdir(join(workspace, 'docs'), { recursive: true });
|
|
235
|
+
await expect(
|
|
236
|
+
hashCargoPathInputs(manifest, workspace, { includeWorkspace: true, closure: join(workspace, 'docs') }),
|
|
237
|
+
).rejects.toThrow('no local Cargo package lies under');
|
|
238
|
+
} finally {
|
|
239
|
+
await rm(root, { recursive: true, force: true });
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('leaves gitignored sources beneath a package out of the digest but keeps an ignored package root', async () => {
|
|
244
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-hash-ignored-'));
|
|
245
|
+
try {
|
|
246
|
+
const { workspace, manifest } = await workspaceCargoFixture(root);
|
|
247
|
+
execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
|
|
248
|
+
await writeFile(join(workspace, '.gitignore'), 'target/\ngenerated.rs\ncrates/leaf/\n');
|
|
249
|
+
const options = { includeWorkspace: true };
|
|
250
|
+
const initial = await hashCargoPathInputs(manifest, workspace, options);
|
|
251
|
+
|
|
252
|
+
// A source a producer generates later: a fresh tree and a built one hash alike.
|
|
253
|
+
await writeFile(join(workspace, 'crates/bridge/src/generated.rs'), 'pub const GENERATED: u8 = 1;\n');
|
|
254
|
+
expect(await hashCargoPathInputs(manifest, workspace, options)).toBe(initial);
|
|
255
|
+
|
|
256
|
+
// An untracked source nobody ignores is an edit like any other.
|
|
257
|
+
await writeFile(join(workspace, 'crates/bridge/src/extra.rs'), 'pub const EXTRA: u8 = 1;\n');
|
|
258
|
+
const untracked = await hashCargoPathInputs(manifest, workspace, options);
|
|
259
|
+
expect(untracked).not.toBe(initial);
|
|
260
|
+
|
|
261
|
+
// A package whose own directory is ignored still hashes its sources.
|
|
262
|
+
await writeFile(join(workspace, 'crates/leaf/src/lib.rs'), 'pub fn answer() -> u8 { 3 }\n');
|
|
263
|
+
expect(await hashCargoPathInputs(manifest, workspace, options)).not.toBe(untracked);
|
|
264
|
+
} finally {
|
|
265
|
+
await rm(root, { recursive: true, force: true });
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('hashes a Cargo workspace that lives inside a directory its enclosing repository ignores', async () => {
|
|
270
|
+
// A sandbox's TMPDIR, or a checkout under another repository's ignored scratch directory: every
|
|
271
|
+
// package sits beneath one ignored ancestor, where `git ls-files` refuses the pathspec outright.
|
|
272
|
+
const repository = await mkdtemp(join(tmpdir(), 'cargo-hash-enclosed-'));
|
|
273
|
+
try {
|
|
274
|
+
execFileSync('git', ['init', '--quiet', repository], { stdio: 'pipe' });
|
|
275
|
+
await writeFile(join(repository, '.gitignore'), 'scratch/\n');
|
|
276
|
+
const { workspace, manifest, leafSource } = await workspaceCargoFixture(join(repository, 'scratch/deep'));
|
|
277
|
+
const options = { includeWorkspace: true };
|
|
278
|
+
const initial = await hashCargoPathInputs(manifest, workspace, options);
|
|
279
|
+
await writeFile(leafSource, 'pub fn answer() -> u8 { 2 }\n');
|
|
280
|
+
expect(await hashCargoPathInputs(manifest, workspace, options)).not.toBe(initial);
|
|
281
|
+
} finally {
|
|
282
|
+
await rm(repository, { recursive: true, force: true });
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
185
286
|
it.each(['helper', 'cli'])(
|
|
186
287
|
'include-workspace keeps a custom Nx cached Cargo consumer fresh via %s',
|
|
187
288
|
async (mode) => {
|
|
@@ -244,14 +345,7 @@ await appendFile(${JSON.stringify(executions)}, result);
|
|
|
244
345
|
const run = async () => {
|
|
245
346
|
const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
|
|
246
347
|
cwd: workspace,
|
|
247
|
-
env:
|
|
248
|
-
...process.env,
|
|
249
|
-
NX_WORKSPACE_ROOT_PATH: workspace,
|
|
250
|
-
NX_DAEMON: 'false',
|
|
251
|
-
NX_ISOLATE_PLUGINS: 'false',
|
|
252
|
-
NX_WORKSPACE_DATA_DIRECTORY: join(workspace, '.nx/workspace-data'),
|
|
253
|
-
NX_CACHE_DIRECTORY: join(workspace, '.nx/cache'),
|
|
254
|
-
},
|
|
348
|
+
env: fixtureNxEnv(workspace),
|
|
255
349
|
stdout: 'pipe',
|
|
256
350
|
stderr: 'pipe',
|
|
257
351
|
});
|