From 26bc53e571f8438b862fd820d68991cd2a1a24d4 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Sun, 31 May 2020 02:08:50 +0300 Subject: [PATCH] PY-31937 Path equality instead of identity equality for modules associated with Python environments When you have the same module opened several times in different projects you get different objects for the same module *.iml, so you have to compare them by their path instead of their identity. GitOrigin-RevId: e0fec8727ed1ce7b90d72d73aebc69fd45eb79ed --- .../python/inspections/PyInterpreterInspection.java | 10 +++++----- .../python/packaging/pipenv/PyPipEnvPackageManager.kt | 9 +++------ python/src/com/jetbrains/python/sdk/PySdkExt.kt | 4 ++++ python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/python/src/com/jetbrains/python/inspections/PyInterpreterInspection.java b/python/src/com/jetbrains/python/inspections/PyInterpreterInspection.java index e5fedf3bd4b6..399f265af7da 100644 --- a/python/src/com/jetbrains/python/inspections/PyInterpreterInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyInterpreterInspection.java @@ -20,6 +20,7 @@ import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService; import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel; import com.intellij.openapi.util.UserDataHolderBase; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.util.PathUtil; @@ -101,12 +102,11 @@ public class PyInterpreterInspection extends PyInspection { registerProblem(node, PyPsiBundle.message("python.sdk.no.interpreter.configured.owner", interpreterOwner), fixes.toArray(LocalQuickFix.EMPTY_ARRAY)); } else { - final Module associatedModule = PySdkExtKt.getAssociatedModule(sdk); - final String associatedName = associatedModule != null ? associatedModule.getName() : PySdkExtKt.getAssociatedModulePath(sdk); // TODO: Introduce an inspection extension - if (PipenvKt.isPipEnv(sdk) && associatedModule != module) { - final String message = associatedName != null ? - "Pipenv interpreter is associated with another " + interpreterOwner + ": '" + associatedName + "'" : + final String associatedModulePath = PySdkExtKt.getAssociatedModulePath(sdk); + if (PipenvKt.isPipEnv(sdk) && (associatedModulePath == null || PySdkExtKt.isAssociatedWithAnotherModule(sdk, module))) { + final String message = associatedModulePath != null ? + "Pipenv interpreter is associated with another " + interpreterOwner + ": '" + associatedModulePath + "'" : "Pipenv interpreter is not associated with any " + interpreterOwner; registerProblem(node, message, fixes.toArray(LocalQuickFix.EMPTY_ARRAY)); } diff --git a/python/src/com/jetbrains/python/packaging/pipenv/PyPipEnvPackageManager.kt b/python/src/com/jetbrains/python/packaging/pipenv/PyPipEnvPackageManager.kt index 9bf5a3254978..15cdabc0ef21 100644 --- a/python/src/com/jetbrains/python/packaging/pipenv/PyPipEnvPackageManager.kt +++ b/python/src/com/jetbrains/python/packaging/pipenv/PyPipEnvPackageManager.kt @@ -12,12 +12,9 @@ import com.intellij.openapi.roots.OrderRootType import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile import com.jetbrains.python.packaging.* -import com.jetbrains.python.sdk.PythonSdkType -import com.jetbrains.python.sdk.associatedModule -import com.jetbrains.python.sdk.baseDir +import com.jetbrains.python.sdk.* import com.jetbrains.python.sdk.pipenv.pipFileLockRequirements import com.jetbrains.python.sdk.pipenv.runPipEnv -import com.jetbrains.python.sdk.pythonSdk /** * @author vlan @@ -49,7 +46,7 @@ class PyPipEnvPackageManager(val sdk: Sdk) : PyPackageManager() { runPipEnv(sdk, *args.toTypedArray()) } finally { - sdk.associatedModule?.baseDir?.refresh(true, false) + sdk.associatedModuleDir?.refresh(true, false) refreshAndGetPackages(true) } } @@ -61,7 +58,7 @@ class PyPipEnvPackageManager(val sdk: Sdk) : PyPackageManager() { runPipEnv(sdk, *args.toTypedArray()) } finally { - sdk.associatedModule?.baseDir?.refresh(true, false) + sdk.associatedModuleDir?.refresh(true, false) refreshAndGetPackages(true) } } diff --git a/python/src/com/jetbrains/python/sdk/PySdkExt.kt b/python/src/com/jetbrains/python/sdk/PySdkExt.kt index 6cef1141a964..f10cd318416e 100644 --- a/python/src/com/jetbrains/python/sdk/PySdkExt.kt +++ b/python/src/com/jetbrains/python/sdk/PySdkExt.kt @@ -163,6 +163,7 @@ val Sdk.associatedModulePath: String? // TODO: Support .project associations get() = associatedPathFromAdditionalData /*?: associatedPathFromDotProject*/ +@Deprecated("Use Sdk.associatedModuleDir instead. There may be several Module objects opened in different projects for a single *.iml module file. To be removed in 2021.2") val Sdk.associatedModule: Module? get() { val associatedPath = associatedModulePath @@ -172,6 +173,9 @@ val Sdk.associatedModule: Module? .firstOrNull { it?.basePath == associatedPath } } +val Sdk.associatedModuleDir: VirtualFile? + get() = associatedModulePath?.let { StandardFileSystems.local().findFileByPath(it) } + fun Sdk.adminPermissionsNeeded(): Boolean { val pathToCheck = sitePackagesDirectory?.path ?: homePath ?: return false return !Files.isWritable(Paths.get(pathToCheck)) diff --git a/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt b/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt index b118fa38394f..84be29ed24b7 100644 --- a/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt +++ b/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt @@ -247,7 +247,7 @@ val Sdk.pipFileLockRequirements: List? */ class UsePipEnvQuickFix(sdk: Sdk?, module: Module) : LocalQuickFix { private val quickFixName = when { - sdk != null && sdk.associatedModule != module -> PyBundle.message("python.sdk.pipenv.quickfix.fix.pipenv.name") + sdk != null && sdk.isAssociatedWithAnotherModule(module) -> PyBundle.message("python.sdk.pipenv.quickfix.fix.pipenv.name") else -> PyBundle.message("python.sdk.pipenv.quickfix.use.pipenv.name") } @@ -389,7 +389,7 @@ class PipEnvPipFileWatcher : EditorFactoryListener { } finally { PythonSdkUtil.getSitePackagesDirectory(sdk)?.refresh(true, true) - sdk.associatedModule?.baseDir?.refresh(true, false) + sdk.associatedModuleDir?.refresh(true, false) } } }