Skip to content

Commit

Permalink
Merge pull request #1514 from tgodzik/last-chance
Browse files Browse the repository at this point in the history
improvement: Retry with coursier jar if anything fails
  • Loading branch information
tgodzik authored Jun 12, 2024
2 parents 177750a + 4c32225 commit 517408e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 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 = "\t" + out.toString().trim().split("\n").join("\n\t");
Expand Down Expand Up @@ -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);
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 517408e

Please sign in to comment.