Skip to content

"Get-ChildItem | Format-Wide -AutoSize | Out-String" can't work on Powershell 6.0.1 & 6.0.2 #6471

Description

@andot

Steps to reproduce

Get-ChildItem | Format-Wide -AutoSize | Out-String

or

Get-ChildItem | Format-Wide -AutoSize | Out-String -Stream

Expected behavior



    Directory: C:\Users\andot\Documents\WindowsPowerShell\Modules\lolcat\1.0.1



.git                        LICENSE
lolcat.psd1                        lolcat.psm1
Out-Rainbow.psm1                   Out-StripAnsi.psm1
README.md                   screenshot.png

Actual behavior

out-lineoutput : Array dimensions exceeded supported range.
At line:1 char:1
+ Get-ChildItem | Format-Wide -AutoSize | Out-String -Stream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [out-lineoutput], OutOfMemoryException
+ FullyQualifiedErrorId : System.OutOfMemoryException,Microsoft.PowerShell.Commands.OutLineOutputCommand

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> $PSVersionTable                                                 
Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec...
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

If no -AutoSize param, the result will be fine. And it works well in WindowsPowerShell 5.1 with or without -AutoSize param.

Activity

  1. stknohg commented on Mar 23, 2018

    @stknohg
    Contributor

    Hi, I found an another workaround.

    Workaround

    Specify Out-String -Width parameter explicitly and set the appropriate value.

    e.g.

    Get-ChildItem | Format-Wide -AutoSize | Out-String -Width $Host.UI.RawUI.WindowSize.Width

    Cause

    When specify Format-Wide -AutoSize parameter, display items per row is decided from output width.
    In this case, output width is depend on Out-String -Width value.

    When -Width parameter is not specified, int.MaxValue is adopted as output width.
    (This behavior is from PowerShell Core 6.0. #5193)

    When initializing WideOutputContext, itemsPerRow value(=items per row) reaches int.MaxValue.
    Therefore, creation of the _buffer buffer fails, resulting in an error.

    /// <summary>
    /// initialize column widths
    /// </summary>
    internal override void Initialize()
    {
    // set the hard wider default, to be used if no other info is available
    int itemsPerRow = 2;
    // get the header info and the view hint
    WideFormattingHint hint = this.InnerCommand.RetrieveFormattingHint() as WideFormattingHint;
    // give a preference to the hint, if there
    if (hint != null && hint.maxWidth > 0)
    {
    itemsPerRow = TableWriter.ComputeWideViewBestItemsPerRowFit(hint.maxWidth, this.InnerCommand._lo.ColumnNumber);
    }
    else if (this.CurrentWideHeaderInfo.columns > 0)
    {
    itemsPerRow = this.CurrentWideHeaderInfo.columns;
    }
    // create a buffer object to hold partial rows
    _buffer = new StringValuesBuffer(itemsPerRow);
    // initialize the writer
    int[] columnWidths = new int[itemsPerRow];
    int[] alignment = new int[itemsPerRow];
    for (int k = 0; k < itemsPerRow; k++)
    {
    columnWidths[k] = 0; // autosize
    alignment[k] = TextAlignment.Left;
    }
    this.Writer.Initialize(0, this.InnerCommand._lo.ColumnNumber, columnWidths, alignment, false);
    }

  2. andot commented on Mar 23, 2018

    @andot
    Author

    Thank you! Now on with Powershell 6.x, my lolcat Module can works well.

  3. added
    Issue-BugIssue has been identified as a bug in the product
    WG-Enginecore PowerShell engine, interpreter, and runtime
    on Mar 24, 2018
  4. iSazonov commented on Mar 24, 2018

    @iSazonov
    Collaborator

    Could we use int.MaxValue-1 as init value?

  5. stknohg commented on Mar 24, 2018

    @stknohg
    Contributor

    Ilya (@iSazonov) No, but we can use Console.WindowWidth same as TableOutputContext.

    int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber;
    // Tables need to use spaces for padding to maintain table look even if console window is resized.
    // For all other output, we use int.MaxValue if the user didn't explicitly specify a width.
    // If we detect that int.MaxValue is used, first we try to get the current console window width.
    // However, if we can't read that (for example, implicit remoting has no console window), we default
    // to something reasonable: 120 columns.
    if (columnsOnTheScreen == int.MaxValue)
    {
    try
    {
    columnsOnTheScreen = Console.WindowWidth;
    }
    catch
    {
    columnsOnTheScreen = 120;
    }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions