Skip to content

Commit 85d2f53

Browse files
author
Akos Kitta
committed
Handle compiler errors gracefully.
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent df6212d commit 85d2f53

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
Binary file not shown.

src/extension.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ function findExecutables(): LanguageServerExecutables | undefined {
223223

224224
interface CompileResult {
225225
readonly builder_result: { build_path: string };
226+
readonly compiler_err?: string;
226227
}
227228
interface Platform {
228229
readonly boards: Board[];
@@ -337,10 +338,13 @@ export function activate(context: ExtensionContext) {
337338
}
338339
// save all dirt editor for the sketch
339340
await Promise.all(vscode.workspace.textDocuments.filter(document => getSketchPath(document.uri) === sketch).filter(document => document.isDirty).map(document => document.save()));
340-
const raw = await cliExec(['compile', '-b', fqbn, sketch, '--format', 'json']);
341+
const raw = await cliExec(['compile', '-b', fqbn, sketch, '--config-file', `"${cliConfigPath}"`,'--format', 'json']);
341342
const languageClient = sketchContext?.languageClient;
342343
if (languageClient) {
343344
const result = JSON.parse(raw) as CompileResult;
345+
if (result.compiler_err) {
346+
vscode.window.showErrorMessage(`Compilation failed: ${result.compiler_err}`);
347+
}
344348
const buildOutputUri = Uri.file(result.builder_result.build_path).toString();
345349
languageClient.sendNotification(DidCompleteBuildNotification.TYPE, { buildOutputUri });
346350
}
@@ -374,7 +378,7 @@ async function selectFqbn(): Promise<string | undefined> {
374378
return undefined;
375379
}
376380
async function coreList(): Promise<Platform[]> {
377-
const raw = await cliExec(['core', 'list', '--format', 'json']);
381+
const raw = await cliExec(['core', 'list', '--config-file', `"${cliConfigPath}"`, '--format', 'json']);
378382
return JSON.parse(raw) as Platform[];
379383
}
380384
async function installedBoards(): Promise<(Board & { fqbn: string })[]> {
@@ -403,6 +407,16 @@ async function cliExec(args: string[] = []): Promise<string> {
403407
console.log('cli exec OK with args: ' + JSON.stringify(args), raw);
404408
return resolve(raw);
405409
} else {
410+
const raw = Buffer.concat(out).toString('utf-8');
411+
let json: CompileResult | undefined;
412+
try {
413+
json = JSON.parse(raw) as CompileResult;
414+
} catch {}
415+
// TODO: this should not be here.
416+
// Quick workaround for https://github.com/arduino/arduino-ide/issues/714#issuecomment-1198304868.
417+
if (json && json.compiler_err) {
418+
return resolve(raw);
419+
}
406420
const error = Buffer.concat(err).toString('utf-8');
407421
console.error('cli exec err with args: ' + JSON.stringify(args), error);
408422
return reject(error);
@@ -511,9 +525,10 @@ async function startLanguageServer(context: ExtensionContext, sketchContext: Ske
511525
return true;
512526
}
513527

528+
const cliConfigPath = path.join(os.homedir(), '.arduinoIDE/arduino-cli.yaml');
514529
async function buildLanguageClient(config: LanguageServerConfig & LanguageServerExecutables, sketchContext: SketchContext): Promise<LanguageClient> {
515530
const { lsPath: command, clangdPath, board, flags, env, log } = config;
516-
const args = ['-cli', config.cliPath, '-cli-config', path.join(os.homedir(), '.arduinoIDE/arduino-cli.yaml'), '-clangd', clangdPath, '-fqbn', board.fqbn ?? 'arduino:avr:uno', '-skip-libraries-discovery-on-rebuild'];
531+
const args = ['-cli', config.cliPath, '-cli-config', cliConfigPath, '-clangd', clangdPath, '-fqbn', board.fqbn ?? 'arduino:avr:uno', '-skip-libraries-discovery-on-rebuild'];
517532
if (board.name) {
518533
args.push('-board-name', board.name);
519534
}

0 commit comments

Comments
 (0)