[python] (PY-84116) get base path for poetry from project if module is not defined

* make it similar to other tools
+ add trace context for remote sdks

(cherry picked from commit 04bc4bee6467cb708cb39875bf31448461e2bca3)

GitOrigin-RevId: 38bdf8c12a8af95ac3ceab29193b854b95eb40e4
This commit is contained in:
Vitaly Legchilkin
2025-10-23 09:04:31 +00:00
committed by intellij-monorepo-bot
parent b5b5bad3bb
commit dbe4ecc864
5 changed files with 22 additions and 26 deletions
@@ -1759,6 +1759,7 @@ tracecontext.generating.git=Generating git
tracecontext.packaging.tool.window=Packaging Tool Window
tracecontext.packages.sdk.controller=Packages SDK Controller
tracecontext.add.local.python.sdk.dialog=Add Local Python SDK Dialog
tracecontext.add.remote.python.sdk.dialog=Add {0} Python SDK Dialog
tracecontext.new.project.wizard=New Project Wizard
tracecontext.loading.interpreter.list=Loading Interpreter List
tracecontext.detecting.conda.executable.and.environments=Detecting Conda Executable and environments
@@ -79,7 +79,8 @@ abstract class PythonAddEnvironment<P : PathHolder>(open val model: PythonAddInt
protected suspend fun setupSdk(moduleOrProject: ModuleOrProject): PyResult<Sdk> {
val sdk = getOrCreateSdk(moduleOrProject).getOr { return it }
moduleOrProject.moduleIfExists?.excludeInnerVirtualEnv(sdk)
moduleOrProject.project.excludeInnerVirtualEnv(sdk)
moduleOrProject.moduleIfExists?.let { sdk.setAssociationToModule(it) }
return Result.success(sdk)
}
@@ -12,6 +12,7 @@ import com.jetbrains.python.sdk.ModuleOrProject
import com.jetbrains.python.sdk.add.v2.*
import com.jetbrains.python.sdk.basePath
import com.jetbrains.python.sdk.legacy.PythonSdkUtil
import com.jetbrains.python.sdk.moduleIfExists
import com.jetbrains.python.sdk.poetry.createPoetrySdk
import com.jetbrains.python.sdk.poetry.detectPoetryEnvs
import com.jetbrains.python.sdk.poetry.isPoetry
@@ -31,16 +32,11 @@ internal class PoetryExistingEnvironmentSelector<P : PathHolder>(model: PythonMu
PythonSdkUtil.getAllSdks().find { sdk -> sdk.isPoetry && sdk.homePath == pythonBinaryPath.toString() }?.let { return Result.success(it) }
val module = when (moduleOrProject) {
is ModuleOrProject.ModuleAndProject -> {
moduleOrProject.module
}
else -> null
}
val moduleBasePath = module?.basePath?.let { Path.of(it) } ?: error("module base path is not valid: ${module?.basePath}")
val basePathString = moduleOrProject.moduleIfExists?.basePath ?: moduleOrProject.project.basePath
val basePath = basePathString?.let { Path.of(it) } ?: error("module base path is not valid: $basePathString")
return createPoetrySdk(
moduleBasePath,
basePath,
existingSdks = ProjectJdkTable.getInstance().allJdks.toList(),
pythonBinaryPath = pythonBinaryPath
)
@@ -46,15 +46,15 @@ suspend fun createNewPoetrySdk(
@Internal
suspend fun createPoetrySdk(
moduleBasePath: Path,
basePath: Path,
existingSdks: List<Sdk>,
pythonBinaryPath: PathHolder.Eel,
): PyResult<Sdk> = createSdk(
pythonBinaryPath = pythonBinaryPath,
existingSdks = existingSdks,
associatedProjectPath = moduleBasePath.toString(),
suggestedSdkName = suggestedSdkName(moduleBasePath),
sdkAdditionalData = PyPoetrySdkAdditionalData(moduleBasePath)
associatedProjectPath = basePath.toString(),
suggestedSdkName = suggestedSdkName(basePath),
sdkAdditionalData = PyPoetrySdkAdditionalData(basePath)
)
internal val Sdk.isPoetry: Boolean
@@ -66,16 +66,6 @@ internal val Sdk.isPoetry: Boolean
return getOrCreateAdditionalData() is PyPoetrySdkAdditionalData
}
internal fun sdkHomes(sdks: List<Sdk>): Set<String> {
return sdks.mapNotNull { it.homePath }.toSet()
}
internal fun allModules(project: Project?): List<Module> {
return project?.let {
ModuleUtil.getModulesOfType(it, PythonModuleTypeBase.getInstance())
}?.sortedBy { it.name } ?: emptyList()
}
private suspend fun setUpPoetry(moduleBasePath: Path, basePythonBinaryPath: PythonBinary?, installPackages: Boolean): PyResult<PythonBinary> {
val init = PyProjectToml.findInRoot(moduleBasePath) == null
val pythonHomePath = setupPoetry(moduleBasePath, basePythonBinaryPath, installPackages, init).getOr { return it }
@@ -4,6 +4,7 @@ package com.jetbrains.python.target
import com.intellij.execution.target.CustomToolLanguageConfigurable
import com.intellij.execution.target.LanguageRuntimeType
import com.intellij.execution.target.TargetEnvironmentConfiguration
import com.intellij.execution.target.getTargetType
import com.intellij.openapi.module.Module
import com.intellij.openapi.observable.properties.AtomicProperty
import com.intellij.openapi.options.BoundConfigurable
@@ -17,8 +18,8 @@ import com.intellij.platform.ide.progress.runWithModalProgressBlocking
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.intellij.util.ui.launchOnShow
import com.jetbrains.python.PyBundle
import com.jetbrains.python.PyBundle.message
import com.jetbrains.python.TraceContext
import com.jetbrains.python.errorProcessing.ErrorSink
import com.jetbrains.python.errorProcessing.emit
import com.jetbrains.python.newProjectWizard.projectPath.ProjectPathFlows
@@ -47,10 +48,11 @@ class PythonLanguageRuntimeUI(
private val errorSink: ErrorSink = ShowingMessageErrorSync
override fun createPanel(): DialogPanel {
val targetEnvironmentConfiguration = targetSupplier.get()
val model = PythonLocalAddInterpreterModel(
ProjectPathFlows.create(Path.of(project.basePath!!)),
FileSystem.Target(
targetEnvironmentConfiguration = targetSupplier.get(),
targetEnvironmentConfiguration = targetEnvironmentConfiguration,
pythonLanguageRuntimeConfiguration = config,
)
)
@@ -69,7 +71,13 @@ class PythonLanguageRuntimeUI(
minimumSize = Dimension(800, 400)
}
dialogPanel.launchOnShow("PythonAddLocalInterpreterDialog launchOnShow") {
dialogPanel.launchOnShow(
debugName = "PythonLanguageRuntimeUI launchOnShow",
context = TraceContext(
title = message("tracecontext.add.remote.python.sdk.dialog", targetEnvironmentConfiguration.getTargetType().displayName),
parentTraceContext = null
)
) {
supervisorScope {
model.initialize(this@supervisorScope)
mainPanel.onShown(this@supervisorScope)