From 03f5e63efedbeeeb532898e50cba50389f72d055 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 24 Feb 2026 21:33:11 +0100 Subject: [PATCH] PY-87445: Run `askUserIfPyProjectMustBeEnabled` _after_ `.venv` exclusion. `startAutoImportIfNeeded` might find `pyproject.toml` in `.venv`, to fix it we now make `startVenvExclusion` `suspend` to wait for the end of exclusion process. We also `dispose` unused model as it is required by doc. GitOrigin-RevId: 37ff5db351a6bed839a0db5e1a26c06d2cf1a1fa --- .../internal/platformBridge/VenvExcluder.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/platformBridge/VenvExcluder.kt b/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/platformBridge/VenvExcluder.kt index 53b03497c1c7..619f3d1cd262 100644 --- a/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/platformBridge/VenvExcluder.kt +++ b/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/platformBridge/VenvExcluder.kt @@ -16,6 +16,7 @@ import com.intellij.psi.search.GlobalSearchScope import com.intellij.python.pyproject.model.internal.PyProjectScopeService import com.jetbrains.python.venvReader.VirtualEnvReader.Companion.DEFAULT_VIRTUALENV_DIRNAME import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -23,7 +24,7 @@ import kotlinx.coroutines.sync.withLock /** * Excludes [DEFAULT_VIRTUALENV_DIRNAME] from [project] as soon as it appears in index, should only be called once */ -internal fun startVenvExclusion(project: Project) { +internal suspend fun startVenvExclusion(project: Project) { VirtualFileManager.getInstance().addAsyncFileListener(project.service().scope) { events -> if (events.none { it is VFileCreateEvent || it is VFileMoveEvent }) { // No need to check anything if no file created @@ -37,10 +38,10 @@ internal fun startVenvExclusion(project: Project) { } } } - excludeEnvs(project) + excludeEnvs(project).join() } -private fun excludeEnvs(project: Project) { +private fun excludeEnvs(project: Project): Job = project.service().scope.launch(Dispatchers.Default) { mutex.withLock { val dirs = readAction { FilenameIndex.getVirtualFilesByName(DEFAULT_VIRTUALENV_DIRNAME, GlobalSearchScope.allScope(project)) } @@ -52,14 +53,17 @@ private fun excludeEnvs(project: Project) { val model = rootManager.modifiableModel val currentRoot = model.contentEntries.firstOrNull { root -> root.file?.let { VfsUtilCore.isAncestor(it, venvToExclude, false) } == true - } ?: return@writeAction - currentRoot.addExcludeFolder(venvToExclude) - model.commit() + } + if (currentRoot != null) { + currentRoot.addExcludeFolder(venvToExclude) + model.commit() + }else { + model.dispose() + } } } } } } -} private val mutex = Mutex()