diff --git a/python/python-features-trainer/src/com/intellij/python/featuresTrainer/ift/PythonBasedLangSupport.kt b/python/python-features-trainer/src/com/intellij/python/featuresTrainer/ift/PythonBasedLangSupport.kt index 105a2b2a3825..abd664eb475f 100644 --- a/python/python-features-trainer/src/com/intellij/python/featuresTrainer/ift/PythonBasedLangSupport.kt +++ b/python/python-features-trainer/src/com/intellij/python/featuresTrainer/ift/PythonBasedLangSupport.kt @@ -95,8 +95,8 @@ abstract class PythonBasedLangSupport : AbstractLangSupport() { existingSdks: List, 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) } } diff --git a/python/src/com/jetbrains/python/sdk/PySdkExt.kt b/python/src/com/jetbrains/python/sdk/PySdkExt.kt index 041d7ad34e40..bd0d708e6f1b 100644 --- a/python/src/com/jetbrains/python/sdk/PySdkExt.kt +++ b/python/src/com/jetbrains/python/sdk/PySdkExt.kt @@ -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, diff --git a/python/src/com/jetbrains/python/sdk/add/PyAddNewCondaEnvPanel.kt b/python/src/com/jetbrains/python/sdk/add/PyAddNewCondaEnvPanel.kt index 1839818bb617..ac5b0fbbe782 100644 --- a/python/src/com/jetbrains/python/sdk/add/PyAddNewCondaEnvPanel.kt +++ b/python/src/com/jetbrains/python/sdk/add/PyAddNewCondaEnvPanel.kt @@ -107,7 +107,7 @@ open class PyAddNewCondaEnvPanel( override fun validateAll(): List = emptyList() // Pre target validation is not supported - override fun getOrCreateSdk(): Sdk? { + override fun getOrCreateSdk(): Sdk { val condaPath = condaPathField.text val task = object : Task.WithResult(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) } diff --git a/python/src/com/jetbrains/python/sdk/add/PyAddNewVirtualEnvPanel.kt b/python/src/com/jetbrains/python/sdk/add/PyAddNewVirtualEnvPanel.kt index 3749d2bba62d..d029d90ed65b 100644 --- a/python/src/com/jetbrains/python/sdk/add/PyAddNewVirtualEnvPanel.kt +++ b/python/src/com/jetbrains/python/sdk/add/PyAddNewVirtualEnvPanel.kt @@ -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? { diff --git a/python/src/com/jetbrains/python/sdk/add/target/PyAddVirtualEnvPanel.kt b/python/src/com/jetbrains/python/sdk/add/target/PyAddVirtualEnvPanel.kt index 996971a3a992..b9ab2e4ed610 100644 --- a/python/src/com/jetbrains/python/sdk/add/target/PyAddVirtualEnvPanel.kt +++ b/python/src/com/jetbrains/python/sdk/add/target/PyAddVirtualEnvPanel.kt @@ -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) } diff --git a/python/src/com/jetbrains/python/sdk/add/v2/PythonAddInterpreterPresenter.kt b/python/src/com/jetbrains/python/sdk/add/v2/PythonAddInterpreterPresenter.kt index dc9b9d322586..fcfce2067490 100644 --- a/python/src/com/jetbrains/python/sdk/add/v2/PythonAddInterpreterPresenter.kt +++ b/python/src/com/jetbrains/python/sdk/add/v2/PythonAddInterpreterPresenter.kt @@ -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 } diff --git a/python/src/com/jetbrains/python/sdk/configuration/PyProjectVirtualEnvConfiguration.kt b/python/src/com/jetbrains/python/sdk/configuration/PyProjectVirtualEnvConfiguration.kt index 245c25414388..43fb50761025 100644 --- a/python/src/com/jetbrains/python/sdk/configuration/PyProjectVirtualEnvConfiguration.kt +++ b/python/src/com/jetbrains/python/sdk/configuration/PyProjectVirtualEnvConfiguration.kt @@ -46,10 +46,10 @@ fun createVirtualEnvSynchronously( inheritSitePackages: Boolean = false, makeShared: Boolean = false, targetPanelExtension: TargetPanelExtension? = null, -): Result { +): 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? { diff --git a/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt b/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt index 3978cf553f6d..f6fad6056e93 100644 --- a/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt +++ b/python/src/com/jetbrains/python/sdk/pipenv/pipenv.kt @@ -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 } diff --git a/python/src/com/jetbrains/python/sdk/poetry/poetry.kt b/python/src/com/jetbrains/python/sdk/poetry/poetry.kt index cfe801a8fc46..c54153d1f046 100644 --- a/python/src/com/jetbrains/python/sdk/poetry/poetry.kt +++ b/python/src/com/jetbrains/python/sdk/poetry/poetry.kt @@ -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 ->