From 8e7135469220c069cd674e0b14478ffc153c825f Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 29 May 2024 16:28:59 +0200 Subject: [PATCH] improvement: Retry with coursier jar if anything fails Previously, if anything failed with coursier native image, for example we wouldn't have proper encoding then user would not be able to do anything. Now, if there is Java avaialable we can retry with the embedded coursier jar. --- .../src/setupCoursier.ts | 5 +- packages/metals-vscode/src/extension.ts | 76 ++++++++++++------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/packages/metals-languageclient/src/setupCoursier.ts b/packages/metals-languageclient/src/setupCoursier.ts index 953803a2..b261da25 100644 --- a/packages/metals-languageclient/src/setupCoursier.ts +++ b/packages/metals-languageclient/src/setupCoursier.ts @@ -19,7 +19,8 @@ export async function setupCoursier( javaVersion: JavaVersion, coursierFetchPath: string, extensionPath: string, - output: OutputChannel + output: OutputChannel, + forceCoursierJar: boolean ): Promise<{ coursier: string; javaHome: string }> { const handleOutput = (out: Buffer) => { const msg = out.toString().trim(); @@ -92,7 +93,7 @@ export async function setupCoursier( /* If we couldn't download coursier, but we have Java * we can still fall back to jar based launcher. */ - if (!coursier && javaHome) { + if ((!coursier || forceCoursierJar) && javaHome) { coursier = path.join(extensionPath, "coursier-fallback.jar"); } diff --git a/packages/metals-vscode/src/extension.ts b/packages/metals-vscode/src/extension.ts index 5b537403..006de20a 100644 --- a/packages/metals-vscode/src/extension.ts +++ b/packages/metals-vscode/src/extension.ts @@ -207,7 +207,8 @@ function debugInformation( async function fetchAndLaunchMetals( context: ExtensionContext, serverVersion: string, - javaVersion: JavaVersion + javaVersion: JavaVersion, + forceCoursierJar = false ) { outputChannel.appendLine(`Metals version: ${serverVersion}`); @@ -228,9 +229,24 @@ async function fetchAndLaunchMetals( javaVersion, metalsDirPath, context.extensionPath, - outputChannel + outputChannel, + forceCoursierJar ); + const canRetryWithJar = + javaHome && !coursier.endsWith(".jar") && !forceCoursierJar; + + function retry(error: any): Promise { + if (canRetryWithJar) { + outputChannel.appendLine( + "Trying again with the embedded coursier. This might take longer." + ); + return fetchAndLaunchMetals(context, serverVersion, javaVersion, true); + } else { + return Promise.reject(error); + } + } + const javaConfig = getJavaConfig({ workspaceRoot: workspace.workspaceFolders ? workspace.workspaceFolders[0]?.uri.fsPath @@ -262,7 +278,7 @@ async function fetchAndLaunchMetals( serverProperties, javaConfig, serverVersion - ).catch((reason) => { + ).catch((reason): Promise => { outputChannel.appendLine( "Launching Metals failed with the following:" ); @@ -270,7 +286,7 @@ async function fetchAndLaunchMetals( outputChannel.appendLine( debugInformation(serverVersion, serverProperties, javaConfig) ); - throw reason; + return retry(reason); }); }, (reason) => { @@ -283,30 +299,34 @@ async function fetchAndLaunchMetals( debugInformation(serverVersion, serverProperties, javaConfig) ); } - const msg = (() => { - const proxy = - `See https://scalameta.org/metals/docs/editors/vscode/#http-proxy for instructions ` + - `if you are using an HTTP proxy.`; - if (process.env.FLATPAK_SANDBOX_DIR) { - return ( - `Failed to download Metals. It seems you are running Visual Studio Code inside the ` + - `Flatpak sandbox, which is known to interfere with the download of Metals. ` + - `Please, try running Visual Studio Code without Flatpak.` - ); - } else { - return ( - `Failed to download Metals, make sure you have an internet connection, ` + - `the Metals version '${serverVersion}'. ` + - proxy - ); - } - })(); - outputChannel.show(); - window.showErrorMessage(msg, openSettingsAction).then((choice) => { - if (choice === openSettingsAction) { - commands.executeCommand(workbenchCommands.openSettings); - } - }); + if (canRetryWithJar) { + return retry(reason); + } else { + const msg = (() => { + const proxy = + `See https://scalameta.org/metals/docs/editors/vscode/#http-proxy for instructions ` + + `if you are using an HTTP proxy.`; + if (process.env.FLATPAK_SANDBOX_DIR) { + return ( + `Failed to download Metals. It seems you are running Visual Studio Code inside the ` + + `Flatpak sandbox, which is known to interfere with the download of Metals. ` + + `Please, try running Visual Studio Code without Flatpak.` + ); + } else { + return ( + `Failed to download Metals, make sure you have an internet connection, ` + + `the Metals version '${serverVersion}'. ` + + proxy + ); + } + })(); + outputChannel.show(); + window.showErrorMessage(msg, openSettingsAction).then((choice) => { + if (choice === openSettingsAction) { + commands.executeCommand(workbenchCommands.openSettings); + } + }); + } } ); }