Skip to content

Packaging: Use pip list --format=json for better parsing#1543

Merged
edvilme merged 4 commits into
mainfrom
vscode-python-environments-pip-list-json
May 27, 2026
Merged

Packaging: Use pip list --format=json for better parsing#1543
edvilme merged 4 commits into
mainfrom
vscode-python-environments-pip-list-json

Conversation

@edvilme

@edvilme edvilme commented May 26, 2026

Copy link
Copy Markdown
Contributor

According to the docs, pip list supports a JSON format for output, which is better suited for computer parsing without having to worry about rendering edge cases or building custom parsing logic.

According to [the docs](https://pip.pypa.io/en/stable/cli/pip_list/#cmdoption-format), pip list supports a JSON format for output, which is better suited for computer parsing without having to worry about rendering edge cases or building custom parsing logic.
@edvilme edvilme added the enhancement New feature or request label May 26, 2026
@edvilme edvilme added debt Code quality issues and removed enhancement New feature or request labels May 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the built-in package refresh flow to use pip list --format=json and switches parsing from custom text parsing to JSON parsing, aiming for more reliable machine-readable output.

Changes:

  • Update package listing commands to request JSON output (--format=json) and parse it via a new JSON parser.
  • Replace the previous text-based pip list parser with parsePipListJson() (JSON-based).
  • Update and expand unit tests to cover JSON parsing plus a couple of edge cases.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/managers/builtin/utils.ts Switches package refresh to pip list --format=json and uses the JSON parser (also adds uv args for JSON).
src/managers/builtin/pipListUtils.ts Replaces text parsing with JSON parsing (parsePipListJson).
src/test/managers/builtin/pipListUtils.unit.test.ts Updates unit tests to validate JSON parsing and adds edge-case tests.
src/test/managers/builtin/piplist1.actual.txt Removes legacy text output fixture.
src/test/managers/builtin/piplist2.actual.txt Removes legacy text output fixture.
src/test/managers/builtin/piplist3.actual.txt Removes legacy text output fixture.
Comments suppressed due to low confidence (1)

src/managers/builtin/utils.ts:208

  • runPython() currently concatenates stdout + stderr into the returned string (see helpers.ts), but pip list --format=json needs the returned data to be valid JSON. Any pip notices/warnings written to stderr (common on success) will corrupt the JSON and make parsePipListJson() return [], silently hiding packages. Consider changing this call to use a helper that returns stdout only (while still logging stderr), or adjust runPython() to optionally not append stderr for JSON-producing commands.
    try {
        return await runPython(
            environment.execInfo.run.executable,
            ['-m', 'pip', 'list', '--format=json'],
            undefined,
            log,
            undefined,
            PIP_LIST_TIMEOUT_MS,
        );

Comment thread src/managers/builtin/utils.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@edvilme edvilme merged commit 4a0d852 into main May 27, 2026
200 of 204 checks passed
@edvilme edvilme deleted the vscode-python-environments-pip-list-json branch May 27, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debt Code quality issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants