Skip to content

Commit 9457cb3

Browse files
authored
Don't rely on file watcher for detecting wpilib prefs (#839)
double check the existence of the wpilib prefs file if file watcher thinks it doesn't exist Uses synchronous file APIs so it's less invasive then #837. Updating the prefs file synchronously should be very rare.
1 parent f5010f3 commit 9457cb3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

vscode-wpilib/src/preferences.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
2+
import * as fs from 'fs';
23
import * as jsonc from 'jsonc-parser';
34
import * as path from 'path';
45
import * as vscode from 'vscode';
56
import { IPreferences } from 'vscode-wpilibapi';
67
import { localize as i18n } from './locale';
78
import { IPreferencesJson } from './shared/preferencesjson';
89
import { existsAsync, mkdirAsync, readFileAsync, writeFileAsync } from './utilities';
10+
import { logger } from './logger';
911

1012
const defaultPreferences: IPreferencesJson = {
1113
currentLanguage: 'none',
@@ -85,6 +87,28 @@ export class Preferences implements IPreferences {
8587
}
8688

8789
public getIsWPILibProject(): boolean {
90+
// If we already know, return it
91+
if (this.isWPILibProject) {
92+
return true;
93+
}
94+
95+
// Synchronously check for the preferences file in the workspace root.
96+
// This is intentionally sync to avoid updating vscode-wpilibapi
97+
// This can happen if the file watcher missed an update.
98+
try {
99+
const configFilePath = Preferences.getPrefrencesFilePath(this.workspace.uri.fsPath);
100+
if (fs.existsSync(configFilePath)) {
101+
vscode.commands.executeCommand('setContext', 'isWPILibProject', true);
102+
this.isWPILibProject = true;
103+
this.preferencesFile = vscode.Uri.file(configFilePath);
104+
this.updatePreferences().catch((err) => {
105+
logger.error('Failed to update WPILib preferences', err);
106+
});
107+
}
108+
} catch (err) {
109+
logger.error('Failed to update WPILib preferences', err);
110+
}
111+
88112
return this.isWPILibProject;
89113
}
90114

0 commit comments

Comments
 (0)