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

Fallback to BuildShip when importer connect hit max attempts #1560

Merged
merged 2 commits into from
Aug 6, 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 @@ -152,7 +152,11 @@ public boolean applies(Collection<IPath> projectConfigurations, IProgressMonitor
public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
IPath rootPath = ResourceUtils.filePathFromURI(rootFolder.toURI().toString());
BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath, true);

if (buildServer == null) {
JavaLanguageServerPlugin.logError("Reach the maximum number of attempts to connect to the build server, use BuildShip instead");
this.isResolved = false;
return;
}
// for all the path in this.directories, find the out most directory which belongs
// to rootFolder and use that directory as the root folder for the build server.
// TODO: consider the following folder structure
Expand Down Expand Up @@ -181,10 +185,9 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled
buildServer.onBuildInitialized();
// TODO: save the capabilities of this server
} catch (CompletionException e) {
Throwable cause = e.getCause();
if (e.getCause() instanceof ResponseErrorException responseError) {
if ("Unhandled method build/initialize".equals(responseError.getMessage())) {
JavaLanguageServerPlugin.logException("Failed to start Gradle Build Server, use BuildShip instead", null);
JavaLanguageServerPlugin.logError("Failed to start Gradle Build Server, use BuildShip instead");
this.isResolved = false;
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.microsoft.gradle.bs.importer;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
Expand All @@ -18,6 +15,7 @@
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.osgi.framework.BundleContext;

import com.microsoft.gradle.bs.importer.model.NamedPipeConnectionException;
import com.microsoft.java.builder.BuildStateManager;

import ch.epfl.scala.bsp4j.BuildClient;
Expand Down Expand Up @@ -79,7 +77,7 @@ public static BuildServerConnection getBuildServerConnection(IPath rootPath) thr
* Get the build server connection for the given root path.
* @param rootPath the root path of the workspace.
* @param createIfMissing whether to create a new build server connection if it doesn't exist.
* @return the build server connection.
* @return the build server connection. If fail to connect before max attempt, returns null and fallback to BuildShip.
* @throws CoreException
*/
public static BuildServerConnection getBuildServerConnection(IPath rootPath, boolean createIfMissing) throws CoreException {
Expand Down Expand Up @@ -114,6 +112,8 @@ public static BuildServerConnection getBuildServerConnection(IPath rootPath, boo
client.onConnectWithServer(server);
instance.buildServers.put(rootPath, Pair.of(server, client));
return server;
} catch (NamedPipeConnectionException e) {
return null;
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
"Failed to start build server.", e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.core.runtime.Platform;

import com.microsoft.gradle.bs.importer.model.NamedPipeConnectionException;
import com.microsoft.gradle.bs.importer.model.Telemetry;

/**
Expand Down Expand Up @@ -90,7 +92,7 @@ private void initializeNamedPipe() {
Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(),
telemetry);
if (attempts == MAX_ATTEMPTS) {
throw new RuntimeException("Failed to connect to the named pipe after " + MAX_ATTEMPTS + " attempts");
throw new NamedPipeConnectionException("Failed to connect to extension", MAX_ATTEMPTS);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.microsoft.gradle.bs.importer.model;

public class NamedPipeConnectionException extends RuntimeException {
public NamedPipeConnectionException(String message, int maxAttempts) {
super(String.format("%s, Max attempts: %d", message, maxAttempts));
}
}
7 changes: 4 additions & 3 deletions extension/src/bs/JdtlsImporterConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ export class JdtlsImporterConnector {
private importerConnection: rpc.MessageConnection | null = null;
private importerPipeServer: net.Server;
private importerPipePath: string;
private readonly context: vscode.ExtensionContext;
private readonly _onImporterReady: vscode.EventEmitter<string> = new vscode.EventEmitter<string>();

constructor(context: vscode.ExtensionContext) {
this.context = context;
constructor(private readonly context: vscode.ExtensionContext) {
this.registerCommand();
}

Expand All @@ -33,6 +31,9 @@ export class JdtlsImporterConnector {
return new Promise((resolve) => {
this._onImporterReady.event((resolvedPath) => {
this.importerPipePath = resolvedPath;
sendInfo("", {
kind: "JdtlsImporterConnectorReceivedPipePath",
});
resolve();
});
});
Expand Down
Loading