diff --git a/packages/metals-languageclient/src/__tests__/setupCoursier.test.ts b/packages/metals-languageclient/src/__tests__/setupCoursier.test.ts index 09c49ba2..53d7bd75 100644 --- a/packages/metals-languageclient/src/__tests__/setupCoursier.test.ts +++ b/packages/metals-languageclient/src/__tests__/setupCoursier.test.ts @@ -50,7 +50,8 @@ describe("setupCoursier", () => { "17", tmpDir, process.cwd(), - new LogOutputChannel() + new LogOutputChannel(), + false ); expect(fs.existsSync(coursier)).toBeTruthy; expect(fs.existsSync(javaHome)).toBeTruthy; diff --git a/packages/metals-languageclient/src/setupCoursier.ts b/packages/metals-languageclient/src/setupCoursier.ts index 2523d180..7e1e5690 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 = "\t" + out.toString().trim().split("\n").join("\n\t"); @@ -75,7 +76,12 @@ export async function setupCoursier( return ((await getJavaPath).stdout as string).trim(); }; - var coursier = await resolveCoursier(); + var coursier: string | undefined; + if (forceCoursierJar) { + coursier = undefined; + } else { + coursier = await resolveCoursier(); + } output.appendLine(`Using coursier located at ${coursier}`); var javaHome = await getJavaHome(javaVersion, output); 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); + } + }); + } } ); }