[kotlin] Do not hold a project reference to avoid leakage

#KTIJ-28814 Fixed

GitOrigin-RevId: a4b6dea223897a2d0a71d5efe0bf5af5564dabf9
This commit is contained in:
Vladimir Dolzhenko
2024-07-31 09:33:44 +02:00
committed by intellij-monorepo-bot
parent c413e128d1
commit 5fd6e71b42
5 changed files with 22 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.gradleJava
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId.getProjectId
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.project.Project
import com.intellij.util.EnvironmentUtil
@@ -12,7 +13,7 @@ import org.jetbrains.kotlin.idea.core.script.scriptingInfoLog
import org.jetbrains.kotlin.idea.gradle.KotlinIdeaGradleBundle
import org.jetbrains.kotlin.idea.gradle.scripting.GradleKotlinScriptDefinitionWrapper
import org.jetbrains.kotlin.idea.gradleJava.scripting.GradleScriptDefinitionsContributor
import org.jetbrains.kotlin.idea.gradleJava.scripting.importing.KotlinDslSyncListener
import org.jetbrains.kotlin.idea.gradleJava.scripting.importing.kotlinDslSyncListenerInstance
import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinitionAdapterFromNewAPIBase
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
import org.jetbrains.kotlin.scripting.definitions.getEnvironment
@@ -126,14 +127,16 @@ class ErrorGradleScriptDefinition(project: Project, message: String? = null) :
}
class ErrorScriptDependenciesResolver(
private val project: Project,
project: Project,
private val message: String? = null
) : DependenciesResolver {
private val projectId: String = getProjectId(project)
override fun resolve(scriptContents: ScriptContents, environment: Environment): DependenciesResolver.ResolveResult {
val importInProgress =
KotlinDslSyncListener.instance?.tasks?.let { importTasks ->
synchronized(importTasks) { importTasks.values.any { it.project == project } }
} ?: false
kotlinDslSyncListenerInstance?.tasks?.let { importTasks ->
synchronized(importTasks) { importTasks.values.any { it.projectId == projectId } }
} == true
val failureMessage = if (importInProgress) {
KotlinIdeaGradleBundle.message("error.text.highlighting.is.impossible.during.gradle.import")
} else {

View File

@@ -21,7 +21,7 @@ import java.io.File
fun saveGradleBuildEnvironment(resolverCtx: ProjectResolverContext) {
val task = resolverCtx.externalSystemTaskId
val tasks = KotlinDslSyncListener.instance?.tasks ?: return
val tasks = kotlinDslSyncListenerInstance?.tasks ?: return
synchronized(tasks) { tasks[task] }?.let { sync ->
val gradleHome = resolverCtx.getRootModel(GradleBuildScriptClasspathModel::class.java)?.gradleHomeDir?.path
?: resolverCtx.settings?.gradleHome
@@ -128,8 +128,9 @@ private fun KotlinDslScriptsModel.toListOfScriptModels(project: Project): List<K
}
class KotlinDslGradleBuildSync(val workingDir: String, val taskId: ExternalSystemTaskId) {
val ts = System.currentTimeMillis()
var project: Project? = null
val creationTimestamp: Long = System.currentTimeMillis()
// TODO: projectId is inconsistent - see com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId.getProjectId
var projectId: String? = null
var gradleVersion: String? = null
var gradleHome: String? = null
var javaHome: String? = null

View File

@@ -46,8 +46,8 @@ class KotlinDslScriptSyncContributor : GradleSyncContributor {
override suspend fun onModelFetchCompleted(context: ProjectResolverContext, storage: MutableEntityStorage) {
val project = context.project()
val taskId = context.externalSystemTaskId
val tasks = KotlinDslSyncListener.instance?.tasks
val sync = tasks?.let { synchronized(tasks) { tasks[taskId] } }
val tasks = kotlinDslSyncListenerInstance?.tasks ?: return
val sync = synchronized(tasks) { tasks[taskId] }
blockingContext {
for (buildModel in context.allBuilds) {

View File

@@ -18,11 +18,15 @@ import org.jetbrains.plugins.gradle.settings.GradleSettings
import org.jetbrains.plugins.gradle.util.GradleConstants
import java.util.*
val kotlinDslSyncListenerInstance: KotlinDslSyncListener?
get() =
ExternalSystemTaskNotificationListener.EP_NAME.findExtension(KotlinDslSyncListener::class.java)
class KotlinDslSyncListener : ExternalSystemTaskNotificationListener {
companion object {
val instance: KotlinDslSyncListener?
get() =
ExternalSystemTaskNotificationListener.EP_NAME.findExtension(KotlinDslSyncListener::class.java)
kotlinDslSyncListenerInstance
}
internal val tasks = WeakHashMap<ExternalSystemTaskId, KotlinDslGradleBuildSync>()
@@ -36,7 +40,7 @@ class KotlinDslSyncListener : ExternalSystemTaskNotificationListener {
// project may be null in case of new project
val project = id.findProject() ?: return
task.project = project
task.projectId = id.ideProjectId
GradleBuildRootsManager.getInstance(project)?.markImportingInProgress(workingDir)
}
@@ -66,7 +70,7 @@ class KotlinDslSyncListener : ExternalSystemTaskNotificationListener {
val gradleJvm = GradleSettings.getInstance(project).getLinkedProjectSettings(sync.workingDir)?.gradleJvm
try {
ExternalSystemJdkUtil.getJdk(project, gradleJvm)?.homePath
} catch (e: Exception) {
} catch (_: Exception) {
null
}
}

View File

@@ -180,7 +180,7 @@ class GradleBuildRootsManager(val project: Project) : GradleBuildRootsLocator(pr
scriptingDebugLog { "save script models after import: ${sync.models}" }
val newData = GradleBuildRootData(sync.ts, sync.projectRoots, gradleHome, sync.javaHome, sync.models)
val newData = GradleBuildRootData(sync.creationTimestamp, sync.projectRoots, gradleHome, sync.javaHome, sync.models)
val mergedData = if (sync.failed && oldRoot is Imported) merge(oldRoot.data, newData) else newData
val newRoot = tryCreateImportedRoot(sync.workingDir, LastModifiedFiles()) { mergedData } ?: return null