cleanup [python]: Module.baseDir and disposed modules problem improvement.

`basePath` and `rootManager` are useless, and will be dropped soon. `baseDir` is also doubtful, but still usable.

`ModuleRootManager.getInstance` throws exception if module is disposed, so we use workspace model to make sure module isn't disposed.

GitOrigin-RevId: 01507a46539195907fb74e1e43c178fb5ff968c0
This commit is contained in:
Ilya.Kazakevich
2026-01-27 19:45:04 +01:00
committed by intellij-monorepo-bot
parent 174ec9b882
commit 98320dd3d0
3 changed files with 22 additions and 2 deletions

View File

@@ -49,6 +49,8 @@ jvm_library(
"//platform/eel-provider",
"//python/impl.helperLocator:community-helpersLocator",
"//python/common",
"//platform/workspace/jps",
"//platform/workspace/storage",
],
runtime_deps = ["//platform/backend"]
)
@@ -92,6 +94,8 @@ jvm_library(
"//platform/eel-provider",
"//python/impl.helperLocator:community-helpersLocator",
"//python/common",
"//platform/workspace/jps",
"//platform/workspace/storage",
]
)
### auto-generated section `build intellij.python.sdk` end

View File

@@ -42,5 +42,7 @@
<orderEntry type="module" module-name="intellij.platform.backend" scope="RUNTIME" />
<orderEntry type="module" module-name="intellij.python.community.helpersLocator" />
<orderEntry type="module" module-name="intellij.python.common" />
<orderEntry type="module" module-name="intellij.platform.workspace.jps" />
<orderEntry type="module" module-name="intellij.platform.workspace.storage" />
</component>
</module>

View File

@@ -6,20 +6,34 @@ import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.util.FileName
import com.intellij.workspaceModel.ide.legacyBridge.findModuleEntity
import org.jetbrains.annotations.ApiStatus.Internal
import java.nio.file.Path
@get:Deprecated("Use root manager directly and obey its contract",
replaceWith = ReplaceWith(" ModuleRootManager.getInstance(this)"),
level = DeprecationLevel.WARNING)
val Module.rootManager: ModuleRootManager
get() = ModuleRootManager.getInstance(this)
/**
* It could be loosely described as a "root directory" of a module. Something, you usually look for `pyproject.toml` in.
* `null` means module is broken: i.e. not a Python module (and doesn't have a baseDir) or it was already disposed, or directory
* was removed. Such modules should be ignored.
*/
val Module.baseDir: VirtualFile?
get() {
val roots = rootManager.contentRoots
val entity = findModuleEntity() ?: return null
val vfsMan = VirtualFileManager.getInstance()
val roots = entity.contentRoots.asSequence().mapNotNull { vfsMan.findFileByUrl(it.url.url) }
val moduleFile = moduleFile ?: return roots.firstOrNull()
return roots.firstOrNull { VfsUtil.isAncestor(it, moduleFile, true) } ?: roots.firstOrNull()
}
@get:Deprecated("Representing path as string is discouraged", replaceWith = ReplaceWith("baseBase?.path"), level = DeprecationLevel.WARNING)
val Module.basePath: String?
get() = baseDir?.path
@@ -30,7 +44,7 @@ suspend fun findAmongRoots(module: Module, fileName: String): VirtualFile? = rea
logger.warn("Module $module is disposed, and can't have $fileName")
}
else {
for (root in module.rootManager.contentRoots) {
for (root in ModuleRootManager.getInstance(module).contentRoots) {
val file = root.findChild(fileName)
if (file != null) {
return@readAction file