PY-80388: Exclude conda from system pythons.

What happened: `WinPythonSdkFlavor` returned all pythons from the `Path` environment variable.
Conda might be installed there.

How did we fix it: all flavors now report paths using protected functions. The parent class then filters results to drop pythons with incorrect flavor.

See `com.jetbrains.python.sdk.flavors.PythonSdkFlavor.suggestLocalHomePaths`.

GitOrigin-RevId: 7f2ef52a427ac07a625305c8371d985e2a7e0575
This commit is contained in:
Ilya.Kazakevich
2025-04-16 23:49:14 +02:00
committed by intellij-monorepo-bot
parent 9dd749c19b
commit 2d1ff1fec7
6 changed files with 42 additions and 24 deletions

View File

@@ -17,7 +17,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PatternUtil;
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.icons.PythonPsiApiIcons;
import com.jetbrains.python.run.CommandLinePatcher;
@@ -33,10 +32,11 @@ import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.jetbrains.python.venvReader.ResolveUtilKt.tryResolvePath;
import static com.jetbrains.python.sdk.flavors.PySdkFlavorUtilKt.getFileExecutionError;
import static com.jetbrains.python.sdk.flavors.PySdkFlavorUtilKt.getFileExecutionErrorOnEdt;
import static com.jetbrains.python.venvReader.ResolveUtilKt.tryResolvePath;
/**
@@ -97,21 +97,31 @@ public abstract class PythonSdkFlavor<D extends PyFlavorData> {
}
/**
* On local targets some flavours could be detected. It returns path to python interpreters for such cases.
* Flavors that are aware of some system pythons must return them there.
*/
@RequiresBackgroundThread
public @NotNull Collection<@NotNull Path> suggestLocalHomePaths(final @Nullable Module module, final @Nullable UserDataHolder context) {
return ContainerUtil.map(suggestHomePaths(module, context), Path::of);
}
/**
* @deprecated use {@link #suggestLocalHomePaths(Module, UserDataHolder)}
*/
@Deprecated(forRemoval = true)
public Collection<String> suggestHomePaths(final @Nullable Module module, final @Nullable UserDataHolder context) {
protected @NotNull Collection<@NotNull Path> suggestLocalHomePathsImpl(final @Nullable Module module,
final @Nullable UserDataHolder context) {
return Collections.emptyList();
}
/**
* On local targets some flavors could be detected. It returns a path to python interpreters for such cases.
*/
@RequiresBackgroundThread
public final @NotNull Collection<@NotNull Path> suggestLocalHomePaths(final @Nullable Module module,
final @Nullable UserDataHolder context) {
return suggestLocalHomePathsImpl(module, context).stream().filter(path -> {
var flavor = tryDetectFlavorByLocalPath(path.toString());
boolean correctFlavor = flavor != null && flavor.getClass().equals(getClass());
// Some flavors might report foreign pythons: i.e Windows might find conda on PATH.
if (!correctFlavor) {
LOG.info(String.format("Path %s has a wrong flavor, not %s, skipping", path, this));
return false;
}
return true;
}).collect(Collectors.toSet()).stream().sorted().toList();
}
/**
* Flavor is added to result in {@link #getApplicableFlavors()} if this method returns true.
@@ -275,7 +285,7 @@ public abstract class PythonSdkFlavor<D extends PyFlavorData> {
}
/**
* Detects {@link PythonSdkFlavor} for local python path
* Detects {@link PythonSdkFlavor} for a local python path
*/
@RequiresBackgroundThread(generateAssertion = false) //No warning yet as there are usages: to be fixed
public static @Nullable PythonSdkFlavor<?> tryDetectFlavorByLocalPath(@NotNull String sdkPath) {