-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix the CLR internal error and null ref exception when running show-command with PowerShell API
#26669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a null reference exception that occurs when running Show-Command within a PowerShell API context using PowerShell.Create(). The issue arises because the default host (and potentially custom hosts) can have a null PrivateData property.
Key Changes:
- Added null-conditional operator when accessing
cmdlet.Host.PrivateData.Properties - Added explanatory comment about why
PrivateDatamay be null
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
show-command in API from within PowerShellshow-command with PowerShell API
|
Interesting why is a reflection and manual dll loading used at all? We could create and reference psevdo Microsoft.PowerShell.GraphicalHost project with all needed placeholders for methods and properties and replace the psevdo dll with real one in bin folder. Strong typing and automatic loading should work. |
|
I think the loose dependency is what we want. Not all environment and applications support/care about WPF/WinForm, so there shouldn't be a hard dependency between |
|
Yes, the reflection wrapper is for avoiding project reference to WPF in SMA. My ask is about why not use strong typing instead of reflection. If we created dummy placeholder project (referenced in SMA) with all needed API we could replace the dll of the project with real Microsoft.PowerShell.GraphicalHost.dll (we have to copy it to bin folder in any case). Since both dll are signed by one key this should works. |
PR Summary
Fix #26665
Two issues are addressed in this PR:
GraphicalHostReflectionWrapperalways tries to loadMicrosoft.PowerShell.GraphicalHostwith the assembly version v3.0.0.0, which is the version of the Windows PowerShell 5.1 assemblies.PowerShell/src/System.Management.Automation/utils/GraphicalHostReflectionWrapper.cs
Lines 89 to 94 in 4838a8d
I believe this is the cause of the reported CLR internal error, because even though the SDK package doesn't contain the
Microsoft.PowerShell.GraphicalHostassembly, the v3.0.0.0 version of the assembly is available in GAC (as part of Windows PS v5.1), and PowerShell engine does look for assembly in GAC as the last resort. So, in your case, the assembly gets loaded from GAC, which targets .NET Framework and causes the CLR internal error.Show-Commandfails when running it in PowerShell API from within PowerShell:When running in a PowerShell object created by
PowerShell.Create()the default host is in use. The value ofcmdlet.Host.PrivateDatais null for the default host and it also could be null for a custom host.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header