Skip to content

Fix python sys.path problems #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.util.module
import org.jetbrains.kotlin.idea.util.projectStructure.sdk
import org.jetbrains.kotlin.konan.file.File
import org.utbot.common.PathUtil.toPath
import org.utbot.common.appendHtmlLine
import org.utbot.framework.UtSettings
Expand Down Expand Up @@ -335,7 +334,8 @@ fun getDirectoriesForSysPath(
file.fromImports.forEach { importTarget ->
importTarget.resolveImportSourceCandidates().forEach {
val directory = it.parent
if (directory is PsiDirectory ) {
val isRelativeImport = importTarget.relativeLevel > 0 // If we have `from . import a` we don't need to add syspath
if (directory is PsiDirectory && !isRelativeImport) {
// If we have `from a.b.c import d` we need to add syspath to module `a` only
val additionalLevel = importTarget.importSourceQName?.componentCount?.dec() ?: 0
directory.topParent(additionalLevel)?.let { dir ->
Expand All @@ -347,7 +347,9 @@ fun getDirectoriesForSysPath(

// Select modules only from this project but not from installation directory
importedPaths.forEach {
if (it.isProjectSubmodule(ancestor) && !it.path.split(File.separator).contains("site-packages")) {
val path = it.toNioPath()
val hasSitePackages = (0 until(path.nameCount)).any { i -> path.subpath(i, i+1).toString() == "site-packages"}
if (it.isProjectSubmodule(ancestor) && !hasSitePackages) {
sources.add(it)
}
}
Expand Down