Skip to content

Commit 44330f7

Browse files
author
Akos Kitta
committed
feat: let the editor handle any debug errors
arduino/arduino-ide#808 Signed-off-by: Akos Kitta <[email protected]>
1 parent 54ca0db commit 44330f7

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

Diff for: src/extension.ts

+37-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22
import { promises as fs } from 'fs';
3-
import { spawnSync } from 'child_process';
3+
import { spawn } from 'child_process';
44
import deepEqual from 'deep-equal';
55
import WebRequest from 'web-request';
66
import deepmerge from 'deepmerge';
@@ -108,34 +108,52 @@ export function activate(context: ExtensionContext) {
108108
);
109109
}
110110

111+
async function exec(command: string, args: string[]): Promise<{ stdout: string, stderr: string }> {
112+
return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
113+
let out = '';
114+
let err = '';
115+
const cp = spawn(command, args);
116+
cp.stdout.on('data', data => out += data.toString());
117+
cp.stderr.on('data', data => err += data.toString());
118+
cp.on('error', reject);
119+
cp.on('close', (code, signal) => {
120+
const stdout = out.trim();
121+
const stderr = err.trim();
122+
if (code) {
123+
reject(new Error(stderr ?? `Exit code: ${code}`));
124+
}
125+
if (signal) {
126+
reject(new Error(stderr ?? `Exit signal: ${signal}`));
127+
}
128+
if (err.trim()) {
129+
reject(new Error(stderr));
130+
}
131+
resolve({ stdout, stderr });
132+
});
133+
});
134+
}
135+
111136
async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boolean> {
112137
let info: DebugInfo | undefined = undefined;
113-
let rawStdout: string | undefined = undefined;
114-
let rawStdErr: string | undefined = undefined;
138+
let stderr: string | undefined = undefined;
139+
let stdout: string | undefined = undefined;
115140
try {
116141
const args = ['debug', '-I', '-b', config.board.fqbn, config.sketchPath, '--format', 'json'];
117-
const { stdout, stderr } = spawnSync(config?.cliPath || '.', args, { encoding: 'utf8' });
118-
rawStdout = stdout.trim();
119-
rawStdErr = stderr.trim();
142+
const result = await exec(config?.cliPath || '.', args);
143+
stdout = result.stdout;
144+
stderr = result.stderr;
120145
} catch (err) {
121-
showError(err);
122-
return false;
146+
throw err;
123147
}
124-
if (!rawStdout) {
125-
if (rawStdErr) {
126-
if (rawStdErr.toLowerCase().indexOf('compiled sketch not found in') !== -1) {
127-
vscode.window.showErrorMessage(`Sketch '${path.basename(config.sketchPath)}' was not compiled. Please compile the sketch and start debugging again.`);
128-
} else {
129-
vscode.window.showErrorMessage(rawStdErr);
130-
}
148+
if (!stdout) {
149+
if (stderr) {
150+
throw new Error(stderr);
131151
}
132-
return false;
133152
}
134153
try {
135-
info = JSON.parse(rawStdout);
154+
info = JSON.parse(stdout);
136155
} catch (err) {
137-
console.error(`Could not parse JSON: <${rawStdout}>`);
138-
showError(err);
156+
throw err;
139157
}
140158
if (!info) {
141159
return false;
@@ -270,12 +288,6 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
270288
);
271289
}
272290

273-
function showError(err: unknown): void {
274-
console.error(err);
275-
const message = err instanceof Error ? err.message : typeof err === 'string' ? err : String(err);
276-
vscode.window.showErrorMessage(message);
277-
}
278-
279291
/**
280292
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
281293
*/

0 commit comments

Comments
 (0)