diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodProjectManager.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodProjectManager.kt index bff55823fdbc3f..e0c2eccf2a5a20 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodProjectManager.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodProjectManager.kt @@ -12,15 +12,19 @@ import com.intellij.openapi.project.ModuleListener import com.intellij.openapi.project.Project import com.intellij.openapi.projectRoots.ProjectJdkTable import com.intellij.openapi.projectRoots.Sdk +import com.intellij.openapi.projectRoots.SdkType +import com.intellij.openapi.projectRoots.impl.JavaHomeFinder +import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil import com.intellij.openapi.roots.ModuleRootModificationUtil import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.openapi.util.registry.Registry import com.intellij.util.application import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.future.await import kotlinx.coroutines.launch import java.util.concurrent.CompletableFuture - +@Suppress("UnstableApiUsage", "OPT_IN_USAGE") class GitpodProjectManager( private val project: Project ) { @@ -33,15 +37,25 @@ class GitpodProjectManager( * It is a workaround for https://youtrack.jetbrains.com/issue/GTW-88 */ private fun configureSdks() { - if (application.isHeadlessEnvironment) { + if (application.isHeadlessEnvironment || Registry.get("gitpod.autoJdk.disabled").asBoolean()) { return } val pendingSdk = CompletableFuture() application.invokeLaterOnWriteThread { application.runWriteAction { try { - ProjectJdkTable.getInstance().preconfigure() - pendingSdk.complete(ProjectJdkTable.getInstance().allJdks.firstOrNull()) + val jdkTable = ProjectJdkTable.getInstance() + jdkTable.preconfigure() + val preferredJdkHomePath = JavaHomeFinder.getFinder().findExistingJdks().firstOrNull() + if (preferredJdkHomePath != null) { + val sdk = SdkConfigurationUtil.createAndAddSDK( + preferredJdkHomePath, + SdkType.findByName(jdkTable.defaultSdkType.name)!! + ) + pendingSdk.complete(sdk) + } else { + pendingSdk.complete(jdkTable.allJdks.firstOrNull()) + } } catch (t: Throwable) { pendingSdk.completeExceptionally(t) } @@ -49,7 +63,7 @@ class GitpodProjectManager( } GlobalScope.launch { val sdk = pendingSdk.await() ?: return@launch - thisLogger().warn("gitpod: '${project.name}' project: SDK detected: $sdk") + thisLogger().warn("gitpod: '${project.name}' project: preferred SDK detected: $sdk") project.messageBus.connect().subscribe(ProjectTopics.MODULES, object : ModuleListener { override fun moduleAdded(project: Project, module: Module) { configureSdk(sdk) @@ -65,7 +79,6 @@ class GitpodProjectManager( val projectRootManager = ProjectRootManager.getInstance(project) if (projectRootManager.projectSdk == null) { projectRootManager.projectSdk = sdk - thisLogger().warn("gitpod: '${project.name}' project: SDK was auto preconfigured: $sdk") } } } @@ -73,9 +86,8 @@ class GitpodProjectManager( ModuleRootModificationUtil.updateModel(module) { m -> if (m.sdk == null) { m.sdk = sdk - thisLogger().warn("gitpod: '${module.name}' module: SDK was auto preconfigured: $sdk") } } } } -} \ No newline at end of file +} diff --git a/components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml b/components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml index 4b521062dfb338..89315499d0c00c 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml +++ b/components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml @@ -33,6 +33,7 @@ +