@boringparts/verule 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/dist/additional-inputs.d.ts +13 -0
- package/dist/additional-inputs.js +68 -0
- package/dist/cli-command-identity.d.ts +13 -0
- package/dist/cli-command-identity.js +15 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +42 -0
- package/dist/config-loader.d.ts +14 -0
- package/dist/config-loader.js +105 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/inline-ignore.d.ts +44 -0
- package/dist/inline-ignore.js +297 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +594 -0
- package/dist/observation.d.ts +111 -0
- package/dist/observation.js +767 -0
- package/dist/projects.d.ts +21 -0
- package/dist/projects.js +168 -0
- package/dist/report-wire.d.ts +1623 -0
- package/dist/report-wire.js +738 -0
- package/dist/session-command.d.ts +48 -0
- package/dist/session-command.js +73 -0
- package/dist/session.d.ts +18 -0
- package/dist/session.js +812 -0
- package/dist/shared-session.d.ts +13 -0
- package/dist/shared-session.js +338 -0
- package/dist/types.d.ts +328 -0
- package/dist/types.js +41 -0
- package/dist/watch-command.d.ts +839 -0
- package/dist/watch-command.js +104 -0
- package/dist/watch.d.ts +59 -0
- package/dist/watch.js +806 -0
- package/package.json +44 -3
- package/README.md +0 -3
package/dist/watch.js
ADDED
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync, watch, } from "node:fs";
|
|
3
|
+
import { dirname, relative, resolve } from "node:path";
|
|
4
|
+
import { defineConfig, getSessionWorkspaceFiles, openSession, } from "./session.js";
|
|
5
|
+
import { VeruleExecutionError, } from "./types.js";
|
|
6
|
+
function normalizeRelPath(absCwd, absPath) {
|
|
7
|
+
return relative(absCwd, absPath).replace(/\\/gu, "/");
|
|
8
|
+
}
|
|
9
|
+
function normalizeAbsPath(path) {
|
|
10
|
+
return resolve(path).replace(/\\/gu, "/");
|
|
11
|
+
}
|
|
12
|
+
function isIgnoredRelativePath(relPath) {
|
|
13
|
+
const parts = relPath.split("/");
|
|
14
|
+
return parts.includes(".git") || parts.includes("node_modules");
|
|
15
|
+
}
|
|
16
|
+
function hashFileContent(absPath) {
|
|
17
|
+
try {
|
|
18
|
+
const stat = statSync(absPath);
|
|
19
|
+
if (!stat.isFile())
|
|
20
|
+
return undefined;
|
|
21
|
+
return createHash("sha256").update(readFileSync(absPath)).digest("hex");
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function toExecutionErrorInfo(err) {
|
|
28
|
+
if (err instanceof VeruleExecutionError) {
|
|
29
|
+
return {
|
|
30
|
+
code: err.code,
|
|
31
|
+
message: err.message,
|
|
32
|
+
...(err.conflict !== undefined ? { conflict: err.conflict } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
code: "ENGINE_ERROR",
|
|
37
|
+
message: err instanceof Error ? err.message : String(err),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function initialRecomputationScope(session) {
|
|
41
|
+
const files = new Set();
|
|
42
|
+
for (const project of session.projects) {
|
|
43
|
+
for (const file of project.sourceFiles) {
|
|
44
|
+
files.add(file);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
recomputedProjects: session.projects.map((p) => p.projectId),
|
|
49
|
+
reusedProjects: [],
|
|
50
|
+
recomputedFiles: [...files].toSorted(),
|
|
51
|
+
reusedFiles: [],
|
|
52
|
+
removedFiles: [],
|
|
53
|
+
invalidatedBy: ["initial"],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function recordSessionFiles(absCwd, session, knownFileHashes, knownWorkspaceFiles, loadedWorkspaceFiles, preservedFiles) {
|
|
57
|
+
const projectFiles = new Set();
|
|
58
|
+
for (const project of session.projects) {
|
|
59
|
+
const configAbs = normalizeAbsPath(resolve(absCwd, project.configFile));
|
|
60
|
+
projectFiles.add(configAbs);
|
|
61
|
+
const configHash = hashFileContent(configAbs);
|
|
62
|
+
if (configHash !== undefined) {
|
|
63
|
+
knownFileHashes.set(configAbs, configHash);
|
|
64
|
+
knownWorkspaceFiles.add(configAbs);
|
|
65
|
+
}
|
|
66
|
+
for (const relFile of project.sourceFiles) {
|
|
67
|
+
const fileAbs = normalizeAbsPath(resolve(absCwd, relFile));
|
|
68
|
+
projectFiles.add(fileAbs);
|
|
69
|
+
const fileHash = hashFileContent(fileAbs);
|
|
70
|
+
if (fileHash !== undefined) {
|
|
71
|
+
knownFileHashes.set(fileAbs, fileHash);
|
|
72
|
+
knownWorkspaceFiles.add(fileAbs);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const nextWorkspaceFiles = new Set();
|
|
77
|
+
for (const relFile of getSessionWorkspaceFiles(session)) {
|
|
78
|
+
const fileAbs = normalizeAbsPath(resolve(absCwd, relFile));
|
|
79
|
+
nextWorkspaceFiles.add(fileAbs);
|
|
80
|
+
const fileHash = hashFileContent(fileAbs);
|
|
81
|
+
if (fileHash !== undefined) {
|
|
82
|
+
knownFileHashes.set(fileAbs, fileHash);
|
|
83
|
+
knownWorkspaceFiles.add(fileAbs);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const oldFile of loadedWorkspaceFiles) {
|
|
87
|
+
if (!nextWorkspaceFiles.has(oldFile) &&
|
|
88
|
+
!projectFiles.has(oldFile) &&
|
|
89
|
+
!preservedFiles.has(oldFile)) {
|
|
90
|
+
knownFileHashes.delete(oldFile);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
loadedWorkspaceFiles.clear();
|
|
94
|
+
for (const file of nextWorkspaceFiles)
|
|
95
|
+
loadedWorkspaceFiles.add(file);
|
|
96
|
+
}
|
|
97
|
+
function collectWorkspaceFiles(absCwd, dir, out) {
|
|
98
|
+
let entries;
|
|
99
|
+
try {
|
|
100
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
for (const entry of entries) {
|
|
106
|
+
const absChild = normalizeAbsPath(resolve(dir, entry.name));
|
|
107
|
+
const rel = normalizeRelPath(absCwd, absChild);
|
|
108
|
+
if (isIgnoredRelativePath(rel)) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (entry.isDirectory()) {
|
|
112
|
+
collectWorkspaceFiles(absCwd, absChild, out);
|
|
113
|
+
}
|
|
114
|
+
else if (entry.isFile()) {
|
|
115
|
+
out.add(absChild);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function createAbortPromise(signal) {
|
|
120
|
+
if (!signal) {
|
|
121
|
+
return {
|
|
122
|
+
promise: new Promise(() => { }),
|
|
123
|
+
dispose: () => { },
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (signal.aborted) {
|
|
127
|
+
return {
|
|
128
|
+
promise: Promise.resolve(),
|
|
129
|
+
dispose: () => { },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
let onAbort;
|
|
133
|
+
const promise = new Promise((resolvePromise) => {
|
|
134
|
+
onAbort = () => resolvePromise();
|
|
135
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
promise,
|
|
139
|
+
dispose: () => {
|
|
140
|
+
if (onAbort) {
|
|
141
|
+
signal.removeEventListener("abort", onAbort);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Shared incremental workspace controller used by both `watchProject` and `startSharedSession`.
|
|
148
|
+
* Serializes filesystem watcher updates, synchronous disk checks, config reloads, and checks
|
|
149
|
+
* on a single underlying `AnalysisSession`.
|
|
150
|
+
*/
|
|
151
|
+
export function createWorkspaceController(options) {
|
|
152
|
+
const absCwd = resolve(options.cwd);
|
|
153
|
+
let adapterResolutionRoot = resolve(options.adapterResolutionRoot ?? absCwd);
|
|
154
|
+
const debounceMs = options.debounceMs ?? 50;
|
|
155
|
+
let session;
|
|
156
|
+
let lastConfig;
|
|
157
|
+
const configWatchFiles = new Set((options.watchFiles ?? []).map((file) => normalizeAbsPath(resolve(absCwd, file))));
|
|
158
|
+
const additionalInputFiles = new Set();
|
|
159
|
+
const loadedWorkspaceFiles = new Set();
|
|
160
|
+
const knownFileHashes = new Map();
|
|
161
|
+
const knownWorkspaceFiles = new Set();
|
|
162
|
+
for (const file of configWatchFiles) {
|
|
163
|
+
const hash = hashFileContent(file);
|
|
164
|
+
if (hash !== undefined) {
|
|
165
|
+
knownFileHashes.set(file, hash);
|
|
166
|
+
knownWorkspaceFiles.add(file);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (existsSync(absCwd)) {
|
|
170
|
+
collectWorkspaceFiles(absCwd, absCwd, knownWorkspaceFiles);
|
|
171
|
+
}
|
|
172
|
+
const externalWatchers = new Map();
|
|
173
|
+
let rootWatcher;
|
|
174
|
+
const pendingPaths = new Set();
|
|
175
|
+
let debounceTimer;
|
|
176
|
+
let closed = false;
|
|
177
|
+
let lastFailure;
|
|
178
|
+
let pendingRetryConfig = false;
|
|
179
|
+
const pendingRetryChanged = new Set();
|
|
180
|
+
const pendingRetryCreated = new Set();
|
|
181
|
+
const pendingRetryDeleted = new Set();
|
|
182
|
+
let cachedDefaultReport;
|
|
183
|
+
let queue = Promise.resolve();
|
|
184
|
+
function runSerialized(fn) {
|
|
185
|
+
const next = queue.then(fn, fn);
|
|
186
|
+
queue = next.then(() => undefined, () => undefined);
|
|
187
|
+
return next;
|
|
188
|
+
}
|
|
189
|
+
function clearDebounce() {
|
|
190
|
+
if (debounceTimer !== undefined) {
|
|
191
|
+
clearTimeout(debounceTimer);
|
|
192
|
+
debounceTimer = undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function schedulePath(absPath) {
|
|
196
|
+
if (closed)
|
|
197
|
+
return;
|
|
198
|
+
const normalized = normalizeAbsPath(absPath);
|
|
199
|
+
const rel = normalizeRelPath(absCwd, normalized);
|
|
200
|
+
const isExplicitWatchFile = configWatchFiles.has(normalized) ||
|
|
201
|
+
additionalInputFiles.has(normalized) ||
|
|
202
|
+
loadedWorkspaceFiles.has(normalized);
|
|
203
|
+
if (!isExplicitWatchFile && isIgnoredRelativePath(rel)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (existsSync(normalized)) {
|
|
207
|
+
try {
|
|
208
|
+
if (statSync(normalized).isDirectory()) {
|
|
209
|
+
for (const entry of readdirSync(normalized)) {
|
|
210
|
+
const child = normalizeAbsPath(resolve(normalized, entry));
|
|
211
|
+
const childRel = normalizeRelPath(absCwd, child);
|
|
212
|
+
if (!isIgnoredRelativePath(childRel) && existsSync(child)) {
|
|
213
|
+
try {
|
|
214
|
+
if (statSync(child).isFile()) {
|
|
215
|
+
pendingPaths.add(child);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
// Ignore transient entries during directory scans.
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
pendingPaths.add(normalized);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
pendingPaths.add(normalized);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
pendingPaths.add(normalized);
|
|
234
|
+
}
|
|
235
|
+
clearDebounce();
|
|
236
|
+
debounceTimer = setTimeout(() => {
|
|
237
|
+
debounceTimer = undefined;
|
|
238
|
+
options.onBatchReady?.();
|
|
239
|
+
}, debounceMs);
|
|
240
|
+
}
|
|
241
|
+
function syncWatchers() {
|
|
242
|
+
if (closed)
|
|
243
|
+
return;
|
|
244
|
+
if (!rootWatcher && existsSync(absCwd)) {
|
|
245
|
+
rootWatcher = watch(absCwd, { recursive: true }, (_event, filename) => {
|
|
246
|
+
if (typeof filename === "string" && filename.length > 0) {
|
|
247
|
+
schedulePath(resolve(absCwd, filename));
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
for (const knownPath of knownFileHashes.keys()) {
|
|
251
|
+
schedulePath(knownPath);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
const absCwdNorm = normalizeAbsPath(absCwd);
|
|
257
|
+
const neededDirs = new Set();
|
|
258
|
+
for (const filePath of [
|
|
259
|
+
...configWatchFiles,
|
|
260
|
+
...additionalInputFiles,
|
|
261
|
+
...loadedWorkspaceFiles,
|
|
262
|
+
]) {
|
|
263
|
+
const rel = normalizeRelPath(absCwd, filePath);
|
|
264
|
+
const isRootWatchedFile = filePath.startsWith(`${absCwdNorm}/`) && !isIgnoredRelativePath(rel);
|
|
265
|
+
if (filePath !== absCwdNorm && !isRootWatchedFile) {
|
|
266
|
+
neededDirs.add(normalizeAbsPath(dirname(filePath)));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
for (const [dir, watcher] of externalWatchers) {
|
|
270
|
+
if (!neededDirs.has(dir)) {
|
|
271
|
+
watcher.close();
|
|
272
|
+
externalWatchers.delete(dir);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
for (const dir of neededDirs) {
|
|
276
|
+
if (!externalWatchers.has(dir) && existsSync(dir)) {
|
|
277
|
+
const watcher = watch(dir, (_event, filename) => {
|
|
278
|
+
if (typeof filename === "string" && filename.length > 0) {
|
|
279
|
+
const candidate = normalizeAbsPath(resolve(dir, filename));
|
|
280
|
+
if (configWatchFiles.has(candidate) ||
|
|
281
|
+
additionalInputFiles.has(candidate) ||
|
|
282
|
+
loadedWorkspaceFiles.has(candidate)) {
|
|
283
|
+
schedulePath(candidate);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
for (const filePath of [
|
|
288
|
+
...configWatchFiles,
|
|
289
|
+
...additionalInputFiles,
|
|
290
|
+
...loadedWorkspaceFiles,
|
|
291
|
+
]) {
|
|
292
|
+
if (normalizeAbsPath(dirname(filePath)) === dir) {
|
|
293
|
+
schedulePath(filePath);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
externalWatchers.set(dir, watcher);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function syncSessionAdditionalInputs() {
|
|
303
|
+
if (!session || closed)
|
|
304
|
+
return;
|
|
305
|
+
const nextAdditionalFiles = new Set();
|
|
306
|
+
for (const entry of session.additionalInputs) {
|
|
307
|
+
const absFile = normalizeAbsPath(resolve(absCwd, entry.file));
|
|
308
|
+
nextAdditionalFiles.add(absFile);
|
|
309
|
+
if (!knownFileHashes.has(absFile)) {
|
|
310
|
+
const hash = entry.exists && entry.digest !== undefined
|
|
311
|
+
? entry.digest
|
|
312
|
+
: hashFileContent(absFile);
|
|
313
|
+
if (hash !== undefined) {
|
|
314
|
+
knownFileHashes.set(absFile, hash);
|
|
315
|
+
knownWorkspaceFiles.add(absFile);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
additionalInputFiles.clear();
|
|
320
|
+
for (const file of nextAdditionalFiles) {
|
|
321
|
+
additionalInputFiles.add(file);
|
|
322
|
+
}
|
|
323
|
+
syncWatchers();
|
|
324
|
+
}
|
|
325
|
+
async function resolveCurrentConfig() {
|
|
326
|
+
if (options.loadConfig) {
|
|
327
|
+
const loaded = await options.loadConfig();
|
|
328
|
+
adapterResolutionRoot = resolve(absCwd, loaded.adapterResolutionRoot ?? adapterResolutionRoot);
|
|
329
|
+
if (loaded.watchFiles) {
|
|
330
|
+
configWatchFiles.clear();
|
|
331
|
+
for (const file of loaded.watchFiles) {
|
|
332
|
+
const absFile = normalizeAbsPath(resolve(absCwd, file));
|
|
333
|
+
configWatchFiles.add(absFile);
|
|
334
|
+
const hash = hashFileContent(absFile);
|
|
335
|
+
if (hash !== undefined) {
|
|
336
|
+
knownFileHashes.set(absFile, hash);
|
|
337
|
+
knownWorkspaceFiles.add(absFile);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return defineConfig(loaded.config);
|
|
342
|
+
}
|
|
343
|
+
return options.config ? defineConfig(options.config) : defineConfig({});
|
|
344
|
+
}
|
|
345
|
+
function classifyPaths(paths) {
|
|
346
|
+
const changed = [];
|
|
347
|
+
const created = [];
|
|
348
|
+
const deleted = [];
|
|
349
|
+
let touchedConfig = false;
|
|
350
|
+
const uniquePaths = new Set(paths);
|
|
351
|
+
for (const absPath of uniquePaths) {
|
|
352
|
+
const rel = normalizeRelPath(absCwd, absPath);
|
|
353
|
+
const prevHash = knownFileHashes.get(absPath);
|
|
354
|
+
const nextHash = hashFileContent(absPath);
|
|
355
|
+
if (nextHash !== undefined) {
|
|
356
|
+
knownWorkspaceFiles.add(absPath);
|
|
357
|
+
if (prevHash === undefined) {
|
|
358
|
+
if (configWatchFiles.has(absPath)) {
|
|
359
|
+
touchedConfig = true;
|
|
360
|
+
}
|
|
361
|
+
knownFileHashes.set(absPath, nextHash);
|
|
362
|
+
created.push(rel);
|
|
363
|
+
}
|
|
364
|
+
else if (nextHash !== prevHash) {
|
|
365
|
+
if (configWatchFiles.has(absPath)) {
|
|
366
|
+
touchedConfig = true;
|
|
367
|
+
}
|
|
368
|
+
knownFileHashes.set(absPath, nextHash);
|
|
369
|
+
changed.push(rel);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
knownWorkspaceFiles.delete(absPath);
|
|
374
|
+
if (prevHash !== undefined) {
|
|
375
|
+
if (configWatchFiles.has(absPath)) {
|
|
376
|
+
touchedConfig = true;
|
|
377
|
+
}
|
|
378
|
+
knownFileHashes.delete(absPath);
|
|
379
|
+
deleted.push(rel);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return { changed, created, deleted, touchedConfig };
|
|
384
|
+
}
|
|
385
|
+
function drainPendingPaths() {
|
|
386
|
+
if (pendingPaths.size === 0)
|
|
387
|
+
return [];
|
|
388
|
+
const paths = [...pendingPaths];
|
|
389
|
+
pendingPaths.clear();
|
|
390
|
+
return paths;
|
|
391
|
+
}
|
|
392
|
+
function recordPendingFailure(err, changed, created, deleted, reloadConfigNeeded) {
|
|
393
|
+
for (const file of changed)
|
|
394
|
+
pendingRetryChanged.add(file);
|
|
395
|
+
for (const file of created)
|
|
396
|
+
pendingRetryCreated.add(file);
|
|
397
|
+
for (const file of deleted)
|
|
398
|
+
pendingRetryDeleted.add(file);
|
|
399
|
+
if (reloadConfigNeeded) {
|
|
400
|
+
pendingRetryConfig = true;
|
|
401
|
+
}
|
|
402
|
+
lastFailure = toExecutionErrorInfo(err);
|
|
403
|
+
cachedDefaultReport = undefined;
|
|
404
|
+
throw err;
|
|
405
|
+
}
|
|
406
|
+
function clearFailureState() {
|
|
407
|
+
lastFailure = undefined;
|
|
408
|
+
pendingRetryConfig = false;
|
|
409
|
+
pendingRetryChanged.clear();
|
|
410
|
+
pendingRetryCreated.clear();
|
|
411
|
+
pendingRetryDeleted.clear();
|
|
412
|
+
}
|
|
413
|
+
async function applyPathsInternal(paths) {
|
|
414
|
+
const classified = classifyPaths(paths);
|
|
415
|
+
const mergedChanged = [
|
|
416
|
+
...new Set([...pendingRetryChanged, ...classified.changed]),
|
|
417
|
+
];
|
|
418
|
+
const mergedCreated = [
|
|
419
|
+
...new Set([...pendingRetryCreated, ...classified.created]),
|
|
420
|
+
];
|
|
421
|
+
const mergedDeleted = [
|
|
422
|
+
...new Set([...pendingRetryDeleted, ...classified.deleted]),
|
|
423
|
+
];
|
|
424
|
+
const needConfigReload = classified.touchedConfig || pendingRetryConfig || !lastConfig;
|
|
425
|
+
const hasFileChanges = mergedChanged.length > 0 ||
|
|
426
|
+
mergedCreated.length > 0 ||
|
|
427
|
+
mergedDeleted.length > 0;
|
|
428
|
+
if (!hasFileChanges && !classified.touchedConfig) {
|
|
429
|
+
return { applied: false };
|
|
430
|
+
}
|
|
431
|
+
let reloadedConfig;
|
|
432
|
+
if (needConfigReload) {
|
|
433
|
+
try {
|
|
434
|
+
reloadedConfig = await resolveCurrentConfig();
|
|
435
|
+
lastConfig = reloadedConfig;
|
|
436
|
+
syncWatchers();
|
|
437
|
+
}
|
|
438
|
+
catch (err) {
|
|
439
|
+
syncWatchers();
|
|
440
|
+
recordPendingFailure(err, mergedChanged, mergedCreated, mergedDeleted, true);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (!session) {
|
|
444
|
+
try {
|
|
445
|
+
session = await openSession({
|
|
446
|
+
cwd: absCwd,
|
|
447
|
+
config: lastConfig,
|
|
448
|
+
adapterResolutionRoot,
|
|
449
|
+
...(options.observe !== undefined
|
|
450
|
+
? { observe: options.observe }
|
|
451
|
+
: {}),
|
|
452
|
+
...(options.observeLimit !== undefined
|
|
453
|
+
? { observeLimit: options.observeLimit }
|
|
454
|
+
: {}),
|
|
455
|
+
});
|
|
456
|
+
recordSessionFiles(absCwd, session, knownFileHashes, knownWorkspaceFiles, loadedWorkspaceFiles, new Set([...configWatchFiles, ...additionalInputFiles]));
|
|
457
|
+
syncSessionAdditionalInputs();
|
|
458
|
+
clearFailureState();
|
|
459
|
+
cachedDefaultReport = undefined;
|
|
460
|
+
return {
|
|
461
|
+
applied: true,
|
|
462
|
+
trigger: "initial",
|
|
463
|
+
session,
|
|
464
|
+
recomputation: initialRecomputationScope(session),
|
|
465
|
+
memory: session.memory,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
catch (err) {
|
|
469
|
+
syncWatchers();
|
|
470
|
+
recordPendingFailure(err, mergedChanged, mergedCreated, mergedDeleted, reloadedConfig !== undefined);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
let updateResult;
|
|
474
|
+
try {
|
|
475
|
+
updateResult = await session.update({
|
|
476
|
+
changed: mergedChanged,
|
|
477
|
+
created: mergedCreated,
|
|
478
|
+
deleted: mergedDeleted,
|
|
479
|
+
adapterResolutionRoot,
|
|
480
|
+
...(reloadedConfig !== undefined ? { config: reloadedConfig } : {}),
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
catch (err) {
|
|
484
|
+
syncWatchers();
|
|
485
|
+
recordPendingFailure(err, mergedChanged, mergedCreated, mergedDeleted, reloadedConfig !== undefined);
|
|
486
|
+
}
|
|
487
|
+
recordSessionFiles(absCwd, session, knownFileHashes, knownWorkspaceFiles, loadedWorkspaceFiles, new Set([...configWatchFiles, ...additionalInputFiles]));
|
|
488
|
+
syncSessionAdditionalInputs();
|
|
489
|
+
clearFailureState();
|
|
490
|
+
cachedDefaultReport = undefined;
|
|
491
|
+
return {
|
|
492
|
+
applied: true,
|
|
493
|
+
trigger: "change",
|
|
494
|
+
session,
|
|
495
|
+
recomputation: updateResult.recomputation,
|
|
496
|
+
memory: updateResult.memory,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Synchronously inspects all tracked files and scans for newly added workspace files
|
|
501
|
+
* so an immediate query after saving never races with OS watcher debounce.
|
|
502
|
+
*/
|
|
503
|
+
function collectDiskSyncCandidates() {
|
|
504
|
+
clearDebounce();
|
|
505
|
+
const candidates = new Set(drainPendingPaths());
|
|
506
|
+
for (const file of knownFileHashes.keys()) {
|
|
507
|
+
candidates.add(file);
|
|
508
|
+
}
|
|
509
|
+
for (const file of configWatchFiles) {
|
|
510
|
+
candidates.add(file);
|
|
511
|
+
}
|
|
512
|
+
for (const file of additionalInputFiles) {
|
|
513
|
+
candidates.add(file);
|
|
514
|
+
}
|
|
515
|
+
if (existsSync(absCwd)) {
|
|
516
|
+
const currentDiskFiles = new Set();
|
|
517
|
+
collectWorkspaceFiles(absCwd, absCwd, currentDiskFiles);
|
|
518
|
+
for (const diskFile of currentDiskFiles) {
|
|
519
|
+
if (!knownWorkspaceFiles.has(diskFile)) {
|
|
520
|
+
candidates.add(diskFile);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return [...candidates];
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
absCwd,
|
|
528
|
+
get session() {
|
|
529
|
+
return session;
|
|
530
|
+
},
|
|
531
|
+
syncWatchers,
|
|
532
|
+
syncAdditionalInputs: syncSessionAdditionalInputs,
|
|
533
|
+
openInitial: () => runSerialized(async () => {
|
|
534
|
+
syncWatchers();
|
|
535
|
+
try {
|
|
536
|
+
lastConfig = await resolveCurrentConfig();
|
|
537
|
+
syncWatchers();
|
|
538
|
+
session = await openSession({
|
|
539
|
+
cwd: absCwd,
|
|
540
|
+
config: lastConfig,
|
|
541
|
+
adapterResolutionRoot,
|
|
542
|
+
...(options.observe !== undefined
|
|
543
|
+
? { observe: options.observe }
|
|
544
|
+
: {}),
|
|
545
|
+
...(options.observeLimit !== undefined
|
|
546
|
+
? { observeLimit: options.observeLimit }
|
|
547
|
+
: {}),
|
|
548
|
+
});
|
|
549
|
+
recordSessionFiles(absCwd, session, knownFileHashes, knownWorkspaceFiles, loadedWorkspaceFiles, new Set([...configWatchFiles, ...additionalInputFiles]));
|
|
550
|
+
syncSessionAdditionalInputs();
|
|
551
|
+
clearFailureState();
|
|
552
|
+
cachedDefaultReport = undefined;
|
|
553
|
+
return {
|
|
554
|
+
session,
|
|
555
|
+
recomputation: initialRecomputationScope(session),
|
|
556
|
+
memory: session.memory,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
catch (err) {
|
|
560
|
+
syncWatchers();
|
|
561
|
+
lastFailure = toExecutionErrorInfo(err);
|
|
562
|
+
throw err;
|
|
563
|
+
}
|
|
564
|
+
}),
|
|
565
|
+
applyPendingBatch: () => runSerialized(async () => {
|
|
566
|
+
const paths = drainPendingPaths();
|
|
567
|
+
if (paths.length === 0) {
|
|
568
|
+
return { applied: false };
|
|
569
|
+
}
|
|
570
|
+
return applyPathsInternal(paths);
|
|
571
|
+
}),
|
|
572
|
+
checkLatest: (params) => runSerialized(async () => {
|
|
573
|
+
const candidates = collectDiskSyncCandidates();
|
|
574
|
+
if (candidates.length > 0) {
|
|
575
|
+
await applyPathsInternal(candidates);
|
|
576
|
+
}
|
|
577
|
+
if (lastFailure !== undefined) {
|
|
578
|
+
throw new VeruleExecutionError(lastFailure.code, lastFailure.message, lastFailure.conflict !== undefined
|
|
579
|
+
? { conflict: lastFailure.conflict }
|
|
580
|
+
: undefined);
|
|
581
|
+
}
|
|
582
|
+
if (!session) {
|
|
583
|
+
throw new VeruleExecutionError("SESSION_CLOSED", "Analysis session is not open.");
|
|
584
|
+
}
|
|
585
|
+
const isDefaultQuery = !params?.ruleIds?.length &&
|
|
586
|
+
params?.at === undefined &&
|
|
587
|
+
(params?.observe === undefined || params.observe === "off") &&
|
|
588
|
+
params?.observeLimit === undefined;
|
|
589
|
+
if (isDefaultQuery &&
|
|
590
|
+
cachedDefaultReport !== undefined &&
|
|
591
|
+
cachedDefaultReport.snapshotId === session.snapshotId) {
|
|
592
|
+
const checkReport = cachedDefaultReport.report;
|
|
593
|
+
return checkReport;
|
|
594
|
+
}
|
|
595
|
+
const checkOptions = params !== undefined
|
|
596
|
+
? {
|
|
597
|
+
...(params.ruleIds !== undefined
|
|
598
|
+
? { ruleIds: params.ruleIds }
|
|
599
|
+
: {}),
|
|
600
|
+
...(params.at !== undefined ? { at: params.at } : {}),
|
|
601
|
+
...(params.observe !== undefined
|
|
602
|
+
? { observe: params.observe }
|
|
603
|
+
: {}),
|
|
604
|
+
...(params.observeLimit !== undefined
|
|
605
|
+
? { observeLimit: params.observeLimit }
|
|
606
|
+
: {}),
|
|
607
|
+
}
|
|
608
|
+
: undefined;
|
|
609
|
+
let report;
|
|
610
|
+
try {
|
|
611
|
+
report = await session.check(checkOptions);
|
|
612
|
+
}
|
|
613
|
+
finally {
|
|
614
|
+
syncSessionAdditionalInputs();
|
|
615
|
+
}
|
|
616
|
+
if (isDefaultQuery) {
|
|
617
|
+
cachedDefaultReport = {
|
|
618
|
+
snapshotId: session.snapshotId,
|
|
619
|
+
configDigest: report.identity.configDigest,
|
|
620
|
+
report,
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return report;
|
|
624
|
+
}),
|
|
625
|
+
close: async () => {
|
|
626
|
+
closed = true;
|
|
627
|
+
clearDebounce();
|
|
628
|
+
rootWatcher?.close();
|
|
629
|
+
rootWatcher = undefined;
|
|
630
|
+
for (const watcher of externalWatchers.values()) {
|
|
631
|
+
watcher.close();
|
|
632
|
+
}
|
|
633
|
+
externalWatchers.clear();
|
|
634
|
+
await runSerialized(async () => {
|
|
635
|
+
if (session) {
|
|
636
|
+
const toClose = session;
|
|
637
|
+
session = undefined;
|
|
638
|
+
await toClose.close();
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
},
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Watches a project directory (and any external config dependency files), updating an
|
|
646
|
+
* {@link AnalysisSession} incrementally and yielding a complete report or failure record
|
|
647
|
+
* after each initial check and subsequent save.
|
|
648
|
+
*/
|
|
649
|
+
export async function* watchProject(options) {
|
|
650
|
+
const signal = options.signal;
|
|
651
|
+
let sequence = 0;
|
|
652
|
+
let wakeQueue;
|
|
653
|
+
let pendingBatchReady = false;
|
|
654
|
+
function notifyQueue() {
|
|
655
|
+
if (wakeQueue) {
|
|
656
|
+
const wake = wakeQueue;
|
|
657
|
+
wakeQueue = undefined;
|
|
658
|
+
wake();
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
const controller = createWorkspaceController({
|
|
662
|
+
cwd: options.cwd,
|
|
663
|
+
...(options.config !== undefined ? { config: options.config } : {}),
|
|
664
|
+
...(options.adapterResolutionRoot !== undefined
|
|
665
|
+
? { adapterResolutionRoot: options.adapterResolutionRoot }
|
|
666
|
+
: {}),
|
|
667
|
+
...(options.loadConfig !== undefined
|
|
668
|
+
? { loadConfig: options.loadConfig }
|
|
669
|
+
: {}),
|
|
670
|
+
...(options.watchFiles !== undefined
|
|
671
|
+
? { watchFiles: options.watchFiles }
|
|
672
|
+
: {}),
|
|
673
|
+
...(options.observe !== undefined ? { observe: options.observe } : {}),
|
|
674
|
+
...(options.debounceMs !== undefined
|
|
675
|
+
? { debounceMs: options.debounceMs }
|
|
676
|
+
: {}),
|
|
677
|
+
onBatchReady: () => {
|
|
678
|
+
pendingBatchReady = true;
|
|
679
|
+
notifyQueue();
|
|
680
|
+
},
|
|
681
|
+
});
|
|
682
|
+
async function waitForBatchReady() {
|
|
683
|
+
if (signal?.aborted)
|
|
684
|
+
return false;
|
|
685
|
+
if (pendingBatchReady) {
|
|
686
|
+
pendingBatchReady = false;
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
const abortHandle = createAbortPromise(signal);
|
|
690
|
+
try {
|
|
691
|
+
await Promise.race([
|
|
692
|
+
new Promise((resolveWait) => {
|
|
693
|
+
wakeQueue = resolveWait;
|
|
694
|
+
}),
|
|
695
|
+
abortHandle.promise,
|
|
696
|
+
]);
|
|
697
|
+
}
|
|
698
|
+
finally {
|
|
699
|
+
wakeQueue = undefined;
|
|
700
|
+
abortHandle.dispose();
|
|
701
|
+
}
|
|
702
|
+
if (signal?.aborted)
|
|
703
|
+
return false;
|
|
704
|
+
const ready = pendingBatchReady;
|
|
705
|
+
pendingBatchReady = false;
|
|
706
|
+
return ready;
|
|
707
|
+
}
|
|
708
|
+
const abortSignalListener = () => notifyQueue();
|
|
709
|
+
signal?.addEventListener("abort", abortSignalListener, { once: true });
|
|
710
|
+
try {
|
|
711
|
+
if (signal?.aborted)
|
|
712
|
+
return;
|
|
713
|
+
sequence += 1;
|
|
714
|
+
try {
|
|
715
|
+
const initial = await controller.openInitial();
|
|
716
|
+
let report;
|
|
717
|
+
try {
|
|
718
|
+
report = await initial.session.check();
|
|
719
|
+
}
|
|
720
|
+
finally {
|
|
721
|
+
controller.syncAdditionalInputs();
|
|
722
|
+
}
|
|
723
|
+
yield {
|
|
724
|
+
kind: "report",
|
|
725
|
+
sequence,
|
|
726
|
+
trigger: "initial",
|
|
727
|
+
snapshotId: initial.session.snapshotId,
|
|
728
|
+
report,
|
|
729
|
+
recomputation: initial.recomputation,
|
|
730
|
+
memory: initial.memory,
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
const info = toExecutionErrorInfo(err);
|
|
735
|
+
yield {
|
|
736
|
+
kind: "failed",
|
|
737
|
+
sequence,
|
|
738
|
+
trigger: "initial",
|
|
739
|
+
code: info.code,
|
|
740
|
+
message: info.message,
|
|
741
|
+
...(info.conflict !== undefined ? { conflict: info.conflict } : {}),
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
while (true) {
|
|
745
|
+
if (signal?.aborted)
|
|
746
|
+
break;
|
|
747
|
+
const ready = await waitForBatchReady();
|
|
748
|
+
if (!ready)
|
|
749
|
+
break;
|
|
750
|
+
let applied;
|
|
751
|
+
try {
|
|
752
|
+
applied = await controller.applyPendingBatch();
|
|
753
|
+
}
|
|
754
|
+
catch (err) {
|
|
755
|
+
sequence += 1;
|
|
756
|
+
const info = toExecutionErrorInfo(err);
|
|
757
|
+
yield {
|
|
758
|
+
kind: "failed",
|
|
759
|
+
sequence,
|
|
760
|
+
trigger: "change",
|
|
761
|
+
code: info.code,
|
|
762
|
+
message: info.message,
|
|
763
|
+
...(info.conflict !== undefined ? { conflict: info.conflict } : {}),
|
|
764
|
+
};
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
if (!applied.applied) {
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
sequence += 1;
|
|
771
|
+
try {
|
|
772
|
+
let report;
|
|
773
|
+
try {
|
|
774
|
+
report = await applied.session.check();
|
|
775
|
+
}
|
|
776
|
+
finally {
|
|
777
|
+
controller.syncAdditionalInputs();
|
|
778
|
+
}
|
|
779
|
+
yield {
|
|
780
|
+
kind: "report",
|
|
781
|
+
sequence,
|
|
782
|
+
trigger: "change",
|
|
783
|
+
snapshotId: applied.session.snapshotId,
|
|
784
|
+
report,
|
|
785
|
+
recomputation: applied.recomputation,
|
|
786
|
+
memory: applied.memory,
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
catch (err) {
|
|
790
|
+
const info = toExecutionErrorInfo(err);
|
|
791
|
+
yield {
|
|
792
|
+
kind: "failed",
|
|
793
|
+
sequence,
|
|
794
|
+
trigger: "change",
|
|
795
|
+
code: info.code,
|
|
796
|
+
message: info.message,
|
|
797
|
+
...(info.conflict !== undefined ? { conflict: info.conflict } : {}),
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
finally {
|
|
803
|
+
signal?.removeEventListener("abort", abortSignalListener);
|
|
804
|
+
await controller.close();
|
|
805
|
+
}
|
|
806
|
+
}
|