Skip to content

Commit b7d8c08

Browse files
committed
Use Java SDK defined on JAVA_HOME by default on JetBrains IDEs
1 parent 135bc0f commit b7d8c08

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodProjectManager.kt

+24-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import com.intellij.openapi.project.ModuleListener
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.projectRoots.ProjectJdkTable
1414
import com.intellij.openapi.projectRoots.Sdk
15+
import com.intellij.openapi.projectRoots.SdkType
16+
import com.intellij.openapi.projectRoots.impl.JavaHomeFinder
17+
import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
1518
import com.intellij.openapi.roots.ModuleRootModificationUtil
1619
import com.intellij.openapi.roots.ProjectRootManager
20+
import com.intellij.openapi.util.registry.Registry
1721
import com.intellij.util.application
1822
import kotlinx.coroutines.GlobalScope
1923
import kotlinx.coroutines.future.await
2024
import kotlinx.coroutines.launch
2125
import java.util.concurrent.CompletableFuture
2226

23-
27+
@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
2428
class GitpodProjectManager(
2529
private val project: Project
2630
) {
@@ -33,15 +37,30 @@ class GitpodProjectManager(
3337
* It is a workaround for https://youtrack.jetbrains.com/issue/GTW-88
3438
*/
3539
private fun configureSdks() {
36-
if (application.isHeadlessEnvironment) {
40+
if (application.isHeadlessEnvironment || Registry.get("gitpod.autoJdk.disabled").asBoolean()) {
3741
return
3842
}
3943
val pendingSdk = CompletableFuture<Sdk>()
4044
application.invokeLaterOnWriteThread {
4145
application.runWriteAction {
4246
try {
43-
ProjectJdkTable.getInstance().preconfigure()
44-
pendingSdk.complete(ProjectJdkTable.getInstance().allJdks.firstOrNull())
47+
val jdkTable = ProjectJdkTable.getInstance()
48+
jdkTable.preconfigure()
49+
val preconfiguredJdk = ProjectRootManager.getInstance(project).projectSdk
50+
val preferredJdkHomePath = JavaHomeFinder.getFinder().findExistingJdks().firstOrNull()
51+
pendingSdk.complete(
52+
when {
53+
preconfiguredJdk != null -> preconfiguredJdk
54+
preferredJdkHomePath != null ->
55+
jdkTable.allJdks.find { sdk -> sdk.homePath == preferredJdkHomePath }
56+
?: SdkConfigurationUtil.createAndAddSDK(
57+
preferredJdkHomePath,
58+
SdkType.findByName(jdkTable.defaultSdkType.name)!!
59+
)
60+
61+
else -> jdkTable.allJdks.firstOrNull()
62+
}
63+
)
4564
} catch (t: Throwable) {
4665
pendingSdk.completeExceptionally(t)
4766
}
@@ -78,4 +97,4 @@ class GitpodProjectManager(
7897
}
7998
}
8099
}
81-
}
100+
}

components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<gateway.customization.name implementation="io.gitpod.jetbrains.remote.GitpodGatewayClientCustomizationProvider"/>
3434
<gateway.customization.performance id="gitpodMetricsControl" order="before cpuControl" implementation="io.gitpod.jetbrains.remote.GitpodMetricControlProvider"/>
3535
<gateway.customization.metrics id="gitpodMetricsProvider" implementation="io.gitpod.jetbrains.remote.GitpodMetricProvider" />
36+
<registryKey key="gitpod.autoJdk.disabled" defaultValue="false" description="Disable auto-detection of JDK for the project and its modules" restartRequired="true"/>
3637
</extensions>
3738

3839
</idea-plugin>

0 commit comments

Comments
 (0)