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: Use jar based coursier if unable to download native image #1499

Merged
merged 1 commit into from
May 13, 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 @@ -49,6 +49,7 @@ describe("setupCoursier", () => {
const { coursier, javaHome } = await setupCoursier(
"17",
tmpDir,
process.cwd(),
new LogOutputChannel()
);
expect(fs.existsSync(coursier)).toBeTruthy;
Expand Down
25 changes: 17 additions & 8 deletions packages/metals-languageclient/src/fetchMetals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface PackedChildPromise {
export async function fetchMetals({
serverVersion,
serverProperties,
javaConfig: { javaOptions, coursier, extraEnv },
javaConfig: { javaOptions, coursier, extraEnv, javaPath },
outputChannel,
}: FetchMetalsOptions): Promise<PackedChildPromise> {
const serverDependency = calcServerDependency(serverVersion);
Expand All @@ -35,11 +35,7 @@ export async function fetchMetals({
);
}

// Convert Java properties to the "-J" argument form used by Coursier
const javaArgs = javaOptions.concat(fetchProperties).map((p) => `-J${p}`);

const coursierArgs = [
...javaArgs,
"fetch",
"-p",
"--ttl",
Expand All @@ -64,9 +60,22 @@ export async function fetchMetals({
},
};

return {
promise: spawn(coursier, coursierArgs, environment),
};
if (coursier.endsWith(".jar")) {
const jarArgs = [
...javaOptions,
...fetchProperties,
"-Dfile.encoding=UTF-8",
"-jar",
coursier,
].concat(coursierArgs);
return { promise: spawn(javaPath, jarArgs, environment) };
} else {
// Convert Java properties to the "-J" argument form used by Coursier
const javaArgs = javaOptions.concat(fetchProperties).map((p) => `-J${p}`);
return {
promise: spawn(coursier, javaArgs.concat(coursierArgs), environment),
};
}
}

export function calcServerDependency(serverVersion: string): string {
Expand Down
25 changes: 20 additions & 5 deletions packages/metals-languageclient/src/setupCoursier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const coursierCommit = "11b428f35ca84a598ca30cce1c35ae4f375e5ee3";
export async function setupCoursier(
javaVersion: JavaVersion,
coursierFetchPath: string,
extensionPath: string,
output: OutputChannel
): Promise<{ coursier: string; javaHome: string }> {
const handleOutput = (out: Buffer) => {
Expand All @@ -40,11 +41,14 @@ export async function setupCoursier(

return fetchCoursier(coursierFetchPath, handleOutput)
.then(() => defaultCoursier)
.catch((err) => {
.catch((_) => {
output.appendLine(
"Failed to fetch coursier. You may want to try installing coursier manually and adding it to PATH."
);
throw err;
output.appendLine(
"Will try to use jar based coursier if Java is available on the machine."
);
return undefined;
});
};

Expand All @@ -67,12 +71,12 @@ export async function setupCoursier(
return ((await getJavaPath).stdout as string).trim();
};

const coursier = await resolveCoursier();
var coursier = await resolveCoursier();
output.appendLine(`Using coursier located at ${coursier}`);

var javaHome = await getJavaHome(javaVersion, output);

if (!javaHome) {
if (!javaHome && coursier) {
output.appendLine(
`No installed java with version ${javaVersion} found. Will fetch one using coursier.`
);
Expand All @@ -81,7 +85,18 @@ export async function setupCoursier(

output.appendLine(`Using Java Home: ${javaHome}`);

return { coursier, javaHome };
/* If we couldn't download coursier, but we have Java
* we can still fall back to jar based launcher.
*/
if (!coursier && javaHome) {
coursier = path.join(extensionPath, "./coursier-fallback.jar");
}

if (javaHome && coursier) return { coursier, javaHome };
else
throw Error(
"Cannot resolve Java home or coursier, please provide at least JAVA_HOME."
);
}

export async function validateCoursier(
Expand Down
Binary file added packages/metals-vscode/coursier-fallback.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
await fetchAndLaunchMetals(context, serverVersion, javaVersion);
} catch (err) {
outputChannel.appendLine(`${err}`);
window.showErrorMessage(`${err}`);
}
}
);
Expand Down Expand Up @@ -217,6 +218,7 @@ async function fetchAndLaunchMetals(
const { coursier, javaHome } = await metalsLanguageClient.setupCoursier(
javaVersion,
metalsDirPath,
context.extensionPath,
outputChannel
);

Expand Down
Loading