dotnet/interactive

Polyglot Notebook VS Code: Notebooks outputting .NET objects instead of PowerShell

Opened this issue · 7 comments

Describe the bug

When working with variable containing PowerShell objects or object collections the cell output is make up of .NET style objects rather than PowerShell CLI output. This also often contains errors that are not consistent with the same commands run in the VS Code terminal.

This has started happening on all my devices, both personal and work/corporate managed.

Please complete the following:

Image
  • OS
    • Windows 11 (4 different machines, both x64 and Arm64 versions)
  • Browser
    • Edge
  • Frontend
    • Visual Studio Code

Screenshots

Image Image

This issue was previously raised in the VS Code repo but has been closed as an extension related issue not a VS Code bug.

microsoft/vscode#257266

For context, I want to extend on a few points made in original issue thread before it was closed:
This issue occurs with many commands including git clone, docker build, helm release etc. Notebooks of reasonable complexity produce thousands of these objects, rendering themselves practically unusable.

I see the same behavior with docker buildx build command. It appears PowerShell treats the output of some commands as stderr rather than stdout. This is not specific to commands run from a notebook: running the same command in a regular pwsh terminal also produces output on stderr (but since the output color does not change to red, we cannot see it without trying to capture the output).

As a temporary workaround, I redirect stderr to stdout for the affected commands:

<my_command> 2>&1 | Out-String -Stream | Write-Host

This forces stderr to be redirected to stdout, captures the full string, and displays it cleanly.

This behavior was introduced by #3912, the intent of which was to align PowerShell outputs more closely to C# outputs. I think this deserves a discussion about how to accommodate both of these scenarios.

@jonsequitur
I would really like to know who thought it was a good idea to ignore, for example, Format-Table command, and output .NET object.

Default output for PowerShell cells makes notebooks unusable without additional tweaks.

Default output for PowerShell cells makes notebooks unusable without additional tweaks.

+3000!

I basically had to version-pin the extension to avoid getting these bits because we are scripters and not C# developers.

Summary

As a work around, add | Out-Default to the end of your commands

Fixing objects

Try appending Out-Default to the end of your commands. That seems to force the expected behavior

Get-Process Code | Select-Object -First 1
Get-Location | Ft -Auto *

# Becomes
Get-Process Code | Select-Object -First 1 | Out-Default
Get-Location | Ft -Auto * | Out-Default
Image

Fixing Stderr

Redirection from above worked.
I was also able to get different output using PSNativeCommandUseErrorActionPreference

pushd 'g:\temp'
# I think it's scoped, but I wrapped it to be safe
& { 
    try {
        $PSNativeCommandUseErrorActionPreference = $true
        $ErrorActionPreference = 'stop'
        $res = git log
    } catch {
        $_ | fl * -force | Out-Default
        throw
    } finally {
        $ErrorActionPreference = 'continue'
    }
}
Image