Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37d8173

Browse filesBrowse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
feat: let the editor handle any debug errors
arduino/arduino-ide#808 Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent 54ca0db commit 37d8173
Copy full SHA for 37d8173

File tree

1 file changed

+34
-32
lines changed
Filter options

1 file changed

+34
-32
lines changed

‎src/extension.ts

Copy file name to clipboardExpand all lines: src/extension.ts
+34-32Lines changed: 34 additions & 32 deletions
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,37 +108,45 @@ 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;
115138
try {
116139
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();
120-
} catch (err) {
121-
showError(err);
122-
return false;
123-
}
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-
}
140+
const { stdout, stderr } = await exec(config?.cliPath || '.', args);
141+
if (!stdout && stderr) {
142+
throw new Error(stderr);
143+
}
144+
info = JSON.parse(stdout);
145+
if (!info) {
146+
return false;
131147
}
132-
return false;
133-
}
134-
try {
135-
info = JSON.parse(rawStdout);
136148
} catch (err) {
137-
console.error(`Could not parse JSON: <${rawStdout}>`);
138-
showError(err);
139-
}
140-
if (!info) {
141-
return false;
149+
throw err;
142150
}
143151
const defaultDebugConfig = {
144152
cwd: '${workspaceRoot}',
@@ -270,12 +278,6 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
270278
);
271279
}
272280

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-
279281
/**
280282
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
281283
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.