Posh icon indicating copy to clipboard operation
Posh copied to clipboard

Bug - Posh breaks Get-FormatData

Open trackd opened this issue 1 year ago • 2 comments

Description

Not sure if this is a powershell bug or if it's something in this module that breaks it.

After importing Posh you can no longer inspect Formatdata for filesystem.

expected:

Get-FormatData -TypeName System.IO.*

TypeNames                                                               FormatViewDefinition
---------                                                               --------------------
{System.IO.FileSystemInfo, System.IO.FileInfo, System.IO.DirectoryInfo} {CustomGci}
{System.IO.DirectoryInfo, System.IO.FileInfo}                           {children, childrenWithHardlink, children, children}

and ofcourse the format that Posh adds.

a bit of a sidenote, i would really like some settings so you could configure which part of Posh to enable?

As i already have a pretty nice format setup that i like for filesystem i kind of want to keep that. But there are other parts of Posh that i would still like to use.

Also would be nice to be able to select some sort of theme so there are some options or you could provide your own theme.

really cool module btw! :)

This is on Powershell 7.4.1, but i can reproduce the exact same error on 5.1, 7.5.0-preview.1

Reproducable Steps

pwsh -nop -c 'Import-Module Posh;Get-Module Posh;Get-FormatData -TypeName System.IO.*;Get-Error'
# outputs
Posh @ 0.2

PowerShell made Posh - Spice up Your Shell

        Posh is a PowerShell module that improves the shell experience.

        It :

           * Helps you to customize the shell.
           * Helps you learn PowerShell
           * Improves built-in formatting
           * Extends built-in types


To see PowerShell news, get $Posh.News


To see everything Posh can do: $posh | Get-Member


Get-FormatData: Unable to cast object of type 'Microsoft.PowerShell.Commands.Internal.Format.ControlReference' to type 'Microsoft.PowerShell.Commands.Internal.Format.ComplexControlBody'.

Exception             : System.InvalidCastException: Unable to cast object of type 'Microsoft.PowerShell.Commands.Internal.Format.ControlReference' to type 'Microsoft.Po
                        werShell.Commands.Internal.Format.ComplexControlBody'.
                           at System.Management.Automation.CustomItemBase.Create(FormatToken token)
                           at System.Management.Automation.CustomControlEntry..ctor(ComplexControlEntryDefinition entry)
                           at System.Management.Automation.CustomControl..ctor(ComplexControlBody body, ViewDefinition viewDefinition)
                           at Microsoft.PowerShell.Commands.GetFormatDataCommand.ProcessRecord()
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Get-FormatData], InvalidCastException
FullyQualifiedErrorId : System.InvalidCastException,Microsoft.PowerShell.Commands.GetFormatDataCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Links

No response

trackd avatar Jan 21 '24 18:01 trackd

@trackd Thanks for reminding me about this behavior. And, sorry, this is actually PowerShell's bug, not Posh's (and not EZOut's, either). I've opened this issue in the PowerShell repo to track it

Get-FormatData does not handle formatting that makes use of controls (even though controls are part of the formatting engine).

I may try to fix it some day in the PowerShell codebase, but it is solidly outside of the scope of this module's control.

Feel free to open an issue in the PowerShell repo to track it, as they do not currently appear to have one.

Additionally:

Get-FormatData isn't broken for everything, it's broken for "enumerating all formatters", because it terminates on the first error (which is bad form for PowerShell cmdlets).

Get-FormatData -TypeName abc will still work

Please let me know if you have more questions, and please help escalate this issue on the PowerShell repo.

StartAutomating avatar Jan 24 '24 19:01 StartAutomating

Thanks for reminding me about this behavior. And, sorry, this is actually PowerShell's bug

yea i did feel that regardless of the cause it (Get-FormatData) should handle exceptions more gracefully than that

i was just not sure if you were doing some trickery ;-)

There was the question of some form of configuration or control of the module.

I'd like to selectively disable some formats if possible.

would also be cool if it supported themeing, so you could set your own color preference for things like format files

something like $PSStyle in a .psd1/.json in $env:appdata\powershell\Posh with a hashtable of colorpreference maybe, and if that doesnt exist just use defaults.

just a thought.

trackd avatar Jan 24 '24 19:01 trackd