Skip to content

Commit 75f8065

Browse files
committed
Replace parsePipList with parsePipListJson for better integration
1 parent 8fc6694 commit 75f8065

5 files changed

Lines changed: 47 additions & 34 deletions

File tree

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ export interface Package extends PackageInfo {
582582
* The ID of the package.
583583
*/
584584
readonly pkgId: PackageId;
585+
586+
/**
587+
* Indicates whether the package is a transitive dependency (i.e., installed as a dependency of another package rather than directly by the user).
588+
*/
589+
isTransitive?: boolean;
585590
}
586591

587592
/**

src/managers/builtin/pipListUtils.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,22 @@ export function isValidVersion(version: string): boolean {
99
version,
1010
);
1111
}
12-
export function parsePipList(data: string): PipPackage[] {
13-
const collection: PipPackage[] = [];
1412

15-
const lines = data.split('\n').splice(2);
16-
for (let line of lines) {
17-
if (line.trim() === '' || line.startsWith('Package') || line.startsWith('----') || line.startsWith('[')) {
18-
continue;
19-
}
20-
const parts = line.split(' ').filter((e) => e);
21-
if (parts.length === 2) {
22-
const name = parts[0].trim();
23-
const version = parts[1].trim();
24-
if (!isValidVersion(version)) {
25-
continue;
26-
}
27-
const pkg = {
28-
name,
29-
version,
30-
displayName: name,
31-
description: version,
32-
};
33-
collection.push(pkg);
13+
export function parsePipListJson(data: string): PipPackage[] {
14+
try {
15+
const json = JSON.parse(data);
16+
if (Array.isArray(json)) {
17+
return json
18+
.filter((item) => typeof item.name === 'string' && typeof item.version === 'string')
19+
.map(({ name, version }) => ({
20+
name,
21+
version,
22+
displayName: name,
23+
description: version,
24+
}));
3425
}
26+
} catch {
27+
// If JSON parsing fails, return an empty array
3528
}
36-
return collection;
29+
return [];
3730
}

src/managers/builtin/utils.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '../common/nativePythonFinder';
2424
import { shortVersion, sortEnvironments } from '../common/utils';
2525
import { runPython, runUV, shouldUseUv } from './helpers';
26-
import { parsePipList, PipPackage } from './pipListUtils';
26+
import { parsePipListJson, PipPackage } from './pipListUtils';
2727

2828
const PIXI_EXTENSION_ID = 'renan-r-santos.pixi-code';
2929
const PIXI_RECOMMEND_DONT_ASK_KEY = 'pixi-extension-recommend-dont-ask';
@@ -190,7 +190,7 @@ async function refreshPipPackagesRaw(environment: PythonEnvironment, log?: LogOu
190190
const useUv = await shouldUseUv(log, environment.environmentPath.fsPath);
191191
if (useUv) {
192192
return await runUV(
193-
['pip', 'list', '--python', environment.execInfo.run.executable],
193+
['pip', 'list', '--python', environment.execInfo.run.executable, '--format=json'],
194194
undefined,
195195
log,
196196
undefined,
@@ -200,7 +200,7 @@ async function refreshPipPackagesRaw(environment: PythonEnvironment, log?: LogOu
200200
try {
201201
return await runPython(
202202
environment.execInfo.run.executable,
203-
['-m', 'pip', 'list'],
203+
['-m', 'pip', 'list', '--format=json'],
204204
undefined,
205205
log,
206206
undefined,
@@ -222,20 +222,19 @@ export async function refreshPipPackages(
222222
): Promise<PipPackage[] | undefined> {
223223
let data: string;
224224
try {
225+
const loadPackages = async () => refreshPipPackagesRaw(environment, log);
225226
if (options?.showProgress) {
226227
data = await withProgress(
227228
{
228229
location: ProgressLocation.Notification,
229230
},
230-
async () => {
231-
return await refreshPipPackagesRaw(environment, log);
232-
},
231+
loadPackages,
233232
);
234233
} else {
235-
data = await refreshPipPackagesRaw(environment, log);
234+
data = await loadPackages();
236235
}
237236

238-
return parsePipList(data);
237+
return parsePipListJson(data);
239238
} catch (e) {
240239
log?.error('Error refreshing packages', e);
241240
showErrorMessageWithLogs(SysManagerStrings.packageRefreshError, log);

src/test/managers/builtin/pipListUtils.unit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'assert';
22
import * as fs from 'fs-extra';
33
import * as path from 'path';
4-
import { parsePipList } from '../../../managers/builtin/pipListUtils';
4+
import { parsePipListJson } from '../../../managers/builtin/pipListUtils';
55
import { EXTENSION_TEST_ROOT } from '../../constants';
66

77
const TEST_DATA_ROOT = path.join(EXTENSION_TEST_ROOT, 'managers', 'builtin');
@@ -16,7 +16,7 @@ suite('Pip List Parser tests', () => {
1616
await fs.readFile(path.join(TEST_DATA_ROOT, `${testName}.expected.json`), 'utf8'),
1717
);
1818

19-
const actualPackages = parsePipList(pipListOutput);
19+
const actualPackages = parsePipListJson(pipListOutput);
2020

2121
assert.equal(actualPackages.length, expected.packages.length, 'Unexpected number of packages');
2222
actualPackages.forEach((actualPackage) => {

0 commit comments

Comments
 (0)