Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: Retry with coursier jar if anything fails #1514

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = out.toString().trim();
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 @@
async function fetchAndLaunchMetals(
context: ExtensionContext,
serverVersion: string,
javaVersion: JavaVersion
javaVersion: JavaVersion,
forceCoursierJar = false
) {
outputChannel.appendLine(`Metals version: ${serverVersion}`);

Expand All @@ -228,9 +229,24 @@
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rerun the whole fetchAndLaunchMetals it will again try to download coursier, this seems redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked it while resolving coursier:

  if (forceCoursierJar) {
    coursier = undefined;
  } else {
    coursier = await resolveCoursier();
  }

} else {
return Promise.reject(error);
}
}

const javaConfig = getJavaConfig({
workspaceRoot: workspace.workspaceFolders
? workspace.workspaceFolders[0]?.uri.fsPath
Expand Down Expand Up @@ -262,15 +278,15 @@
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 @@
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
Loading