1. Description of problem

Some operations involving searching $PATH are extremely slow. This could include very slow shell command execution, tab completion, and rendering of certain themes in ohmyzsh and oh-my-bash.

The $PATH environment variable is very long in said scenarios:

$ echo $PATH
/home/mantle/.local/bin:/home/mantle/.sdkman/candidates/gradle/current/bin:/home/mantle/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1/libnvvp:/mnt/d/Program Files/Python311/Scripts/:/mnt/d/Program Files/Python311/: [SNIP] :/mnt/c/ProgramData/mingw64/mingw64/bin:/mnt/c/Users/Mantle/AppData/Roaming/npm:/mnt/d/bdist/floss-v3.1.0-windows:/mnt/c/Users/Mantle/.dotnet/tools:/home/mantle/.dotnet/tools
$ echo $PATH | wc -m -
3058 -

2. Solution

Open /etc/wsl.conf and append these lines:

[interop]
appendWindowsPath=false

Then restart WSL.

> wsl.exe --shutdown

Observe the updated $PATH.

$ echo $PATH
/home/mantle/.local/bin:/home/mantle/.sdkman/candidates/gradle/current/bin:/home/mantle/dist/verible-v0.0-3638-ge3ef2a37/bin:/home/mantle/.cargo/bin:/usr/lib/ccache:/home/mantle/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/home/mantle/.dotnet/tools
$ echo $PATH | wc -m -
395 -

3. Discussion

The solution is first found in microsoft/WSL#1493, and it initially there is an obscure registry key that controlled this behavior. Since Windows Build 17713, Microsoft added a entry in wsl.conf that allows convenient control. Since iterating all direct descendent of all $PATH entries is a costly operation, especially for mounted host drives, disabling this behavior can significantly speedup related operations.