Fix: Use %1 instead of %V! for folder context menu paths#26718
Fix: Use %1 instead of %V! for folder context menu paths#26718tekintian wants to merge 1 commit intoPowerShell:masterfrom
Conversation
- Changed %V! to %1 in both openpwsh\command and runas\command registry keys - %V! returns background path (current folder), while %1 returns selected item path - Fixes issue where right-clicking a subfolder would open parent folder instead - Aligns with PowerShell file context menu which already uses %1 correctly - Resolves network drive path issues in Windows VM shared directories
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
yotsuda
left a comment
There was a problem hiding this comment.
The ! in %V! is a dummy character paired with -RemoveWorkingDirectoryTrailingCharacter — together they avoid a \" escaping issue on root paths (#8287). Removing the ! means the switch will strip the last real character of the path instead (e.g., C:\Users → C:\User).
Background
When a path like C:\ is quoted on the Windows command line, the trailing \" is interpreted as an escaped quote rather than the end of the string (see #8278). PR #8287 introduced a workaround: append a dummy ! to the path and use -RemoveWorkingDirectoryTrailingCharacter to strip it:
pwsh -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "C:\!"
# "C:\!" → no escaping issue → strip "!" → "C:\"
The problem with this PR
This PR changes %V! to %1, removing the ! but keeping -RemoveWorkingDirectoryTrailingCharacter. This causes two issues:
- Non-root paths: the last real character is stripped (
C:\Users→C:\User) - Root paths: the
\"escaping issue returns ("C:\"→ broken command line)
I verified this locally:
| Input | Result |
|---|---|
pwsh -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "C:\Users!" |
C:\Users ✅ |
pwsh -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "C:\Users" |
falls back to default ❌ |
pwsh -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "C:\!" |
C:\ ✅ |
pwsh -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "C:\" |
falls back to default ❌ |
Suggested fix
If %1 is the correct variable for the cascading context menu, just preserve the sentinel !. See the inline suggestions below.
| </RegistryKey> | ||
| <RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\openpwsh\command"> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | |
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
I have reviewed the code changes and agree with yotsuda's analysis. The PR correctly identifies that %1 should be used for the selected item path instead of %V!, but removing the trailing sentinel character ! while keeping -RemoveWorkingDirectoryTrailingCharacter will cause the issues yotsuda described. The suggested fix to use %1! maintains both the correct path variable and the workaround for the escaping issue from #8287.
| </RegistryKey> | ||
| <RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\runas\command"> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | |
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
Agreed. The same issue applies here for the elevated (runas) context menu entry. Using %1! instead of %1 ensures proper path handling.
aaron-seq
left a comment
There was a problem hiding this comment.
The PR correctly identifies the issue with using %V! vs %1, but as yotsuda pointed out, the trailing ! character must be preserved to work with -RemoveWorkingDirectoryTrailingCharacter. Please update both lines to use %1! instead of %1 to maintain the workaround for the escaping issue from #8287.
| </RegistryKey> | ||
| <RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\openpwsh\command"> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
I have reviewed the code changes and agree with yotsuda's analysis. The PR correctly identifies that %1 should be used for the selected item path instead of %V!, but removing the trailing sentinel character ! while keeping -RemoveWorkingDirectoryTrailingCharacter will cause the issues yotsuda described. The suggested fix to use %1! maintains both the correct path variable and the workaround for the escaping issue from #8287.
| </RegistryKey> | ||
| <RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\runas\command"> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> | ||
| <RegistryValue Type="string" Value="[VersionFolder]pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%1" -Command "$host.UI.RawUI.WindowTitle = '$(var.ProductName) $(var.SimpleProductVersion) ($(sys.BUILDARCH))'""/> |
There was a problem hiding this comment.
Agreed. The same issue applies here for the elevated (runas) context menu entry. Using %1! instead of %1 ensures proper path handling.
Fix: Correct folder path resolution in Explorer context menu for network drives
Problem Description
When using PowerShell installed in a Windows VM with shared directories from the host (e.g.,
Z:\mapped to\\Mac\work), right-clicking on a subfolder and selecting "Open here as Administrator" would:Set-Location: Cannot find drive. A drive with the name 'Z' does not exist.Example: Right-clicking
Z:\projects\electron\double-mouse-downloaderwould openZ:\projects\electroninstead of the selected folder.Root Cause
The Explorer context menu registry entries were using
%V!placeholder, which returns the background path (current folder) rather than the selected item path.%V!= background path (where right-click was invoked)%1= selected item path (the actual folder/file clicked)This caused the context menu to always use the parent folder path, regardless of which subfolder was actually selected.
Solution
Changed both folder context menu entries in
assets/wix/Product.wxsfrom%V!to%1:openpwsh\command- Normal PowerShell launchrunas\command- Elevated PowerShell launch (Administrator)This aligns with the PowerShell file context menu (line 477) which already correctly uses
'%1'for the selected file path.Changes Made
assets/wix/Product.wxs%V!→%1Testing
Z:\) now work correctly in elevated PowerShell sessionsRelated Issue
This fixes the issue described in the bug report where users couldn't directly operate on folders in Windows VM shared directories.