Skip to content

Warn When the Windows Host Path Grows Past the Length Explorer Silently Drops #3475

Closed

Description

@ptr727

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_SETTINGCHANGE with Environment, and Explorer rebuilds its environment through shell32 RegenerateUserEnvironment.
  • Past the limit that call still returns success, but the Path it produces holds the machine entries only. Other user variables survive, only Path loses 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 as some-tool init pwsh | Invoke-Expression then 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:Path in 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:

  1. Combined expanded length. Expand the machine and user Path registry values (read with DoNotExpandEnvironmentNames, then expanded), join them, and compare against 4095. Warn above a margin (for example 3800) and report clearly once over.
  2. Explorer actually dropped it. In a child pwsh -NoProfile, set $env:Path to a sentinel, call RegenerateUserEnvironment(out prev, true) from shell32, and check that a user entry such as %LOCALAPPDATA%\Microsoft\WindowsApps is 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.
  3. Duplicates, within machine, within user, and between the two, compared after expansion, case folding, and trimming a trailing \ or \..
  4. Missing directories, an entry whose expanded path does not exist.
  5. Fully shadowed entries, a directory whose every executable (by PATHEXT, plus .ps1) is already found in an earlier Path directory. 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:

  1. Leave the machine Path at its default (roughly 600 characters expanded).
  2. Set the user Path to %LOCALAPPDATA%\Microsoft\WindowsApps followed by enough entries of the form C:\Tools\placeholder-directory-0001 through C:\Tools\placeholder-directory-0120 (each created, so none is missing) that the combined expanded length is about 4150.
  3. Broadcast WM_SETTINGCHANGE (or open and OK the Environment Variables dialog).
  4. Run the step 2 probe above. The regenerated Path holds the machine entries only.
  5. 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.
  6. 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, then RTL_USER_PROCESS_PARAMETERS.Environment at offset 0x80 and EnvironmentSize at 0x3F0 on x64). explorer.exe some-script.cmd is 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementscript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions