-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Reproduction steps
- Unzip the attached PowershellCommandLet.zip
- Open Powershell 7.5
- Open the the folder "Powershell\PowershellCmdlet\bin\Debug\net8.0-windows7.0" in Powershell 7.5. In this folder you will find the already compiled Powershell module PowershellCmdlet.dll and its two assemblies it depends on.
- Import the module with "Import-Module -Name ".\PowershellCmdlet.dll""
- Execute the Commandlet with "Add-TestCmdlet"
- The Commandlet has some output. But at the end there is an exception:
Add-TestCmdlet: External component has thrown an exception.
The short story
I think Powershell 7.5 is unable to run Powershell Commandlets implemented in C#, C++ and C++/Cli using .net8.0 in some cases.
If I execute the same commandlet with Powershell 7.4 all is fine.
The long story
If you attach Visual Studio as debugger to the pwsh.exe and execute the Add-TestCmdlet-Commandlet you will see the following error:
The specified runtimeconfig.json [C:\Users\wg\source\repos\Powershell\PowershellCmdlet\bin\Debug\net8.0-windows7.0\CPPClrAndNative.runtimeconfig.json] does not exist
To fix this error I added already CPPClrAndNative.runtimeconfig.json to PowershellCmdlet and set the property "Copy to Output Directory" to "Copy if newer".
If you execute Add-TestCmdlet-Commandlet in Powershell again the following Exception occurs:
The specified framework 'Microsoft.NETCore.App', version '8.0.11', apply_patches=1, version_compatibility_range=minor is incompatible with the previously loaded version '9.0.1'.
Where does the Exception occur?
In load_fxr_and_get_delegate in fxr_resolver.h:
As you can see the rc has the value -2147450715. According to https://github.com/dotnet/runtime/blob/main/docs/design/features/host-error-codes.md it means the following:
"Host configuration is incompatible with existing host context - returned by hostfxr_initialize_for_runtime_config. The component being initialized requires framework which is not available or incompatible with the frameworks loaded by the runtime already in the process. For example trying to load a component which requires 3.0 into a process which is already running a 2.0 runtime."
Again: If I execute the same commandlet with Powershell 7.4 all is fine.
Expected behavior
The attached Add-TestCmdlet writes some output to the console and is executing without an exception.
Actual behavior
The attached Add-TestCmdlet writes some output to the console but before it finish there is an exception:
Add-TestCmdlet: External component has thrown an exception.
Error details
PS C:\Users\wg\source\repos\Powershell\PowershellCmdlet\bin\Debug\net8.0-windows7.0> Get-Error
Type : System.Management.Automation.CmdletInvocationException
ErrorRecord :
Exception :
Type : System.Runtime.InteropServices.SEHException
ErrorCode : -2147467259
TargetSite :
Name : NativeClass.Test
MemberType : Method
Module : CPPClr.dll
Message : External component has thrown an exception.
Source : CPPClr
HResult : -2147467259
StackTrace :
at NativeClass.Test(NativeClass*)
at CPPclr.ManagedClass.Test()
at PowershellCmdlet.TestCmdlet.ProcessRecord() in
C:\Users\wg\source\repos\Powershell\PowershellCmdlet\TestCmdlet.cs:line 14
at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo : NotSpecified: (:) [Add-TestCmdlet], SEHException
FullyQualifiedErrorId : System.Runtime.InteropServices.SEHException,PowershellCmdlet.TestCmdlet
InvocationInfo :
MyCommand : Add-TestCmdlet
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 2
Line : Add-TestCmdlet
Statement : Add-TestCmdlet
PositionMessage : At line:1 char:1
+ Add-TestCmdlet
+ ~~~~~~~~~~~~~~
InvocationName : Add-TestCmdlet
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
0
1
TargetSite :
Name : Invoke
DeclaringType : [System.Management.Automation.Runspaces.PipelineBase]
MemberType : Method
Module : System.Management.Automation.dll
Message : External component has thrown an exception.
Data : System.Collections.ListDictionaryInternal
InnerException :
Type : System.Runtime.InteropServices.SEHException
ErrorCode : -2147467259
TargetSite :
Name : NativeClass.Test
MemberType : Method
Module : CPPClr.dll
Message : External component has thrown an exception.
Source : CPPClr
HResult : -2147467259
StackTrace :
at NativeClass.Test(NativeClass*)
at CPPclr.ManagedClass.Test()
at PowershellCmdlet.TestCmdlet.ProcessRecord() in
C:\Users\wg\source\repos\Powershell\PowershellCmdlet\TestCmdlet.cs:line 14
at System.Management.Automation.CommandProcessor.ProcessRecord()
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown,
ExecutionOptions options)
Environment data
PS C:\Users\wg\source\repos\Powershell\PowershellCmdlet\bin\Debug\net8.0-windows7.0> $PSVersionTable
Name Value
---- -----
PSVersion 7.5.0
PSEdition Core
GitCommitId 7.5.0
OS Microsoft Windows 10.0.26100
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
I have attached a Visual Studio Solution for reproducing the issue:
PowershellCommandLet.zip
Projects in the attached Visual Studio Solution
PowershellCmdlet.dll:
- Programming language: C#
- Targetframework: net8.0-windows7.0
- Implements a simple Powershell Cmdlet named Add-TestCmdlet. The Cmdlet calls ManagedClass.Test in CPPClr.dll.
CPPClr.dll:
- Programming language: C++/Cli
- Targetframework: net8.0
- ManagedClass.Test calls NativeClass::Test in CPPClrAndNative.dll
CPPClrAndNative.dll:
- Programming language: C++ and C++/Cli
- Targetframework (for the C++/Cli part): net8.0
- NativeClass::Test calls function ClrFunction which calls Console::WriteLine which is managed. When calling ClrFunction the crash occurs!