Repository: microsoft/terminal (language: cpp)

Issue:
PATH not getting constructed correctly
### Windows Terminal version

1.18.1421.0

### Windows build number

10.0.22621.0

### Other Software

_No response_

### Steps to reproduce

If I launch the new preview terminal, it appears to not be expanding my PATH correctly. This causes apps I regularly use to not be launchable from my terminal.


### Expected Behavior

Notice how the bolded items are properly expanded by the release version of terminal:

1.16.10262.0:
C:\Program Files\PowerShell\7;C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Python310\Scripts\;C:\Python310\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\Go\bin;C:\Program Files (x86)\dotnet-core-uninstall\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\Modules\TShell\TShell\;C:\Program Files\PowerShell\7\;C:\Program Files\Git\cmd;**C:\Users\sotteson\AppData\Local\Microsoft\WindowsApps;C:\Users\sotteson\.dotnet\tools**;C:\Users\sotteson\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\sotteson\AppData\Roaming\npm;D:\data\steve\OneDrive\bin;C:\Users\sotteson\go\bin;C:\Program Files (x86)\GnuWin32\bin;C:\msys64\mingw64\bin

### Actual Behavior

The bolded items are not properly expanded.

1.18.1421.0:
C:\Program Files\PowerShell\7;C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Python310\Scripts\;C:\Python310\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\Go\bin;C:\Program Files (x86)\dotnet-core-uninstall\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\Modules\TShell\TShell\;C:\Program Files\PowerShell\7\;C:\Program Files\Git\cmd;**%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;%USERPROFILE%\.dotnet\tools**;C:\Users\sotteson\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\sotteson\AppData\Roaming\npm;D:\data\steve\OneDrive\bin;%USERPROFILE%\go\bin;C:\Program Files\Go\bin;C:\Program Files (x86)\GnuWin32\bin;C:\msys64\mingw64\bin;




Discussion / hints:
Looks like setting "compatibility.reloadEnvironmentVariables": false fixes it. I'm going to debug it and see if I can tell what's going on.
Maybe the contents of 
* `HKEY_CURRENT_USER\Environment` 
* `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment`

might be enlightening?
I figured it out. On my system, [HKEY_CURRENT_USER\Environment]Path is of type REG_SZ instead of REG_EXPAND_SZ. The code in env.h then won't expand it. I'm going to submit a simple PR that fixes it.
Thanks!

I'm surprised that it works elsewhere for you. There might be some double expansion going on that shouldn't.

The code in `til/env` was lifted and munged from `RegenerateUserEnvironment` (shell32, internal, reSearch will help :)) and I cannot recall whether _it_ did this expansion...
Just took a look at that code...they're careful to always expand path variables even if it's REG_SZ. My fix should have the same result.

Fix patch:
diff --git a/src/inc/til/env.h b/src/inc/til/env.h
index 1ed0ada927f..36b682cfbd3 100644
--- a/src/inc/til/env.h
+++ b/src/inc/til/env.h
@@ -313,6 +313,15 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
 
                             if (valueNameSize)
                             {
+                                const bool isPathVar = is_path_var(valueName);
+
+                                // On some systems we've seen path variables that are REG_SZ instead
+                                // of REG_EXPAND_SZ. We should always treat them as REG_EXPAND_SZ.
+                                if (isPathVar && type == REG_SZ)
+                                {
+                                    type = REG_EXPAND_SZ;
+                                }
+
                                 std::wstring data;
                                 if (pass == 0 && (type == REG_SZ) && valueDataSize >= sizeof(wchar_t))
                                 {
@@ -335,7 +344,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
 
                                 if (!data.empty())
                                 {
-                                    if (is_path_var(valueName))
+                                    if (isPathVar)
                                     {
                                         concat_var(valueName, std::move(data));
                                     }


Test patch: