From 2c35f381675f6f9a72dda8dc8d8cce6f7a5247c4 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tamarin Date: Wed, 15 Feb 2023 16:30:46 +0300 Subject: [PATCH 1/2] Fix problem with sys paths from multimodule imports --- .../plugin/language/python/PythonDialogProcessor.kt | 12 ++++++++++-- .../utbot/intellij/plugin/language/python/Utils.kt | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt index 37767a1e11..2d9784953a 100644 --- a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt +++ b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt @@ -318,7 +318,11 @@ fun getDirectoriesForSysPath( if (element != null) { val directory = element.parent if (directory is PsiDirectory) { - importedPaths.add(directory.virtualFile) + // If we have `import a.b.c` we need to add syspath to module `a` only + val additionalLevel = importTarget.importedQName?.componentCount?.dec()?.dec() ?: 0 + directory.topParent(additionalLevel)?.let { dir -> + importedPaths.add(dir.virtualFile) + } } } } @@ -329,7 +333,11 @@ fun getDirectoriesForSysPath( importTarget.resolveImportSourceCandidates().forEach { val directory = it.parent if (directory is PsiDirectory ) { - importedPaths.add(directory.virtualFile) + // If we have `from a.b.c import d` we need to add syspath to module `a` only + val additionalLevel = importTarget.importSourceQName?.componentCount?.dec()?.dec() ?: 0 + directory.topParent(additionalLevel)?.let { dir -> + importedPaths.add(dir.virtualFile) + } } } } diff --git a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/Utils.kt b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/Utils.kt index b4e6ae8267..dbf941e6ee 100644 --- a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/Utils.kt +++ b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/Utils.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement import org.utbot.python.utils.RequirementsUtils import kotlin.random.Random @@ -38,3 +39,11 @@ fun VirtualFile.isProjectSubmodule(ancestor: VirtualFile?): Boolean { fun checkModuleIsInstalled(pythonPath: String, moduleName: String): Boolean { return RequirementsUtils.requirementsAreInstalled(pythonPath, listOf(moduleName)) } + +fun PsiDirectory.topParent(level: Int): PsiDirectory? { + var directory: PsiDirectory? = this + repeat(level) { + directory = directory?.parent + } + return directory +} From 4ee2f6d00cedc69c67a658f1170f7827b9f9324a Mon Sep 17 00:00:00 2001 From: Vyacheslav Tamarin Date: Mon, 20 Feb 2023 10:23:15 +0300 Subject: [PATCH 2/2] Fixed `sys.path` collection --- .../intellij/plugin/language/python/PythonDialogProcessor.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt index 2d9784953a..4e7175b1db 100644 --- a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt +++ b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt @@ -319,7 +319,7 @@ fun getDirectoriesForSysPath( val directory = element.parent if (directory is PsiDirectory) { // If we have `import a.b.c` we need to add syspath to module `a` only - val additionalLevel = importTarget.importedQName?.componentCount?.dec()?.dec() ?: 0 + val additionalLevel = importTarget.importedQName?.componentCount?.dec() ?: 0 directory.topParent(additionalLevel)?.let { dir -> importedPaths.add(dir.virtualFile) } @@ -334,7 +334,7 @@ fun getDirectoriesForSysPath( val directory = it.parent if (directory is PsiDirectory ) { // If we have `from a.b.c import d` we need to add syspath to module `a` only - val additionalLevel = importTarget.importSourceQName?.componentCount?.dec()?.dec() ?: 0 + val additionalLevel = importTarget.importSourceQName?.componentCount?.dec() ?: 0 directory.topParent(additionalLevel)?.let { dir -> importedPaths.add(dir.virtualFile) }