Skip to content

Read PSModulePath under the key Windows actually exports - #8304

Merged
danielhanchen merged 2 commits into
mainfrom
fix-windows-psmodulepath-key-case
Aug 10, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-windows-psmodulepath-key-case

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

parity (windows-latest) has been red on every branch since #8161 merged, with five failures in tests/test_installer_profile_hardening.py:

assert entries[1:] == _PS7_MODULE_PATH.split(";")[:2]
E   AssertionError: assert [] == ['C:\\Users\\...\\7\\Modules']

assert studio_cmd._profile_probe_env("powershell.exe")["PSModulePath"] == _PS7_MODULE_PATH
E   KeyError: 'PSModulePath'

Cause

os.environ upper-cases keys on Windows (docs: "On Windows, the keys are converted to uppercase. This also applies when getting, setting, or deleting an item"). os.environ itself stays case-insensitive because it uppercases on both ends, but dict(os.environ) is a plain dict holding PSMODULEPATH, and a plain dict is not.

So in _profile_probe_env:

reordered = _windows_powershell_module_path(env.get("PSModulePath", ""))
if reordered:
    env["PSModulePath"] = reordered
  1. the get always returned the empty default, never the caller's module path;
  2. _windows_powershell_module_path("") therefore returned just %SystemRoot%\System32\WindowsPowerShell\v1.0\Modules;
  3. the assignment added a second key next to the untouched PSMODULEPATH.

That is the "reordered, not pruned" invariant broken in production, not only in the tests: the probe child was handed two entries differing only by case, and the surviving value had the caller's PowerShell 7 module directories dropped rather than demoted. parity (ubuntu-latest) stayed green because POSIX os.environ preserves case.

test_the_module_path_is_untouched_off_windows pins the mechanism down: it forces platform.system() to Linux, so _profile_probe_env returns dict(os.environ) unmodified, and the lookup still raises KeyError. The key is PSMODULEPATH.

Fix

Resolve the key case-insensitively and update it in place, so nothing is added:

key = next((k for k in env if k.upper() == "PSMODULEPATH"), "PSModulePath")
reordered = _windows_powershell_module_path(env.get(key, ""))
if reordered:
    env[key] = reordered

The five existing assertions read the same key out of a plain dict, so they go through a small _module_path(env) helper that does the same lookup.

Tests

Added test_the_probe_reads_the_module_path_windows_actually_exports, which sets only the uppercase key so it reproduces the defect on any platform. Against the old code it fails exactly as Windows CI does:

assert [k for k in env if k.upper() == "PSMODULEPATH"] == ["PSMODULEPATH"]
E   AssertionError: assert ['PSMODULEPAT...PSModulePath'] == ['PSMODULEPATH']

tests/test_installer_profile_hardening.py: 75 passed. Ruff clean on both files.

os.environ upper-cases keys on Windows, so dict(os.environ) carries PSMODULEPATH
and a plain dict is case-sensitive. _profile_probe_env read "PSModulePath",
always got the empty default, and wrote back a second key differing only by case,
so the probe child lost the caller's module entries instead of having them
reordered behind Windows PowerShell's own. Resolve the key case-insensitively and
update it in place.

This is what has failed parity (windows-latest) on every branch since #8161:
the five PSModulePath tests read the same missing key out of the returned dict.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 24c43b0f7d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit c82836d into main Aug 10, 2026
89 checks passed
@danielhanchen
danielhanchen deleted the fix-windows-psmodulepath-key-case branch August 10, 2026 06:27
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.

1 participant