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

fix: Allow to use environment executable java to launch servers #1263

Merged
merged 2 commits into from
Aug 26, 2022
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
3 changes: 3 additions & 0 deletions extension/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const VSCODE_TRIGGER_COMPLETION = "editor.action.triggerSuggest";

export const GRADLE_BUILD_FILE_NAMES = ["build.gradle", "settings.gradle", "build.gradle.kts", "settings.gradle.kts"];

export const NO_JAVA_EXECUTABLE =
"No Java executable found, please consider to configure your 'java.jdt.ls.java.home' setting or set JAVA_HOME in your path or put a Java executable in your path.";

export enum CompletionKinds {
DEPENDENCY_GROUP = "dependency_group",
DEPENDENCY_ARTIFACT = "dependency_artifact",
Expand Down
33 changes: 14 additions & 19 deletions extension/src/languageServer/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { GradleBuildContentProvider } from "../client/GradleBuildContentProvider
import { GradleBuild, GradleProject } from "../proto/gradle_pb";
import { RootProjectsStore } from "../stores";
import {
getConfigGradleJavaHome,
checkEnvJavaExecutable,
getConfigJavaImportGradleHome,
getConfigJavaImportGradleUserHome,
getConfigJavaImportGradleVersion,
getConfigJavaImportGradleWrapperEnabled,
getJavaHome,
} from "../util/config";
import { prepareLanguageServerParams } from "./utils";
const CHANNEL_NAME = "Gradle for Java (Language Server)";
Expand All @@ -28,7 +29,7 @@ export async function startLanguageServer(
): Promise<void> {
void vscode.window.withProgress({ location: vscode.ProgressLocation.Window }, (progress) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return new Promise<void>(async (resolve, _reject) => {
return new Promise<void>(async (resolve, reject) => {
progress.report({
message: "Initializing Gradle Language Server",
});
Expand All @@ -53,30 +54,24 @@ export async function startLanguageServer(
serverOptions = awaitServerConnection.bind(null, port);
} else {
// keep consistent with gRPC server
const javaHome = getConfigGradleJavaHome() || process.env.JAVA_HOME;
if (!javaHome) {
void vscode.window
.showErrorMessage(
'There is no valid JAVA_HOME setting to launch Gradle Language Server. Please check your "java.jdt.ls.java.home" setting.',
"Open Settings"
)
.then((answer) => {
if (answer === "Open Settings") {
void vscode.commands.executeCommand(
"workbench.action.openSettings",
"java.jdt.ls.java.home"
);
}
});
return;
const javaHome = getJavaHome();
let javaCommand;
if (javaHome) {
javaCommand = path.join(javaHome, "bin", "java");
} else {
if (!checkEnvJavaExecutable()) {
// we have already show error message in gRPC server for no java executable found, so here we will just reject and return
return reject();
}
javaCommand = "java";
}
const args = [
...prepareLanguageServerParams(),
"-jar",
path.resolve(context.extensionPath, "lib", "gradle-language-server.jar"),
];
serverOptions = {
command: path.join(javaHome, "bin", "java"),
command: javaCommand,
args: args,
};
}
Expand Down
5 changes: 5 additions & 0 deletions extension/src/server/GradleServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as kill from "tree-kill";
import { getGradleServerCommand, getGradleServerEnv } from "./serverUtil";
import { isDebuggingServer } from "../util";
import { Logger } from "../logger/index";
import { NO_JAVA_EXECUTABLE } from "../constant";

const SERVER_LOGLEVEL_REGEX = /^\[([A-Z]+)\](.*)$/;
const DOWNLOAD_PROGRESS_CHAR = ".";
Expand Down Expand Up @@ -40,6 +41,10 @@ export class GradleServer {
const cwd = this.context.asAbsolutePath("lib");
const cmd = path.join(cwd, getGradleServerCommand());
const env = getGradleServerEnv();
if (!env) {
await vscode.window.showErrorMessage(NO_JAVA_EXECUTABLE);
return;
}
const args = [String(this.port)];

this.logger.debug("Starting server");
Expand Down
8 changes: 5 additions & 3 deletions extension/src/server/serverUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getConfigGradleJavaHome } from "../util/config";
import { checkEnvJavaExecutable, getJavaHome } from "../util/config";

export function getGradleServerCommand(): string {
const platform = process.platform;
Expand All @@ -15,13 +15,15 @@ export interface ProcessEnv {
[key: string]: string | undefined;
}

export function getGradleServerEnv(): ProcessEnv {
const javaHome = getConfigGradleJavaHome();
export function getGradleServerEnv(): ProcessEnv | undefined {
const javaHome = getJavaHome();
const env = { ...process.env };
if (javaHome) {
Object.assign(env, {
VSCODE_JAVA_HOME: javaHome,
});
} else if (!checkEnvJavaExecutable()) {
return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

what will happen if undefined is returned? compared with previous behavior where env is returned.

Copy link
Member

Choose a reason for hiding this comment

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

prompt NO_JAVA_EXECUTABLE early before actually executing it?

Copy link
Collaborator Author

@CsCherrYY CsCherrYY Aug 26, 2022

Choose a reason for hiding this comment

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

prompt NO_JAVA_EXECUTABLE early before actually executing it?

Yes. Please refer: https://github.com/microsoft/vscode-gradle/pull/1263/files#diff-3369ade313508c278005bfe83330cb8fb4852a75783f5f5843d8d89a46423fc6R43-R46, if we can't find a proper java executable, will return early.

The previous behavior is always to run the start script via cmd variable, in this way, Gradle will report an error if no java executable can be found.

}
return env;
}
14 changes: 14 additions & 0 deletions extension/src/util/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from "child_process";
import * as vscode from "vscode";
import { GradleConfig } from "../proto/gradle_pb";
import { RootProject } from "../rootProject/RootProject";
Expand Down Expand Up @@ -28,6 +29,19 @@ export function getConfigGradleJavaHome(): string | null {
return getConfigJavaImportGradleJavaHome() || getJdtlsConfigJavaHome() || getConfigJavaHome();
}

export function getJavaHome(): string | undefined {
return getConfigGradleJavaHome() || process.env.JAVA_HOME;
}

export function checkEnvJavaExecutable(): boolean {
try {
execSync("java -version", { stdio: "pipe" });
} catch (e) {
return false;
}
return true;
}

export function getConfigJavaImportGradleUserHome(): string | null {
return vscode.workspace.getConfiguration("java").get<string | null>("import.gradle.user.home", null);
}
Expand Down