@smoothbricks/nx-plugin 0.4.27 → 0.4.29
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 +203 -83
- package/dist/bounded-test-policy.d.ts +10 -3
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +19 -25
- package/dist/cargo-closure-input.d.ts +12 -0
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +29 -7
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +42 -19
- package/dist/ensure-built-receipt.d.ts +34 -6
- package/dist/ensure-built-receipt.d.ts.map +1 -1
- package/dist/ensure-built-receipt.js +265 -159
- package/dist/ensure-built-witness.d.ts +27 -2
- package/dist/ensure-built-witness.d.ts.map +1 -1
- package/dist/ensure-built-witness.js +2176 -15
- package/dist/ensure-built.d.ts +99 -1
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +34 -53
- package/dist/executors/bounded-exec/cpu-pool.d.ts +16 -5
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool.js +43 -19
- package/dist/executors/bounded-exec/executor.d.ts +11 -2
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +139 -39
- package/dist/executors/bounded-exec/ram-temp.d.ts +17 -11
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
- package/dist/executors/bounded-exec/ram-temp.js +27 -13
- package/dist/generators/bun-test-tracing/generator.js +2 -2
- package/dist/graph-input-contract.d.ts +98 -0
- package/dist/graph-input-contract.d.ts.map +1 -0
- package/dist/graph-input-contract.js +624 -0
- package/dist/index.d.ts +12 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -3
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +5 -2
- package/dist/managed-files/publish-workflow.d.ts +11 -1
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/producer-receipts.d.ts +19 -0
- package/dist/producer-receipts.d.ts.map +1 -0
- package/dist/producer-receipts.js +921 -0
- package/dist/testing.d.ts +8 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +58 -0
- package/managed/raw/patches/nx@23.2.1.patch +328 -50
- package/managed/raw/tooling/direnv/developer-links.ts +147 -21
- package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
- package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
- package/managed/raw/tooling/direnv/repo-path +15 -2
- package/managed/raw/tooling/direnv/setup-environment.ts +363 -125
- package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
- package/nextest.toml +1 -1
- package/package.json +11 -6
- package/src/__tests__/fixture-nx-env.ts +7 -0
- package/src/__tests__/owned-installation.ts +174 -0
- package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
- package/src/bounded-test-policy.test.ts +36 -22
- package/src/bounded-test-policy.ts +19 -27
- package/src/bun-bootstrap.test.ts +1 -1
- package/src/cargo-closure-input.test.ts +60 -11
- package/src/cargo-closure-input.ts +28 -6
- package/src/cargo-resolve-cache.test.ts +140 -25
- package/src/cargo-source-hash.test.ts +78 -3
- package/src/cargo-source-hash.ts +48 -30
- package/src/cross-check-policy.test.ts +2 -2
- package/src/ensure-built-receipt.test.ts +111 -16
- package/src/ensure-built-receipt.ts +251 -149
- package/src/ensure-built-witness.test.ts +197 -13
- package/src/ensure-built-witness.ts +187 -15
- package/src/ensure-built.test.ts +156 -102
- package/src/ensure-built.ts +114 -64
- package/src/executors/bounded-exec/cpu-pool.test.ts +116 -32
- package/src/executors/bounded-exec/cpu-pool.ts +55 -17
- package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
- package/src/executors/bounded-exec/executor-host.test.ts +51 -41
- package/src/executors/bounded-exec/executor.test.ts +155 -35
- package/src/executors/bounded-exec/executor.ts +156 -45
- package/src/executors/bounded-exec/progress.test.ts +1 -1
- package/src/executors/bounded-exec/ram-temp-host.test.ts +46 -17
- package/src/executors/bounded-exec/ram-temp.test.ts +65 -3
- package/src/executors/bounded-exec/ram-temp.ts +32 -16
- package/src/executors/bounded-exec/verdict.test.ts +17 -12
- package/src/generators/bounded-test-targets/generator.test.ts +1 -3
- package/src/generators/bun-test-tracing/generator.test.ts +2 -2
- package/src/generators/bun-test-tracing/generator.ts +2 -2
- package/src/graph-input-contract.test.ts +371 -0
- package/src/graph-input-contract.ts +605 -0
- package/src/index.test.ts +3 -3
- package/src/index.ts +24 -3
- package/src/linked-snapshots.nx.test.ts +112 -30
- package/src/managed-files/ci-workflow.ts +5 -2
- package/src/managed-files/publish-workflow.ts +12 -1
- package/src/managed-files/repo-path.test.ts +44 -1
- package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
- package/src/nextest-code-bound.test.ts +23 -17
- package/src/nextest-partitions.test.ts +7 -1
- package/src/nx-cache-restore-mtime.test.ts +2 -2
- package/src/nx-daemon-claim.test.ts +5 -5
- package/src/nx-daemon-idle-cpu.test.ts +0 -1
- package/src/nx-daemon-idle-policy.test.ts +56 -0
- package/src/nx-daemon-ignore-files.test.ts +2 -2
- package/src/nx-daemon-validity.test.ts +31 -9
- package/src/nx-dot-input-policy.test.ts +5 -5
- package/src/nx-files-archive-racy.test.ts +1 -1
- package/src/nx-incomplete-run-summary.test.ts +1 -1
- package/src/nx-plugin-load-errors.test.ts +2 -2
- package/src/nx-project-graph-errors.test.ts +70 -0
- package/src/nx-skipped-task-exit.test.ts +2 -2
- package/src/nx-store-resolution.test.ts +7 -25
- package/src/nx-task-output-lease.test.ts +3 -3
- package/src/nx-use-local.test.ts +7 -5
- package/src/producer-receipts.test.ts +164 -0
- package/src/producer-receipts.ts +631 -0
- package/src/runtime-input.test.ts +1 -1
- package/src/serial-group-wiring.test.ts +4 -4
- package/src/task-results.test.ts +18 -16
- package/src/test-timings.test.ts +2 -2
- package/src/testing.test.ts +5 -5
- package/src/testing.ts +59 -0
- package/src/typecheck-test-policy.test.ts +1 -1
- package/src/typescript-resolution.test.ts +76 -0
- package/test-timings.json +5 -0
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import type { BigIntStats } from 'node:fs';
|
|
4
|
+
import {
|
|
5
|
+
accessSync,
|
|
6
|
+
constants,
|
|
7
|
+
existsSync,
|
|
8
|
+
lstatSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
readFileSync,
|
|
12
|
+
realpathSync,
|
|
13
|
+
renameSync,
|
|
14
|
+
rmSync,
|
|
15
|
+
writeFileSync,
|
|
16
|
+
} from 'node:fs';
|
|
17
|
+
import { createRequire } from 'node:module';
|
|
18
|
+
import { delimiter, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
19
|
+
import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
|
|
20
|
+
import type * as NxDaemonEnvironment from 'nx/src/daemon/client/daemon-environment';
|
|
21
|
+
import type * as NxNative from 'nx/src/native';
|
|
22
|
+
import type { FileData } from 'nx/src/native';
|
|
23
|
+
import type * as NxCacheDirectory from 'nx/src/utils/cache-directory';
|
|
24
|
+
import type * as TypeScript from 'typescript';
|
|
25
|
+
import typia from 'typia';
|
|
26
|
+
import type { NodeWitness, WorkspaceSnapshot } from './ensure-built-witness.js';
|
|
27
|
+
import { nodeStamp, nodesUnchanged, workspaceSnapshot } from './ensure-built-witness.js';
|
|
28
|
+
|
|
29
|
+
/** An owner-approved contract, pinned to the reviewed module bytes, never an inferred approval. */
|
|
30
|
+
export interface GraphInputContract {
|
|
31
|
+
readonly version: 1;
|
|
32
|
+
readonly kind: 'native-only' | 'tsconfig-extends' | 'reference-scan';
|
|
33
|
+
readonly modules: readonly { readonly path: string; readonly sha256: string }[];
|
|
34
|
+
readonly packages: readonly string[];
|
|
35
|
+
}
|
|
36
|
+
interface ResolutionWitness {
|
|
37
|
+
readonly kind: 'node' | 'bun';
|
|
38
|
+
readonly from: string;
|
|
39
|
+
readonly specifier: string;
|
|
40
|
+
readonly target: string | null;
|
|
41
|
+
}
|
|
42
|
+
interface ToolWitness {
|
|
43
|
+
readonly name: 'node' | 'bun' | 'git';
|
|
44
|
+
readonly path: string;
|
|
45
|
+
}
|
|
46
|
+
interface GitQueryWitness {
|
|
47
|
+
readonly cwd: string;
|
|
48
|
+
readonly args:
|
|
49
|
+
| readonly ['config', '--show-origin', '--null', '--list']
|
|
50
|
+
| readonly ['ls-files', '-z', '--cached', '--others', '--exclude-standard']
|
|
51
|
+
| readonly ['check-attr', '--all', '-z', '--stdin']
|
|
52
|
+
| readonly ['var', 'GIT_ATTR_SYSTEM']
|
|
53
|
+
| readonly ['var', 'GIT_ATTR_GLOBAL']
|
|
54
|
+
| readonly ['rev-parse', '--shared-index-path'];
|
|
55
|
+
readonly stdin: string;
|
|
56
|
+
readonly sha256: string;
|
|
57
|
+
}
|
|
58
|
+
interface ReadWitnessBasis {
|
|
59
|
+
readonly version: 1;
|
|
60
|
+
readonly workspaceRoot: string;
|
|
61
|
+
readonly plugin: string;
|
|
62
|
+
readonly contractSha256: string;
|
|
63
|
+
}
|
|
64
|
+
export type GraphInputReadWitness =
|
|
65
|
+
| (ReadWitnessBasis & {
|
|
66
|
+
readonly status: 'complete';
|
|
67
|
+
readonly nativeIdentity: string;
|
|
68
|
+
readonly nativeNodes: readonly NodeWitness[];
|
|
69
|
+
readonly nodes: readonly GraphInputNodeWitness[];
|
|
70
|
+
readonly missing: readonly string[];
|
|
71
|
+
readonly resolutions: readonly ResolutionWitness[];
|
|
72
|
+
readonly tools: readonly ToolWitness[];
|
|
73
|
+
readonly gitQueries: readonly GitQueryWitness[];
|
|
74
|
+
readonly environmentIdentity: string;
|
|
75
|
+
})
|
|
76
|
+
| (ReadWitnessBasis & { readonly status: 'unproved'; readonly detail: string });
|
|
77
|
+
export interface GraphInputContractProof {
|
|
78
|
+
readonly plugins: readonly string[];
|
|
79
|
+
readonly identity: string;
|
|
80
|
+
readonly packages: readonly { readonly name: string; readonly from: string }[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function digest(value: string | Uint8Array): string {
|
|
84
|
+
return createHash('sha256').update(value).digest('hex');
|
|
85
|
+
}
|
|
86
|
+
function systemError(error: unknown): error is Error & { readonly code: string } {
|
|
87
|
+
return error instanceof Error && 'code' in error && typeof error.code === 'string';
|
|
88
|
+
}
|
|
89
|
+
function nativeIdentity(files: readonly FileData[]): string {
|
|
90
|
+
return digest(
|
|
91
|
+
JSON.stringify(
|
|
92
|
+
files.map(({ file, hash }) => [file, hash]).sort(([left], [right]) => (left ?? '').localeCompare(right ?? '')),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
/** Ancestors prove path binding; only a read directory's membership belongs to the input closure. */
|
|
97
|
+
interface GraphInputNodeWitness extends NodeWitness {
|
|
98
|
+
readonly scope: 'path' | 'node';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function pathStamp(stats: BigIntStats): string {
|
|
102
|
+
return `${stats.dev}:${stats.ino}:${stats.mode}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function readNodesUnchanged(nodes: readonly GraphInputNodeWitness[]): boolean {
|
|
106
|
+
for (const node of nodes) {
|
|
107
|
+
try {
|
|
108
|
+
const stats = lstatSync(node.path, { bigint: true });
|
|
109
|
+
if ((node.scope === 'path' ? pathStamp(stats) : nodeStamp(stats)) !== node.stamp) return false;
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (systemError(error)) return false;
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function workspaceRequire(root: string): NodeRequire {
|
|
119
|
+
return createRequire(join(root, 'package.json'));
|
|
120
|
+
}
|
|
121
|
+
function dataDirectory(root: string): string {
|
|
122
|
+
const module: typeof NxCacheDirectory = workspaceRequire(root)('nx/src/utils/cache-directory');
|
|
123
|
+
return module.workspaceDataDirectoryForWorkspace(root);
|
|
124
|
+
}
|
|
125
|
+
function captureNative(root: string, previous?: WorkspaceSnapshot): WorkspaceSnapshot {
|
|
126
|
+
const module: typeof NxNative = workspaceRequire(root)('nx/src/native');
|
|
127
|
+
return workspaceSnapshot(root, new module.WorkspaceContext(root, dataDirectory(root)), previous);
|
|
128
|
+
}
|
|
129
|
+
function environmentIdentity(root: string): string {
|
|
130
|
+
const module: typeof NxDaemonEnvironment = workspaceRequire(root)('nx/src/daemon/client/daemon-environment');
|
|
131
|
+
return digest(
|
|
132
|
+
JSON.stringify(
|
|
133
|
+
Object.entries(module.normalizeDaemonEnvironmentForGraph(module.getDaemonEnv())).sort(([left], [right]) =>
|
|
134
|
+
left.localeCompare(right),
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
function contractOf(options: unknown): GraphInputContract | null {
|
|
140
|
+
if (typeof options !== 'object' || options === null || !('graphInputContract' in options)) return null;
|
|
141
|
+
const contract = options.graphInputContract;
|
|
142
|
+
return typia.is<GraphInputContract>(contract) &&
|
|
143
|
+
contract.version === 1 &&
|
|
144
|
+
contract.modules.length > 0 &&
|
|
145
|
+
contract.modules.every(({ sha256 }) => /^[a-f0-9]{64}$/.test(sha256))
|
|
146
|
+
? contract
|
|
147
|
+
: null;
|
|
148
|
+
}
|
|
149
|
+
function witnessPath(root: string, plugin: string): string {
|
|
150
|
+
return join(dataDirectory(root), `graph-input-${digest(plugin)}.json`);
|
|
151
|
+
}
|
|
152
|
+
function gitEnvironment(): NodeJS.ProcessEnv {
|
|
153
|
+
return { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
|
|
154
|
+
}
|
|
155
|
+
function gitQuery(path: string, query: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'>): string {
|
|
156
|
+
return execFileSync(path, ['-c', 'core.excludesFile=/dev/null', ...query.args], {
|
|
157
|
+
cwd: query.cwd,
|
|
158
|
+
env: gitEnvironment(),
|
|
159
|
+
input: query.stdin,
|
|
160
|
+
encoding: 'utf8',
|
|
161
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Retain read/resolution byproducts at the reader; never reconstruct its extends/import algorithm. */
|
|
166
|
+
export class GraphInputReadCollector {
|
|
167
|
+
private readonly contract: GraphInputContract | null;
|
|
168
|
+
private readonly before: WorkspaceSnapshot | undefined;
|
|
169
|
+
private readonly environment: string | undefined;
|
|
170
|
+
private readonly nodes = new Map<string, GraphInputNodeWitness>();
|
|
171
|
+
private readonly missing = new Set<string>();
|
|
172
|
+
private readonly resolutions: ResolutionWitness[] = [];
|
|
173
|
+
private readonly tools = new Map<ToolWitness['name'], ToolWitness>();
|
|
174
|
+
private readonly gitQueries: GitQueryWitness[] = [];
|
|
175
|
+
private readonly scanned = new Set<string>();
|
|
176
|
+
private readonly gitScopes = new Set<string>();
|
|
177
|
+
private detail: string | undefined;
|
|
178
|
+
constructor(
|
|
179
|
+
readonly workspaceRoot: string,
|
|
180
|
+
readonly plugin: string,
|
|
181
|
+
options: unknown,
|
|
182
|
+
) {
|
|
183
|
+
this.contract = contractOf(options);
|
|
184
|
+
if (this.contract === null) return;
|
|
185
|
+
try {
|
|
186
|
+
this.before = captureNative(workspaceRoot);
|
|
187
|
+
this.environment = environmentIdentity(workspaceRoot);
|
|
188
|
+
if (process.versions.bun === undefined) this.executable('node', process.execPath);
|
|
189
|
+
else {
|
|
190
|
+
const executable = this.executable('bun');
|
|
191
|
+
this.file(process.execPath);
|
|
192
|
+
if (executable !== realpathSync(process.execPath))
|
|
193
|
+
this.refuse('Bun runtime is not the PATH-selected executable');
|
|
194
|
+
}
|
|
195
|
+
if (!this.before.stable) this.refuse('native file list contains an unproved node');
|
|
196
|
+
for (const name of ['NODE_OPTIONS', 'BUN_OPTIONS', 'LD_PRELOAD', 'DYLD_INSERT_LIBRARIES']) {
|
|
197
|
+
if (process.env[name]) this.refuse(`unbounded loader environment: ${name}`);
|
|
198
|
+
}
|
|
199
|
+
} catch (error) {
|
|
200
|
+
this.refuse(error instanceof Error ? error.message : String(error));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
refuse(detail: string): void {
|
|
204
|
+
this.detail ??= detail;
|
|
205
|
+
}
|
|
206
|
+
private protect(work: () => void): void {
|
|
207
|
+
if (this.contract === null || this.detail !== undefined) return;
|
|
208
|
+
try {
|
|
209
|
+
work();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
this.refuse(error instanceof Error ? error.message : String(error));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/** Observe each alias and its physical target, including a missing candidate's exact path. */
|
|
215
|
+
file(path: string): void {
|
|
216
|
+
this.protect(() => this.observe(resolve(path)));
|
|
217
|
+
}
|
|
218
|
+
private observe(path: string, membership = false): void {
|
|
219
|
+
// Every ancestor binds the path, without treating unrelated siblings as reads.
|
|
220
|
+
const parent = dirname(path);
|
|
221
|
+
if (parent !== path && !this.nodes.has(parent) && !this.missing.has(parent)) this.observe(parent);
|
|
222
|
+
let stats: BigIntStats;
|
|
223
|
+
try {
|
|
224
|
+
stats = lstatSync(path, { bigint: true });
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (systemError(error) && ['ENOENT', 'ENOTDIR'].includes(error.code)) {
|
|
227
|
+
this.missing.add(path);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
const previous = this.nodes.get(path);
|
|
233
|
+
const scope = previous?.scope === 'node' || !stats.isDirectory() || membership ? 'node' : 'path';
|
|
234
|
+
const stamp = scope === 'path' ? pathStamp(stats) : nodeStamp(stats);
|
|
235
|
+
if (previous !== undefined && previous.stamp !== (previous.scope === scope ? stamp : pathStamp(stats)))
|
|
236
|
+
this.refuse(`input changed during read: ${path}`);
|
|
237
|
+
if (previous === undefined || previous.scope !== scope) this.nodes.set(path, { path, scope, stamp });
|
|
238
|
+
if (stats.isSymbolicLink()) this.observe(realpathSync(path), membership);
|
|
239
|
+
else if (stats.isFile()) accessSync(path, constants.R_OK);
|
|
240
|
+
else if (!stats.isDirectory()) this.refuse(`unproved filesystem node: ${path}`);
|
|
241
|
+
}
|
|
242
|
+
read<T>(path: string, reader: () => T): T {
|
|
243
|
+
this.file(path);
|
|
244
|
+
return reader();
|
|
245
|
+
}
|
|
246
|
+
exists(path: string): boolean {
|
|
247
|
+
this.file(path);
|
|
248
|
+
return existsSync(path);
|
|
249
|
+
}
|
|
250
|
+
/** The whole immediate candidate directory, including absent/alternative extensions and alias nodes. */
|
|
251
|
+
directory(path: string): void {
|
|
252
|
+
this.protect(() => {
|
|
253
|
+
this.observe(resolve(path), true);
|
|
254
|
+
if (this.missing.has(resolve(path))) return;
|
|
255
|
+
for (const name of readdirSync(path).sort()) this.observe(join(path, name));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
/** A finite candidate namespace: every node and alias, not only the previously selected file. */
|
|
259
|
+
namespace(path: string): void {
|
|
260
|
+
this.protect(() => this.walk(resolve(path)));
|
|
261
|
+
}
|
|
262
|
+
private walk(path: string): void {
|
|
263
|
+
this.observe(path, true);
|
|
264
|
+
if (this.missing.has(path)) return;
|
|
265
|
+
const physical = realpathSync(path);
|
|
266
|
+
if (this.scanned.has(physical)) return;
|
|
267
|
+
this.scanned.add(physical);
|
|
268
|
+
const stats = lstatSync(physical);
|
|
269
|
+
if (stats.isDirectory()) {
|
|
270
|
+
for (const name of readdirSync(physical).sort()) this.walk(join(physical, name));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
resolve(from: string, specifier: string, resolver: () => string, kind: ResolutionWitness['kind'] = 'node'): string {
|
|
274
|
+
this.protect(() => {
|
|
275
|
+
const require = createRequire(join(from, 'package.json'));
|
|
276
|
+
if (specifier.startsWith('.') || isAbsolute(specifier)) {
|
|
277
|
+
// Census the literal candidate's directory, independent of which extension/index the resolver selects.
|
|
278
|
+
const base = resolve(from, specifier);
|
|
279
|
+
this.directory(dirname(base));
|
|
280
|
+
if (existsSync(base) && lstatSync(base).isDirectory()) this.walk(base);
|
|
281
|
+
} else {
|
|
282
|
+
const name = specifier.startsWith('@')
|
|
283
|
+
? specifier.split('/').slice(0, 2).join('/')
|
|
284
|
+
: (specifier.split('/')[0] ?? specifier);
|
|
285
|
+
for (const search of require.resolve.paths(specifier) ?? []) this.walk(join(search, name));
|
|
286
|
+
}
|
|
287
|
+
for (let directory = resolve(from); ; directory = dirname(directory)) {
|
|
288
|
+
this.observe(join(directory, 'package.json'));
|
|
289
|
+
this.observe(join(directory, 'tsconfig.json'));
|
|
290
|
+
this.observe(join(directory, 'jsconfig.json'));
|
|
291
|
+
if (directory === dirname(directory)) break;
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
try {
|
|
295
|
+
const target = resolver();
|
|
296
|
+
this.file(target);
|
|
297
|
+
this.protect(() => this.resolutions.push({ kind, from, specifier, target: realpathSync(target) }));
|
|
298
|
+
return target;
|
|
299
|
+
} catch (error) {
|
|
300
|
+
this.protect(() => this.resolutions.push({ kind, from, specifier, target: null }));
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
executable(name: ToolWitness['name'], actual?: string): string | undefined {
|
|
305
|
+
let found: string | undefined;
|
|
306
|
+
this.protect(() => {
|
|
307
|
+
if (actual !== undefined) {
|
|
308
|
+
this.observe(actual);
|
|
309
|
+
found = realpathSync(actual);
|
|
310
|
+
} else {
|
|
311
|
+
for (const directory of (process.env.PATH ?? '').split(delimiter)) {
|
|
312
|
+
const candidate = resolve(directory || '.', name);
|
|
313
|
+
this.observe(candidate);
|
|
314
|
+
if (this.missing.has(candidate)) continue;
|
|
315
|
+
try {
|
|
316
|
+
accessSync(candidate, constants.X_OK);
|
|
317
|
+
} catch {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
found = realpathSync(candidate);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (found === undefined) this.refuse(`unresolved executable: ${name}`);
|
|
325
|
+
else this.tools.set(name, { name, path: found });
|
|
326
|
+
});
|
|
327
|
+
return found;
|
|
328
|
+
}
|
|
329
|
+
/** Git itself supplies effective config/include origins, membership and attributes; no config reparser. */
|
|
330
|
+
git(root: string, paths: readonly string[]): void {
|
|
331
|
+
this.protect(() => {
|
|
332
|
+
const scope = realpathSync(root);
|
|
333
|
+
if (this.gitScopes.has(scope)) return;
|
|
334
|
+
this.gitScopes.add(scope);
|
|
335
|
+
const tool = this.executable('git');
|
|
336
|
+
if (tool === undefined) return;
|
|
337
|
+
const config: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'> = {
|
|
338
|
+
cwd: scope,
|
|
339
|
+
args: ['config', '--show-origin', '--null', '--list'],
|
|
340
|
+
stdin: '',
|
|
341
|
+
};
|
|
342
|
+
const output = gitQuery(tool, config);
|
|
343
|
+
this.gitQueries.push({ ...config, sha256: digest(output) });
|
|
344
|
+
const fields = output.split('\0');
|
|
345
|
+
for (let index = 0; index + 1 < fields.length; index += 2) {
|
|
346
|
+
const origin = fields[index] ?? '';
|
|
347
|
+
const entry = fields[index + 1] ?? '';
|
|
348
|
+
if (origin.startsWith('file:')) this.observe(resolve(scope, origin.slice(5)));
|
|
349
|
+
if (/^(?:core\.fsmonitor|core\.attributesfile)\n/iu.test(entry)) {
|
|
350
|
+
const value = entry.slice(entry.indexOf('\n') + 1);
|
|
351
|
+
if (entry.startsWith('core.fsmonitor\n') && value !== '' && value !== 'false')
|
|
352
|
+
this.refuse('Git fsmonitor has an unbounded external input closure');
|
|
353
|
+
if (entry.startsWith('core.attributesfile\n'))
|
|
354
|
+
this.observe(isAbsolute(value) ? value : resolve(scope, value));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (const variable of ['GIT_ATTR_SYSTEM', 'GIT_ATTR_GLOBAL'] as const) {
|
|
358
|
+
const query: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'> = {
|
|
359
|
+
cwd: scope,
|
|
360
|
+
args: ['var', variable],
|
|
361
|
+
stdin: '',
|
|
362
|
+
};
|
|
363
|
+
const output = gitQuery(tool, query);
|
|
364
|
+
this.gitQueries.push({ ...query, sha256: digest(output) });
|
|
365
|
+
const path = output.trim();
|
|
366
|
+
if (path !== '') this.observe(resolve(scope, path));
|
|
367
|
+
}
|
|
368
|
+
const splitIndex: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'> = {
|
|
369
|
+
cwd: scope,
|
|
370
|
+
args: ['rev-parse', '--shared-index-path'],
|
|
371
|
+
stdin: '',
|
|
372
|
+
};
|
|
373
|
+
const sharedIndex = gitQuery(tool, splitIndex);
|
|
374
|
+
this.gitQueries.push({ ...splitIndex, sha256: digest(sharedIndex) });
|
|
375
|
+
if (sharedIndex.trim() !== '') this.observe(resolve(scope, sharedIndex.trim()));
|
|
376
|
+
for (const name of ['index', 'info/exclude', 'info/attributes']) {
|
|
377
|
+
const path = execFileSync(tool, ['-c', 'core.excludesFile=/dev/null', 'rev-parse', '--git-path', name], {
|
|
378
|
+
cwd: scope,
|
|
379
|
+
env: gitEnvironment(),
|
|
380
|
+
encoding: 'utf8',
|
|
381
|
+
}).trim();
|
|
382
|
+
this.observe(resolve(scope, path));
|
|
383
|
+
}
|
|
384
|
+
const membership: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'> = {
|
|
385
|
+
cwd: scope,
|
|
386
|
+
args: ['ls-files', '-z', '--cached', '--others', '--exclude-standard'],
|
|
387
|
+
stdin: '',
|
|
388
|
+
};
|
|
389
|
+
const members = gitQuery(tool, membership);
|
|
390
|
+
this.gitQueries.push({ ...membership, sha256: digest(members) });
|
|
391
|
+
const names = [...new Set([...members.split('\0').filter(Boolean), ...paths])].sort();
|
|
392
|
+
for (const path of names) this.observe(resolve(scope, path));
|
|
393
|
+
const attributes: Pick<GitQueryWitness, 'cwd' | 'args' | 'stdin'> = {
|
|
394
|
+
cwd: scope,
|
|
395
|
+
args: ['check-attr', '--all', '-z', '--stdin'],
|
|
396
|
+
stdin: names.map((path) => path + '\0').join(''),
|
|
397
|
+
};
|
|
398
|
+
this.gitQueries.push({ ...attributes, sha256: digest(gitQuery(tool, attributes)) });
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
finish(): GraphInputReadWitness {
|
|
402
|
+
const basis: ReadWitnessBasis = {
|
|
403
|
+
version: 1,
|
|
404
|
+
workspaceRoot: this.workspaceRoot,
|
|
405
|
+
plugin: this.plugin,
|
|
406
|
+
contractSha256: digest(JSON.stringify(this.contract)),
|
|
407
|
+
};
|
|
408
|
+
this.protect(() => {
|
|
409
|
+
if (this.before === undefined) return;
|
|
410
|
+
const after = captureNative(this.workspaceRoot, this.before);
|
|
411
|
+
if (
|
|
412
|
+
!after.stable ||
|
|
413
|
+
nativeIdentity(after.files) !== nativeIdentity(this.before.files) ||
|
|
414
|
+
!nodesUnchanged(this.before.nodes) ||
|
|
415
|
+
!readNodesUnchanged([...this.nodes.values()])
|
|
416
|
+
)
|
|
417
|
+
this.refuse('graph inputs changed during the owning reader');
|
|
418
|
+
if (environmentIdentity(this.workspaceRoot) !== this.environment)
|
|
419
|
+
this.refuse('graph environment changed during the owning reader');
|
|
420
|
+
if (!missingStillMissing([...this.missing]))
|
|
421
|
+
this.refuse('a missing resolution candidate appeared during the owning reader');
|
|
422
|
+
});
|
|
423
|
+
const witness: GraphInputReadWitness =
|
|
424
|
+
this.detail === undefined && this.before !== undefined && this.environment !== undefined
|
|
425
|
+
? {
|
|
426
|
+
...basis,
|
|
427
|
+
status: 'complete',
|
|
428
|
+
nativeIdentity: nativeIdentity(this.before.files),
|
|
429
|
+
nativeNodes: this.before.nodes,
|
|
430
|
+
nodes: [...this.nodes.values()],
|
|
431
|
+
missing: [...this.missing].sort(),
|
|
432
|
+
resolutions: this.resolutions,
|
|
433
|
+
tools: [...this.tools.values()],
|
|
434
|
+
gitQueries: this.gitQueries,
|
|
435
|
+
environmentIdentity: this.environment,
|
|
436
|
+
}
|
|
437
|
+
: { ...basis, status: 'unproved', detail: this.detail ?? 'no owner-approved graphInputContract' };
|
|
438
|
+
if (this.contract !== null) {
|
|
439
|
+
try {
|
|
440
|
+
const path = witnessPath(this.workspaceRoot, this.plugin);
|
|
441
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
442
|
+
const temporary = `${path}.${randomUUID()}`;
|
|
443
|
+
try {
|
|
444
|
+
writeFileSync(temporary, JSON.stringify(witness), { flag: 'wx', mode: 0o600 });
|
|
445
|
+
renameSync(temporary, path);
|
|
446
|
+
} finally {
|
|
447
|
+
rmSync(temporary, { force: true });
|
|
448
|
+
}
|
|
449
|
+
} catch (error) {
|
|
450
|
+
if (!systemError(error)) throw error;
|
|
451
|
+
return {
|
|
452
|
+
...basis,
|
|
453
|
+
status: 'unproved',
|
|
454
|
+
detail: `Cannot publish optional graph-input evidence: ${error.message}`,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return witness;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function readInputWitness(path: string): GraphInputReadWitness | null {
|
|
463
|
+
try {
|
|
464
|
+
return typia.json.isParse<GraphInputReadWitness>(readFileSync(path, 'utf8'));
|
|
465
|
+
} catch (error) {
|
|
466
|
+
if (error instanceof SyntaxError || systemError(error)) return null;
|
|
467
|
+
throw error;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
function missingStillMissing(paths: readonly string[]): boolean {
|
|
471
|
+
return paths.every((path) => {
|
|
472
|
+
try {
|
|
473
|
+
lstatSync(path);
|
|
474
|
+
return false;
|
|
475
|
+
} catch (error) {
|
|
476
|
+
if (systemError(error) && ['ENOENT', 'ENOTDIR'].includes(error.code)) return true;
|
|
477
|
+
throw error;
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
/** Every local plugin must prove its own approved code and actual outside-input byproducts. */
|
|
482
|
+
export function graphInputContractIdentity(
|
|
483
|
+
root: string,
|
|
484
|
+
nxJson: NxJsonConfiguration,
|
|
485
|
+
snapshot: WorkspaceSnapshot,
|
|
486
|
+
): GraphInputContractProof | null {
|
|
487
|
+
const plugins: string[] = [];
|
|
488
|
+
const packages: { name: string; from: string }[] = [];
|
|
489
|
+
const identities: unknown[] = [];
|
|
490
|
+
if (!snapshot.stable) return null;
|
|
491
|
+
try {
|
|
492
|
+
for (const configured of nxJson.plugins ?? []) {
|
|
493
|
+
const plugin = typeof configured === 'string' ? configured : configured.plugin;
|
|
494
|
+
if (plugin === '@smoothbricks/nx-plugin') continue;
|
|
495
|
+
if (typeof configured === 'string' || !plugin.startsWith('./')) return null;
|
|
496
|
+
const contract = contractOf(configured.options);
|
|
497
|
+
if (contract === null || contract.modules[0]?.path !== plugin.slice(2)) return null;
|
|
498
|
+
const nodes: NodeWitness[] = [];
|
|
499
|
+
const paths = new Set(contract.modules.map(({ path }) => resolve(root, path)));
|
|
500
|
+
const require = workspaceRequire(root);
|
|
501
|
+
const ts: typeof TypeScript = require('typescript');
|
|
502
|
+
for (const module of contract.modules) {
|
|
503
|
+
const path = resolve(root, module.path);
|
|
504
|
+
if (isAbsolute(module.path) || relative(root, path).startsWith('..')) return null;
|
|
505
|
+
const before = lstatSync(path, { bigint: true });
|
|
506
|
+
const text = readFileSync(path, 'utf8');
|
|
507
|
+
if (digest(text) !== module.sha256) return null;
|
|
508
|
+
nodes.push({ path, stamp: nodeStamp(before) });
|
|
509
|
+
const physical = realpathSync(path);
|
|
510
|
+
nodes.push({ path: physical, stamp: nodeStamp(lstatSync(physical, { bigint: true })) });
|
|
511
|
+
for (const imported of ts.preProcessFile(text, true, true).importedFiles) {
|
|
512
|
+
const specifier = imported.fileName;
|
|
513
|
+
if (specifier.startsWith('node:')) continue;
|
|
514
|
+
if (specifier.startsWith('.')) {
|
|
515
|
+
const base = resolve(dirname(path), specifier);
|
|
516
|
+
const candidates = [
|
|
517
|
+
base,
|
|
518
|
+
base.replace(/\.js$/u, '.ts'),
|
|
519
|
+
`${base}.ts`,
|
|
520
|
+
`${base}.js`,
|
|
521
|
+
join(base, 'index.ts'),
|
|
522
|
+
join(base, 'index.js'),
|
|
523
|
+
];
|
|
524
|
+
if (!candidates.some((candidate) => paths.has(candidate))) return null;
|
|
525
|
+
} else {
|
|
526
|
+
const name = specifier.startsWith('@')
|
|
527
|
+
? specifier.split('/').slice(0, 2).join('/')
|
|
528
|
+
: (specifier.split('/')[0] ?? specifier);
|
|
529
|
+
if (!contract.packages.includes(name)) return null;
|
|
530
|
+
packages.push({ name, from: dirname(physical) });
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (!nodesUnchanged(nodes)) return null;
|
|
535
|
+
let witness: GraphInputReadWitness | null = null;
|
|
536
|
+
if (contract.kind !== 'native-only') {
|
|
537
|
+
witness = readInputWitness(witnessPath(root, plugin));
|
|
538
|
+
if (
|
|
539
|
+
witness === null ||
|
|
540
|
+
witness.status !== 'complete' ||
|
|
541
|
+
witness.workspaceRoot !== root ||
|
|
542
|
+
witness.plugin !== plugin ||
|
|
543
|
+
witness.contractSha256 !== digest(JSON.stringify(contract)) ||
|
|
544
|
+
witness.nativeIdentity !== nativeIdentity(snapshot.files) ||
|
|
545
|
+
witness.environmentIdentity !== environmentIdentity(root) ||
|
|
546
|
+
!nodesUnchanged(witness.nativeNodes) ||
|
|
547
|
+
!readNodesUnchanged(witness.nodes) ||
|
|
548
|
+
!missingStillMissing(witness.missing)
|
|
549
|
+
)
|
|
550
|
+
return null;
|
|
551
|
+
for (const tool of witness.tools) {
|
|
552
|
+
if (realpathSync(tool.path) !== tool.path) return null;
|
|
553
|
+
}
|
|
554
|
+
const bunResolutions = witness.resolutions.filter(({ kind }) => kind === 'bun');
|
|
555
|
+
if (bunResolutions.length > 0) {
|
|
556
|
+
const bun = witness.tools.find(({ name }) => name === 'bun');
|
|
557
|
+
if (bun === undefined) return null;
|
|
558
|
+
const recipe =
|
|
559
|
+
"console.log(JSON.stringify(JSON.parse(require('node:fs').readFileSync(0,'utf8')).map(({from,specifier})=>{try{return require('node:fs').realpathSync(Bun.resolveSync(specifier,from))}catch{return null}})))";
|
|
560
|
+
const actual = execFileSync(bun.path, ['--eval', recipe], {
|
|
561
|
+
input: JSON.stringify(bunResolutions),
|
|
562
|
+
cwd: root,
|
|
563
|
+
encoding: 'utf8',
|
|
564
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
565
|
+
});
|
|
566
|
+
const targets = typia.json.isParse<(string | null)[]>(actual);
|
|
567
|
+
if (
|
|
568
|
+
targets === null ||
|
|
569
|
+
targets.length !== bunResolutions.length ||
|
|
570
|
+
targets.some((target, index) => target !== bunResolutions[index]?.target)
|
|
571
|
+
)
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
for (const resolution of witness.resolutions) {
|
|
575
|
+
if (resolution.kind !== 'node') continue;
|
|
576
|
+
let target: string | null;
|
|
577
|
+
try {
|
|
578
|
+
target = realpathSync(createRequire(join(resolution.from, 'package.json')).resolve(resolution.specifier));
|
|
579
|
+
} catch {
|
|
580
|
+
target = null;
|
|
581
|
+
}
|
|
582
|
+
if (target !== resolution.target) return null;
|
|
583
|
+
}
|
|
584
|
+
const git = witness.tools.find(({ name }) => name === 'git');
|
|
585
|
+
for (const query of witness.gitQueries) {
|
|
586
|
+
if (git === undefined || digest(gitQuery(git.path, query)) !== query.sha256) return null;
|
|
587
|
+
}
|
|
588
|
+
if (
|
|
589
|
+
!nodesUnchanged(witness.nativeNodes) ||
|
|
590
|
+
!readNodesUnchanged(witness.nodes) ||
|
|
591
|
+
!missingStillMissing(witness.missing)
|
|
592
|
+
)
|
|
593
|
+
return null;
|
|
594
|
+
}
|
|
595
|
+
plugins.push(plugin);
|
|
596
|
+
identities.push({ plugin, contract, nodes, witness });
|
|
597
|
+
}
|
|
598
|
+
return { plugins, packages, identity: JSON.stringify(identities) };
|
|
599
|
+
} catch (error) {
|
|
600
|
+
if (systemError(error) && ['EACCES', 'EPERM', 'EIO'].includes(error.code)) throw error;
|
|
601
|
+
// Optional evidence never hides uncertainty behind a guessed local-plugin contract.
|
|
602
|
+
if (error instanceof Error) return null;
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
}
|
package/src/index.test.ts
CHANGED
|
@@ -1559,7 +1559,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
1559
1559
|
} finally {
|
|
1560
1560
|
await workspace.cleanup();
|
|
1561
1561
|
}
|
|
1562
|
-
}
|
|
1562
|
+
});
|
|
1563
1563
|
|
|
1564
1564
|
// A consumer links the plugin from a checkout of its own, outside the
|
|
1565
1565
|
// workspace, and its forks sit at different depths. Nx hashes a project's
|
|
@@ -2058,7 +2058,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
2058
2058
|
cache: true,
|
|
2059
2059
|
dependsOn: ['cargo-test', 'napi-debug', 'tsc-js', '^build'],
|
|
2060
2060
|
options: {
|
|
2061
|
-
command: 'bun test --config=../bunfig.toml --timeout=
|
|
2061
|
+
command: 'bun test --config=../bunfig.toml --timeout=0 native.test.ts',
|
|
2062
2062
|
cwd: 'packages/cowshed/src',
|
|
2063
2063
|
env: { NAPI_DEBUG_ADDON: '1' },
|
|
2064
2064
|
idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
|
|
@@ -2069,7 +2069,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
2069
2069
|
await workspace.write('packages/cowshed/bunfig.napi-test.toml', '[test]\n');
|
|
2070
2070
|
const targetsWithDedicatedBunfig = await inferProjectTargets(workspace, 'packages/cowshed/package.json');
|
|
2071
2071
|
expect(targetsWithDedicatedBunfig['napi-test']?.options?.command).toBe(
|
|
2072
|
-
'bun test --config=../bunfig.napi-test.toml --timeout=
|
|
2072
|
+
'bun test --config=../bunfig.napi-test.toml --timeout=0 native.test.ts',
|
|
2073
2073
|
);
|
|
2074
2074
|
} finally {
|
|
2075
2075
|
await workspace.cleanup();
|