Skip to content

Commit

Permalink
improvement: Retry with coursier jar if anything fails
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgodzik committed May 29, 2024
1 parent cda40b5 commit 8e71354
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
5 changes: 3 additions & 2 deletions packages/metals-languageclient/src/setupCoursier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}

Expand Down
76 changes: 48 additions & 28 deletions packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ function debugInformation(
async function fetchAndLaunchMetals(
context: ExtensionContext,
serverVersion: string,
javaVersion: JavaVersion
javaVersion: JavaVersion,
forceCoursierJar = false
) {
outputChannel.appendLine(`Metals version: ${serverVersion}`);

Expand All @@ -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<any> {

Check warning on line 239 in packages/metals-vscode/src/extension.ts

View workflow job for this annotation

GitHub Actions / Eslint

Unexpected any. Specify a different type

Check warning on line 239 in packages/metals-vscode/src/extension.ts

View workflow job for this annotation

GitHub Actions / Eslint

Unexpected any. Specify a different type
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
Expand Down Expand Up @@ -262,15 +278,15 @@ async function fetchAndLaunchMetals(
serverProperties,
javaConfig,
serverVersion
).catch((reason) => {
).catch((reason): Promise<any> => {

Check warning on line 281 in packages/metals-vscode/src/extension.ts

View workflow job for this annotation

GitHub Actions / Eslint

Unexpected any. Specify a different type
outputChannel.appendLine(
"Launching Metals failed with the following:"
);
outputChannel.appendLine(reason.message);
outputChannel.appendLine(
debugInformation(serverVersion, serverProperties, javaConfig)
);
throw reason;
return retry(reason);
});
},
(reason) => {
Expand All @@ -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);
}
});
}
}
);
}
Expand Down

0 comments on commit 8e71354

Please sign in to comment.