Skip to content

Commit c6025b3

Browse files
committed
enumerate system-wide conda environments on Windows in addition to user-scoped ones
#5
1 parent fe18a7e commit c6025b3

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/CondaEnvironment.cs

+25-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public sealed class CondaEnvironment: PythonEnvironment {
5555
CancellationToken cancellation = default)
5656
=> GetEnvironmentRoots(cancellation)
5757
.SelectMany(home => {
58+
cancellation.ThrowIfCancellationRequested();
5859
var environment = DetectCondaEnvironment(home, cancellation);
5960
return environment is null
6061
? Array.Empty<CondaEnvironment>()
@@ -64,14 +65,23 @@ public sealed class CondaEnvironment: PythonEnvironment {
6465
static IEnumerable<DirectoryInfo> GetEnvironmentRoots(CancellationToken cancellation) {
6566
string userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
6667
string condaEnvironmentsFilePath = Path.Combine(userHome, ".conda", "environments.txt");
67-
if (!File.Exists(condaEnvironmentsFilePath))
68-
yield break;
68+
if (File.Exists(condaEnvironmentsFilePath)) {
69+
foreach (var potentialRoot in ReadEnvironmentRootsFile(condaEnvironmentsFilePath))
70+
yield return potentialRoot;
71+
}
6972

7073
cancellation.ThrowIfCancellationRequested();
7174

75+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
76+
foreach (var potentialRoot in GetSystemWideEnvironmentRoots(cancellation))
77+
yield return potentialRoot;
78+
}
79+
}
80+
81+
static IEnumerable<DirectoryInfo> ReadEnvironmentRootsFile(string filePath) {
7282
string[] pythonHomes;
7383
try {
74-
pythonHomes = File.ReadAllLines(condaEnvironmentsFilePath);
84+
pythonHomes = File.ReadAllLines(filePath);
7585
}
7686
catch (FileNotFoundException) { yield break; }
7787
catch (DirectoryNotFoundException) { yield break; }
@@ -84,6 +94,18 @@ static IEnumerable<DirectoryInfo> GetEnvironmentRoots(CancellationToken cancella
8494
}
8595
}
8696

97+
static IEnumerable<DirectoryInfo> GetSystemWideEnvironmentRoots(CancellationToken cancellation) {
98+
string systemWideEnvironmentsPath = Path.Combine(
99+
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
100+
"Anaconda3",
101+
"envs"
102+
);
103+
var systemWideEnvironmentsDir = new DirectoryInfo(systemWideEnvironmentsPath);
104+
return systemWideEnvironmentsDir.Exists
105+
? systemWideEnvironmentsDir.EnumerateDirectories()
106+
: Array.Empty<DirectoryInfo>();
107+
}
108+
87109
CondaEnvironment(FileInfo interpreterPath, DirectoryInfo home, FileInfo? dll, Version? languageVersion, Architecture? architecture) : base(interpreterPath, home, dll, languageVersion, architecture) { }
88110
}
89111
}

0 commit comments

Comments
 (0)