Pycharm: next iteration on not to hide errors rally.

No need to return null: throwing exception in case of severe unexpected error is also ok.

GitOrigin-RevId: f8dde3f6d95c08abd97143a5ce3dc9b6eee33e54
This commit is contained in:
Ilya.Kazakevich
2024-07-18 23:36:05 +02:00
committed by intellij-monorepo-bot
parent 09c2ab97bc
commit 76865ffa15
9 changed files with 22 additions and 23 deletions

View File

@@ -95,8 +95,8 @@ abstract class PythonBasedLangSupport : AbstractLangSupport() {
existingSdks: List<Sdk>,
module: Module?): Sdk? {
val venvRoot = FileUtil.toSystemDependentName(PySdkSettings.instance.getPreferredVirtualEnvBasePath(project.basePath))
val venvSdk = createVirtualEnvSynchronously(preferredSdk, existingSdks, venvRoot, project.basePath, project, module, project).getOrLogException(LOGGER)
return venvSdk?.also {
val venvSdk = createVirtualEnvSynchronously(preferredSdk, existingSdks, venvRoot, project.basePath, project, module, project)
return venvSdk.also {
SdkConfigurationUtil.addSdk(it)
}
}

View File

@@ -154,7 +154,7 @@ fun createSdkByGenerateTask(
baseSdk: Sdk?,
associatedProjectPath: String?,
suggestedSdkName: String?,
): Sdk? {
): Sdk {
val homeFile = try {
val homePath = ProgressManager.getInstance().run(generateSdkHomePath)
StandardFileSystems.local().refreshAndFindFileByPath(homePath) ?: throw ExecutionException(
@@ -163,7 +163,7 @@ fun createSdkByGenerateTask(
}
catch (e: ExecutionException) {
showSdkExecutionException(baseSdk, e, PyBundle.message("python.sdk.failed.to.create.interpreter.title"))
return null
throw e
}
val suggestedName = suggestedSdkName ?: suggestAssociatedSdkName(homeFile.path, associatedProjectPath)
return SdkConfigurationUtil.setupSdk(existingSdks.toTypedArray(), homeFile,

View File

@@ -107,7 +107,7 @@ open class PyAddNewCondaEnvPanel(
override fun validateAll(): List<ValidationInfo> =
emptyList() // Pre target validation is not supported
override fun getOrCreateSdk(): Sdk? {
override fun getOrCreateSdk(): Sdk {
val condaPath = condaPathField.text
val task = object : Task.WithResult<String, ExecutionException>(project, PyBundle.message("python.sdk.creating.conda.environment.title"), false) {
override fun compute(indicator: ProgressIndicator): String {
@@ -117,7 +117,7 @@ open class PyAddNewCondaEnvPanel(
}
val shared = makeSharedField.isSelected
val associatedPath = if (!shared) projectBasePath else null
val sdk = createSdkByGenerateTask(task, existingSdks, null, associatedPath, null) ?: return null
val sdk = createSdkByGenerateTask(task, existingSdks, null, associatedPath, null)
if (!shared) {
sdk.associateWithModule(module, newProjectPath)
}

View File

@@ -78,7 +78,7 @@ open class PyAddNewVirtualEnvPanel(private val project: Project?,
override fun getOrCreateSdk(): Sdk? {
return createVirtualEnvSynchronously(baseSdkField.selectedSdk, existingSdks, pathField.text, newProjectPath, project, module, context,
inheritSitePackagesField.isSelected, makeSharedField.isSelected).getOrLogException(thisLogger())
inheritSitePackagesField.isSelected, makeSharedField.isSelected)
}
override fun getStatisticInfo(): InterpreterStatisticsInfo? {

View File

@@ -173,7 +173,7 @@ class PyAddVirtualEnvPanel(project: Project?,
}
}
override fun getOrCreateSdk(): Sdk? {
override fun getOrCreateSdk(): Sdk {
// applies components' states for bound properties (e.g. selected radio button to `isCreateNewVirtualenv` field)
contentPanel.apply()
@@ -182,25 +182,25 @@ class PyAddVirtualEnvPanel(project: Project?,
if (isCreateNewVirtualenv) return createNewVirtualenvSdk(targetEnvironmentConfiguration)
val item = interpreterCombobox.selectedItem
val item = interpreterCombobox.selectedItem as ExistingPySdkComboBoxItem
// there should *not* be other items other than `ExistingPySdkComboBoxItem`
return if (item is ExistingPySdkComboBoxItem) configureExistingVirtualenvSdk(targetEnvironmentConfiguration, item.sdk) else null
return configureExistingVirtualenvSdk(targetEnvironmentConfiguration, item.sdk)
}
/**
* Note: there is a careful work with SDK names because of the caching of Python package managers in
* [com.jetbrains.python.packaging.PyPackageManagersImpl.forSdk].
*/
private fun createNewVirtualenvSdk(targetEnvironmentConfiguration: TargetEnvironmentConfiguration?): Sdk? {
private fun createNewVirtualenvSdk(targetEnvironmentConfiguration: TargetEnvironmentConfiguration?): Sdk {
// TODO [targets] Do *not* silently `return null`
val baseSelectedSdk = baseInterpreterCombobox.selectedSdk ?: return null
val baseSelectedSdk = baseInterpreterCombobox.selectedSdk
val virtualenvRoot = locationField.text
val baseSdk = if (targetEnvironmentConfiguration != null) {
// TODO [targets] why don't we use `baseSelectedSdk`
// tune `baseSelectedSdk`
val sdkAdditionalData = PyTargetAwareAdditionalData(PyFlavorAndData(PyFlavorData.Empty, virtualEnvSdkFlavor))
sdkAdditionalData.targetEnvironmentConfiguration = targetEnvironmentConfiguration
val homePath = baseSelectedSdk.homePath ?: return null
val homePath = baseSelectedSdk.homePath!!
// suggesting the proper name for the base SDK fixes the problem with clashing caching key of Python package manager
val customSdkSuggestedName = PythonInterpreterTargetEnvironmentFactory.findDefaultSdkName(project, sdkAdditionalData, version = null)
sdkAdditionalData.interpreterPath = homePath
@@ -210,13 +210,13 @@ class PyAddVirtualEnvPanel(project: Project?,
baseSelectedSdk
}
return createVirtualEnvSynchronously(baseSdk, existingSdks, virtualenvRoot, projectBasePath, project, module, context,
isInheritSitePackages, false, targetPanelExtension).getOrLogException(LOGGER)
isInheritSitePackages, false, targetPanelExtension)
}
private fun configureExistingVirtualenvSdk(targetEnvironmentConfiguration: TargetEnvironmentConfiguration?, selectedSdk: Sdk): Sdk? {
private fun configureExistingVirtualenvSdk(targetEnvironmentConfiguration: TargetEnvironmentConfiguration?, selectedSdk: Sdk): Sdk {
if (targetEnvironmentConfiguration == null) {
return when (selectedSdk) {
is PyDetectedSdk -> selectedSdk.setupAssociated(existingSdks, newProjectPath ?: project?.basePath).getOrLogException(LOGGER)?.apply {
is PyDetectedSdk -> selectedSdk.setupAssociated(existingSdks, newProjectPath ?: project?.basePath).getOrThrow().apply {
// TODO [targets] Restore `makeSharedField` flag
associateWithModule(module, newProjectPath)
}

View File

@@ -49,7 +49,7 @@ internal fun PythonAddInterpreterPresenter.setupVirtualenv(venvPath: Path, proje
val venvPathOnTarget = getPathOnTarget(venvPath)
val savedSdk = installBaseSdk(baseSdk, state.allSdks.get()) ?: return null
val sdk = createVirtualEnvSynchronously(savedSdk, state.allSdks.get(), venvPathOnTarget,
projectPath, null, null).getOrThrow()
projectPath, null, null)
SdkConfigurationUtil.addSdk(sdk)
return sdk
}

View File

@@ -46,10 +46,10 @@ fun createVirtualEnvSynchronously(
inheritSitePackages: Boolean = false,
makeShared: Boolean = false,
targetPanelExtension: TargetPanelExtension? = null,
): Result<Sdk> {
): Sdk {
val targetEnvironmentConfiguration = baseSdk.targetEnvConfiguration
val installedSdk: Sdk = if (targetEnvironmentConfiguration == null) {
installSdkIfNeeded(baseSdk, module, existingSdks, context).getOrElse { return Result.failure(it) }
installSdkIfNeeded(baseSdk, module, existingSdks, context).getOrThrow()
}
else {
baseSdk
@@ -79,7 +79,6 @@ fun createVirtualEnvSynchronously(
if (it == null) {
// here is the local machine case
createSdkByGenerateTask(task, existingSdks, installedSdk, associatedPath, null)
?: return Result.failure(Throwable("Error calling createSdkByGenerateTask"))
}
else {
val homePath = ProgressManager.getInstance().run(task)
@@ -99,7 +98,7 @@ fun createVirtualEnvSynchronously(
// If we would like to store preferred paths for non-local targets we need to use some key to identify the exact target.
PySdkSettings.instance.onVirtualEnvCreated(installedSdk, FileUtil.toSystemIndependentName(venvRoot), projectPath)
}
return Result.success(venvSdk)
return venvSdk
}
fun findPreferredVirtualEnvBaseSdk(existingBaseSdks: List<Sdk>): Sdk? {

View File

@@ -148,7 +148,7 @@ fun setupPipEnvSdkUnderProgress(project: Project?,
return PythonSdkUtil.getPythonExecutable(pipEnv) ?: FileUtil.join(pipEnv, "bin", "python")
}
}
return createSdkByGenerateTask(task, existingSdks, null, projectPath, suggestedSdkName(projectPath))?.apply {
return createSdkByGenerateTask(task, existingSdks, null, projectPath, suggestedSdkName(projectPath)).apply {
associateWithModule(module, newProjectPath)
isPipEnv = true
}

View File

@@ -192,7 +192,7 @@ fun setupPoetrySdkUnderProgress(project: Project?,
}
}
return createSdkByGenerateTask(task, existingSdks, null, projectPath, suggestedSdkName(projectPath))?.apply {
return createSdkByGenerateTask(task, existingSdks, null, projectPath, suggestedSdkName(projectPath)).apply {
associateWithModule(module ?: project?.modules?.firstOrNull(), newProjectPath)
isPoetry = true
// project?.let { project ->