-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Python installed from Windows Store #2122
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For posterity, in early iterations of this PR I had some code like: /// Given a path, return the "real" path.
///
/// The only modifications applied here are for the Windows Store. Specifically, the Windows Store
/// Python executable is located at a path like:
///
/// `C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbs5n2kfra8p0`.
///
/// However, users actually aren't allowed to run executables from this path directly. So, instead,
/// we need to use a path like:
///
/// `C:\Users\crmar\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbs5n2kfra8p0`
///
/// In practice, this is achieved by replacing `%ProgramFiles%\WindowsApps` with `%LocalAppData%\Microsoft\WindowsApps`.
///
/// See: <https://github.com/python-poetry/poetry/pull/5931>
fn real_path(path: &Path) -> io::Result<PathBuf> {
#[cfg(windows)]
if path.is_absolute() {
if path.components().any(|component| component.as_os_str() == "Microsoft") {
return real_windows_path(path);
}
}
Ok(path.to_path_buf())
}
#[cfg(windows)]
fn real_windows_path(path: &Path) -> io::Result<PathBuf> {
use windows_sys::Win32::UI::Shell::{CSIDL_LOCAL_APPDATA, CSIDL_PROFILE};
let program_files = windows_folder(CSIDL_PROGRAM_FILES)?;
let local_appdata = windows_folder(CSIDL_LOCAL_APPDATA)?;
let suffix = path.strip_prefix(&program_files)?;
Ok(local_appdata.join(suffix))
}
#[cfg(windows)]
fn windows_folder(csidl: u32) -> Option<PathBuf> {
use std::env;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;
use windows_sys::Win32::Foundation::{MAX_PATH, S_OK};
use windows_sys::Win32::UI::Shell::{SHGetFolderPathW};
unsafe {
let mut path: Vec<u16> = Vec::with_capacity(MAX_PATH as usize);
match SHGetFolderPathW(0, csidl as i32, 0, 0, path.as_mut_ptr()) {
S_OK => {
let len = wcslen(path.as_ptr());
path.set_len(len);
let s = OsString::from_wide(&path);
Some(PathBuf::from(s))
}
_ => None,
}
}
}
#[cfg(windows)]
extern "C" {
fn wcslen(buf: *const u16) -> usize;
} But this ended up not being necessary or sufficient, using |
8272a19
to
6b1305c
Compare
1a283ed
to
e27eb43
Compare
6b1305c
to
61b267c
Compare
e27eb43
to
f3b4dc0
Compare
konstin
approved these changes
Mar 3, 2024
f3b4dc0
to
f639e55
Compare
f639e55
to
9d06969
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
After #2121, the only remaining issue is that calling
canonicalize
on these Pythons returns an error.Closes #2105.
Test Plan
Uninstalled all python.org Pythons on my Windows machine, then created a virtualenv. The resulting config file:
Prior to this PR, it would fail with a canonicalization error.
Prior to #2121, it would leave a "bad" Python in the config file:
Which, once activated, would fail with: