FSharp.Management icon indicating copy to clipboard operation
FSharp.Management copied to clipboard

TFailure of PsCmdletResult<TSuccess, TFailure> is not returned on errors

Open nightroman opened this issue 7 years ago • 0 comments

In the following script

    #I @"packages\FSharp.Management\lib\net40"
    #r "FSharp.Management.PowerShell.dll"
    #r "System.Management.Automation"

    open FSharp.Management
    open System.Management.Automation

    type PS = PowerShellProvider< "Microsoft.PowerShell.Management;Microsoft.PowerShell.Core" >

    PS.``Get-Variable`` (name=[|"Host"|])
    |> printfn "%A"

    PS.``Get-Variable`` (name=[|"Missing"|])
    |> printfn "%A"

    PS.``Get-Variable`` (name=[|"Missing"|], errorAction=ActionPreference.Stop)
    |> printfn "%A"

the type of PS.``Get-Variable`` is PsCmdletResult<List<PSVariable>,List<ErrorRecord>>.

The script makes three calls:

  1. Get-Variable returns a variable, the result is Success [System.Management.Automation.PSVariable].
  2. Get-Variable writes a non-terminating error, the result is Success [].
  3. Get-Variable writes a terminating error, this results in an exception.
    [Loading ...\TestError.fsx]

    Success [System.Management.Automation.PSVariable]

    Success []

    System.Management.Automation.ActionPreferenceStopException: The running command
    stopped because the preference variable "ErrorActionPreference" or common
    parameter is set to Stop: Cannot find a variable with the name 'Missing'.
    ...

    Stopped due to error

Thus, TFailure of PsCmdletResult<TSuccess, TFailure> is never used.

Is this a bug? What is the expected result in (2) and (3), by design?

The design of the result type is not clear, though. It does not cover all possible cases, for example a case when a command writes both data and errors, e.g. with existing and missing variables specified at the same time on Get-Variable calls.

nightroman avatar Aug 03 '17 04:08 nightroman