Skip to content

Commit 9be0201

Browse files
authored
Fix: Use JSON package listing in conda environment (#1008)
Closes #417
1 parent 8eefdb5 commit 9be0201

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/managers/conda/condaUtils.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,31 +1145,57 @@ export async function deleteCondaEnvironment(environment: PythonEnvironment, log
11451145
);
11461146
}
11471147

1148+
/**
1149+
* JSON structure returned by `conda list --json`
1150+
*/
1151+
interface CondaPackageJson {
1152+
name: string;
1153+
version: string;
1154+
build_string?: string;
1155+
channel?: string;
1156+
}
1157+
1158+
/**
1159+
* Refreshes the list of packages installed in a conda environment.
1160+
* Uses `conda list -p <prefix> --json` for reliable parsing.
1161+
*
1162+
* @param environment The Python environment to get packages for
1163+
* @param api The Python environment API
1164+
* @param manager The package manager instance
1165+
* @returns Promise resolving to an array of Package objects
1166+
*/
11481167
export async function refreshPackages(
11491168
environment: PythonEnvironment,
11501169
api: PythonEnvironmentApi,
11511170
manager: PackageManager,
11521171
): Promise<Package[]> {
1153-
let args = ['list', '-p', environment.environmentPath.fsPath];
1172+
const args = ['list', '-p', environment.environmentPath.fsPath, '--json'];
11541173
const data = await runCondaExecutable(args);
1155-
const content = data.split(/\r?\n/).filter((l) => !l.startsWith('#'));
1174+
1175+
let condaPackages: CondaPackageJson[];
1176+
try {
1177+
condaPackages = JSON.parse(data) as CondaPackageJson[];
1178+
} catch (e) {
1179+
traceError(`Failed to parse conda list JSON output: ${data}`, e);
1180+
return [];
1181+
}
1182+
11561183
const packages: Package[] = [];
1157-
content.forEach((l) => {
1158-
const parts = l.split(' ').filter((p) => p.length > 0);
1159-
if (parts.length >= 3) {
1184+
for (const condaPkg of condaPackages) {
1185+
if (condaPkg.name && condaPkg.version) {
11601186
const pkg = api.createPackageItem(
11611187
{
1162-
name: parts[0],
1163-
displayName: parts[0],
1164-
version: parts[1],
1165-
description: parts[1],
1188+
name: condaPkg.name,
1189+
displayName: condaPkg.name,
1190+
version: condaPkg.version,
1191+
description: condaPkg.version,
11661192
},
11671193
environment,
11681194
manager,
11691195
);
11701196
packages.push(pkg);
11711197
}
1172-
});
1198+
}
11731199
return packages;
11741200
}
11751201

0 commit comments

Comments
 (0)