PY-78762: Activate conda in Powershell.

We used to use `Invoke-Expression` but then migrated to `&`.

However, a conda activation script is a command (code block), not a file, so we need to use `Invoke-Expression` as '&' doesn't support it.

We check if a file exists, and if it does -- we use '&' which is safe and fast. We use `Invoke-Expression` otherwise.


Merge-request: IJ-MR-158505
Merged-by: Ilya Kazakevich <ilya.kazakevich@jetbrains.com>

GitOrigin-RevId: ea0772b3f5f9641a85b542903c44c3b78aed0715
This commit is contained in:
Ilya Kazakevich
2025-03-28 14:51:58 +00:00
committed by intellij-monorepo-bot
parent a6ff3d6fbc
commit ca46fea6b0
5 changed files with 85 additions and 13 deletions
@@ -13,5 +13,7 @@
<orderEntry type="module" module-name="intellij.python.community.testFramework.testEnv.conda" scope="TEST" />
<orderEntry type="library" scope="TEST" name="JUnit5" level="project" />
<orderEntry type="library" scope="TEST" name="jetbrains-annotations" level="project" />
<orderEntry type="module" module-name="intellij.platform.execution" scope="TEST" />
<orderEntry type="module" module-name="intellij.platform.core" scope="TEST" />
</component>
</module>
@@ -0,0 +1,30 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.python.community.junit5Tests.framework.conda
import com.intellij.execution.processTools.getResultStdoutStr
import com.jetbrains.python.psi.LanguageLevel
import com.jetbrains.python.sdk.flavors.conda.NewCondaEnvRequest
import com.jetbrains.python.sdk.flavors.conda.PyCondaCommand
import com.jetbrains.python.sdk.flavors.conda.PyCondaEnv
import com.jetbrains.python.sdk.flavors.conda.PyCondaEnvIdentity
import org.jetbrains.annotations.ApiStatus
import java.nio.file.Path
import kotlin.io.path.pathString
@ApiStatus.Internal
/**
* Create conda env in [pathToCreateNewEnvIn] using [existingEnv] as a base
*/
suspend fun createCondaEnv(
existingEnv: PyCondaEnv,
pathToCreateNewEnvIn: Path,
): PyCondaEnv {
val process = PyCondaEnv.createEnv(
PyCondaCommand(existingEnv.fullCondaPathOnTarget, null),
NewCondaEnvRequest.EmptyUnnamedEnv(LanguageLevel.PYTHON311, pathToCreateNewEnvIn.pathString)
).getOrThrow()
process.getResultStdoutStr().getOrThrow()
val env = PyCondaEnv(PyCondaEnvIdentity.UnnamedEnv(pathToCreateNewEnvIn.pathString, false), existingEnv.fullCondaPathOnTarget)
return env
}