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
This commit is contained in:
Andrey Vlasovskikh
2020-05-31 02:39:21 +03:00
committed by intellij-monorepo-bot
parent c7f57e1356
commit 26bc53e571
4 changed files with 14 additions and 13 deletions
@@ -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));
}
@@ -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)
}
}
@@ -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))
@@ -247,7 +247,7 @@ val Sdk.pipFileLockRequirements: List<PyRequirement>?
*/
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)
}
}
}