Skip to content

Commit 66642eb

Browse files
Peter WonePeter Wone
Peter Wone
authored and
Peter Wone
committed
Windows codepage bugfix
1 parent 872f346 commit 66642eb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/arduino/arduino.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ArduinoWorkspace } from "../common/workspace";
2323
import { SerialMonitor } from "../serialmonitor/serialMonitor";
2424
import { UsbDetector } from "../serialmonitor/usbDetector";
2525
import { ProgrammerManager } from "./programmerManager";
26+
import { messages } from './../common/constants';
2627

2728
/**
2829
* Represent an Arduino application based on the official Arduino IDE.
@@ -133,7 +134,7 @@ export class ArduinoApp {
133134
const prebuildargs = dc.prebuild.split(" ");
134135
const prebuildCommand = prebuildargs.shift();
135136
try {
136-
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath});
137+
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, { shell: true, cwd: ArduinoWorkspace.rootPath });
137138
} catch (ex) {
138139
arduinoChannel.error(`Run prebuild failed: \n${ex.error}`);
139140
return;
@@ -215,7 +216,7 @@ export class ArduinoApp {
215216

216217
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
217218
const args = ["--upload", "--board", boardDescriptor, "--port", dc.port, "--useprogrammer",
218-
"--pref", "programmer=" + selectProgrammer, appPath];
219+
"--pref", "programmer=" + selectProgrammer, appPath];
219220
if (VscodeSettings.getInstance().logLevel === "verbose") {
220221
args.push("--verbose");
221222
}
@@ -269,7 +270,7 @@ export class ArduinoApp {
269270
const prebuildargs = dc.prebuild.split(" ");
270271
const prebuildCommand = prebuildargs.shift();
271272
try {
272-
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath});
273+
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, { shell: true, cwd: ArduinoWorkspace.rootPath });
273274
} catch (ex) {
274275
arduinoChannel.error(`Run prebuild failed: \n${ex.error}`);
275276
return;
@@ -303,7 +304,12 @@ export class ArduinoApp {
303304
arduinoChannel.end(`Finished verify sketch - ${dc.sketch}${os.EOL}`);
304305
return true;
305306
} catch (reason) {
306-
arduinoChannel.error(`Exit with code=${reason.code}${os.EOL}`);
307+
const msg = reason.code ?
308+
`Exit with code=${reason.code}${os.EOL}` :
309+
reason.message ?
310+
reason.message :
311+
JSON.stringify(reason);
312+
arduinoChannel.error(msg);
307313
return false;
308314
}
309315

@@ -315,7 +321,7 @@ export class ArduinoApp {
315321
return;
316322
}
317323
const cppConfigFile = fs.readFileSync(configFilePath, "utf8");
318-
const cppConfig = JSON.parse(cppConfigFile) as {configurations: Array<{includePath: string[], forcedInclude: string[]}>};
324+
const cppConfig = JSON.parse(cppConfigFile) as { configurations: Array<{ includePath: string[], forcedInclude: string[] }> };
319325
const libPaths = this.getDefaultPackageLibPaths();
320326
const defaultForcedInclude = this.getDefaultForcedIncludeFiles();
321327
const configuration = cppConfig.configurations[0];

src/common/util.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import * as path from "path";
99
import * as properties from "properties";
1010
import * as vscode from "vscode";
1111
import * as WinReg from "winreg";
12+
import { arduinoChannel } from "./outputChannel";
13+
import { messages } from './constants';
1214

1315
const encodingMapping: object = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../misc", "codepageMapping.json"), "utf8"));
1416

@@ -208,7 +210,13 @@ export function spawn(command: string, outputChannel: vscode.OutputChannel, args
208210

209211
let codepage = "65001";
210212
if (os.platform() === "win32") {
211-
codepage = childProcess.execSync("chcp").toString().split(":").pop().trim();
213+
try {
214+
let chcp = childProcess.execSync("chcp");
215+
codepage = chcp.toString().split(":").pop().trim();
216+
} catch (error) {
217+
arduinoChannel.warning(`Defaulting to code page 850 because chcp failed.\rEnsure your path includes %SystemRoot%\system32\r${error.message}`);
218+
codepage = "850";
219+
}
212220
}
213221

214222
if (outputChannel) {

0 commit comments

Comments
 (0)