Skip to content

Check pip availability before running ensurepip#392

Open
priyagupta108 wants to merge 1 commit into
actions:mainfrom
priyagupta108:skip-ensurepip
Open

Check pip availability before running ensurepip#392
priyagupta108 wants to merge 1 commit into
actions:mainfrom
priyagupta108:skip-ensurepip

Conversation

@priyagupta108
Copy link
Copy Markdown
Contributor

@priyagupta108 priyagupta108 commented May 8, 2026

This pull request improves the robustness of the Python installation scripts across macOS, Linux, and Windows by ensuring that pip is only bootstrapped with ensurepip if it is not already present. This prevents unnecessary reinstallation and potential errors during setup.

Related issues:
actions/setup-python#1217
actions/setup-python#1295

Copilot AI review requested due to automatic review settings May 8, 2026 07:59
@priyagupta108 priyagupta108 requested a review from a team as a code owner May 8, 2026 07:59
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request improves the robustness of the Python toolcache setup templates by only running ensurepip when pip is not already importable for the target interpreter, avoiding redundant bootstrapping steps across Windows, Linux, and macOS.

Changes:

  • Windows: check import pip before invoking python -m ensurepip.
  • Linux/macOS: gate ./python -m ensurepip behind an import pip check (with stderr suppressed).
  • Keep the existing pip install --upgrade --force-reinstall pip ... upgrade flow unchanged.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
installers/win-setup-template.ps1 Adds a pip import check before running ensurepip, then upgrades/reinstalls pip as before.
installers/nix-setup-template.sh Runs ensurepip only when pip isn’t importable, then performs the same pip upgrade/reinstall.
installers/macos-pkg-setup-template.sh Mirrors the nix template change: conditionally run ensurepip based on import pip, then upgrade/reinstall.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@priyagupta108 priyagupta108 self-assigned this May 8, 2026
@xgboosted
Copy link
Copy Markdown

Review comment for installers/win-setup-template.ps1

Line 143–145 — ensurepip exit code not checked

Original code ran ensurepip and pip install in a single cmd.exe /c "... && ..." chain,
so ensurepip failure would short-circuit pip install and $LASTEXITCODE would reflect the failure.

New code splits them into separate invocations without checking ensurepip's result:

cmd.exe /c "$PythonExePath -c ""import pip"""
if ($LASTEXITCODE -ne 0) {
    cmd.exe /c "$PythonExePath -m ensurepip"
    # ← no check here; ensurepip failure is silently swallowed
}
cmd.exe /c "$PythonExePath -m pip install --upgrade --force-reinstall pip --no-warn-script-location"
if ($LASTEXITCODE -ne 0) {
    Throw "Error happened during pip installation / upgrade"
}

If ensurepip fails, execution continues to pip install, which will also fail and
trigger the Throw — so the end result is the same. But the error message will blame
pip install rather than ensurepip, making diagnosis harder.

Suggested fix:

cmd.exe /c "$PythonExePath -c ""import pip"""
if ($LASTEXITCODE -ne 0) {
    cmd.exe /c "$PythonExePath -m ensurepip"
    if ($LASTEXITCODE -ne 0) {
        Throw "Error happened during ensurepip bootstrap"
    }
}
cmd.exe /c "$PythonExePath -m pip install --upgrade --force-reinstall pip --no-warn-script-location"
if ($LASTEXITCODE -ne 0) {
    Throw "Error happened during pip installation / upgrade"
}

Not a blocker — happy path unaffected. Worth fixing for debuggability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants