fix: jsonc parsing in the IDE2 backend

Occurred when `settings.json` contained comments or a trailing comma.

Closes #1945

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta
2023-03-10 13:55:34 +01:00
committed by Akos Kitta
parent 24dc0bbc88
commit fb10de1446
7 changed files with 118 additions and 57 deletions

View File

@@ -43,6 +43,7 @@ import {
firstToUpperCase,
startsWithUpperCase,
} from '../common/utils';
import { SettingsReader } from './settings-reader';
const RecentSketches = 'recent-sketches.json';
const DefaultIno = `void setup() {
@@ -86,6 +87,9 @@ export class SketchesServiceImpl
@inject(IsTempSketch)
private readonly isTempSketch: IsTempSketch;
@inject(SettingsReader)
private readonly settingsReader: SettingsReader;
async getSketches({ uri }: { uri?: string }): Promise<SketchContainer> {
const root = await this.root(uri);
if (!root) {
@@ -631,38 +635,11 @@ export class SketchesServiceImpl
return crypto.createHash('md5').update(path).digest('hex').toUpperCase();
}
// Returns the default.ino from the settings or from default folder.
private async readSettings(): Promise<Record<string, unknown> | undefined> {
const configDirUri = await this.envVariableServer.getConfigDirUri();
const configDirPath = FileUri.fsPath(configDirUri);
try {
const raw = await fs.readFile(join(configDirPath, 'settings.json'), {
encoding: 'utf8',
});
return this.tryParse(raw);
} catch (err) {
if (ErrnoException.isENOENT(err)) {
return undefined;
}
throw err;
}
}
private tryParse(raw: string): Record<string, unknown> | undefined {
try {
return JSON.parse(raw);
} catch {
return undefined;
}
}
// Returns the default.ino from the settings or from default folder.
private async loadInoContent(): Promise<string> {
if (!this.inoContent) {
this.inoContent = new Deferred<string>();
const settings = await this.readSettings();
const settings = await this.settingsReader.read();
if (settings) {
const inoBlueprintPath = settings['arduino.sketch.inoBlueprint'];
if (inoBlueprintPath && typeof inoBlueprintPath === 'string') {