Repository navigation
Description
Problem
On Windows, Explorer silently drops the entire user Path once the combined machine and user Path, after variable expansion, grows past about 4095 characters. Nothing reports an error, and the host looks healthy from most places a maintainer would check.
The mechanism:
- Every time an installer changes an environment variable it broadcasts
WM_SETTINGCHANGEwithEnvironment, and Explorer rebuilds its environment through shell32RegenerateUserEnvironment. - Past the limit that call still returns success, but the
Pathit produces holds the machine entries only. Other user variables survive, onlyPathloses its user half. - Every process launched from the Start menu, the taskbar, or a shortcut inherits Explorer's environment, so an editor such as VS Code, and every terminal it opens, loses every per-user tool directory (
%LOCALAPPDATA%\Microsoft\WindowsApps,%LOCALAPPDATA%\Microsoft\WinGet\Links, per-user installs). A profile line such assome-tool init pwsh | Invoke-Expressionthen fails with "The term 'some-tool' is not recognized". - A fresh logon and each new Windows Terminal tab build the environment through
CreateEnvironmentBlock, which has no such limit, so$env:Pathin a terminal looks correct and the failure looks like an editor bug. Closing every editor instance changes nothing, because the stale copy lives in Explorer. - The limit applies to the expanded length. Rewriting entries with
%LOCALAPPDATA%shorthands shrinks the stored value but not the expanded one, and does not help.
A host drifts into this one install at a time, because winget portable packages, editor shims, and language toolchains each append a long directory to the user Path. The step that crosses the limit is an ordinary install, often one the fleet's own install-tools.ps1 or upgrade-host.ps1 performs, which is the moment worth warning at.
Proposed Enhancement
Add a read-only Path health check to the Windows host scripts, run by install-tools.ps1 (in -Report and after any install or upgrade) and by upgrade-host.ps1 -Status and after an upgrade. It warns and never edits Path itself, since which entry to drop is the maintainer's call.
Checks, each a warning line in the report and a field in -Json:
- Combined expanded length. Expand the machine and user
Pathregistry values (read withDoNotExpandEnvironmentNames, then expanded), join them, and compare against 4095. Warn above a margin (for example 3800) and report clearly once over. - Explorer actually dropped it. In a child
pwsh -NoProfile, set$env:Pathto a sentinel, callRegenerateUserEnvironment(out prev, true)from shell32, and check that a user entry such as%LOCALAPPDATA%\Microsoft\WindowsAppsis present in the result. This reproduces Explorer's own rebuild exactly, so it detects the failure directly rather than inferring it from a length threshold that may vary between Windows builds. - Duplicates, within machine, within user, and between the two, compared after expansion, case folding, and trimming a trailing
\or\.. - Missing directories, an entry whose expanded path does not exist.
- Fully shadowed entries, a directory whose every executable (by
PATHEXT, plus.ps1) is already found in an earlierPathdirectory. Removing one changes no command resolution, so it is the safe first candidate to suggest when the length check fires.
Remedy text in the report should name the shadowed and missing entries as the candidates, say that a refresh is needed afterwards (sign out and back in, or broadcast WM_SETTINGCHANGE, then restart the editor), and say that shortening entries with environment variables does not help.
Reproduction With Constructed Data
On a disposable account or VM:
- Leave the machine
Pathat its default (roughly 600 characters expanded). - Set the user
Pathto%LOCALAPPDATA%\Microsoft\WindowsAppsfollowed by enough entries of the formC:\Tools\placeholder-directory-0001throughC:\Tools\placeholder-directory-0120(each created, so none is missing) that the combined expanded length is about 4150. - Broadcast
WM_SETTINGCHANGE(or open and OK the Environment Variables dialog). - Run the step 2 probe above. The regenerated
Pathholds the machine entries only. - Launch any editor from the Start menu and open its terminal. Per-user commands are not found, while a new Windows Terminal tab finds them.
- Remove entries until the combined length is about 3700 and broadcast again. The probe now returns the full
Path, and Explorer's copy recovers without a sign out.
Notes
- Reading Explorer's live environment for a diagnostic needs the process environment block (
NtQueryInformationProcess, thenRTL_USER_PROCESS_PARAMETERS.Environmentat offset0x80andEnvironmentSizeat0x3F0on x64).explorer.exe some-script.cmdis not a usable probe, because the launch is handed to a fresh helper Explorer process whose environment is rebuilt correctly, so it reports healthy while the shell process is not. The step 2 probe avoids needing either. - The Linux scripts have no equivalent failure, so this is Windows only.
Activity
Reword the Path Warning and Its README Note So Each Reads Plainly
Warn When the Windows Host Path Nears the Length Explorer Drops (#3863)
Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 (#…