tslaws 1.3.0 → 1.3.1
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/dist/canonicalPath.d.ts +33 -0
- package/dist/canonicalPath.js +81 -0
- package/dist/check.d.ts +3 -3
- package/dist/check.js +7 -3
- package/dist/cli.js +2 -1
- package/dist/githubCli.js +145 -32
- package/dist/isolate.d.ts +1 -1
- package/dist/isolate.js +6 -5
- package/dist/lawRuntime.js +92 -19
- package/dist/pool.d.ts +24 -0
- package/dist/pool.js +63 -0
- package/dist/projects.js +4 -3
- package/dist/sandbox.js +72 -45
- package/dist/seal.js +3 -2
- package/dist/stateWorker.d.ts +1 -0
- package/dist/stateWorker.js +12 -0
- package/package.json +6 -6
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drops the `\\?\` prefix Node's native realpath can return on Windows. The permission model does not treat
|
|
3
|
+
* `\\?\D:\repo` as `D:\repo`, so a grant written with the prefix does not cover the path a process opens.
|
|
4
|
+
*/
|
|
5
|
+
export declare function plainPath(path: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Resolves `dir` to its canonical Windows filesystem casing without following symlinks that point elsewhere.
|
|
8
|
+
* On Windows, VS Code (`Uri.fsPath`) and some shells pass lowercase drive letters (`d:\...`) while Node's
|
|
9
|
+
* permission model, symlink targets and `tsc-laws` use canonical casing (`D:\...`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function canonicalCwd(dir: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* The first prefixes of `opened` and `actual` that are not the same directory name. Windows reports
|
|
14
|
+
* one directory as an 8.3 name and a long name (`C:\Users\RUNNER~1` and `C:\Users\runneradmin`), and a
|
|
15
|
+
* junction has a different name from its target. A path under either prefix is the other path with
|
|
16
|
+
* that prefix swapped. Undefined when the two names differ only by case, or not at all: case is
|
|
17
|
+
* already one path to the permission model.
|
|
18
|
+
*/
|
|
19
|
+
export declare function spellingRoots(opened: string, actual: string): {
|
|
20
|
+
opened: string;
|
|
21
|
+
actual: string;
|
|
22
|
+
} | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* `path` with a prefix of `from` replaced by `to`. Comparison is by directory name, so `RUNNER~1`
|
|
25
|
+
* does not match `RUNNER~10`, and the result keeps the slashes `path` was written with. `path`
|
|
26
|
+
* itself when it does not start at `from`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function retargetPath(path: string, from: string, to: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* `path` written the way `like` is written, when `like` and `unlike` are one directory under two
|
|
31
|
+
* names. `path` itself when they are the same name, or when `path` is not under `unlike`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function ontoSpelling(path: string, like: string, unlike: string): string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
/** `path` split on either slash. Empty pieces stay, so a UNC prefix round-trips. */
|
|
4
|
+
const pieces = (path) => path.split(/[\\/]/);
|
|
5
|
+
/** The slash `path` is written with. A path that uses both keeps the backslash. */
|
|
6
|
+
const divider = (path) => (path.includes('\\') ? '\\' : '/');
|
|
7
|
+
/**
|
|
8
|
+
* Drops the `\\?\` prefix Node's native realpath can return on Windows. The permission model does not treat
|
|
9
|
+
* `\\?\D:\repo` as `D:\repo`, so a grant written with the prefix does not cover the path a process opens.
|
|
10
|
+
*/
|
|
11
|
+
export function plainPath(path) {
|
|
12
|
+
if (path.startsWith('\\\\?\\UNC\\'))
|
|
13
|
+
return `\\\\${path.slice(8)}`;
|
|
14
|
+
if (path.startsWith('\\\\?\\'))
|
|
15
|
+
return path.slice(4);
|
|
16
|
+
return path;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolves `dir` to its canonical Windows filesystem casing without following symlinks that point elsewhere.
|
|
20
|
+
* On Windows, VS Code (`Uri.fsPath`) and some shells pass lowercase drive letters (`d:\...`) while Node's
|
|
21
|
+
* permission model, symlink targets and `tsc-laws` use canonical casing (`D:\...`).
|
|
22
|
+
*/
|
|
23
|
+
export function canonicalCwd(dir) {
|
|
24
|
+
if (process.platform !== 'win32')
|
|
25
|
+
return dir;
|
|
26
|
+
const resolved = plainPath(resolve(dir));
|
|
27
|
+
try {
|
|
28
|
+
const real = plainPath(realpathSync.native(resolved));
|
|
29
|
+
if (real.toLowerCase() === resolved.toLowerCase())
|
|
30
|
+
return real;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Fall back to uppercasing the drive letter when the path does not exist yet.
|
|
34
|
+
}
|
|
35
|
+
return resolved.replace(/^[a-z](?=:)/, (drive) => drive.toUpperCase());
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The first prefixes of `opened` and `actual` that are not the same directory name. Windows reports
|
|
39
|
+
* one directory as an 8.3 name and a long name (`C:\Users\RUNNER~1` and `C:\Users\runneradmin`), and a
|
|
40
|
+
* junction has a different name from its target. A path under either prefix is the other path with
|
|
41
|
+
* that prefix swapped. Undefined when the two names differ only by case, or not at all: case is
|
|
42
|
+
* already one path to the permission model.
|
|
43
|
+
*/
|
|
44
|
+
export function spellingRoots(opened, actual) {
|
|
45
|
+
const left = pieces(opened);
|
|
46
|
+
const right = pieces(actual);
|
|
47
|
+
let index = 0;
|
|
48
|
+
while (index < left.length && index < right.length && left[index].toLowerCase() === right[index].toLowerCase())
|
|
49
|
+
index += 1;
|
|
50
|
+
if (index >= left.length || index >= right.length)
|
|
51
|
+
return undefined;
|
|
52
|
+
const mark = divider(opened);
|
|
53
|
+
return { opened: left.slice(0, index + 1).join(mark), actual: right.slice(0, index + 1).join(mark) };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* `path` with a prefix of `from` replaced by `to`. Comparison is by directory name, so `RUNNER~1`
|
|
57
|
+
* does not match `RUNNER~10`, and the result keeps the slashes `path` was written with. `path`
|
|
58
|
+
* itself when it does not start at `from`.
|
|
59
|
+
*/
|
|
60
|
+
export function retargetPath(path, from, to) {
|
|
61
|
+
if (from === '')
|
|
62
|
+
return path;
|
|
63
|
+
const file = pieces(path);
|
|
64
|
+
const root = pieces(from);
|
|
65
|
+
if (file.length < root.length)
|
|
66
|
+
return path;
|
|
67
|
+
for (let index = 0; index < root.length; index += 1)
|
|
68
|
+
if (file[index].toLowerCase() !== root[index].toLowerCase())
|
|
69
|
+
return path;
|
|
70
|
+
return [...pieces(to), ...file.slice(root.length)].join(divider(path));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* `path` written the way `like` is written, when `like` and `unlike` are one directory under two
|
|
74
|
+
* names. `path` itself when they are the same name, or when `path` is not under `unlike`.
|
|
75
|
+
*/
|
|
76
|
+
export function ontoSpelling(path, like, unlike) {
|
|
77
|
+
const roots = spellingRoots(like, unlike);
|
|
78
|
+
if (roots === undefined)
|
|
79
|
+
return path;
|
|
80
|
+
return retargetPath(path, roots.actual, roots.opened);
|
|
81
|
+
}
|
package/dist/check.d.ts
CHANGED
|
@@ -11,16 +11,16 @@ export interface CheckReport {
|
|
|
11
11
|
ok: boolean;
|
|
12
12
|
}
|
|
13
13
|
/** The `@tslaws-exempt` functions of the project, for `tslaws accept` to record. */
|
|
14
|
-
export declare function exemptionsOf(
|
|
14
|
+
export declare function exemptionsOf(rawOptions: CheckOptions): string[];
|
|
15
15
|
/**
|
|
16
16
|
* The hash of each SMT-LIB problem the prover would send for each law now, rebuilt from the compiled code without
|
|
17
17
|
* running a solver. A certificate is for a law only while its problem hash is one of these.
|
|
18
18
|
*/
|
|
19
|
-
export declare function problemHashesOf(
|
|
19
|
+
export declare function problemHashesOf(rawOptions: CheckOptions, laws: ManifestLaw[]): Map<string, Set<string>>;
|
|
20
20
|
/**
|
|
21
21
|
* The certificates `tslaws accept` writes into the lock. The ones in the old lock stay only while their law and their
|
|
22
22
|
* problem are unchanged. The ones `tslaws check --certify` left in the sidecar file enter only after `verifyRecord`
|
|
23
23
|
* passes: the file is not trusted. Every record turned away is reported through `say`.
|
|
24
24
|
*/
|
|
25
25
|
export declare function certificatesToAccept(options: CheckOptions, laws: ManifestLaw[], held: Record<string, CertificateRecord> | undefined, say: (text: string) => void): Promise<Record<string, CertificateRecord>>;
|
|
26
|
-
export declare function check(
|
|
26
|
+
export declare function check(rawOptions: CheckOptions, enforceLock: boolean): Promise<CheckReport>;
|
package/dist/check.js
CHANGED
|
@@ -22,6 +22,7 @@ import { inProposals } from "./lawFiles.js";
|
|
|
22
22
|
import { classifyLawFile } from "./lawKinds.js";
|
|
23
23
|
import { normaliseProposal } from "./proposals.js";
|
|
24
24
|
import { declarationOf, exercisesOf, markFailing, Sources } from "./exercises.js";
|
|
25
|
+
import { canonicalCwd } from "./canonicalPath.js";
|
|
25
26
|
let checkSequence = 0;
|
|
26
27
|
/** Runs at most `limit` tasks at once. The CPU limit is the process-wide budget (pool.ts); this holds the single page. */
|
|
27
28
|
class Semaphore {
|
|
@@ -48,7 +49,8 @@ class Semaphore {
|
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
/** The `@tslaws-exempt` functions of the project, for `tslaws accept` to record. */
|
|
51
|
-
export function exemptionsOf(
|
|
52
|
+
export function exemptionsOf(rawOptions) {
|
|
53
|
+
const options = { ...rawOptions, cwd: canonicalCwd(rawOptions.cwd) };
|
|
52
54
|
const covered = coverFiles(options.cwd, readConfig(options.cwd).cover ?? []);
|
|
53
55
|
const compiled = [...new Set([...options.files, ...covered])];
|
|
54
56
|
const built = build(options.compiler, compiled, options.cwd);
|
|
@@ -65,7 +67,8 @@ export function exemptionsOf(options) {
|
|
|
65
67
|
* The hash of each SMT-LIB problem the prover would send for each law now, rebuilt from the compiled code without
|
|
66
68
|
* running a solver. A certificate is for a law only while its problem hash is one of these.
|
|
67
69
|
*/
|
|
68
|
-
export function problemHashesOf(
|
|
70
|
+
export function problemHashesOf(rawOptions, laws) {
|
|
71
|
+
const options = { ...rawOptions, cwd: canonicalCwd(rawOptions.cwd) };
|
|
69
72
|
const covered = coverFiles(options.cwd, readConfig(options.cwd).cover ?? []);
|
|
70
73
|
const built = build(options.compiler, [...new Set([...options.files, ...covered])], options.cwd);
|
|
71
74
|
const program = built.program ?? ts.createProgram(declarationFiles(built.outDir), withResolution(built.outDir, compilerOptions));
|
|
@@ -135,7 +138,8 @@ export async function certificatesToAccept(options, laws, held, say) {
|
|
|
135
138
|
say('no new certificates were recorded: cvc5 and Carcara are needed to check them again when accepting\n');
|
|
136
139
|
return accepted;
|
|
137
140
|
}
|
|
138
|
-
export async function check(
|
|
141
|
+
export async function check(rawOptions, enforceLock) {
|
|
142
|
+
const options = { ...rawOptions, cwd: canonicalCwd(rawOptions.cwd) };
|
|
139
143
|
// A proposal placed in .tslaws/proposals by hand may write its imports from the project root; store them where they resolve.
|
|
140
144
|
for (const file of options.files)
|
|
141
145
|
if (inProposals(file))
|
package/dist/cli.js
CHANGED
|
@@ -30,6 +30,7 @@ import { lockLevels } from "./lawKinds.js";
|
|
|
30
30
|
import { securityUsage } from "./security/cli.js";
|
|
31
31
|
import { plural } from "./plural.js";
|
|
32
32
|
import { configureJobs } from "./pool.js";
|
|
33
|
+
import { canonicalCwd } from "./canonicalPath.js";
|
|
33
34
|
const usage = `tslaws init [--force] scaffold tslaws.json, .gitignore, AGENTS.md, Claude Code hooks and a starter law (never writes the lock)
|
|
34
35
|
tslaws check [files...] [--reporter text|json|junit|github|ndjson] [--law NAME[,NAME]] [--json] [--summary] [--watch] [--runs N] [--seed N] [--no-prove] [--certify] [--require-certify] [--no-regressions] [--no-sandbox] [--allow-e2e] [--bound N] [--proof-timeout MS] [--property-timeout MS] [--jobs N] [--proposed] [--compiler PATH] [--browser chromium|firefox|webkit] [--browser-mode page|iframe] [--headed]
|
|
35
36
|
tslaws ui [files...] [--port N] [--no-open] [--tab] open a dashboard of the laws in its own desktop window (--tab: a browser tab): tree, filters, search, source, live reruns on save
|
|
@@ -715,7 +716,7 @@ export async function main(args) {
|
|
|
715
716
|
return 0;
|
|
716
717
|
}
|
|
717
718
|
const { command, flags, files, projects, filters } = parse(args);
|
|
718
|
-
const cwd = process.cwd();
|
|
719
|
+
const cwd = canonicalCwd(process.cwd());
|
|
719
720
|
if (flags.has('jobs')) {
|
|
720
721
|
const given = Number(flags.get('jobs'));
|
|
721
722
|
if (!Number.isInteger(given) || given < 1) {
|
package/dist/githubCli.js
CHANGED
|
@@ -24,14 +24,16 @@ import { readFileSync, writeFileSync, appendFileSync, existsSync } from 'node:fs
|
|
|
24
24
|
import { createHash } from 'node:crypto';
|
|
25
25
|
import { execFileSync } from 'node:child_process';
|
|
26
26
|
import { join } from 'node:path';
|
|
27
|
+
import { Worker } from 'node:worker_threads';
|
|
27
28
|
import { exemptionsOf } from "./check.js";
|
|
28
29
|
import { checkIsolated, readReport } from "./isolate.js";
|
|
30
|
+
import { jobs, largestShare, projectShare, runShared } from "./pool.js";
|
|
29
31
|
import { useSandbox } from "./sandbox.js";
|
|
30
32
|
import { extendsTarget, readConfig } from "./coverage.js";
|
|
31
33
|
import { discoverFromFiles, discoverySignature } from "./projects.js";
|
|
32
34
|
import { build, readManifest } from "./compile.js";
|
|
33
35
|
import { acceptedFingerprints, inert, recordComment, decide, parseCommands, isSharedBot, parseGithubConfig, proposalsOf, pullRequestVerdict, readApprovals, renderSticky, renderStickyAll, resolveNames, stickyMarkerFor, } from "./github.js";
|
|
34
|
-
import { lockLevels } from "./lawKinds.js";
|
|
36
|
+
import { classifyLawFile, lockLevels } from "./lawKinds.js";
|
|
35
37
|
import { acceptChanges, lawKey, lockChanges, parseLock, readLock, writeLockValue, } from "./lock.js";
|
|
36
38
|
import { escapeGithubData, escapeGithubProperty } from "./reporters.js";
|
|
37
39
|
import { plural } from "./plural.js";
|
|
@@ -239,10 +241,10 @@ function resultsOf(ctx, file, mode, sha) {
|
|
|
239
241
|
* The report of one project's laws: from the results file when `--results` names one, else from a run in a child with no
|
|
240
242
|
* credentials. Either way it is validated as data before anything acts on it.
|
|
241
243
|
*/
|
|
242
|
-
async function runLaws(ctx, unit, mode, sha, state, only) {
|
|
244
|
+
async function runLaws(ctx, unit, mode, sha, state, only, share) {
|
|
243
245
|
const file = ctx.flags.get('results');
|
|
244
246
|
if (typeof file !== 'string')
|
|
245
|
-
return runIsolated(unit, state, only);
|
|
247
|
+
return runIsolated(unit, state, only, share);
|
|
246
248
|
return readReport(resultEntry(ctx, file, unit, mode, sha).report, expectedFor(state, only));
|
|
247
249
|
}
|
|
248
250
|
/**
|
|
@@ -277,9 +279,90 @@ const expectedFor = (state, only) => ({
|
|
|
277
279
|
...(only === undefined ? {} : { required: new Set(only) }),
|
|
278
280
|
});
|
|
279
281
|
/** The laws of a project run in a child with no credentials: all of them, or only the laws with these keys. */
|
|
280
|
-
function runIsolated(unit, state, only) {
|
|
282
|
+
function runIsolated(unit, state, only, share) {
|
|
281
283
|
const options = only === undefined ? unit.options : { ...unit.options, onlyKeys: [...only] };
|
|
282
|
-
return checkIsolated(options, true, expectedFor(state, only));
|
|
284
|
+
return checkIsolated(options, true, expectedFor(state, only), process.env, share);
|
|
285
|
+
}
|
|
286
|
+
function stateOutcome(unit) {
|
|
287
|
+
try {
|
|
288
|
+
return { ok: true, state: projectState(unit.options) };
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
return { ok: false, error };
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* The state of every project, in the order of `units`. Reading one compiles it, which holds a thread, so several are
|
|
296
|
+
* read on threads of their own, at most `size` at once. One project alone is read here: a thread would only add its
|
|
297
|
+
* start. A thread that ends without an answer is that project's failure, not the run's.
|
|
298
|
+
*/
|
|
299
|
+
async function stateOutcomes(units, size) {
|
|
300
|
+
if (units.length < 2 || size < 2)
|
|
301
|
+
return units.map(stateOutcome);
|
|
302
|
+
const extension = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
303
|
+
const outcomes = Array.from({ length: units.length });
|
|
304
|
+
let next = 0;
|
|
305
|
+
const thread = async () => {
|
|
306
|
+
let worker;
|
|
307
|
+
try {
|
|
308
|
+
while (next < units.length) {
|
|
309
|
+
const index = next;
|
|
310
|
+
next += 1;
|
|
311
|
+
worker ??= new Worker(new URL(`./stateWorker${extension}`, import.meta.url));
|
|
312
|
+
const live = worker;
|
|
313
|
+
// A function cannot cross to a thread, and reading the state reports no events.
|
|
314
|
+
const { onEvent: _onEvent, ...options } = units[index].options;
|
|
315
|
+
const answer = await new Promise((resolve) => {
|
|
316
|
+
const fail = (error) => settle({ ok: false, error: String(error) });
|
|
317
|
+
const ended = (code) => fail(`the thread that read the project ended (exit ${code})`);
|
|
318
|
+
// Only these three are taken off again. The thread keeps listeners of its own on the same emitter, and
|
|
319
|
+
// without them it never hears the next project.
|
|
320
|
+
const settle = (value) => {
|
|
321
|
+
live.off('message', settle);
|
|
322
|
+
live.off('error', fail);
|
|
323
|
+
live.off('exit', ended);
|
|
324
|
+
resolve(value);
|
|
325
|
+
};
|
|
326
|
+
live.on('message', settle);
|
|
327
|
+
live.on('error', fail);
|
|
328
|
+
live.on('exit', ended);
|
|
329
|
+
live.postMessage(options);
|
|
330
|
+
});
|
|
331
|
+
outcomes[index] = answer.ok ? answer : { ok: false, error: new Error(answer.error) };
|
|
332
|
+
if (!answer.ok) {
|
|
333
|
+
// A thread that failed may not answer again: the next project gets a new one.
|
|
334
|
+
void live.terminate();
|
|
335
|
+
worker = undefined;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
finally {
|
|
340
|
+
void worker?.terminate();
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
await Promise.all(Array.from({ length: Math.min(size, units.length) }, thread));
|
|
344
|
+
return outcomes;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The slots a project's laws are given when the projects of a repository are checked at once (see `projectShare`).
|
|
348
|
+
* A project that could not be read runs nothing, so it asks for the least.
|
|
349
|
+
*/
|
|
350
|
+
function shareOf(unit, outcome, size, most) {
|
|
351
|
+
if (!outcome.ok)
|
|
352
|
+
return 1;
|
|
353
|
+
const mode = readConfig(unit.options.cwd).levels ?? 'names';
|
|
354
|
+
const holdsPage = new Map();
|
|
355
|
+
let pageLaws = 0;
|
|
356
|
+
for (const law of outcome.state.laws) {
|
|
357
|
+
let page = holdsPage.get(law.file);
|
|
358
|
+
if (page === undefined) {
|
|
359
|
+
page = classifyLawFile(unit.options.cwd, law.file, mode).kind !== 'node';
|
|
360
|
+
holdsPage.set(law.file, page);
|
|
361
|
+
}
|
|
362
|
+
if (page)
|
|
363
|
+
pageLaws += 1;
|
|
364
|
+
}
|
|
365
|
+
return projectShare(pageLaws, size, most);
|
|
283
366
|
}
|
|
284
367
|
/**
|
|
285
368
|
* What a run that proves nothing still has to find out, as the run of every law used to: that the project builds, and
|
|
@@ -351,13 +434,18 @@ async function collect(ctx, mode, file) {
|
|
|
351
434
|
const sha = String(mode === 'report'
|
|
352
435
|
? (ev.pull_request?.head?.sha ?? ctx.env.GITHUB_SHA)
|
|
353
436
|
: (mergeGroup?.head_sha ?? ev.after ?? ctx.env.GITHUB_SHA));
|
|
354
|
-
|
|
355
|
-
|
|
437
|
+
// The projects are compiled side by side, and their laws then run side by side, each on its share.
|
|
438
|
+
const size = jobs();
|
|
439
|
+
const most = largestShare(size, targets.length);
|
|
440
|
+
const states = await stateOutcomes(targets.map(({ unit }) => unit), size);
|
|
441
|
+
const run = async ({ unit }, outcome, share) => {
|
|
356
442
|
const entry = { lockSha: lockShaOf(unit) };
|
|
357
443
|
try {
|
|
358
|
-
|
|
444
|
+
if (!outcome.ok)
|
|
445
|
+
throw outcome.error;
|
|
446
|
+
const state = outcome.state;
|
|
359
447
|
if (mode === 'report')
|
|
360
|
-
entry.report = await runIsolated(unit, state);
|
|
448
|
+
entry.report = await runIsolated(unit, state, undefined, share);
|
|
361
449
|
else {
|
|
362
450
|
// A merge acts only on what the lock does not record, which it works out again from the same lock and files.
|
|
363
451
|
// On merge_group, also compare against the queue's base lock when its commit is present locally so any law
|
|
@@ -374,7 +462,7 @@ async function collect(ctx, mode, file) {
|
|
|
374
462
|
]
|
|
375
463
|
: lawsToProve(lockChanges(state.lock, state.laws, state.exempt, state.levels));
|
|
376
464
|
if (only.length > 0)
|
|
377
|
-
entry.report = await runIsolated(unit, state, only);
|
|
465
|
+
entry.report = await runIsolated(unit, state, only, share);
|
|
378
466
|
else
|
|
379
467
|
assertRunnable(unit, state);
|
|
380
468
|
}
|
|
@@ -382,8 +470,20 @@ async function collect(ctx, mode, file) {
|
|
|
382
470
|
catch (error) {
|
|
383
471
|
entry.error = error instanceof Error ? error.message : String(error);
|
|
384
472
|
}
|
|
385
|
-
|
|
386
|
-
}
|
|
473
|
+
return entry;
|
|
474
|
+
};
|
|
475
|
+
const entries = await runShared(targets.map((target, index) => ({
|
|
476
|
+
share: shareOf(target.unit, states[index], size, most),
|
|
477
|
+
run: (share) => run(target, states[index], share),
|
|
478
|
+
})), size);
|
|
479
|
+
const projects = {};
|
|
480
|
+
targets.forEach(({ unit }, index) => {
|
|
481
|
+
const entry = entries[index];
|
|
482
|
+
// `run` catches what a project throws, so a rejection here is a fault of the runner itself.
|
|
483
|
+
if (entry.status === 'rejected')
|
|
484
|
+
throw entry.reason;
|
|
485
|
+
projects[unit.path] = entry.value;
|
|
486
|
+
});
|
|
387
487
|
writeFileSync(file, JSON.stringify({ version: 1, mode, sha, projects }));
|
|
388
488
|
ctx.out(`tslaws: ran the laws of ${targets.length} project${targets.length === 1 ? '' : 's'}; results are in ${file}\n`);
|
|
389
489
|
return 0;
|
|
@@ -396,29 +496,42 @@ async function report(ctx) {
|
|
|
396
496
|
const ev = event(ctx.env);
|
|
397
497
|
const pr = Number(ctx.flags.get('pr') ?? ev.pull_request?.number ?? NaN);
|
|
398
498
|
const sha = String(ev.pull_request?.head?.sha ?? ctx.env.GITHUB_SHA ?? '');
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
499
|
+
// The projects are compiled side by side, and their laws then run side by side, each on its share.
|
|
500
|
+
const size = jobs();
|
|
501
|
+
const most = largestShare(size, active.length);
|
|
502
|
+
const states = await stateOutcomes(active.map((a) => a.unit), size);
|
|
503
|
+
const outcomes = await runShared(active.map((a, index) => ({
|
|
504
|
+
share: shareOf(a.unit, states[index], size, most),
|
|
505
|
+
run: async (share) => {
|
|
506
|
+
const outcome = states[index];
|
|
507
|
+
if (!outcome.ok)
|
|
508
|
+
throw outcome.error;
|
|
509
|
+
const { state } = outcome;
|
|
510
|
+
const checked = await runLaws(ctx, a.unit, 'report', sha, state, undefined, share);
|
|
511
|
+
return {
|
|
406
512
|
checked,
|
|
407
513
|
proposals: proposalsOf(lockChanges(state.lock, state.laws, state.exempt, state.levels), checked, a.unit.path),
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
proposals: [],
|
|
418
|
-
failed: error instanceof Error ? error.message : String(error),
|
|
419
|
-
});
|
|
514
|
+
};
|
|
515
|
+
},
|
|
516
|
+
})), size);
|
|
517
|
+
const ran = [];
|
|
518
|
+
active.forEach((a, index) => {
|
|
519
|
+
const outcome = outcomes[index];
|
|
520
|
+
if (outcome.status === 'fulfilled') {
|
|
521
|
+
ran.push({ active: a, ...outcome.value });
|
|
522
|
+
return;
|
|
420
523
|
}
|
|
421
|
-
|
|
524
|
+
const error = outcome.reason;
|
|
525
|
+
if (!monorepo)
|
|
526
|
+
throw error;
|
|
527
|
+
// One project that cannot run must not hide the others' reports.
|
|
528
|
+
ran.push({
|
|
529
|
+
active: a,
|
|
530
|
+
checked: undefined,
|
|
531
|
+
proposals: [],
|
|
532
|
+
failed: error instanceof Error ? error.message : String(error),
|
|
533
|
+
});
|
|
534
|
+
});
|
|
422
535
|
let api;
|
|
423
536
|
let comments = [];
|
|
424
537
|
try {
|
package/dist/isolate.d.ts
CHANGED
|
@@ -30,4 +30,4 @@ export type ChildRequest = {
|
|
|
30
30
|
jobs?: number;
|
|
31
31
|
};
|
|
32
32
|
/** Runs `check(options, enforceLock)` in a credential-free child and returns its validated report. */
|
|
33
|
-
export declare function checkIsolated(options: CheckOptions, enforceLock: boolean, expected: Expected, env?: NodeJS.ProcessEnv): Promise<CheckReport>;
|
|
33
|
+
export declare function checkIsolated(options: CheckOptions, enforceLock: boolean, expected: Expected, env?: NodeJS.ProcessEnv, share?: number): Promise<CheckReport>;
|
package/dist/isolate.js
CHANGED
|
@@ -218,11 +218,12 @@ export function readReport(value, expected) {
|
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
220
|
/** Runs `check(options, enforceLock)` in a credential-free child and returns its validated report. */
|
|
221
|
-
export function checkIsolated(options, enforceLock, expected, env = process.env) {
|
|
222
|
-
// The child is a slot of the budget while it runs, and it is handed the budget's size, not a fresh one of its own
|
|
223
|
-
|
|
221
|
+
export function checkIsolated(options, enforceLock, expected, env = process.env, share) {
|
|
222
|
+
// The child is a slot of the budget while it runs, and it is handed the budget's size, not a fresh one of its own:
|
|
223
|
+
// or, when several projects are checked at once, the share of it the caller set aside for this one.
|
|
224
|
+
return budget.run(() => forkCheck(options, enforceLock, expected, env, share ?? jobs()));
|
|
224
225
|
}
|
|
225
|
-
function forkCheck(options, enforceLock, expected, env) {
|
|
226
|
+
function forkCheck(options, enforceLock, expected, env, share) {
|
|
226
227
|
const extension = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
227
228
|
const { onEvent: _onEvent, ...data } = options;
|
|
228
229
|
return new Promise((resolve, reject) => {
|
|
@@ -257,6 +258,6 @@ function forkCheck(options, enforceLock, expected, env) {
|
|
|
257
258
|
});
|
|
258
259
|
child.on('error', (error) => done(() => reject(error)));
|
|
259
260
|
child.on('exit', (code, signal) => done(() => reject(new Error(`the check process ended without a report (${signal ?? `exit ${code}`})`))));
|
|
260
|
-
child.send({ options: data, enforceLock, jobs:
|
|
261
|
+
child.send({ options: data, enforceLock, jobs: share });
|
|
261
262
|
});
|
|
262
263
|
}
|
package/dist/lawRuntime.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// checker found: tsconfig `paths`, and packages whose `exports` or `types` point at TypeScript source. Two hooks, put in
|
|
3
3
|
// once per process (and once per property worker): a resolver that asks TypeScript when Node cannot resolve a name, and
|
|
4
4
|
// a loader for `.ts` files that no compiler wrote out (a workspace package's source).
|
|
5
|
-
import { existsSync as fsExists, readFileSync, statSync } from 'node:fs';
|
|
5
|
+
import { existsSync as fsExists, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
6
6
|
import Module, { createRequire } from 'node:module';
|
|
7
7
|
import { dirname, extname, join, relative, resolve, sep } from 'node:path';
|
|
8
|
+
import { ontoSpelling, plainPath } from "./canonicalPath.js";
|
|
8
9
|
import { installPluginHooks, pluginExtensionNeeded } from "./pluginHooks.js";
|
|
9
10
|
import { transformPluginFile } from "./pluginHost.js";
|
|
10
11
|
import { transformSync } from 'esbuild';
|
|
@@ -64,6 +65,56 @@ export function runtimeOf(cwd) {
|
|
|
64
65
|
}
|
|
65
66
|
const projects = new Map();
|
|
66
67
|
const byConfig = new Map();
|
|
68
|
+
/** `realpath` of a directory this process has already asked about. A failure is remembered as the path itself. */
|
|
69
|
+
const actualOfCache = new Map();
|
|
70
|
+
/**
|
|
71
|
+
* Where `dir` really is. The permission model answers a path it has not been given with an error, which is the same
|
|
72
|
+
* answer as a directory that is not there: the path we were given is then the one to use.
|
|
73
|
+
*/
|
|
74
|
+
function actualOf(dir) {
|
|
75
|
+
const known = actualOfCache.get(dir);
|
|
76
|
+
if (known !== undefined)
|
|
77
|
+
return known;
|
|
78
|
+
let actual = dir;
|
|
79
|
+
try {
|
|
80
|
+
actual = plainPath(realpathSync.native(dir));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// The directory cannot be resolved from here. Later reads keep using `dir`.
|
|
84
|
+
}
|
|
85
|
+
actualOfCache.set(dir, actual);
|
|
86
|
+
return actual;
|
|
87
|
+
}
|
|
88
|
+
/** `file` written with the same directory names as `owner.cwd`. An 8.3 name and a long name are one directory. */
|
|
89
|
+
function asProject(file, owner) {
|
|
90
|
+
return ontoSpelling(file, owner.cwd, actualOf(owner.cwd));
|
|
91
|
+
}
|
|
92
|
+
/** Whether `file` is `dir` or a file under it, including when the two use different names for one directory. */
|
|
93
|
+
function underDir(dir, file) {
|
|
94
|
+
const prefix = dir.endsWith(sep) ? dir : dir + sep;
|
|
95
|
+
if (file === dir || file.startsWith(prefix))
|
|
96
|
+
return true;
|
|
97
|
+
const opened = ontoSpelling(file, dir, actualOf(dir));
|
|
98
|
+
return opened === dir || opened.startsWith(prefix);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The other name of `path`, when a registered project is reached by an 8.3 name or a junction and `path` uses one of
|
|
102
|
+
* the two. Undefined when every project has a single name, or `path` is under neither name.
|
|
103
|
+
*/
|
|
104
|
+
function otherOf(path) {
|
|
105
|
+
for (const project of projects.values()) {
|
|
106
|
+
const actual = actualOf(project.cwd);
|
|
107
|
+
if (actual.toLowerCase() === project.cwd.toLowerCase())
|
|
108
|
+
continue;
|
|
109
|
+
const opened = ontoSpelling(path, project.cwd, actual);
|
|
110
|
+
if (opened !== path)
|
|
111
|
+
return opened;
|
|
112
|
+
const closed = ontoSpelling(path, actual, project.cwd);
|
|
113
|
+
if (closed !== path)
|
|
114
|
+
return closed;
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
67
118
|
// Compiled laws are CommonJS (see `build` in compile.ts), and these hooks are the CommonJS loader's own (`_resolveFilename`,
|
|
68
119
|
// `_extensions`), so `createRequire` is unavoidable: it loads TypeScript lazily, inside the synchronous resolver hook.
|
|
69
120
|
const load = createRequire(import.meta.url);
|
|
@@ -144,7 +195,7 @@ export function installLawRuntime(runtime) {
|
|
|
144
195
|
/** The project a file belongs to: the one with the deepest directory holding it, else the most recently registered. */
|
|
145
196
|
function ownerOf(file) {
|
|
146
197
|
const all = [...projects.values()];
|
|
147
|
-
const holding = all.filter((p) =>
|
|
198
|
+
const holding = all.filter((p) => underDir(p.cwd, file)).sort((a, b) => b.cwd.length - a.cwd.length);
|
|
148
199
|
return holding[0] ?? all.at(-1);
|
|
149
200
|
}
|
|
150
201
|
/**
|
|
@@ -160,12 +211,13 @@ function twinOf(file, importer) {
|
|
|
160
211
|
if (!/\.[cm]?tsx?$/.test(file) || /\.d\.[cm]?ts$/.test(file))
|
|
161
212
|
return undefined;
|
|
162
213
|
const owner = ownerOf(importer);
|
|
163
|
-
if (owner === undefined || !
|
|
214
|
+
if (owner === undefined || !underDir(owner.cwd, file))
|
|
164
215
|
return undefined;
|
|
165
216
|
const root = compiledRoot(owner);
|
|
166
|
-
|
|
217
|
+
const opened = asProject(file, owner);
|
|
218
|
+
if (opened.startsWith(root + sep))
|
|
167
219
|
return undefined;
|
|
168
|
-
const twin = resolve(root, relative(owner.cwd,
|
|
220
|
+
const twin = resolve(root, relative(owner.cwd, opened)).replace(/\.[cm]?tsx?$/, '.js');
|
|
169
221
|
return existsSync(twin) ? twin : undefined;
|
|
170
222
|
}
|
|
171
223
|
const jsToTs = {
|
|
@@ -201,10 +253,10 @@ function beside(request, parent) {
|
|
|
201
253
|
const from = parent?.filename;
|
|
202
254
|
if (from === undefined || !request.startsWith('.'))
|
|
203
255
|
return undefined;
|
|
204
|
-
const owner = [...projects].find(([top]) =>
|
|
256
|
+
const owner = [...projects].find(([top]) => underDir(top, from))?.[1];
|
|
205
257
|
if (owner === undefined)
|
|
206
258
|
return undefined;
|
|
207
|
-
const file = resolve(owner.cwd, relative(compiledRoot(owner), dirname(from)), request);
|
|
259
|
+
const file = resolve(owner.cwd, relative(compiledRoot(owner), dirname(asProject(from, owner))), request);
|
|
208
260
|
return existsSync(file) && statSync(file).isFile() ? file : undefined;
|
|
209
261
|
}
|
|
210
262
|
/** The options for a file that is not a compiled law: those of the nearest tsconfig, as the package would resolve. */
|
|
@@ -224,7 +276,7 @@ function namedInPaths(request, parent) {
|
|
|
224
276
|
const from = parent?.filename;
|
|
225
277
|
if (from === undefined)
|
|
226
278
|
return false;
|
|
227
|
-
const owner = [...projects].find(([top]) =>
|
|
279
|
+
const owner = [...projects].find(([top]) => underDir(top, from))?.[1];
|
|
228
280
|
const options = owner === undefined ? optionsAround(from) : owner.options;
|
|
229
281
|
return Object.keys((options?.paths ?? {})).some((pattern) => {
|
|
230
282
|
if (pattern === '*')
|
|
@@ -249,8 +301,15 @@ function readableSys(sys) {
|
|
|
249
301
|
return otherwise;
|
|
250
302
|
}
|
|
251
303
|
};
|
|
304
|
+
// `fileExists` and `readFile` answer from either name of a directory. The path map is written with the name the
|
|
305
|
+
// build opened, and a grant is the name `realpath` reports; on Windows those two differ for an 8.3 directory.
|
|
252
306
|
return {
|
|
253
|
-
fileExists:
|
|
307
|
+
fileExists: (path) => {
|
|
308
|
+
if (sys.fileExists(path))
|
|
309
|
+
return true;
|
|
310
|
+
const other = otherOf(path);
|
|
311
|
+
return other !== undefined && sys.fileExists(other);
|
|
312
|
+
},
|
|
254
313
|
// `sys` answers "no" for a directory the process may not read. TypeScript then skips the files under it, and a file the
|
|
255
314
|
// process may read (one granted by name, outside the project) sits in one: such a directory is taken to exist.
|
|
256
315
|
directoryExists: (path) => {
|
|
@@ -261,15 +320,23 @@ function readableSys(sys) {
|
|
|
261
320
|
return error.code === 'ERR_ACCESS_DENIED';
|
|
262
321
|
}
|
|
263
322
|
},
|
|
264
|
-
readFile:
|
|
323
|
+
readFile: (path) => {
|
|
324
|
+
const text = sys.readFile(path);
|
|
325
|
+
const other = otherOf(path);
|
|
326
|
+
return text !== undefined || other === undefined ? text : sys.readFile(other);
|
|
327
|
+
},
|
|
265
328
|
// A path it may not resolve is taken as it is: resolving walks the directories above, which the process may not read.
|
|
329
|
+
// A path it can resolve is put back into the name the project was opened with, so the compiled file sits beside it.
|
|
266
330
|
realpath: (path) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
331
|
+
const reported = plainPath(sys.realpath?.(path) ?? path);
|
|
332
|
+
const other = reported === path ? otherOf(path) : undefined;
|
|
333
|
+
const found = other === undefined ? reported : plainPath(sys.realpath?.(other) ?? reported);
|
|
334
|
+
for (const project of projects.values()) {
|
|
335
|
+
const opened = ontoSpelling(found, project.cwd, actualOf(project.cwd));
|
|
336
|
+
if (opened !== found)
|
|
337
|
+
return opened;
|
|
272
338
|
}
|
|
339
|
+
return found;
|
|
273
340
|
},
|
|
274
341
|
getCurrentDirectory: () => sys.getCurrentDirectory(),
|
|
275
342
|
getDirectories: safe(sys.getDirectories, []),
|
|
@@ -279,9 +346,11 @@ function resolveThroughTypeScript(request, parent) {
|
|
|
279
346
|
const from = parent?.filename;
|
|
280
347
|
if (from === undefined)
|
|
281
348
|
return undefined;
|
|
282
|
-
const owner = [...projects].find(([top]) =>
|
|
349
|
+
const owner = [...projects].find(([top]) => underDir(top, from))?.[1];
|
|
283
350
|
// A compiled law resolves as its source does; the source is what imports are relative to.
|
|
284
|
-
const containing = owner === undefined
|
|
351
|
+
const containing = owner === undefined
|
|
352
|
+
? from
|
|
353
|
+
: join(owner.cwd, relative(compiledRoot(owner), asProject(from, owner))).replace(/\.js$/, '.ts');
|
|
285
354
|
const options = owner === undefined ? optionsAround(from) : owner.options;
|
|
286
355
|
if (options === undefined)
|
|
287
356
|
return undefined;
|
|
@@ -313,9 +382,13 @@ function runnableFile(found, owner) {
|
|
|
313
382
|
return existsSync(js) ? js : undefined;
|
|
314
383
|
}
|
|
315
384
|
if (owner !== undefined && /\.[cm]?tsx?$/.test(found) && !/[\\/]node_modules[\\/]/.test(found)) {
|
|
316
|
-
const compiled = resolve(compiledRoot(owner), relative(owner.cwd, found)).replace(/\.[cm]?tsx?$/, '.js');
|
|
385
|
+
const compiled = resolve(compiledRoot(owner), relative(owner.cwd, asProject(found, owner))).replace(/\.[cm]?tsx?$/, '.js');
|
|
317
386
|
if (existsSync(compiled))
|
|
318
387
|
return compiled;
|
|
319
388
|
}
|
|
320
|
-
|
|
389
|
+
if (existsSync(found))
|
|
390
|
+
return found;
|
|
391
|
+
// The name the type checker built may be the one the process was not granted. The other name is the same file.
|
|
392
|
+
const other = otherOf(found);
|
|
393
|
+
return other !== undefined && existsSync(other) ? other : undefined;
|
|
321
394
|
}
|
package/dist/pool.d.ts
CHANGED
|
@@ -61,6 +61,30 @@ export declare class Budget {
|
|
|
61
61
|
}
|
|
62
62
|
/** The one budget of this process. */
|
|
63
63
|
export declare const budget: Budget;
|
|
64
|
+
/**
|
|
65
|
+
* The slots one project's check is given when several projects are checked at once. A browser or end-to-end law holds a
|
|
66
|
+
* page for as long as it runs, so a project of those is faster with every slot it gets: it takes one per such law, up to
|
|
67
|
+
* `most`. A project of Node laws spends most of its time compiling and starting its sandbox, which more slots do not
|
|
68
|
+
* shorten, so it takes two and leaves the rest to the projects beside it.
|
|
69
|
+
*/
|
|
70
|
+
export declare function projectShare(pageLaws: number, size: number, most?: number): number;
|
|
71
|
+
/**
|
|
72
|
+
* The most one of `count` projects may take of a budget of `size`. With room to spare, two slots are kept back from the
|
|
73
|
+
* largest project, so the small ones run beside it and not after it. A small budget, or a project alone, keeps nothing
|
|
74
|
+
* back.
|
|
75
|
+
*/
|
|
76
|
+
export declare function largestShare(size: number, count: number): number;
|
|
77
|
+
/** One piece of work for `runShared`: the slots it asks for, and the work, which is told how many it was given. */
|
|
78
|
+
export interface SharedTask<T> {
|
|
79
|
+
share: number;
|
|
80
|
+
run: (share: number) => Promise<T>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Runs the tasks together for as long as their shares fit in `size`, the largest first, so the one that needs the whole
|
|
84
|
+
* budget has it to itself and the small ones then run side by side. A share above `size` is cut to it. Every task runs
|
|
85
|
+
* whatever the others do; the outcomes come back in the order of the tasks.
|
|
86
|
+
*/
|
|
87
|
+
export declare function runShared<T>(tasks: readonly SharedTask<T>[], size: number): Promise<PromiseSettledResult<T>[]>;
|
|
64
88
|
/**
|
|
65
89
|
* A pool of workers where each request holds one exclusively, so a timeout can terminate its worker without
|
|
66
90
|
* disturbing a neighbour. Workers start on demand and are reused. `size` is read each time, so it may follow the budget.
|
package/dist/pool.js
CHANGED
|
@@ -166,6 +166,69 @@ export class Budget {
|
|
|
166
166
|
}
|
|
167
167
|
/** The one budget of this process. */
|
|
168
168
|
export const budget = new Budget();
|
|
169
|
+
/**
|
|
170
|
+
* The slots one project's check is given when several projects are checked at once. A browser or end-to-end law holds a
|
|
171
|
+
* page for as long as it runs, so a project of those is faster with every slot it gets: it takes one per such law, up to
|
|
172
|
+
* `most`. A project of Node laws spends most of its time compiling and starting its sandbox, which more slots do not
|
|
173
|
+
* shorten, so it takes two and leaves the rest to the projects beside it.
|
|
174
|
+
*/
|
|
175
|
+
export function projectShare(pageLaws, size, most = size) {
|
|
176
|
+
return Math.max(1, Math.min(size, most, Math.max(2, pageLaws)));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The most one of `count` projects may take of a budget of `size`. With room to spare, two slots are kept back from the
|
|
180
|
+
* largest project, so the small ones run beside it and not after it. A small budget, or a project alone, keeps nothing
|
|
181
|
+
* back.
|
|
182
|
+
*/
|
|
183
|
+
export function largestShare(size, count) {
|
|
184
|
+
return count > 1 && size >= 6 ? size - 2 : size;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Runs the tasks together for as long as their shares fit in `size`, the largest first, so the one that needs the whole
|
|
188
|
+
* budget has it to itself and the small ones then run side by side. A share above `size` is cut to it. Every task runs
|
|
189
|
+
* whatever the others do; the outcomes come back in the order of the tasks.
|
|
190
|
+
*/
|
|
191
|
+
export function runShared(tasks, size) {
|
|
192
|
+
const limit = Math.max(1, Math.floor(size));
|
|
193
|
+
const shareOf = (task) => Math.max(1, Math.min(limit, Math.floor(task.share)));
|
|
194
|
+
const outcomes = Array.from({ length: tasks.length });
|
|
195
|
+
// Sorting is stable, so tasks of one size keep the order they were given in.
|
|
196
|
+
const waiting = tasks.map((_, index) => index).sort((a, b) => shareOf(tasks[b]) - shareOf(tasks[a]));
|
|
197
|
+
return new Promise((resolve) => {
|
|
198
|
+
let free = limit;
|
|
199
|
+
let running = 0;
|
|
200
|
+
const start = () => {
|
|
201
|
+
for (let at = 0; at < waiting.length;) {
|
|
202
|
+
const index = waiting[at];
|
|
203
|
+
const task = tasks[index];
|
|
204
|
+
const share = shareOf(task);
|
|
205
|
+
if (share > free) {
|
|
206
|
+
at += 1;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
waiting.splice(at, 1);
|
|
210
|
+
free -= share;
|
|
211
|
+
running += 1;
|
|
212
|
+
// The call is inside the chain, so a task that throws before it returns a promise is an outcome too.
|
|
213
|
+
void Promise.resolve()
|
|
214
|
+
.then(() => task.run(share))
|
|
215
|
+
.then((value) => {
|
|
216
|
+
outcomes[index] = { status: 'fulfilled', value };
|
|
217
|
+
}, (reason) => {
|
|
218
|
+
outcomes[index] = { status: 'rejected', reason };
|
|
219
|
+
})
|
|
220
|
+
.then(() => {
|
|
221
|
+
free += share;
|
|
222
|
+
running -= 1;
|
|
223
|
+
start();
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
if (running === 0 && waiting.length === 0)
|
|
227
|
+
resolve(outcomes);
|
|
228
|
+
};
|
|
229
|
+
start();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
169
232
|
/**
|
|
170
233
|
* A pool of workers where each request holds one exclusively, so a timeout can terminate its worker without
|
|
171
234
|
* disturbing a neighbour. Workers start on demand and are reused. `size` is read each time, so it may follow the budget.
|
package/dist/projects.js
CHANGED
|
@@ -8,6 +8,7 @@ import { projectsProblem, readConfig } from "./coverage.js";
|
|
|
8
8
|
import { globMatcher } from "./globs.js";
|
|
9
9
|
import { customLawFile } from "./lawFiles.js";
|
|
10
10
|
import { readResolution } from "./tsconfigResolve.js";
|
|
11
|
+
import { canonicalCwd } from "./canonicalPath.js";
|
|
11
12
|
/** Directories never searched, for projects or for laws files. */
|
|
12
13
|
const skipped = new Set(['node_modules', '.git', 'dist', '.tslaws']);
|
|
13
14
|
/** `./packages/a/` as `packages/a`; `.` and `./` as ''. */
|
|
@@ -173,7 +174,7 @@ function rootHasLaws(root, projects) {
|
|
|
173
174
|
* own (see `rootHasLaws`); a root whose tslaws.json only lists projects is just the list.
|
|
174
175
|
*/
|
|
175
176
|
export function discoverProjects(root, opts = {}) {
|
|
176
|
-
const dir = resolve(root);
|
|
177
|
+
const dir = canonicalCwd(resolve(root));
|
|
177
178
|
const listed = projectsOf(dir);
|
|
178
179
|
const globs = listed ?? [workspacesOf(dir), pnpmWorkspacesOf(dir)].find((list) => list.length > 0) ?? [];
|
|
179
180
|
const paths = globs.length === 0 ? [] : matchingProjects(dir, globs);
|
|
@@ -201,7 +202,7 @@ export function discoverProjects(root, opts = {}) {
|
|
|
201
202
|
* tslaws.json has a `projects` list.
|
|
202
203
|
*/
|
|
203
204
|
export function isMonorepoRoot(cwd) {
|
|
204
|
-
const dir = resolve(cwd);
|
|
205
|
+
const dir = canonicalCwd(resolve(cwd));
|
|
205
206
|
if (!existsSync(join(dir, 'tslaws.json')))
|
|
206
207
|
return discoverProjects(dir, { standalone: true }).length > 0;
|
|
207
208
|
return projectsOf(dir) !== undefined;
|
|
@@ -406,7 +407,7 @@ function dependencyGraph(root, projects) {
|
|
|
406
407
|
* to a file a tslaws.json extends selects every project.
|
|
407
408
|
*/
|
|
408
409
|
export function affectedProjects(root, files) {
|
|
409
|
-
const dir = resolve(root);
|
|
410
|
+
const dir = canonicalCwd(resolve(root));
|
|
410
411
|
const projects = discoverProjects(dir);
|
|
411
412
|
const every = (reason) => projects.map((project) => ({ project, reason }));
|
|
412
413
|
const bases = new Map();
|
package/dist/sandbox.js
CHANGED
|
@@ -17,6 +17,7 @@ import { homedir } from 'node:os';
|
|
|
17
17
|
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
18
18
|
import { fileURLToPath } from 'node:url';
|
|
19
19
|
import ts from 'typescript';
|
|
20
|
+
import { canonicalCwd, ontoSpelling, plainPath } from "./canonicalPath.js";
|
|
20
21
|
import { lawEnv, readConfig } from "./coverage.js";
|
|
21
22
|
import { scrubbedEnv } from "./isolate.js";
|
|
22
23
|
import { runtimeOf, transformSource } from "./lawRuntime.js";
|
|
@@ -71,6 +72,18 @@ export function warnE2e() {
|
|
|
71
72
|
}
|
|
72
73
|
/** Only these of the parent's own flags reach the process: resource limits and TypeScript support, never code loaders. */
|
|
73
74
|
const keptFlag = /^--(experimental-strip-types|experimental-transform-types|no-experimental-strip-types|max-old-space-size=\d+|max-semi-space-size=\d+|stack-size=\d+)$/;
|
|
75
|
+
/** The path as the file system has it: links followed, and the case Windows and macOS keep. Itself when it does not exist. */
|
|
76
|
+
function real(path) {
|
|
77
|
+
try {
|
|
78
|
+
return plainPath(realpathSync.native(path));
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return plainPath(resolve(path));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Paths compare in one case on a file system that ignores it. */
|
|
85
|
+
const folds = process.platform === 'win32' || process.platform === 'darwin' || !ts.sys.useCaseSensitiveFileNames;
|
|
86
|
+
const fold = (path) => (folds ? path.toLowerCase() : path);
|
|
74
87
|
/**
|
|
75
88
|
* The packages' trees: every `node_modules` above `from`, and the trees its symlinks (workspace packages) point to. A
|
|
76
89
|
* workspace links the project being checked into the root `node_modules` as well; that tree is the project, whose files
|
|
@@ -79,18 +92,23 @@ const keptFlag = /^--(experimental-strip-types|experimental-transform-types|no-e
|
|
|
79
92
|
function modulesAbove(from, project) {
|
|
80
93
|
const modules = [];
|
|
81
94
|
const linked = [];
|
|
82
|
-
|
|
95
|
+
const projectFold = fold(real(project));
|
|
96
|
+
for (let dir = real(from);; dir = dirname(dir)) {
|
|
83
97
|
const modulesDir = join(dir, 'node_modules');
|
|
84
98
|
if (existsSync(modulesDir)) {
|
|
85
99
|
modules.push(modulesDir);
|
|
100
|
+
const modulesFold = fold(modulesDir);
|
|
86
101
|
const scan = (parent, entries) => {
|
|
87
102
|
for (const entry of entries) {
|
|
88
103
|
const path = join(parent, entry.name);
|
|
89
104
|
if (entry.isSymbolicLink()) {
|
|
90
105
|
try {
|
|
91
|
-
const target =
|
|
92
|
-
const
|
|
93
|
-
|
|
106
|
+
const target = real(path);
|
|
107
|
+
const targetFold = fold(target);
|
|
108
|
+
const isProject = projectFold === targetFold ||
|
|
109
|
+
projectFold.startsWith(targetFold + sep) ||
|
|
110
|
+
targetFold.startsWith(projectFold + sep);
|
|
111
|
+
if (!targetFold.startsWith(modulesFold) && !isProject)
|
|
94
112
|
linked.push(target);
|
|
95
113
|
}
|
|
96
114
|
catch {
|
|
@@ -107,31 +125,34 @@ function modulesAbove(from, project) {
|
|
|
107
125
|
return { modules, linked };
|
|
108
126
|
}
|
|
109
127
|
}
|
|
110
|
-
const realOf = (path) => {
|
|
111
|
-
try {
|
|
112
|
-
return realpathSync(path);
|
|
113
|
-
}
|
|
114
|
-
catch {
|
|
115
|
-
return path;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
128
|
/** The tslaws package: `src` or `dist` sits directly under it. */
|
|
119
|
-
const tslawsRoot = fileURLToPath(new URL('../', import.meta.url));
|
|
129
|
+
const tslawsRoot = real(fileURLToPath(new URL('../', import.meta.url)));
|
|
120
130
|
/** What of it the process loads: its code and its `package.json`, not whatever else sits in the package directory. */
|
|
121
131
|
const tslawsCode = [join(tslawsRoot, 'dist'), join(tslawsRoot, 'src'), join(tslawsRoot, 'package.json')];
|
|
132
|
+
/**
|
|
133
|
+
* Every spelling of `path` a process may open. Node's permission check compares the path text: on Windows a
|
|
134
|
+
* lowercase drive letter, an 8.3 name and a long name are different grants, even when they are one directory.
|
|
135
|
+
*/
|
|
136
|
+
function openPaths(path) {
|
|
137
|
+
const resolved = plainPath(resolve(path));
|
|
138
|
+
const actual = real(resolved);
|
|
139
|
+
return resolved === actual ? [actual] : [resolved, actual];
|
|
140
|
+
}
|
|
122
141
|
/** The `--allow-fs-read` paths: the project, its `node_modules`, the tslaws package, and the trees workspace links lead to. */
|
|
123
142
|
export function readableBy(cwd, extra = []) {
|
|
124
|
-
const
|
|
125
|
-
const
|
|
143
|
+
const canonical = canonicalCwd(cwd);
|
|
144
|
+
const project = real(canonical);
|
|
145
|
+
const here = modulesAbove(project, project);
|
|
146
|
+
const there = modulesAbove(tslawsRoot, project);
|
|
126
147
|
return [
|
|
127
148
|
...new Set([
|
|
128
|
-
|
|
149
|
+
...openPaths(canonical),
|
|
129
150
|
...tslawsCode,
|
|
130
151
|
...here.modules,
|
|
131
152
|
...here.linked,
|
|
132
153
|
...there.modules,
|
|
133
154
|
...there.linked,
|
|
134
|
-
...extra,
|
|
155
|
+
...extra.flatMap((entry) => openPaths(entry)),
|
|
135
156
|
]),
|
|
136
157
|
];
|
|
137
158
|
}
|
|
@@ -211,18 +232,6 @@ const isInside = (dir, file) => {
|
|
|
211
232
|
const rel = relative(dir, file);
|
|
212
233
|
return rel !== '' && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
213
234
|
};
|
|
214
|
-
/** The path as the file system has it: links followed, and the case Windows and macOS keep. Itself when it does not exist. */
|
|
215
|
-
function real(path) {
|
|
216
|
-
try {
|
|
217
|
-
return realpathSync.native(path);
|
|
218
|
-
}
|
|
219
|
-
catch {
|
|
220
|
-
return resolve(path);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
/** Paths compare in one case on a file system that ignores it. */
|
|
224
|
-
const folds = process.platform === 'win32' || process.platform === 'darwin' || !ts.sys.useCaseSensitiveFileNames;
|
|
225
|
-
const fold = (path) => (folds ? path.toLowerCase() : path);
|
|
226
235
|
/**
|
|
227
236
|
* The first part of `path` that is a dotfile or a dot directory. A path inside the project is judged below the project,
|
|
228
237
|
* which is readable already wherever it sits. Any other path is judged below the home directory, or in full when it is not
|
|
@@ -352,8 +361,9 @@ function stampsMatch(stamps) {
|
|
|
352
361
|
* package) is source: its tree is trusted as a tree. The project itself is not a package; its files are reviewed one by one.
|
|
353
362
|
*/
|
|
354
363
|
export function trustedPackages(cwd) {
|
|
355
|
-
const project =
|
|
356
|
-
const
|
|
364
|
+
const project = real(resolve(cwd));
|
|
365
|
+
const projectFold = fold(project);
|
|
366
|
+
const cached = trustedPackagesCache.get(projectFold);
|
|
357
367
|
if (cached !== undefined && stampsMatch(cached.stamps))
|
|
358
368
|
return cached.value;
|
|
359
369
|
const stamps = [...manifestStamp(project), ...manifestStamp(tslawsRoot)];
|
|
@@ -364,20 +374,23 @@ export function trustedPackages(cwd) {
|
|
|
364
374
|
for (let dir = from;; dir = dirname(dir)) {
|
|
365
375
|
const candidate = join(dir, 'node_modules', name);
|
|
366
376
|
if (existsSync(join(candidate, 'package.json')))
|
|
367
|
-
return
|
|
377
|
+
return real(candidate);
|
|
368
378
|
if (dirname(dir) === dir)
|
|
369
379
|
return undefined;
|
|
370
380
|
}
|
|
371
381
|
};
|
|
372
382
|
const visit = (name, from) => {
|
|
373
383
|
const dir = find(name, from);
|
|
374
|
-
if (dir === undefined
|
|
384
|
+
if (dir === undefined)
|
|
385
|
+
return;
|
|
386
|
+
const dirFold = fold(dir);
|
|
387
|
+
if (seen.has(dirFold))
|
|
375
388
|
return;
|
|
376
|
-
seen.add(
|
|
389
|
+
seen.add(dirFold);
|
|
377
390
|
if (/[\\/]node_modules[\\/]/.test(dir))
|
|
378
391
|
modules.add(normal(dir));
|
|
379
392
|
// The project's own files are reviewed one by one (through the seals), not as a tree.
|
|
380
|
-
else if (
|
|
393
|
+
else if (dirFold === projectFold || dirFold.startsWith(projectFold + sep))
|
|
381
394
|
return;
|
|
382
395
|
else
|
|
383
396
|
linked.add(dir);
|
|
@@ -394,7 +407,7 @@ export function trustedPackages(cwd) {
|
|
|
394
407
|
const value = { modules: [...modules], linked: [...linked] };
|
|
395
408
|
if (trustedPackagesCache.size > 256)
|
|
396
409
|
trustedPackagesCache.clear();
|
|
397
|
-
trustedPackagesCache.set(
|
|
410
|
+
trustedPackagesCache.set(projectFold, { stamps, value });
|
|
398
411
|
return value;
|
|
399
412
|
}
|
|
400
413
|
/**
|
|
@@ -404,8 +417,11 @@ export function trustedPackages(cwd) {
|
|
|
404
417
|
export function hostConfig(cwd, entries, outDir, laws) {
|
|
405
418
|
const files = reviewedFiles(cwd, entries, laws);
|
|
406
419
|
// What the process cannot read already (outside the project and outside the trees it is given) needs a grant, by name.
|
|
407
|
-
const trees = readableBy(cwd);
|
|
408
|
-
const { granted } = grantOutside(cwd, files.filter((file) =>
|
|
420
|
+
const trees = readableBy(cwd).map((tree) => fold(real(tree)));
|
|
421
|
+
const { granted } = grantOutside(cwd, files.filter((file) => {
|
|
422
|
+
const target = fold(real(file));
|
|
423
|
+
return !trees.some((tree) => target === tree || isInside(tree, target));
|
|
424
|
+
}));
|
|
409
425
|
const base = join(cwd, '.tslaws', 'out');
|
|
410
426
|
const trusted = trustedPackages(cwd);
|
|
411
427
|
const guard = {
|
|
@@ -435,7 +451,12 @@ export function hostConfig(cwd, entries, outDir, laws) {
|
|
|
435
451
|
}
|
|
436
452
|
else if (tsFamily.test(file)) {
|
|
437
453
|
// A file the build compiled loads from its output; only one it left alone loads as source.
|
|
438
|
-
|
|
454
|
+
// The seal records the real path. The build wrote the output under the path it was given. An
|
|
455
|
+
// 8.3 name and that real path are not the same string, so the compiled file is looked up by the
|
|
456
|
+
// name the build used.
|
|
457
|
+
const shown = canonicalCwd(cwd);
|
|
458
|
+
const source = ontoSpelling(file, shown, real(shown));
|
|
459
|
+
const compiled = resolve(outDir, relative(shown, source)).replace(/\.[cm]?tsx?$/, '.js');
|
|
439
460
|
if (!existsSync(compiled))
|
|
440
461
|
attempt(`ts:${at}`, () => transformSource(file, extension));
|
|
441
462
|
}
|
|
@@ -599,13 +620,17 @@ function startProcess(flags) {
|
|
|
599
620
|
return new LawProcess(flags);
|
|
600
621
|
}
|
|
601
622
|
function poolFor(cwd, extra) {
|
|
602
|
-
|
|
623
|
+
// Grants keep the path the build opens. `real` alone drops an 8.3 name or a junction, and the permission
|
|
624
|
+
// check then refuses the compiled law.
|
|
625
|
+
const shown = canonicalCwd(cwd);
|
|
626
|
+
const base = real(shown);
|
|
627
|
+
const key = JSON.stringify([fold(base), extra.map((entry) => fold(real(entry)))]);
|
|
603
628
|
let entry = pools.get(key);
|
|
604
629
|
if (entry === undefined) {
|
|
605
630
|
// Compute permission flags once per pool instead of re-walking node_modules for every forked process.
|
|
606
|
-
const flags = sandboxFlags(
|
|
631
|
+
const flags = sandboxFlags(shown, extra);
|
|
607
632
|
entry = {
|
|
608
|
-
cwd:
|
|
633
|
+
cwd: base,
|
|
609
634
|
pool: new WorkerPool(() => startProcess(flags), jobs, idleMs),
|
|
610
635
|
};
|
|
611
636
|
pools.set(key, entry);
|
|
@@ -614,12 +639,14 @@ function poolFor(cwd, extra) {
|
|
|
614
639
|
}
|
|
615
640
|
/** Ends the idle law processes of a project and of any project below it, for a scratch copy about to be deleted. */
|
|
616
641
|
export function releaseSandbox(cwd) {
|
|
617
|
-
const top =
|
|
618
|
-
for (const [key, { cwd: at, pool }] of pools)
|
|
619
|
-
|
|
642
|
+
const top = fold(real(cwd));
|
|
643
|
+
for (const [key, { cwd: at, pool }] of pools) {
|
|
644
|
+
const atFold = fold(at);
|
|
645
|
+
if (atFold === top || atFold.startsWith(top + sep)) {
|
|
620
646
|
pool.close();
|
|
621
647
|
pools.delete(key);
|
|
622
648
|
}
|
|
649
|
+
}
|
|
623
650
|
}
|
|
624
651
|
/** Starts `n` law processes ahead of the first law, so they load while the code compiles. */
|
|
625
652
|
export function warmSandbox(cwd, n) {
|
package/dist/seal.js
CHANGED
|
@@ -22,6 +22,7 @@ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
|
22
22
|
import { builtinModules } from 'node:module';
|
|
23
23
|
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
24
24
|
import ts from 'typescript';
|
|
25
|
+
import { plainPath } from "./canonicalPath.js";
|
|
25
26
|
import { readConfig } from "./coverage.js";
|
|
26
27
|
import { moduleSpecifiers } from "./lawKinds.js";
|
|
27
28
|
import { identifiersIn, parseLawFile } from "./promote.js";
|
|
@@ -100,10 +101,10 @@ const underNodeModules = /(?:^|[\\/])node_modules[\\/]/;
|
|
|
100
101
|
const slash = (path) => path.replaceAll('\\', '/');
|
|
101
102
|
const realOf = (path) => {
|
|
102
103
|
try {
|
|
103
|
-
return realpathSync(path);
|
|
104
|
+
return plainPath(realpathSync.native(path));
|
|
104
105
|
}
|
|
105
106
|
catch {
|
|
106
|
-
return path;
|
|
107
|
+
return plainPath(path);
|
|
107
108
|
}
|
|
108
109
|
};
|
|
109
110
|
/** The `name` and `version` of the closest package.json above `file` that has both, stopping at a node_modules directory. */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { parentPort } from 'node:worker_threads';
|
|
2
|
+
import { projectState } from "./githubCli.js";
|
|
3
|
+
// Reading a project's state compiles it, and the compiler holds the thread while it does. A root run reads every
|
|
4
|
+
// project, so each is read here, on a thread of its own, and the projects compile side by side.
|
|
5
|
+
parentPort.on('message', (options) => {
|
|
6
|
+
try {
|
|
7
|
+
parentPort.postMessage({ ok: true, state: projectState(options) });
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
parentPort.postMessage({ ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
11
|
+
}
|
|
12
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tslaws",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Runs and checks `law` declarations compiled by tsc-laws: property tests, SMT proofs, tamper lock.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"tslaws": "bin/tslaws.js"
|
|
@@ -89,11 +89,11 @@
|
|
|
89
89
|
}
|
|
90
90
|
},
|
|
91
91
|
"optionalDependencies": {
|
|
92
|
-
"tslaws-compiler-darwin-arm64": "1.3.
|
|
93
|
-
"tslaws-compiler-darwin-x64": "1.3.
|
|
94
|
-
"tslaws-compiler-linux-arm64-gnu": "1.3.
|
|
95
|
-
"tslaws-compiler-linux-x64-gnu": "1.3.
|
|
96
|
-
"tslaws-compiler-win32-x64-msvc": "1.3.
|
|
92
|
+
"tslaws-compiler-darwin-arm64": "1.3.1",
|
|
93
|
+
"tslaws-compiler-darwin-x64": "1.3.1",
|
|
94
|
+
"tslaws-compiler-linux-arm64-gnu": "1.3.1",
|
|
95
|
+
"tslaws-compiler-linux-x64-gnu": "1.3.1",
|
|
96
|
+
"tslaws-compiler-win32-x64-msvc": "1.3.1"
|
|
97
97
|
},
|
|
98
98
|
"publishConfig": {
|
|
99
99
|
"access": "public"
|