Skip to content

Do not crash entire extension when analysis fails #880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

## master

- Better error recovery when analysis fails. https://github.com/rescript-lang/rescript-vscode/pull/880

## 1.32.0

- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881
Expand Down
22 changes: 22 additions & 0 deletions server/src/errorReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type cb = (msg: string) => void;

let subscribers: Array<cb> = [];
const errorLastNotified: Record<string, number> = {};

export const onErrorReported = (cb: (msg: string) => void) => {
subscribers.push(cb);
return () => {
subscribers = subscribers.filter((s) => s !== cb);
};
};

export const reportError = (identifier: string, msg: string) => {
// Warn once per 15 min per error
if (
errorLastNotified[identifier] == null ||
errorLastNotified[identifier] < Date.now() - 15 * 1000 * 60
) {
errorLastNotified[identifier] = Date.now();
subscribers.forEach((cb) => cb(msg));
}
};
19 changes: 19 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { fileURLToPath } from "url";
import { ChildProcess } from "child_process";
import { WorkspaceEdit } from "vscode-languageserver";
import { filesDiagnostics } from "./utils";
import { onErrorReported } from "./errorReporter";

interface extensionConfiguration {
allowBuiltInFormatter: boolean;
Expand Down Expand Up @@ -1306,3 +1307,21 @@ function onMessage(msg: p.Message) {
}
}
}

// Gate behind a debug setting potentially?
onErrorReported((msg) => {
let params: p.ShowMessageParams = {
type: p.MessageType.Warning,
message: `ReScript tooling: Internal error. Something broke. Here's the error message that you can report if you want:

${msg}

(this message will only be reported once every 15 minutes)`,
};
let message: p.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
send(message);
});
13 changes: 10 additions & 3 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as os from "os";
import * as codeActions from "./codeActions";
import * as c from "./constants";
import * as lookup from "./lookup";
import { reportError } from "./errorReporter";

let tempFilePrefix = "rescript_format_file_" + process.pid + "_";
let tempFileId = 0;
Expand Down Expand Up @@ -186,9 +187,15 @@ export let runAnalysisAfterSanityCheck = (
RESCRIPT_VERSION: rescriptVersion,
},
};
let stdout = childProcess.execFileSync(binaryPath, args, options);

return JSON.parse(stdout.toString());
try {
let stdout = childProcess.execFileSync(binaryPath, args, options);
return JSON.parse(stdout.toString());
} catch (e) {
console.error(e);
// Element 0 is the action we're performing
reportError(String(args[0]), String(e));
return null;
}
};

export let runAnalysisCommand = (
Expand Down