Skip to content

Commit

Permalink
improvement: Allow to override mtals java home
Browse files Browse the repository at this point in the history
Peviously, if use had JAVA_HOME and java on PATH pointing to JDK 11, we would download JDK 17 using coursier. This however might not work if there is no internet and there was no way to work around that. Now, we allow users to specify an override for those cases.

Fixes scalameta#1558
  • Loading branch information
tgodzik committed Dec 12, 2024
1 parent 1e10e1a commit 4eb4af5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function detectLaunchConfigurationChanges(
UserConfiguration.ServerVersion,
UserConfiguration.ServerProperties,
UserConfiguration.JavaVersion,
UserConfiguration.MetalsJavaHome,
UserConfiguration.CustomRepositories,
UserConfiguration.CoursierMirror,
...additionalRestartKeys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export enum UserConfiguration {
* Java version. Can be one of 8, 11, 17, 21.
*/
JavaVersion = "javaVersion",
/**
* Java Home to be used by Metals instead of it inferring using javaVersion.
*/
MetalsJavaHome = "metalsJavaHome",
/**
* Additional repositories that can be used to download dependencies.
* https://get-coursier.io/docs/other-repositories
Expand Down
12 changes: 9 additions & 3 deletions packages/metals-languageclient/src/setupCoursier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const coursierCommit = "11b428f35ca84a598ca30cce1c35ae4f375e5ee3";

export async function setupCoursier(
javaVersion: JavaVersion,
javaHomeOverride: string | undefined,
coursierFetchPath: string,
extensionPath: string,
output: OutputChannel,
Expand Down Expand Up @@ -106,7 +107,13 @@ export async function setupCoursier(
}
output.appendLine(`Using coursier located at ${coursier}`);

var javaHome = await getJavaHome(javaVersion, output);
var javaHome: JavaHome | undefined;

if (javaHomeOverride) {
javaHome = await validateJavaVersion(javaHomeOverride, javaVersion, output);
} else {
javaHome = await getJavaHome(javaVersion, output);
}

if (!javaHome && coursier) {
output.appendLine(
Expand Down Expand Up @@ -136,8 +143,7 @@ export async function setupCoursier(
else
throw Error(
`Cannot resolve Java home or coursier, JAVA_HOME should exist with a version of at least ${javaVersion}.` +
`Alternatively, you can reduce the requirement using "metals.javaVersion" setting.`
);
`Alternatively, you can reduce the requirement using "metals.javaVersion" setting override the path using metals.metalsJavaHome setting."
}
export async function validateCoursier(
Expand Down
5 changes: 5 additions & 0 deletions packages/metals-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@
"scope": "machine-overridable",
"markdownDescription": "Optional path to the Java home directory that will be used for compiling the project.\n\nDefaults to JDK used by Metals's server (look: Java Version).\n\nThis Java version should be lower or equal to JDK version used by the Metals's server."
},
"metals.metalsJavaHome": {
"type": "string",
"scope": "machine",
"markdownDescription": "Optional path to the Java home directory that will be used for the running Metals server.\n\nBy default Metals will try to infer it using the version specified in metals.javaVersion.\n\nThis Java version should be higher than 17."
},
"metals.javaVersion": {
"type": "string",
"default": "17",
Expand Down
2 changes: 2 additions & 0 deletions packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { decodeAndShowFile, MetalsFileProvider } from "./metalsContentProvider";
import {
currentWorkspaceFolder,
getJavaVersionFromConfig,
getJavaVersionOverride,
getTextDocumentPositionParams,
getValueFromConfig,
metalsDir,
Expand Down Expand Up @@ -230,6 +231,7 @@ async function fetchAndLaunchMetals(

const { coursier, javaHome } = await metalsLanguageClient.setupCoursier(
javaVersion,
getJavaVersionOverride(),
metalsDirPath,
context.extensionPath,
outputChannel,
Expand Down
7 changes: 7 additions & 0 deletions packages/metals-vscode/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export function getJavaVersionFromConfig() {
return undefined;
}

export function getJavaVersionOverride(): string | undefined {
return workspace
.getConfiguration("metals")
.get<string>(UserConfiguration.MetalsJavaHome)
?.trim();
}

export async function fetchFrom(
url: string,
options?: http.RequestOptions
Expand Down

0 comments on commit 4eb4af5

Please sign in to comment.