Skip to content

Commit e5ab4b5

Browse files
committed
Expand PDM's ~ from USERPROFILE on Windows
PDM expands a `~` in global_project.path or python.install_root with Python's expanduser, which uses USERPROFILE on Windows and ignores HOME. Under Git Bash, HOME is an MSYS path, so the relocated global project or interpreter root was looked for in the wrong place. Assisted-by: Claude Code:claude-opus-5-5
1 parent 1a82a49 commit e5ab4b5

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎crates/socket-patch-core/src/crawlers/python_crawler.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,15 @@ async fn pdm_global_site_packages(home_dir: &Path) -> Vec<PathBuf> {
17031703
let mut projects = Vec::new();
17041704
let mut install_roots = Vec::new();
17051705
let mut venv_roots = Vec::new();
1706-
let var = |name: &str| std::env::var(name).ok();
1706+
// PDM expands `~` with Python's `expanduser`, which reads USERPROFILE
1707+
// on Windows and ignores HOME there (Python 3.8+), so a Git Bash HOME
1708+
// must not decide where a relocated setting points.
1709+
let var = |name: &str| {
1710+
if cfg!(windows) && name == "HOME" {
1711+
return None;
1712+
}
1713+
std::env::var(name).ok()
1714+
};
17071715
for file in &config_files {
17081716
let Ok(text) = read_regular_to_string(file).await else {
17091717
continue;

‎crates/socket-patch-core/tests/crawler_python_e2e.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,17 @@ async fn get_global_python_site_packages_discovers_pdm_managed_interpreters() {
928928

929929
/// `global_project.path` and `python.install_root` in PDM's global
930930
/// config (`$PDM_CONFIG_FILE` here) relocate both, with `~` expanded the
931-
/// way PDM's `expanduser` does (#451).
931+
/// way PDM's `expanduser` does: from USERPROFILE on Windows, where a Git
932+
/// Bash HOME is ignored, and from HOME elsewhere (#451).
932933
#[tokio::test]
933934
#[serial]
934935
async fn get_global_python_site_packages_follows_pdm_config_overrides() {
935936
let tmp = tempfile::tempdir().unwrap();
937+
let home = if cfg!(windows) {
938+
tmp.path().join("msys-home")
939+
} else {
940+
tmp.path().to_path_buf()
941+
};
936942
let config_file = tmp.path().join("pdm-config.toml");
937943
tokio::fs::write(
938944
&config_file,
@@ -943,8 +949,11 @@ async fn get_global_python_site_packages_follows_pdm_config_overrides() {
943949
let project_sp = stage_env(&tmp.path().join("gp").join(".venv")).await;
944950
let python_sp = stage_env(&tmp.path().join("pyroot").join("cpython@3.12.14")).await;
945951

946-
let result =
947-
global_site_packages_with_vars(tmp.path(), &[("PDM_CONFIG_FILE", &config_file)]).await;
952+
let mut vars: Vec<(&str, &Path)> = vec![("PDM_CONFIG_FILE", &config_file)];
953+
if cfg!(windows) {
954+
vars.push(("USERPROFILE", tmp.path()));
955+
}
956+
let result = global_site_packages_with_vars(&home, &vars).await;
948957
assert_surfaces(
949958
&result,
950959
&project_sp,

0 commit comments

Comments
 (0)