bend-lint 0.2.2 → 0.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/README.md +14 -10
- package/package.json +2 -2
- package/src/linter/cli.bend +261 -185
- package/src/linter/{directives.bend → directives/impl.bend} +193 -153
- package/src/linter/directives/spec.bend +82 -0
- package/src/linter/seam.ts +538 -93
- package/src/{files → linter/shared/files}/effs/files.c +7 -0
- package/src/{files/main.bend → linter/shared/files/impl.bend} +24 -32
- package/src/linter/shared/files/spec.bend +39 -0
- package/src/linter/{fixes.bend → shared/fixes/impl.bend} +41 -45
- package/src/linter/shared/fixes/spec.bend +28 -0
- package/src/{json/main.bend → linter/shared/json/impl.bend} +39 -46
- package/src/linter/shared/json/spec.bend +24 -0
- package/src/{text/main.bend → linter/shared/text/impl.bend} +82 -73
- package/src/linter/shared/text/spec.bend +82 -0
- package/src/shared/bend2.ts +0 -171
- package/src/shared/result.ts +0 -57
- package/src/shared/utils.ts +0 -107
- /package/src/{files → linter/shared/files}/effs/files.js +0 -0
package/src/linter/seam.ts
CHANGED
|
@@ -4,20 +4,20 @@
|
|
|
4
4
|
// about Bend files over local TCP until the program exits (Seam in cli.bend sends them, and reads
|
|
5
5
|
// the answers). A check's answer is the linter's program, as Bend's parser and checker give it:
|
|
6
6
|
// each file's syntax tree, and, for facts, what the checker found on its nodes (Lint.Source, in
|
|
7
|
-
//
|
|
7
|
+
// the rule API's spec). When Bend's checker is Bend, glue in Bend makes the same program, and the code here
|
|
8
8
|
// that knows bend.ts goes, so mutation and any optimization are fair here. Node 24.2 or later runs
|
|
9
9
|
// a built program too, but a large program needs --max-old-space-size=8192.
|
|
10
10
|
//
|
|
11
11
|
// It loads a copy of bend.ts in which each TRACED function reports its calls: the parser's give
|
|
12
12
|
// each file's syntax before lowering; the checker's judgments, on each def's elaborated body, give
|
|
13
|
-
// the facts. What a rule sees is
|
|
13
|
+
// the facts. What a rule sees is the rule API's contract, not Bend's: when the checker is Bend, its
|
|
14
14
|
// glue makes each part from what that checker gives, or bend-lint computes it.
|
|
15
15
|
//
|
|
16
16
|
// | What a rule sees | Now, from | Risk |
|
|
17
17
|
// | ------------------------------ | --------------------------------- | ------ |
|
|
18
18
|
// | spans, names, binders, targets | the parser | low |
|
|
19
19
|
// | types: show, same, normal | term_show, term_compare, term_snf | low |
|
|
20
|
-
// | declarations and their uses |
|
|
20
|
+
// | declarations and their uses | the rule API, our own code | none |
|
|
21
21
|
// | syntax before lowering | the parser's calls (KEPT) | medium |
|
|
22
22
|
// | quantity, used | the judgments (JUDGED) | high |
|
|
23
23
|
// | which node a fact is on | span values (`isNodeOf`) | medium |
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
// Imports
|
|
32
32
|
// =======
|
|
33
33
|
|
|
34
|
-
import { spawn } from "node:child_process";
|
|
34
|
+
import { execFile, spawn, spawnSync } from "node:child_process";
|
|
35
|
+
import { createHash } from "node:crypto";
|
|
35
36
|
import { once } from "node:events";
|
|
36
37
|
import * as nodeFs from "node:fs";
|
|
37
38
|
import * as net from "node:net";
|
|
@@ -44,31 +45,28 @@ import type { Ann, Body, Book, Ctx, HTerm, LTerm, Patt, Quant, Span, TLD } from
|
|
|
44
45
|
import type * as BendModule from "bend2/bend.ts";
|
|
45
46
|
|
|
46
47
|
import pkg from "../../package.json" with { type: "json" };
|
|
47
|
-
import {
|
|
48
|
-
getBendCli,
|
|
49
|
-
getBendLintCacheDir,
|
|
50
|
-
getBendVersionNote,
|
|
51
|
-
getClang,
|
|
52
|
-
JAVASCRIPT_NOTE,
|
|
53
|
-
PINNED_BEND_NOTE,
|
|
54
|
-
tryGetBend2SourceUrl,
|
|
55
|
-
} from "../shared/bend2.ts";
|
|
56
|
-
import { unwrap } from "../shared/result.ts";
|
|
57
|
-
import {
|
|
58
|
-
getCommandOutcome,
|
|
59
|
-
getErrorMessageFromUnknown,
|
|
60
|
-
getPathWithForwardSlashes,
|
|
61
|
-
getSha256Hex,
|
|
62
|
-
tryGet,
|
|
63
|
-
tryGetJson,
|
|
64
|
-
tryWriteJsonAtomically,
|
|
65
|
-
writeFileAtomically,
|
|
66
|
-
writeNewFileAtomically,
|
|
67
|
-
} from "../shared/utils.ts";
|
|
68
48
|
|
|
69
49
|
// Types
|
|
70
50
|
// =====
|
|
71
51
|
|
|
52
|
+
// A value under OK, or an error under ERROR, for callers that would rather
|
|
53
|
+
// branch than catch. From iara's src/utils/result.ts, trimmed to what
|
|
54
|
+
// bend-lint uses.
|
|
55
|
+
|
|
56
|
+
type ErrorShape<
|
|
57
|
+
T extends string = string,
|
|
58
|
+
U extends Record<string, unknown> = Record<string, unknown>,
|
|
59
|
+
> = string | Error | { type: T; [ERROR_METADATA]?: U };
|
|
60
|
+
|
|
61
|
+
type ResultOk<T> = Readonly<{ readonly [OK]: T }>;
|
|
62
|
+
|
|
63
|
+
type ResultErr<E extends ErrorShape = ErrorShape> = Readonly<{ readonly [ERROR]: E }>;
|
|
64
|
+
|
|
65
|
+
type Result<T, E extends ErrorShape = ErrorShape> = ResultOk<T> | ResultErr<E>;
|
|
66
|
+
|
|
67
|
+
// How a command ended: its exit code, and what it printed.
|
|
68
|
+
type Outcome = { code: number; out: string; err: string };
|
|
69
|
+
|
|
72
70
|
type Bend = typeof BendModule;
|
|
73
71
|
|
|
74
72
|
type Loaded = { Bend: Bend; base: Book };
|
|
@@ -171,7 +169,8 @@ type Note = { role: "t" | "b"; type: Typed; quantity: Quant };
|
|
|
171
169
|
|
|
172
170
|
// A file the check parsed: its path, its text on disk and as its parser read it (with its import
|
|
173
171
|
// lines blank), its namespace and its import aliases (in order), the parser that read its
|
|
174
|
-
// declarations, the loads of its imports,
|
|
172
|
+
// declarations, the loads of its imports, the calls that parsed its declarations, and where its
|
|
173
|
+
// declarations start in the book's order.
|
|
175
174
|
type Parsed = {
|
|
176
175
|
path: string;
|
|
177
176
|
text: string;
|
|
@@ -181,8 +180,13 @@ type Parsed = {
|
|
|
181
180
|
parser: unknown;
|
|
182
181
|
loads: Frame[];
|
|
183
182
|
frames: Frame[];
|
|
183
|
+
start: number;
|
|
184
184
|
};
|
|
185
185
|
|
|
186
|
+
// A file a check parsed, with its key (the checker's version, the file's path and text, and its
|
|
187
|
+
// imports' keys) and its closure (itself, the files it imports, and Base, as "").
|
|
188
|
+
type Keyed = { file: Parsed; key: string; closure: Set<string> };
|
|
189
|
+
|
|
186
190
|
// Where the spans a file's parser made are on disk: the parser, the text on disk, and, for an
|
|
187
191
|
// offset the parser read, where it is on disk in UTF-16 units; and, for one of those, in code
|
|
188
192
|
// points.
|
|
@@ -207,6 +211,54 @@ type Rules = { ids: string[]; packages: Map<string, string> };
|
|
|
207
211
|
// Constants
|
|
208
212
|
// =========
|
|
209
213
|
|
|
214
|
+
const OK = "OK" as const;
|
|
215
|
+
|
|
216
|
+
const ERROR = "ERROR" as const;
|
|
217
|
+
|
|
218
|
+
const ERROR_METADATA = "ERROR_METADATA" as const;
|
|
219
|
+
|
|
220
|
+
// The Bend bend-lint checks and builds with, bend2: a pinned commit on GitHub,
|
|
221
|
+
// downloaded once, whatever bend is installed. Bend's releases do not run on
|
|
222
|
+
// Windows, so there it is a commit of a fork that does, and bend-lint says so
|
|
223
|
+
// on each run.
|
|
224
|
+
|
|
225
|
+
// The Bend bend-lint uses, as `<owner>/<repo>/<ref>` on GitHub: Bend's newest
|
|
226
|
+
// release this repo's checks passed with (src/meta/sync-bend.ts moves it), or,
|
|
227
|
+
// on Windows, a commit of a fork that runs there.
|
|
228
|
+
const PINNED_BEND: string = process.platform === "win32" ? pkg.bendWindows : pkg.bendRelease;
|
|
229
|
+
|
|
230
|
+
// What bend-lint says on each run on Windows, where it uses the fork.
|
|
231
|
+
const PINNED_BEND_NOTE: string | undefined =
|
|
232
|
+
process.platform === "win32"
|
|
233
|
+
? "bend-lint: on Windows, bend-lint uses a fork of Bend, as Bend's releases do not run there: https://github.com/" +
|
|
234
|
+
pkg.bendWindows.replace(/^([^/]+\/[^/]+)\//, "$1/tree/")
|
|
235
|
+
: undefined;
|
|
236
|
+
|
|
237
|
+
// bend-lint's cache: the downloaded Bend, and what the server keeps.
|
|
238
|
+
const getBendLintCacheDir = (): URL =>
|
|
239
|
+
url.pathToFileURL(
|
|
240
|
+
nodePath.join(
|
|
241
|
+
process.env.XDG_CACHE_HOME ??
|
|
242
|
+
(process.platform === "win32"
|
|
243
|
+
? (process.env.LOCALAPPDATA ?? nodePath.join(os.homedir(), "AppData", "Local"))
|
|
244
|
+
: nodePath.join(os.homedir(), ".cache")),
|
|
245
|
+
"bend-lint",
|
|
246
|
+
) + nodePath.sep,
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// What bend-lint says on each run whose rules it built as JavaScript, as it
|
|
250
|
+
// found no clang (see getClang).
|
|
251
|
+
const JAVASCRIPT_NOTE =
|
|
252
|
+
"bend-lint: found no clang 14 or newer, so the rules run as JavaScript, about 3 to 8 times slower; install clang to speed them up";
|
|
253
|
+
|
|
254
|
+
// What bend-lint says on a run whose rules it built as JavaScript while clang builds them in the
|
|
255
|
+
// background (see getBuiltProgram).
|
|
256
|
+
const BUILDING_NOTE =
|
|
257
|
+
"bend-lint: the rules run as JavaScript this time, about 3 to 8 times slower, while clang builds them in the background";
|
|
258
|
+
|
|
259
|
+
// How the line starts that says the installed bend is another version.
|
|
260
|
+
const VERSION_NOTE = "bend-lint: your bend is ";
|
|
261
|
+
|
|
210
262
|
const ESCAPES: Record<string, string> = { "\\": "\\\\", "\t": "\\t", "\n": "\\n" };
|
|
211
263
|
|
|
212
264
|
const SERVER_CACHE_DIR = new URL("server/", getBendLintCacheDir());
|
|
@@ -217,7 +269,7 @@ const PROGRAMS_DIR = url.fileURLToPath(new URL("programs/", SERVER_CACHE_DIR));
|
|
|
217
269
|
|
|
218
270
|
// Where the programs bend-lint writes (one per list of rules) live: Bend imports only relative
|
|
219
271
|
// paths of plain names, so they live beside cli.bend, which they import.
|
|
220
|
-
|
|
272
|
+
const BUILD = url.fileURLToPath(new URL("build/", import.meta.url));
|
|
221
273
|
|
|
222
274
|
// The config, and the folder of rules beside it.
|
|
223
275
|
const CONFIG = "bend-lint.json";
|
|
@@ -226,7 +278,7 @@ const RULES = "bend-lint-rules";
|
|
|
226
278
|
// bend-lint's version, as its BendHub packages have it: with `.0` added.
|
|
227
279
|
const HUB_VERSION = `${pkg.version}.0`;
|
|
228
280
|
|
|
229
|
-
// The rule API, as rules import it from BendHub: src/linter/
|
|
281
|
+
// The rule API, as rules import it from BendHub: src/linter/api/ and the files it imports, at
|
|
230
282
|
// their paths from src/, in the package bend-lint-api, at bend-lint's version (see ship.ts).
|
|
231
283
|
export const API = `bend-lint-api@${HUB_VERSION}`;
|
|
232
284
|
|
|
@@ -237,9 +289,10 @@ export const DEFAULTS = `${DEFAULTS_NAME}@${HUB_VERSION}`;
|
|
|
237
289
|
|
|
238
290
|
// The ids of bend-lint's own rules in DEFAULTS, as the npm package lists them (see ship.ts); none
|
|
239
291
|
// in this repo, which runs them from bend-lint-rules/.
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
292
|
+
const DEFAULT_IDS_FILE = url.fileURLToPath(new URL("defaults.json", import.meta.url));
|
|
293
|
+
const DEFAULT_IDS = nodeFs.existsSync(DEFAULT_IDS_FILE)
|
|
294
|
+
? (JSON.parse(nodeFs.readFileSync(DEFAULT_IDS_FILE, "utf8")) as string[])
|
|
295
|
+
: undefined;
|
|
243
296
|
|
|
244
297
|
// A path Bend imports: up, then names (letters, digits, _ and -).
|
|
245
298
|
const PLAIN = /^(\.\.\/)*([A-Za-z_][\w-]*\/)*[A-Za-z_][\w-]*\.bend$/;
|
|
@@ -263,8 +316,10 @@ const INIT_CONFIG =
|
|
|
263
316
|
2,
|
|
264
317
|
) + "\n";
|
|
265
318
|
|
|
266
|
-
// An import
|
|
267
|
-
|
|
319
|
+
// An import of a local file, by any path Bend takes (relative, bare or absolute): of a .bend file,
|
|
320
|
+
// or, quoted, of an effect's; but not Base, nor a file from BendHub (`name@version/...`,
|
|
321
|
+
// `0x<hash>/...`).
|
|
322
|
+
export const IMPORT = /^\s*import\s+"?(?!Base(?![\w./]))(?![^\s"/]*@)(?!0x[0-9a-f]+\/)([^\s"]+)/gm;
|
|
268
323
|
|
|
269
324
|
// A drive letter that starts a path with `/`.
|
|
270
325
|
const DRIVE = /^[A-Za-z]:(?=\/)/;
|
|
@@ -430,6 +485,10 @@ const FILES = new Map<unknown, number>();
|
|
|
430
485
|
const ANSWERED: Answered[] = [];
|
|
431
486
|
const NUMBERS = new Map<string, number>();
|
|
432
487
|
|
|
488
|
+
// The records of a file that no fact is on (see `getCheck`), by its key and namespace: each check
|
|
489
|
+
// that parses the file so gives the same.
|
|
490
|
+
const RECORDS = new Map<string, string[]>();
|
|
491
|
+
|
|
433
492
|
const STALE = 7 * 24 * 60 * 60 * 1000;
|
|
434
493
|
|
|
435
494
|
// Touching an executable makes a virus scanner check it again before it runs (about 0.2 s on
|
|
@@ -439,9 +498,227 @@ const USED = 24 * 60 * 60 * 1000;
|
|
|
439
498
|
// A prune stats each entry of its folder (about 0.1 s for 9000 entries), so not on every run.
|
|
440
499
|
const PRUNE_EVERY = 24 * 60 * 60 * 1000;
|
|
441
500
|
|
|
501
|
+
// How long a native build that a run started in the background may take before another run starts
|
|
502
|
+
// it again (as when the first one stopped).
|
|
503
|
+
const BUILDING = 10 * 60 * 1000;
|
|
504
|
+
|
|
442
505
|
// Functions
|
|
443
506
|
// =========
|
|
444
507
|
|
|
508
|
+
const ok = <T>(value: T): ResultOk<T> => ({ [OK]: value });
|
|
509
|
+
|
|
510
|
+
export const error = <E extends ErrorShape>(value: E): ResultErr<E> => ({ [ERROR]: value });
|
|
511
|
+
|
|
512
|
+
// The value, or a throw: an Error as it is, a string as an Error's message,
|
|
513
|
+
// and a typed error as an Error with its metadata's message (else its
|
|
514
|
+
// type), caused by the typed error.
|
|
515
|
+
export const unwrap = <T, E extends ErrorShape>(res: Result<T, E>): T => {
|
|
516
|
+
if (OK in res) {
|
|
517
|
+
return res[OK];
|
|
518
|
+
}
|
|
519
|
+
const e = res[ERROR];
|
|
520
|
+
if (e instanceof Error) {
|
|
521
|
+
throw e;
|
|
522
|
+
}
|
|
523
|
+
if (typeof e === "string") {
|
|
524
|
+
throw new Error(e);
|
|
525
|
+
}
|
|
526
|
+
const said = e[ERROR_METADATA]?.message;
|
|
527
|
+
throw new Error(typeof said === "string" ? said : e.type, { cause: e });
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// How `cmd` ends, run in `cwd`, with `env` added to this process's environment.
|
|
531
|
+
const getCommandOutcome = async (
|
|
532
|
+
cmd: string[],
|
|
533
|
+
cwd: string,
|
|
534
|
+
env: Record<string, string> = {},
|
|
535
|
+
input?: string,
|
|
536
|
+
): Promise<Outcome> => {
|
|
537
|
+
const child = Bun.spawn(cmd, {
|
|
538
|
+
cwd,
|
|
539
|
+
env: { ...process.env, ...env },
|
|
540
|
+
stdin: input === undefined ? "ignore" : new TextEncoder().encode(input),
|
|
541
|
+
stdout: "pipe",
|
|
542
|
+
stderr: "pipe",
|
|
543
|
+
});
|
|
544
|
+
const [code, out, err] = await Promise.all([
|
|
545
|
+
child.exited,
|
|
546
|
+
new Response(child.stdout).text(),
|
|
547
|
+
new Response(child.stderr).text(),
|
|
548
|
+
]);
|
|
549
|
+
return { code, out, err };
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
const getErrorMessageFromUnknown = (e: unknown): string =>
|
|
553
|
+
e instanceof Error ? e.message : String(e);
|
|
554
|
+
|
|
555
|
+
export const getPathWithForwardSlashes = (p: string): string => p.split(nodePath.sep).join("/");
|
|
556
|
+
|
|
557
|
+
// f's value, or undefined if it throws.
|
|
558
|
+
const tryGet = <T>(f: () => T): T | undefined => {
|
|
559
|
+
try {
|
|
560
|
+
return f();
|
|
561
|
+
} catch {
|
|
562
|
+
return undefined;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
export const getSha256Hex = (text: string): string =>
|
|
567
|
+
createHash("sha256").update(text).digest("hex");
|
|
568
|
+
|
|
569
|
+
// Writes `text` to `file` whole or not at all: to a file beside it, in the
|
|
570
|
+
// file's folder, made if need be, then renamed over it.
|
|
571
|
+
export const writeFileAtomically = (file: string, text: string): void => {
|
|
572
|
+
nodeFs.mkdirSync(nodePath.dirname(file), { recursive: true });
|
|
573
|
+
const temp = `${file}.${process.pid}.${Date.now()}`;
|
|
574
|
+
nodeFs.writeFileSync(temp, text);
|
|
575
|
+
try {
|
|
576
|
+
nodeFs.renameSync(temp, file);
|
|
577
|
+
} catch (e) {
|
|
578
|
+
nodeFs.rmSync(temp, { force: true });
|
|
579
|
+
throw e;
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
// Writes `text` to `file` whole, unless `file` is there: a file that runs at once may each write,
|
|
584
|
+
// with the same text. On Windows, a rename over a file that another process has open fails; a run
|
|
585
|
+
// that loses that race keeps the file it found.
|
|
586
|
+
const writeNewFileAtomically = (file: string, text: string): void => {
|
|
587
|
+
try {
|
|
588
|
+
if (!nodeFs.existsSync(file)) {
|
|
589
|
+
writeFileAtomically(file, text);
|
|
590
|
+
}
|
|
591
|
+
} catch (e) {
|
|
592
|
+
if (!nodeFs.existsSync(file)) {
|
|
593
|
+
throw e;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
// The JSON value in `file`, or undefined if it cannot be read.
|
|
599
|
+
const tryGetJson = (file: string): unknown =>
|
|
600
|
+
tryGet(() => JSON.parse(nodeFs.readFileSync(file, "utf8")) as unknown);
|
|
601
|
+
|
|
602
|
+
// Writes `value` to `file` as JSON, whole or not at all; a write that fails is skipped.
|
|
603
|
+
const tryWriteJsonAtomically = (file: string, value: unknown): void => {
|
|
604
|
+
tryGet(() => writeFileAtomically(file, JSON.stringify(value)));
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// The folder at its real path, as a file: URL that ends in `/`, so
|
|
608
|
+
// `new URL("file", folder)` finds a file in it. An error if `dir` cannot be
|
|
609
|
+
// resolved.
|
|
610
|
+
const tryGetUrlFromFolderPathOrUrl = (dir: string | URL): Result<URL, string> => {
|
|
611
|
+
try {
|
|
612
|
+
return ok(url.pathToFileURL(nodeFs.realpathSync(dir) + nodePath.sep));
|
|
613
|
+
} catch (e) {
|
|
614
|
+
return error(getErrorMessageFromUnknown(e));
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
// The clang Bend builds native programs with, if there is one, as Bend's
|
|
619
|
+
// cc_find picks it (main.ts): the first of $CC, clang, each clang-NN on PATH
|
|
620
|
+
// (the newest first), and on Windows where LLVM's installer puts it, that is
|
|
621
|
+
// clang 14 or newer.
|
|
622
|
+
export const getClang = (): string | undefined => {
|
|
623
|
+
const getFileNames = (dir: string): string[] => tryGet(() => nodeFs.readdirSync(dir)) ?? [];
|
|
624
|
+
const numbered = [
|
|
625
|
+
...new Set(
|
|
626
|
+
(process.env.PATH ?? "")
|
|
627
|
+
.split(nodePath.delimiter)
|
|
628
|
+
.flatMap(getFileNames)
|
|
629
|
+
.filter((f) => /^clang-\d+$/.test(f)),
|
|
630
|
+
),
|
|
631
|
+
].sort((a, b) => Number(b.slice(6)) - Number(a.slice(6)));
|
|
632
|
+
return [
|
|
633
|
+
...(process.env.CC === undefined ? [] : [process.env.CC]),
|
|
634
|
+
"clang",
|
|
635
|
+
...numbered,
|
|
636
|
+
...(process.platform === "win32"
|
|
637
|
+
? [nodePath.join(process.env.ProgramFiles ?? "C:\\Program Files", "LLVM", "bin", "clang.exe")]
|
|
638
|
+
: []),
|
|
639
|
+
].find((cc) => {
|
|
640
|
+
const version = /^(?:Apple )?(?:\w+ )?clang version (\d+)/m.exec(
|
|
641
|
+
spawnSync(cc, ["--version"], { encoding: "utf8" }).stdout ?? "",
|
|
642
|
+
)?.[1];
|
|
643
|
+
return version !== undefined && Number(version) >= 14;
|
|
644
|
+
});
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
// The line bend-lint prints when the `bend` on PATH is another version than
|
|
648
|
+
// the Bend in the bend2 folder `dir`, which bend-lint checks and builds with;
|
|
649
|
+
// undefined if it is the same, or if none is installed or `bend version` names
|
|
650
|
+
// no version. It asks `bend version` on each run, as the `bend` on PATH can be
|
|
651
|
+
// a script that runs a checkout, whose version changes while the script does
|
|
652
|
+
// not. It runs by name: Windows runs a bend.cmd only that way.
|
|
653
|
+
const getBendVersionNote = async (dir: URL): Promise<string | undefined> => {
|
|
654
|
+
const version = await new Promise<string | undefined>((resolve) => {
|
|
655
|
+
const resolveVersion = (e: Error | null, stdout: string): void =>
|
|
656
|
+
resolve(/^bend (\d+\.\d+\.\d+)(?:\r?\n|$)/.exec(e === null ? stdout : "")?.[1]);
|
|
657
|
+
try {
|
|
658
|
+
execFile("bend", ["version"], { encoding: "utf8", timeout: 10_000 }, resolveVersion);
|
|
659
|
+
} catch (e) {
|
|
660
|
+
resolveVersion(e as Error, "");
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
const pinned = /^const VERSION = "([^"]+)";$/m.exec(
|
|
664
|
+
nodeFs.readFileSync(new URL("main.ts", dir), "utf8"),
|
|
665
|
+
)?.[1];
|
|
666
|
+
return version === undefined || pinned === undefined || version === pinned
|
|
667
|
+
? undefined
|
|
668
|
+
: `${VERSION_NOTE}${version}, and bend-lint checks with Bend ${pinned}; the two can disagree on a few files`;
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
// How to run the Bend in the bend2 folder `dir`: its CLI (main.ts) on this Bun,
|
|
672
|
+
// in an environment that keeps it from asking bend-lang.com, once a day, for a
|
|
673
|
+
// newer release (see main.ts).
|
|
674
|
+
export const getBendCli = (dir: URL): { cmd: string[]; env: Record<string, string> } => ({
|
|
675
|
+
cmd: [process.execPath, url.fileURLToPath(new URL("main.ts", dir))],
|
|
676
|
+
env: { BEND_NO_TELEMETRY: "1" },
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
// bend2 at `pin` (`<owner>/<repo>/<ref>` on GitHub), kept in <cache>/<pin>/bend2.
|
|
680
|
+
// Each file goes in whole, base.bend last, and none is ever replaced (see
|
|
681
|
+
// writeNewFileAtomically): runs at once may each download, while others read
|
|
682
|
+
// the files.
|
|
683
|
+
const getDownloadedBend = async (pin: string): Promise<URL> => {
|
|
684
|
+
if (!/^[\w.-]+\/[\w.-]+\/[\w.-]+$/.test(pin)) {
|
|
685
|
+
throw new Error("not a GitHub <owner>/<repo>/<ref>: " + pin);
|
|
686
|
+
}
|
|
687
|
+
const dir = new URL(pin + "/bend2/", getBendLintCacheDir());
|
|
688
|
+
// What a download holds besides base.bend and the effs/ files Base imports.
|
|
689
|
+
const FILES = ["bend.ts", "comp.ts", "main.ts", "safe.ts"];
|
|
690
|
+
const isMissing = (file: string): boolean => !nodeFs.existsSync(new URL(file, dir));
|
|
691
|
+
if (![...FILES, "base.bend"].some(isMissing)) {
|
|
692
|
+
return dir;
|
|
693
|
+
}
|
|
694
|
+
const getFetchedFile = async (file: string): Promise<[string, string]> => {
|
|
695
|
+
const res = await fetch(`https://raw.githubusercontent.com/${pin}/bend2/${file}`, {
|
|
696
|
+
signal: AbortSignal.timeout(30_000),
|
|
697
|
+
});
|
|
698
|
+
if (!res.ok) {
|
|
699
|
+
throw new Error(`GitHub answered ${res.status} for bend2/${file}`);
|
|
700
|
+
}
|
|
701
|
+
return [file, await res.text()];
|
|
702
|
+
};
|
|
703
|
+
const base = await getFetchedFile("base.bend");
|
|
704
|
+
const effs = new Set([...base[1].matchAll(/^\s*import "\.\/(effs\/[^"]+)"/gm)].map((m) => m[1]));
|
|
705
|
+
const odd = [...effs].find((f) => !/^effs\/[\w.-]+$/.test(f));
|
|
706
|
+
if (odd !== undefined) {
|
|
707
|
+
throw new Error("base.bend imports " + odd + ", which is not a plain file in effs/");
|
|
708
|
+
}
|
|
709
|
+
[...(await Promise.all([...effs, ...FILES].map(getFetchedFile))), base].forEach(([file, body]) =>
|
|
710
|
+
writeNewFileAtomically(url.fileURLToPath(new URL(file, dir)), body),
|
|
711
|
+
);
|
|
712
|
+
console.error("bend-lint: downloaded bend " + pin + " to " + url.fileURLToPath(dir));
|
|
713
|
+
return dir;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// The bend2 folder to load: the pinned Bend's, downloaded if it is not yet.
|
|
717
|
+
export const tryGetBend2SourceUrl = async (): Promise<Result<URL, string>> =>
|
|
718
|
+
getDownloadedBend(PINNED_BEND).then(tryGetUrlFromFolderPathOrUrl, (e: unknown) =>
|
|
719
|
+
error(`could not download bend ${PINNED_BEND} (${getErrorMessageFromUnknown(e)})`),
|
|
720
|
+
);
|
|
721
|
+
|
|
445
722
|
const getEscapedField = (text: string): string => text.replace(/[\\\t\n]/g, (c) => ESCAPES[c]);
|
|
446
723
|
|
|
447
724
|
const getUnescapedField = (text: string): string =>
|
|
@@ -728,6 +1005,7 @@ const getParsedFiles = (frames: Frame[], out: Parsed[]): Parsed[] => {
|
|
|
728
1005
|
parser: book.kids.find((f) => isFile(f.args[0]))?.args[0],
|
|
729
1006
|
loads,
|
|
730
1007
|
frames: book.kids,
|
|
1008
|
+
start: load.out as number,
|
|
731
1009
|
});
|
|
732
1010
|
}
|
|
733
1011
|
});
|
|
@@ -1057,7 +1335,7 @@ const getDeclarationTrees = (b: Builder, [head, ...frames]: Frame[]): Tree[] =>
|
|
|
1057
1335
|
: [getDeclarationAt(b, "Definition", name, getDeclarationParts(b, head.kids))];
|
|
1058
1336
|
}
|
|
1059
1337
|
const syntax: Syntax =
|
|
1060
|
-
head.name === "parse_fresh" && head.args[2] === b.book.ctrs
|
|
1338
|
+
head.name === "parse_fresh" && (head.args[2] === "ctrs" || head.args[2] === b.book.ctrs)
|
|
1061
1339
|
? "Constructor"
|
|
1062
1340
|
: b.book.tlds[head.out as string]?.$ === "ADT"
|
|
1063
1341
|
? "Datatype"
|
|
@@ -1242,42 +1520,51 @@ const isSameType = (Bend: Bend, held: Held, a: Typed, b: Typed): boolean => {
|
|
|
1242
1520
|
// entry in the scope of its body has, unless it is a parameter its signature declared (a law's fill
|
|
1243
1521
|
// takes the law's); it is used as often as its judgment closed it. Of a node's facts that the
|
|
1244
1522
|
// checker counts the same (same role, quantity and type), the first stays: pattern compilation
|
|
1245
|
-
// checks one term in several branches.
|
|
1523
|
+
// checks one term in several branches. A file's nodes come from `getTrees` (by its parser) once a
|
|
1524
|
+
// fact is read at a span of the file.
|
|
1246
1525
|
const putFacts = (
|
|
1247
1526
|
Bend: Bend,
|
|
1248
1527
|
held: Held,
|
|
1249
1528
|
own: string[],
|
|
1250
1529
|
judgments: Map<LTerm, Judgment>,
|
|
1251
1530
|
wanted: ReadonlySet<string | undefined> | undefined,
|
|
1252
|
-
|
|
1531
|
+
getTrees: (file: unknown) => Tree[],
|
|
1253
1532
|
): void => {
|
|
1254
1533
|
const { binders } = held;
|
|
1255
1534
|
const origins = new Map<string, Tree[]>();
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
if (o.binder !== undefined && BINDS.has(o.syntax)) {
|
|
1261
|
-
declarers.set(o.binder, o);
|
|
1262
|
-
}
|
|
1535
|
+
const indexed = new Set<unknown>();
|
|
1536
|
+
const getNodesAt = (s: Span | undefined): Tree[] => {
|
|
1537
|
+
if (s === undefined) {
|
|
1538
|
+
return [];
|
|
1263
1539
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
const
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1540
|
+
if (!indexed.has(s.file)) {
|
|
1541
|
+
indexed.add(s.file);
|
|
1542
|
+
for (const top of getTrees(s.file)) {
|
|
1543
|
+
const mine = getPreorder([top], []);
|
|
1544
|
+
const declarers = new Map<number, Tree>();
|
|
1545
|
+
for (const o of mine) {
|
|
1546
|
+
if (o.binder !== undefined && BINDS.has(o.syntax)) {
|
|
1547
|
+
declarers.set(o.binder, o);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
for (const o of mine) {
|
|
1551
|
+
if (o.syntax === "Variable" && o.binder !== undefined) {
|
|
1552
|
+
o.declarer = declarers.get(o.binder);
|
|
1553
|
+
}
|
|
1554
|
+
if (o.origin !== undefined) {
|
|
1555
|
+
const key = getSpanKey(o.origin);
|
|
1556
|
+
const known = origins.get(key);
|
|
1557
|
+
if (known === undefined) {
|
|
1558
|
+
origins.set(key, [o]);
|
|
1559
|
+
} else {
|
|
1560
|
+
known.push(o);
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1275
1563
|
}
|
|
1276
1564
|
}
|
|
1277
1565
|
}
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
s === undefined ? [] : (origins.get(getSpanKey(s)) ?? []);
|
|
1566
|
+
return origins.get(getSpanKey(s)) ?? [];
|
|
1567
|
+
};
|
|
1281
1568
|
const getBinderNode = (s: Span | undefined, k: string): Tree | undefined =>
|
|
1282
1569
|
getNodesAt(s).find((o) => BINDS.has(o.syntax) && o.name === k);
|
|
1283
1570
|
const getEntry = (scope: Scope | undefined, level: number): Ann | undefined =>
|
|
@@ -1509,6 +1796,70 @@ const getUntemplated = (x: unknown, bok: Book): unknown => {
|
|
|
1509
1796
|
);
|
|
1510
1797
|
};
|
|
1511
1798
|
|
|
1799
|
+
// The mark, in the cache, that a passed check checked the file of `key` as if alone (see
|
|
1800
|
+
// `getValidity`).
|
|
1801
|
+
const getValidPath = (key: string): string => nodePath.join(CHECKS_DIR, key + ".valid");
|
|
1802
|
+
|
|
1803
|
+
// The keys of `files`, in the order Bend parsed them (a file's imports first), for the checker's
|
|
1804
|
+
// `version`.
|
|
1805
|
+
const getKeyed = (version: string, files: Parsed[]): Keyed[] => {
|
|
1806
|
+
const known = new Map<string, Keyed>();
|
|
1807
|
+
return files.map((file) => {
|
|
1808
|
+
const loads = file.loads.flatMap((l) => known.get(l.args[1] as string) ?? []);
|
|
1809
|
+
const one = {
|
|
1810
|
+
file,
|
|
1811
|
+
key: getSha256Hex(
|
|
1812
|
+
JSON.stringify([version, file.path, file.text, ...loads.map((l) => l.key)]),
|
|
1813
|
+
),
|
|
1814
|
+
closure: new Set(["", file.path, ...loads.flatMap((l) => [...l.closure])]),
|
|
1815
|
+
};
|
|
1816
|
+
known.set(file.path, one);
|
|
1817
|
+
return one;
|
|
1818
|
+
});
|
|
1819
|
+
};
|
|
1820
|
+
|
|
1821
|
+
// What Bend's check of the files `keyed` need not check again, by the book's `order` of
|
|
1822
|
+
// declarations, Base's before `from`. A file is checked as if alone when its closure holds the file
|
|
1823
|
+
// of each fill of a law (a name declared again) before it whose law its closure holds: the checker
|
|
1824
|
+
// unfolds a law from its fill on. `own` gives the key of each such file; `start` the first
|
|
1825
|
+
// declaration of the first file that the check must check: one not checked as if alone, or with no
|
|
1826
|
+
// mark of its key, or whose facts are `wanted` (by its namespace). A use of a mark keeps it from
|
|
1827
|
+
// prune.
|
|
1828
|
+
const getValidity = (
|
|
1829
|
+
keyed: Keyed[],
|
|
1830
|
+
order: string[],
|
|
1831
|
+
from: number,
|
|
1832
|
+
wanted: (ns: string) => boolean,
|
|
1833
|
+
): { start: number; own: string[] } => {
|
|
1834
|
+
const getOwner = (i: number): string =>
|
|
1835
|
+
keyed.filter((k) => k.file.start <= i).at(-1)?.file.path ?? "";
|
|
1836
|
+
const first = new Map<string, number>();
|
|
1837
|
+
const fills = order.flatMap((k, i) => {
|
|
1838
|
+
const at = first.get(k);
|
|
1839
|
+
if (at === undefined) {
|
|
1840
|
+
first.set(k, i);
|
|
1841
|
+
}
|
|
1842
|
+
return at === undefined || i < from ? [] : [{ at: i, law: getOwner(at), filler: getOwner(i) }];
|
|
1843
|
+
});
|
|
1844
|
+
const isOwn = ({ file, closure }: Keyed): boolean =>
|
|
1845
|
+
fills.every(
|
|
1846
|
+
({ at, law, filler }) => at >= file.start || closure.has(filler) || !closure.has(law),
|
|
1847
|
+
);
|
|
1848
|
+
return {
|
|
1849
|
+
start:
|
|
1850
|
+
keyed.find(
|
|
1851
|
+
(one) =>
|
|
1852
|
+
!isOwn(one) ||
|
|
1853
|
+
wanted(one.file.ns) ||
|
|
1854
|
+
tryGet(() => {
|
|
1855
|
+
nodeFs.utimesSync(getValidPath(one.key), new Date(), new Date());
|
|
1856
|
+
return true;
|
|
1857
|
+
}) !== true,
|
|
1858
|
+
)?.file.start ?? order.length,
|
|
1859
|
+
own: keyed.filter(isOwn).map((one) => one.key),
|
|
1860
|
+
};
|
|
1861
|
+
};
|
|
1862
|
+
|
|
1512
1863
|
// The path Bend loads a file at, as `seen` keeps it: real, with `/`; a file with an overlay, at its
|
|
1513
1864
|
// key.
|
|
1514
1865
|
const getLoadPath = (path: string): string =>
|
|
@@ -1520,7 +1871,8 @@ const getLoadPath = (path: string): string =>
|
|
|
1520
1871
|
// and the check's number, here 0 (see `getNumberedAnswer`), or `failed` and each file it read;
|
|
1521
1872
|
// then each file it parsed, if it loaded them: its path, namespace and text, then its nodes (see
|
|
1522
1873
|
// `getTreeRecords`). A passed check gives the facts of the files at `facts.paths` (all, if none).
|
|
1523
|
-
// The hooks are global, so
|
|
1874
|
+
// Bend checks only the declarations that `getValidity` does not skip. The hooks are global, so
|
|
1875
|
+
// checks must not overlap.
|
|
1524
1876
|
const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<Made> => {
|
|
1525
1877
|
const start = performance.now();
|
|
1526
1878
|
const { Bend, base } = await checker.getLoaded();
|
|
@@ -1528,19 +1880,31 @@ const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<
|
|
|
1528
1880
|
const seen = new Map<string, string | null>([[Bend.BASE_BEND, ""]]);
|
|
1529
1881
|
const root: Frame = { name: "check", args: [], out: undefined, end: -1, kids: [] };
|
|
1530
1882
|
const judgments = new Map<LTerm, Judgment>();
|
|
1531
|
-
let
|
|
1883
|
+
let keyed: Keyed[] = [];
|
|
1884
|
+
let wanted: Set<string | undefined> | undefined;
|
|
1532
1885
|
let passed = false;
|
|
1533
1886
|
READS.clear();
|
|
1534
1887
|
FILES.clear();
|
|
1535
1888
|
traceParsing(root);
|
|
1536
1889
|
try {
|
|
1537
1890
|
await Bend.book_load(book, getLoadPath(path), "", seen);
|
|
1538
|
-
|
|
1891
|
+
keyed = getKeyed(checker.version, getParsedFiles(root.kids, []));
|
|
1892
|
+
wanted =
|
|
1893
|
+
facts === undefined || facts.paths.length === 0
|
|
1894
|
+
? undefined
|
|
1895
|
+
: new Set(facts.paths.map((p) => seen.get(getLoadPath(p)) ?? undefined));
|
|
1539
1896
|
if (facts !== undefined) {
|
|
1540
1897
|
traceJudgments(Bend, judgments);
|
|
1541
1898
|
}
|
|
1542
|
-
|
|
1899
|
+
const validity = getValidity(
|
|
1900
|
+
keyed,
|
|
1901
|
+
book.order,
|
|
1902
|
+
base.order.length,
|
|
1903
|
+
(ns) => facts !== undefined && (wanted === undefined || wanted.has(ns)),
|
|
1904
|
+
);
|
|
1905
|
+
Bend.book_valid(book, validity.start);
|
|
1543
1906
|
passed = true;
|
|
1907
|
+
validity.own.forEach((key) => tryGet(() => writeNewFileAtomically(getValidPath(key), "")));
|
|
1544
1908
|
} catch (e) {
|
|
1545
1909
|
// A check that fails gives no facts; `bend <file> --check-only` says why. What bend.ts throws
|
|
1546
1910
|
// for the file is an Err; anything else is a fault of the checker or of this glue, not of the
|
|
@@ -1551,27 +1915,38 @@ const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<
|
|
|
1551
1915
|
} finally {
|
|
1552
1916
|
untrace();
|
|
1553
1917
|
}
|
|
1554
|
-
const files = loaded ? getParsedFiles(root.kids, []) : [];
|
|
1555
|
-
const forest = files.map((file) => getFileTrees(file, book));
|
|
1556
1918
|
const held: Held = { bok: book, types: [], binders: new Map() };
|
|
1919
|
+
// The trees of the files that facts are on, by their parsers: facts build them as they need them.
|
|
1920
|
+
const forest = new Map<unknown, Tree[]>();
|
|
1557
1921
|
if (passed && facts !== undefined) {
|
|
1558
1922
|
putFacts(
|
|
1559
1923
|
Bend,
|
|
1560
1924
|
held,
|
|
1561
1925
|
[...new Set(book.order.slice(base.order.length))],
|
|
1562
1926
|
judgments,
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1927
|
+
wanted,
|
|
1928
|
+
(parser) => {
|
|
1929
|
+
const file = keyed.find((k) => k.file.parser === parser)?.file;
|
|
1930
|
+
const trees = forest.get(parser) ?? (file === undefined ? [] : getFileTrees(file, book));
|
|
1931
|
+
forest.set(parser, trees);
|
|
1932
|
+
return trees;
|
|
1933
|
+
},
|
|
1567
1934
|
);
|
|
1568
1935
|
}
|
|
1569
|
-
const
|
|
1570
|
-
|
|
1936
|
+
const getRecords = (file: Parsed, trees: Tree[]): string[] =>
|
|
1937
|
+
trees.reduce(
|
|
1571
1938
|
(out, t) => getTreeRecords(t, held, out),
|
|
1572
1939
|
[getRecord(["file", file.path, file.ns, file.text])],
|
|
1573
|
-
)
|
|
1574
|
-
)
|
|
1940
|
+
);
|
|
1941
|
+
const program = keyed.flatMap(({ file, key }) => {
|
|
1942
|
+
const trees = forest.get(file.parser);
|
|
1943
|
+
if (trees !== undefined) {
|
|
1944
|
+
return getRecords(file, trees);
|
|
1945
|
+
}
|
|
1946
|
+
const kept = RECORDS.get(key + " " + file.ns) ?? getRecords(file, getFileTrees(file, book));
|
|
1947
|
+
RECORDS.set(key + " " + file.ns, kept);
|
|
1948
|
+
return kept;
|
|
1949
|
+
});
|
|
1575
1950
|
const read = [...READS];
|
|
1576
1951
|
COUNTS.checkerMs += performance.now() - start;
|
|
1577
1952
|
return passed
|
|
@@ -1734,19 +2109,13 @@ export const getNativePath = (source: string, dir: URL): string => {
|
|
|
1734
2109
|
return nodePath.join(PROGRAMS_DIR, name + (process.platform === "win32" ? ".exe" : ""));
|
|
1735
2110
|
};
|
|
1736
2111
|
|
|
1737
|
-
// The
|
|
1738
|
-
//
|
|
1739
|
-
//
|
|
1740
|
-
//
|
|
1741
|
-
//
|
|
1742
|
-
|
|
1743
|
-
const file = nodePath.resolve(source);
|
|
2112
|
+
// The build at `exe` (native, or JavaScript if it ends in .js) of the rules program at `file`, with
|
|
2113
|
+
// the Bend in the bend2 folder `dir`. A build is kept, and used again until a file the program
|
|
2114
|
+
// imports, or the Bend that builds it, changes; its use keeps it from prune (see USED). It goes to
|
|
2115
|
+
// a folder of its own first, so a kept build is never partial, and runs that build one program at
|
|
2116
|
+
// once share one build.
|
|
2117
|
+
const getBuild = async (file: string, exe: string, dir: URL): Promise<string> => {
|
|
1744
2118
|
const from = nodePath.dirname(file);
|
|
1745
|
-
const native = getNativePath(source, dir);
|
|
1746
|
-
const exe =
|
|
1747
|
-
nodeFs.existsSync(native) || getClang() !== undefined
|
|
1748
|
-
? native
|
|
1749
|
-
: native.replace(/(\.exe)?$/, ".js");
|
|
1750
2119
|
const kept = nodeFs.statSync(exe, { throwIfNoEntry: false });
|
|
1751
2120
|
if (kept !== undefined) {
|
|
1752
2121
|
if (Date.now() - kept.mtimeMs > USED) {
|
|
@@ -1762,7 +2131,7 @@ export const getBuiltProgram = async (source: string, dir: URL): Promise<string>
|
|
|
1762
2131
|
const bend = getBendCli(dir);
|
|
1763
2132
|
const ran = await getCommandOutcome([...bend.cmd, file, "-o", out], from, bend.env);
|
|
1764
2133
|
if (ran.code !== 0) {
|
|
1765
|
-
throw new Error(`could not build ${
|
|
2134
|
+
throw new Error(`could not build ${file}:\n${ran.out}${ran.err}`);
|
|
1766
2135
|
}
|
|
1767
2136
|
try {
|
|
1768
2137
|
nodeFs.renameSync(out, exe);
|
|
@@ -1777,19 +2146,57 @@ export const getBuiltProgram = async (source: string, dir: URL): Promise<string>
|
|
|
1777
2146
|
return exe;
|
|
1778
2147
|
};
|
|
1779
2148
|
|
|
2149
|
+
// Starts the native build of the rules program at `file` (its kept path `native`) in a process of
|
|
2150
|
+
// its own, which outlives this run, unless one started it in the last BUILDING.
|
|
2151
|
+
const startNativeBuild = (file: string, native: string): void => {
|
|
2152
|
+
const marker = native + ".building";
|
|
2153
|
+
const started = nodeFs.statSync(marker, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
2154
|
+
if (Date.now() - started < BUILDING) {
|
|
2155
|
+
return;
|
|
2156
|
+
}
|
|
2157
|
+
nodeFs.mkdirSync(PROGRAMS_DIR, { recursive: true });
|
|
2158
|
+
tryGet(() => nodeFs.writeFileSync(marker, String(process.pid)));
|
|
2159
|
+
spawn(process.execPath, [url.fileURLToPath(import.meta.url), "build", file], {
|
|
2160
|
+
detached: true,
|
|
2161
|
+
stdio: "ignore",
|
|
2162
|
+
windowsHide: true,
|
|
2163
|
+
}).unref();
|
|
2164
|
+
};
|
|
2165
|
+
|
|
2166
|
+
// The program the rules program `source` builds to, with the Bend in the bend2 folder `dir`:
|
|
2167
|
+
// native, if clang can build it, else JavaScript, until clang can. A native build takes clang a
|
|
2168
|
+
// while, so, unless `waits`, a run that finds none kept runs the JavaScript build (made in about a
|
|
2169
|
+
// second) and leaves the native one to a process of its own, for the runs after it: both builds
|
|
2170
|
+
// are of one program, so they find the same.
|
|
2171
|
+
const getBuiltProgram = async (source: string, dir: URL, waits: boolean): Promise<string> => {
|
|
2172
|
+
const file = nodePath.resolve(source);
|
|
2173
|
+
const native = getNativePath(source, dir);
|
|
2174
|
+
const js = native.replace(/(\.exe)?$/, ".js");
|
|
2175
|
+
if (nodeFs.existsSync(native) || (getClang() !== undefined && waits)) {
|
|
2176
|
+
return getBuild(file, native, dir);
|
|
2177
|
+
}
|
|
2178
|
+
if (getClang() !== undefined) {
|
|
2179
|
+
startNativeBuild(file, native);
|
|
2180
|
+
}
|
|
2181
|
+
return getBuild(file, js, dir);
|
|
2182
|
+
};
|
|
2183
|
+
|
|
1780
2184
|
// The answer to `check <path> <facts|syntax> <paths...>`: from the cache, if each file the check
|
|
1781
2185
|
// read is unchanged, else from Bend, and a passed check goes to the cache (whole or not at all; a
|
|
1782
2186
|
// cache that cannot be written is skipped). A passed check is numbered as before in this server,
|
|
1783
|
-
// or anew.
|
|
2187
|
+
// or anew. A check that Bend made holds what it holds until `forget`, for the questions of its
|
|
2188
|
+
// rules; one from the cache holds nothing.
|
|
1784
2189
|
const getCheckAnswer = async (checker: Checker, path: string, facts: Wanted): Promise<string> => {
|
|
1785
2190
|
const file = getCachePath(checker, path, facts);
|
|
1786
2191
|
let entry = getCachedEntry(file);
|
|
2192
|
+
let held: Held | undefined;
|
|
1787
2193
|
COUNTS[entry === undefined ? "checked" : "cached"] += 1;
|
|
1788
2194
|
if (entry === undefined) {
|
|
1789
2195
|
const made = await getCheck(checker, path, facts);
|
|
1790
2196
|
if (made.held === undefined) {
|
|
1791
2197
|
return made.answer;
|
|
1792
2198
|
}
|
|
2199
|
+
held = made.held;
|
|
1793
2200
|
entry = {
|
|
1794
2201
|
files: made.read.map(([p, text]) => [p, getSha256Hex(text)]),
|
|
1795
2202
|
answer: made.answer,
|
|
@@ -1803,6 +2210,7 @@ const getCheckAnswer = async (checker: Checker, path: string, facts: Wanted): Pr
|
|
|
1803
2210
|
known !== undefined && ANSWERED[known - 1].digest === entry.digest
|
|
1804
2211
|
? known
|
|
1805
2212
|
: ANSWERED.push({ file, path, facts, digest: entry.digest, types: entry.types });
|
|
2213
|
+
ANSWERED[number - 1].held ??= held;
|
|
1806
2214
|
NUMBERS.set(file, number);
|
|
1807
2215
|
return getNumberedAnswer(entry.answer, number);
|
|
1808
2216
|
};
|
|
@@ -1918,14 +2326,18 @@ const serve = async (
|
|
|
1918
2326
|
|
|
1919
2327
|
// The checker, the rules program `named`, built first, and what to say first of the Bend bend-lint
|
|
1920
2328
|
// uses (see bend2.ts). The checker comes first, so a drift stops the run before a build.
|
|
1921
|
-
const getRunSetup = async (named: string): Promise<[Checker, string, string[]]> => {
|
|
2329
|
+
const getRunSetup = async (named: string, waits: boolean): Promise<[Checker, string, string[]]> => {
|
|
1922
2330
|
const dir = unwrap(await tryGetBend2SourceUrl());
|
|
1923
2331
|
const checker = getChecker(dir);
|
|
1924
2332
|
const [program, version] = await Promise.all([
|
|
1925
|
-
getBuiltProgram(named, dir),
|
|
2333
|
+
getBuiltProgram(named, dir, waits),
|
|
1926
2334
|
getBendVersionNote(dir),
|
|
1927
2335
|
]);
|
|
1928
|
-
const javascript = program.endsWith(".js")
|
|
2336
|
+
const javascript = !program.endsWith(".js")
|
|
2337
|
+
? undefined
|
|
2338
|
+
: getClang() === undefined
|
|
2339
|
+
? JAVASCRIPT_NOTE
|
|
2340
|
+
: BUILDING_NOTE;
|
|
1929
2341
|
return [checker, program, [PINNED_BEND_NOTE, version, javascript].filter((n) => n !== undefined)];
|
|
1930
2342
|
};
|
|
1931
2343
|
|
|
@@ -1939,6 +2351,28 @@ export const getProject = (dir: string): string | undefined => {
|
|
|
1939
2351
|
: getProject(up);
|
|
1940
2352
|
};
|
|
1941
2353
|
|
|
2354
|
+
// The folder a run looks up its config from: that of the first file, glob or folder it names (a
|
|
2355
|
+
// folder is its own), past the value of `--jobs`; else the working folder.
|
|
2356
|
+
const getStart = (args: string[]): string => {
|
|
2357
|
+
const named = args.find((arg, n) => !arg.startsWith("--") && args[n - 1] !== "--jobs");
|
|
2358
|
+
const resolved = nodePath.resolve(named ?? ".");
|
|
2359
|
+
return named === undefined ||
|
|
2360
|
+
nodeFs.statSync(resolved, { throwIfNoEntry: false })?.isDirectory() === true
|
|
2361
|
+
? resolved
|
|
2362
|
+
: nodePath.dirname(resolved);
|
|
2363
|
+
};
|
|
2364
|
+
|
|
2365
|
+
// The rules folder of the project in `project`: the nearest bend-lint-rules up from it, or, if
|
|
2366
|
+
// there is none, the one beside its config.
|
|
2367
|
+
const getRulesFolder = (project: string, dir: string = project): string => {
|
|
2368
|
+
const up = nodePath.dirname(dir);
|
|
2369
|
+
return nodeFs.existsSync(nodePath.join(dir, RULES))
|
|
2370
|
+
? nodePath.join(dir, RULES)
|
|
2371
|
+
: up === dir
|
|
2372
|
+
? nodePath.join(project, RULES)
|
|
2373
|
+
: getRulesFolder(project, up);
|
|
2374
|
+
};
|
|
2375
|
+
|
|
1942
2376
|
// The entries of `value`, if it is a JSON object.
|
|
1943
2377
|
const getObjectEntries = (value: unknown): Array<[string, unknown]> | undefined =>
|
|
1944
2378
|
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
@@ -2040,7 +2474,7 @@ export const getRulesProgram = (project: string, { ids, packages }: Rules): stri
|
|
|
2040
2474
|
return `import ${name}@${version}/${rest.join("/")}.bend as Rule${n}`;
|
|
2041
2475
|
}
|
|
2042
2476
|
const real = tryGet(() =>
|
|
2043
|
-
nodeFs.realpathSync(nodePath.join(project,
|
|
2477
|
+
nodeFs.realpathSync(nodePath.join(getRulesFolder(project), ...id.split("/")) + ".bend"),
|
|
2044
2478
|
);
|
|
2045
2479
|
if (real === undefined) {
|
|
2046
2480
|
throw new Error(`there is no rule ${id}: ${RULES}/${id}.bend does not exist`);
|
|
@@ -2106,6 +2540,15 @@ if (import.meta.main) {
|
|
|
2106
2540
|
// down, not the machine.
|
|
2107
2541
|
tryGet(() => os.setPriority(os.constants.priority.PRIORITY_BELOW_NORMAL));
|
|
2108
2542
|
const args = process.argv.slice(2);
|
|
2543
|
+
// `bend-lint build <program>`: the native build a run started in the background (see
|
|
2544
|
+
// getBuiltProgram), which says nothing.
|
|
2545
|
+
if (args[0] === "build" && args[1] !== undefined) {
|
|
2546
|
+
const dir = unwrap(await tryGetBend2SourceUrl());
|
|
2547
|
+
const native = getNativePath(args[1], dir);
|
|
2548
|
+
await getBuild(nodePath.resolve(args[1]), native, dir).catch(() => undefined);
|
|
2549
|
+
tryGet(() => nodeFs.rmSync(native + ".building", { force: true }));
|
|
2550
|
+
process.exit(0);
|
|
2551
|
+
}
|
|
2109
2552
|
if (args[0] === "init") {
|
|
2110
2553
|
try {
|
|
2111
2554
|
const { wrote, kept } = getInitFiles(process.cwd());
|
|
@@ -2134,7 +2577,7 @@ if (import.meta.main) {
|
|
|
2134
2577
|
if (!relative.startsWith("..") && !nodePath.isAbsolute(relative)) {
|
|
2135
2578
|
getInitFiles(project).wrote.forEach((file) => console.log("bend-lint: wrote " + file));
|
|
2136
2579
|
console.log(PREPARING);
|
|
2137
|
-
await getRunSetup(getProjectProgram(project));
|
|
2580
|
+
await getRunSetup(getProjectProgram(project), true);
|
|
2138
2581
|
}
|
|
2139
2582
|
} catch (e) {
|
|
2140
2583
|
console.error("bend-lint: " + getErrorMessageFromUnknown(e) + "; its first run tries again");
|
|
@@ -2164,10 +2607,12 @@ if (import.meta.main) {
|
|
|
2164
2607
|
)
|
|
2165
2608
|
.map((a) => (getOverlay(a) === undefined ? a : getOverlayKey(a)));
|
|
2166
2609
|
const project =
|
|
2167
|
-
getProject(
|
|
2610
|
+
getProject(getStart(runArgs)) ??
|
|
2168
2611
|
endWithError(
|
|
2169
2612
|
json,
|
|
2170
|
-
"no " +
|
|
2613
|
+
"no " +
|
|
2614
|
+
CONFIG +
|
|
2615
|
+
" in the folder of the first file named, or above it; `bunx bend-lint init` makes one",
|
|
2171
2616
|
);
|
|
2172
2617
|
|
|
2173
2618
|
// `bend-lint prepare` makes what a run needs, Bend and the build of its rules, and lints nothing:
|
|
@@ -2181,7 +2626,7 @@ if (import.meta.main) {
|
|
|
2181
2626
|
.catch((e: unknown) =>
|
|
2182
2627
|
endWithError(json, getErrorMessageFromUnknown(e), nodePath.join(project, CONFIG)),
|
|
2183
2628
|
);
|
|
2184
|
-
const [checker, program, notes] = await getRunSetup(named).catch((e: unknown) =>
|
|
2629
|
+
const [checker, program, notes] = await getRunSetup(named, preparing).catch((e: unknown) =>
|
|
2185
2630
|
endWithError(json, getErrorMessageFromUnknown(e)),
|
|
2186
2631
|
);
|
|
2187
2632
|
const setupMs = performance.now() - started;
|