Skip to content

Commit 6d65594

Browse files
authored
fix: DH-19350: Ruff tries to lint before initializing when disabled (#2429)
Looks like it was an issue caused if Ruff was disabled when we changed settings (we set settings on initial load). This was causing a check to pass and then trying to use Ruff without initializing it Also added some log suppression because Ruff internals call `console.log` when the workspace is created and it's not useful to us. It's something along the lines of `glob 25 created ...` and I just found it unnecessary noise
1 parent 46fc6f5 commit 6d65594

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

packages/console/src/monaco/MonacoProviders.tsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,72 @@ class MonacoProviders extends PureComponent<
2828

2929
static initRuffPromise?: Promise<void>;
3030

31+
static isRuffInitialized = false;
32+
3133
static isRuffEnabled = true;
3234

3335
static ruffSettings: Record<string, unknown> = RUFF_DEFAULT_SETTINGS;
3436

3537
/**
36-
* Loads and initializes Ruff.
38+
* Loads and initializes Ruff if it is enabled.
3739
* Subsequent calls will return the same promise.
3840
*/
3941
static async initRuff(): Promise<void> {
42+
if (!MonacoProviders.isRuffEnabled) {
43+
return;
44+
}
4045
if (MonacoProviders.initRuffPromise) {
4146
return MonacoProviders.initRuffPromise;
4247
}
4348

4449
MonacoProviders.initRuffPromise = init({}).then(() => {
4550
log.debug('Initialized Ruff', Workspace.version());
51+
MonacoProviders.isRuffInitialized = true;
52+
MonacoProviders.updateRuffWorkspace();
53+
});
54+
55+
return MonacoProviders.initRuffPromise;
56+
}
57+
58+
/**
59+
* Updates the current ruff workspace with MonacoProviders.ruffSettings.
60+
* Re-lints all Python models after updating.
61+
*/
62+
static updateRuffWorkspace(): void {
63+
if (!MonacoProviders.isRuffInitialized) {
64+
return;
65+
}
4666

67+
/* eslint-disable no-console */
68+
const prevLog = console.log;
69+
try {
70+
console.log = () => undefined; // Suppress not useful ruff-wasm-web logs when it creates the workspace
4771
MonacoProviders.ruffWorkspace = new Workspace(
4872
MonacoProviders.ruffSettings
4973
);
50-
MonacoProviders.lintAllPython();
51-
});
74+
} finally {
75+
console.log = prevLog; // Restore console.log
76+
}
77+
/* eslint-enable no-console */
5278

53-
return MonacoProviders.initRuffPromise;
79+
MonacoProviders.lintAllPython();
5480
}
5581

5682
/**
5783
* Sets ruff settings
5884
* @param settings The ruff settings
5985
*/
60-
static async setRuffSettings(
86+
static setRuffSettings(
6187
settings: Record<string, unknown> = MonacoProviders.ruffSettings
62-
): Promise<void> {
88+
): void {
6389
MonacoProviders.ruffSettings = settings;
6490

65-
// Ruff has not been initialized yet
66-
if (
67-
MonacoProviders.ruffWorkspace == null &&
68-
MonacoProviders.isRuffEnabled
69-
) {
91+
if (!MonacoProviders.isRuffInitialized) {
7092
MonacoProviders.initRuff();
7193
return;
7294
}
7395

74-
MonacoProviders.ruffWorkspace = new Workspace(settings);
75-
MonacoProviders.lintAllPython();
96+
MonacoProviders.updateRuffWorkspace();
7697
}
7798

7899
static getDiagnostics(model: monaco.editor.ITextModel): Diagnostic[] {

0 commit comments

Comments
 (0)