From 9e9d04f5c41e5c824e83c11270bca2c98624e22b Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 18 Oct 2023 11:14:11 +0800 Subject: [PATCH] fix: Infer the project root before launching build server - If the build files do not exist at the root folder, use the out most folder containing the build files as the root for build server. Signed-off-by: Sheng Chen --- .../GradleBuildServerProjectImporter.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 4128a55ca..9d2fd2a21 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 @@ -93,11 +93,18 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled IPath rootPath = ResourceUtils.filePathFromURI(rootFolder.toURI().toString()); BuildServerConnection buildServer = ImporterPlugin.getBuildServerConnection(rootPath); + // 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. + java.nio.file.Path inferredRoot = this.directories.stream() + .filter(directory -> directory.startsWith(rootFolder.toPath())) + .sorted((p1, p2) -> p1.getNameCount() - p2.getNameCount()) + .findFirst() + .orElse(rootFolder.toPath()); InitializeBuildParams params = new InitializeBuildParams( CLIENT_NAME, CLIENT_VERSION, BSP_VERSION, - rootFolder.toPath().toUri().toString(), + inferredRoot.toUri().toString(), new BuildClientCapabilities(java.util.Collections.singletonList("java")) ); BuildServerPreferences data = getBuildServerPreferences(); @@ -132,6 +139,20 @@ public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceled } } + @Override + public boolean isResolved(File folder) throws OperationCanceledException, CoreException { + // TOOD: Once the upstream GradleProjectImporter has been updated to not import when + // the gradle project has already imported by other importers, we can modify this logic + // so that Maven importer can be involved for other projects. + for (IProject project : ProjectUtils.getAllProjects()) { + if (Utils.isGradleBuildServerProject(project) && + project.getLocation().toPath().startsWith(folder.toPath())) { + return true; + } + } + return false; + } + @Override public void reset() { // do nothing