Skip to content

Commit 83e95d3

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

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
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,23 +37,33 @@ 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 preferredJdkHomePath = JavaHomeFinder.getFinder().findExistingJdks().firstOrNull()
50+
if (preferredJdkHomePath != null) {
51+
val sdk = SdkConfigurationUtil.createAndAddSDK(
52+
preferredJdkHomePath,
53+
SdkType.findByName(jdkTable.defaultSdkType.name)!!
54+
)
55+
pendingSdk.complete(sdk)
56+
} else {
57+
pendingSdk.complete(jdkTable.allJdks.firstOrNull())
58+
}
4559
} catch (t: Throwable) {
4660
pendingSdk.completeExceptionally(t)
4761
}
4862
}
4963
}
5064
GlobalScope.launch {
5165
val sdk = pendingSdk.await() ?: return@launch
52-
thisLogger().warn("gitpod: '${project.name}' project: SDK detected: $sdk")
66+
thisLogger().warn("gitpod: '${project.name}' project: preferred SDK detected: $sdk")
5367
project.messageBus.connect().subscribe(ProjectTopics.MODULES, object : ModuleListener {
5468
override fun moduleAdded(project: Project, module: Module) {
5569
configureSdk(sdk)
@@ -65,17 +79,15 @@ class GitpodProjectManager(
6579
val projectRootManager = ProjectRootManager.getInstance(project)
6680
if (projectRootManager.projectSdk == null) {
6781
projectRootManager.projectSdk = sdk
68-
thisLogger().warn("gitpod: '${project.name}' project: SDK was auto preconfigured: $sdk")
6982
}
7083
}
7184
}
7285
for (module in ModuleManager.getInstance(project).modules) {
7386
ModuleRootModificationUtil.updateModel(module) { m ->
7487
if (m.sdk == null) {
7588
m.sdk = sdk
76-
thisLogger().warn("gitpod: '${module.name}' module: SDK was auto preconfigured: $sdk")
7789
}
7890
}
7991
}
8092
}
81-
}
93+
}

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

Lines changed: 1 addition & 0 deletions
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)