diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java index 9cf4c3997..218dc2db0 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.CompletionException; +import java.util.stream.Collectors; import org.eclipse.core.internal.resources.Project; import org.eclipse.core.internal.resources.ProjectDescription; @@ -150,13 +151,6 @@ public boolean applies(Collection projectConfigurations, IProgressMonitor @Override 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 @@ -166,11 +160,42 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled // |-- sub3 // if user partially selects sub1 and sub2, we should still use ROOT as the root folder // and only import sub1 and sub2 as projects. - java.nio.file.Path inferredRoot = this.directories.stream() + java.nio.file.Path inferredRoot = null; + List sortedDirectories = this.directories.stream() .filter(directory -> directory.startsWith(rootFolder.toPath())) .sorted((p1, p2) -> p1.getNameCount() - p2.getNameCount()) - .findFirst() - .orElse(rootFolder.toPath()); + .collect(Collectors.toList()); + if (sortedDirectories.isEmpty()) { + inferredRoot = null; // theoretically this should not happen + } else if (sortedDirectories.size() == 1) { + inferredRoot = sortedDirectories.get(0); + } else { + if (sortedDirectories.get(0).getNameCount() == sortedDirectories.get(1).getNameCount()) { + // if there are multiple directories with the same name count, we can't determine + // the root folder for the build server. + Telemetry telemetry = new Telemetry("unableToDetermineRootFolder", "true"); + Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(), + telemetry); + inferredRoot = null; + } else { + inferredRoot = sortedDirectories.get(0); + } + } + + if (inferredRoot == null) { + JavaLanguageServerPlugin.logError("Failed to determine the root folder for the build server, use BuildShip instead"); + this.isResolved = false; + return; + } + + 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; + } + InitializeBuildParams params = new InitializeBuildParams( CLIENT_NAME, ImporterPlugin.getBundleVersion(),