Skip to content

Fix Windows --install-pip crash and enable long path support#8255

Open
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:winbuild
Open

Fix Windows --install-pip crash and enable long path support#8255
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:winbuild

Conversation

@youknowone

@youknowone youknowone commented Jul 11, 2026

Copy link
Copy Markdown
Member

Closes #8246.

On Windows, rustpython --install-pip crashed with OSError: [Errno 22] Invalid argument: 'C:\accesstest_deleteme_fishfingers_custard_...'. Two independent defects combined to cause it, plus a related long-path gap; each is fixed below.

1. sys.prefix fell back to a bare drive root

When landmark search fails to locate the stdlib, default_prefix() returned "C:" on Windows. pip then derived site-packages under that bare drive and, via test_writable_dir, walked up to the drive root and tried to create a probe file there — which standard users cannot write.

Fix: fall back to the executable's own directory, which on Windows is the natural prefix (the stdlib lives directly under <prefix>\Lib).

2. C runtime errno was distorted to EINVAL

os.open (and other CRT calls) round-tripped the C runtime errno through a Win32 error code and back. Any errno missing from the small errno_to_winerror table collapsed to ERROR_INVALID_FUNCTION → EINVAL, and a spurious winerror was attached to the OSError. pip only catches FileExistsError/PermissionError, so a distorted error crashed it.

Fix: carry the raw CRT errno as the io::Error payload so posix_errno reports it exactly and no winerror is attached — matching os.open on Windows. fstat now reports ERROR_INVALID_HANDLE explicitly for an invalid fd.

3. No longPathAware manifest

The Windows binary had no application manifest, so paths longer than MAX_PATH were rejected for length (ENOENT), and a path component over 255 chars did not report EINVAL. The executable now embeds a manifest declaring longPathAware, matching python.exe.

Verification (Windows 11)

  • sys.prefix falls back to the executable directory; pip's test_writable_dir probes a writable location instead of the drive root.
  • errno / winerror for open, read, dup, lseek, fstat match CPython.
  • 260+ char paths with valid components now open and write.
  • cargo test (host_env, vm) and test_os, test_io, test_fileio, test_site, test_sysconfig, test_ntpath, test_pathlib, test_tempfile pass.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Windows file error reporting by preserving the original error details.
    • Corrected invalid file-handle failures to return the appropriate operating system error.
    • Fixed Windows installation path detection when the executable location is known or unavailable.
    • Corrected fallback path handling to use an absolute Windows root path.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 2961097b-f73c-4a06-896f-041c2c27fdcd

📥 Commits

Reviewing files that changed from the base of the PR and between a544b7d and 1789266.

📒 Files selected for processing (4)
  • crates/host_env/src/crt_fd.rs
  • crates/host_env/src/fileutils.rs
  • crates/host_env/src/os.rs
  • crates/vm/src/getpath.rs
🚧 Files skipped from review as they are similar to previous changes (4)
  • crates/host_env/src/fileutils.rs
  • crates/vm/src/getpath.rs
  • crates/host_env/src/crt_fd.rs
  • crates/host_env/src/os.rs

📝 Walkthrough

Walkthrough

Windows CRT errno values are preserved in I/O errors, filesystem failure paths return corrected errors, and executable prefix calculation now uses the detected executable directory with an absolute Windows fallback.

Changes

Windows compatibility updates

Layer / File(s) Summary
Preserve CRT errno values
crates/host_env/src/os.rs
Windows errors can retain the original CRT errno through a private payload, and errno_io_error() uses the new construction helper.
Propagate Windows filesystem errors
crates/host_env/src/crt_fd.rs, crates/host_env/src/fileutils.rs
ftruncate preserves CRT errno values, while fstat explicitly returns the last OS error when handle conversion fails.
Configure Windows runtime paths
crates/vm/src/getpath.rs
default_prefix accepts the executable directory, uses it on Windows when available, and falls back to an absolute root otherwise.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: ShaharNaveh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main Windows pip-install fix and mentions a related Windows improvement.
Linked Issues check ✅ Passed The changes address the Windows --install-pip failure by fixing prefix fallback and errno/handle error handling on Windows.
Out of Scope Changes check ✅ Passed The edits stay focused on Windows path, errno, and file-handle behavior needed for the reported install-pip issue.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

getpath: on Windows, default_prefix falls back to the executable
directory instead of a bare "C:" when landmark search fails.

host_env: errno_io_error carries the raw CRT errno as an io::Error
payload (CrtErrno) instead of translating it to a Win32 error code and
back. The round-trip collapsed every errno outside the errno_to_winerror
table to EINVAL and attached a spurious winerror to the OSError.
posix_errno recovers the exact errno from the payload; ftruncate reuses
the same path. fstat now reports ERROR_INVALID_HANDLE for an invalid fd.
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.

rustpython --install-pip isn't working on Windows 11 25H2.

1 participant