From 382286f05ff99a494ca11a447ba3f90d138342cf Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 24 Jul 2024 19:35:12 +0530 Subject: [PATCH] Use `try .. finally` to always reset debounce state --- src/extension.ts | 93 ++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 0aacc42..17e422b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -88,59 +88,58 @@ export async function activate(context: vscode.ExtensionContext): Promise restartInProgress = true; - const projectRoot = await getProjectRoot(); - const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot); + try { + const projectRoot = await getProjectRoot(); + const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot); - if (vscode.workspace.isTrusted) { - if (workspaceSettings.interpreter.length === 0) { - updateStatus( - vscode.l10n.t("Please select a Python interpreter."), - vscode.LanguageStatusSeverity.Error, - ); - traceError( - "Python interpreter missing:\r\n" + - "[Option 1] Select Python interpreter using the ms-python.python.\r\n" + - `[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` + - "Please use Python 3.7 or greater.", - ); + if (vscode.workspace.isTrusted) { + if (workspaceSettings.interpreter.length === 0) { + updateStatus( + vscode.l10n.t("Please select a Python interpreter."), + vscode.LanguageStatusSeverity.Error, + ); + traceError( + "Python interpreter missing:\r\n" + + "[Option 1] Select Python interpreter using the ms-python.python.\r\n" + + `[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` + + "Please use Python 3.7 or greater.", + ); + return; + } - restartInProgress = false; - return; + traceLog(`Using interpreter: ${workspaceSettings.interpreter.join(" ")}`); + const resolvedEnvironment = await resolveInterpreter(workspaceSettings.interpreter); + if (resolvedEnvironment === undefined) { + updateStatus( + vscode.l10n.t("Python interpreter not found."), + vscode.LanguageStatusSeverity.Error, + ); + traceError( + "Unable to find any Python environment for the interpreter path:", + workspaceSettings.interpreter.join(" "), + ); + return; + } else if (!checkVersion(resolvedEnvironment)) { + return; + } } - traceLog(`Using interpreter: ${workspaceSettings.interpreter.join(" ")}`); - const resolvedEnvironment = await resolveInterpreter(workspaceSettings.interpreter); - if (resolvedEnvironment === undefined) { - updateStatus( - vscode.l10n.t("Python interpreter not found."), - vscode.LanguageStatusSeverity.Error, - ); - traceError( - "Unable to find any Python environment for the interpreter path:", - workspaceSettings.interpreter.join(" "), - ); - restartInProgress = false; - return; - } else if (!checkVersion(resolvedEnvironment)) { - restartInProgress = false; - return; + lsClient = await restartServer( + projectRoot, + workspaceSettings, + serverId, + serverName, + outputChannel, + lsClient, + ); + } finally { + // Ensure that we reset the flag in case of an error, early return, or success. + restartInProgress = false; + if (restartQueued) { + restartQueued = false; + await runServer(); } } - - lsClient = await restartServer( - projectRoot, - workspaceSettings, - serverId, - serverName, - outputChannel, - lsClient, - ); - - restartInProgress = false; - if (restartQueued) { - restartQueued = false; - await runServer(); - } }; context.subscriptions.push(