tla-ts 0.0.3-dev.cf3b82c → 0.0.3-dev.cfd469d

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/src/run-cli.ts CHANGED
@@ -10,23 +10,26 @@ import {
10
10
  import { spawnSync } from "node:child_process";
11
11
  import { basename, delimiter, dirname, join, resolve } from "node:path";
12
12
  import { availableParallelism } from "node:os";
13
- import type { CheckResult, CheckStatus, TraceStep } from "./check";
13
+ import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
14
14
  import { defineCommand, type ParsedArgs } from "./cli-options";
15
15
  import { parseConfig } from "./config";
16
16
  import {
17
+ createSanyError,
18
+ formatSanyWarnings,
17
19
  getErrorExitCode,
18
20
  TlaConfigError,
19
21
  TlaError,
20
22
  TlaEvaluationError,
21
23
  TlaProofError,
22
24
  TlaSyntaxError,
25
+ type SanyReport,
23
26
  } from "./errors";
24
27
  import { EXIT_STATUS } from "./exit-codes";
25
28
  import type { LineRange } from "./obligation-generate";
26
29
  import { getAtPrefix, getLocusText } from "./obligation-expression";
27
30
  import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
28
31
  import { check } from "./parallel";
29
- import { parseModule } from "./parser";
32
+ import { parseModuleFile } from "./sany-parse-error";
30
33
  import type { StateDump } from "./state-graph";
31
34
  import {
32
35
  formatProbability,
@@ -135,14 +138,42 @@ const getTemporalHeadline = (names: string[]) =>
135
138
  ? `Error: Temporal property ${names[0]} was violated.\n`
136
139
  : `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
137
140
 
141
+ const getFrames = (callStack: string[]) =>
142
+ callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
143
+
144
+ const printCallStack = (frames: string) =>
145
+ console.log(
146
+ `Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
147
+ );
148
+
149
+ const printReplay = (replay: Replay) => {
150
+ replay.output.forEach((line) => console.log(line));
151
+ const frames = getFrames(replay.callStack ?? []);
152
+ const fingerprint = replay.fingerprint;
153
+ if (replay.isBeforeError) {
154
+ if (frames) printCallStack(frames);
155
+ } else if (replay.callStack && fingerprint) {
156
+ const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
157
+ console.log(
158
+ `Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
159
+ );
160
+ } else if (replay.callStack) {
161
+ printCallStack(frames);
162
+ }
163
+ };
164
+
138
165
  const printResult = (result: CheckResult) => {
139
166
  if (result.failure) {
167
+ const replay = result.failure.replay;
168
+ if (replay?.isBeforeError) printReplay(replay);
140
169
  console.log(`Error: ${result.failure.message}`);
141
170
  if (result.failure.initialState) {
142
171
  console.log("While working on the initial state:");
143
172
  printState(result.failure.initialState);
144
173
  }
145
174
  printTrace(result.failure.trace);
175
+ if (replay && !replay.isBeforeError) printReplay(replay);
176
+ if (result.failure.isWithoutStatistics) return;
146
177
  } else if (result.violation) {
147
178
  const violation = result.violation;
148
179
  const name = violation.name;
@@ -217,10 +248,16 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
217
248
  }
218
249
  const source = readFileSync(specPath, "utf8");
219
250
  const fileName = basename(specPath, ".tla");
220
- const moduleName = parseModule(source).name;
251
+ const moduleName = parseModuleFile(source, fileName).name;
221
252
  if (moduleName !== fileName) {
222
- throw new TlaSyntaxError(
223
- `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
253
+ throw createSanyError(
254
+ [
255
+ {
256
+ location: "Unknown location",
257
+ message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
258
+ },
259
+ ],
260
+ true,
224
261
  );
225
262
  }
226
263
  const configPath =
@@ -286,21 +323,34 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
286
323
  return result.failure?.exitCode ?? EXIT_CODES[result.status];
287
324
  };
288
325
 
289
- // TLC prints the errors it raises itself. tla-ts names the others, such as a module SANY rejects.
290
- const printError = (error: TlaError) => {
326
+ const formatSanyErrors = (report: SanyReport) =>
327
+ `*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
328
+
329
+ // TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
330
+ // others, such as a module it cannot parse.
331
+ const printError = (error: TlaError, path?: string) => {
291
332
  if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
292
333
  return console.log(`Error: ${error.message}`);
293
334
  }
294
- console.error(`${error.name}: ${error.message}`);
335
+ const report = error instanceof TlaSyntaxError ? error.sany : undefined;
336
+ if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
337
+ if (report.parseError !== undefined) console.log(report.parseError);
338
+ if (report.warnings.length > 0) console.log(formatSanyWarnings(report.warnings));
339
+ console.log(
340
+ report.isFatal
341
+ ? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
342
+ : `Semantic errors:\n\n${formatSanyErrors(report)}`,
343
+ );
344
+ console.log("Error: Parsing or semantic analysis failed.");
295
345
  };
296
346
 
297
347
  // Prints a TlaError and returns its exit code.
298
- const reportErrors = async (run: () => number | Promise<number>) => {
348
+ const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
299
349
  try {
300
350
  return await run();
301
351
  } catch (error) {
302
352
  if (!(error instanceof TlaError)) throw error;
303
- printError(error);
353
+ printError(error, path);
304
354
  return getErrorExitCode(error);
305
355
  }
306
356
  };
@@ -311,7 +361,7 @@ const runEach = async (paths: string[], runPath: (path: string) => number | Prom
311
361
  const exitCodes: number[] = [];
312
362
  for (const [index, path] of paths.entries()) {
313
363
  if (index > 0) console.log("");
314
- exitCodes.push(await reportErrors(() => runPath(path)));
364
+ exitCodes.push(await reportErrors(() => runPath(path), path));
315
365
  }
316
366
  return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
317
367
  };