Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Since 7.4.0, Group-Object cannot group strings containing curly braces #20711

Closed
5 tasks done
cschneegans opened this issue Nov 17, 2023 · 4 comments · Fixed by #20745
Closed
5 tasks done

Since 7.4.0, Group-Object cannot group strings containing curly braces #20711

cschneegans opened this issue Nov 17, 2023 · 4 comments · Fixed by #20745
Labels
In-PR Indicates that a PR is out for the issue

Comments

@cschneegans
Copy link

cschneegans commented Nov 17, 2023

Prerequisites

Steps to reproduce

Since 7.4.0, it appears that the Group-Object cmdlet tries to interpret its string input as .NET format strings.

If a string contains curly braces (U+007B and U+007D), the Group-Object cmdlet throws exceptions associated with the [string]::Format method or the -f operator.

Expected behavior

PS> '{' | Group-Object -NoElement;

Count Name
----- ----
    1 {

PS> '}' | Group-Object -NoElement;

Count Name
----- ----
    1 }

Actual behavior

PS> '{' | Group-Object -NoElement;
Group-Object: Input string was not in a correct format. Failure to parse near offset 1. Expected an ASCII digit.

PS> '}' | Group-Object -NoElement;
Group-Object: Input string was not in a correct format. Failure to parse near offset 1. Unexpected closing brace without a corresponding opening brace.

Error details

Exception             : 
    Type       : System.FormatException
    TargetSite : 
        Name          : ThrowFormatInvalidString
        DeclaringType : System.ThrowHelper, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Input string was not in a correct format. Failure to parse near offset 1. Unexpected closing brace without a corresponding opening brace.
    Source     : System.Private.CoreLib
    HResult    : -2146233033
    StackTrace : 
   at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ReadOnlySpan`1 args)
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
   at Microsoft.PowerShell.Commands.GroupInfo.BuildName(List`1 propValues)
   at Microsoft.PowerShell.Commands.GroupInfo..ctor(OrderByPropertyEntry groupValue)
   at Microsoft.PowerShell.Commands.GroupObjectCommand.DoOrderedGrouping(OrderByPropertyEntry currentObjectEntry, Boolean noElement, List`1 groups, Dictionary`2 groupInfoDictionary, OrderByPropertyComparer orderByPropertyComparer)
   at Microsoft.PowerShell.Commands.GroupObjectCommand.EndProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
CategoryInfo          : NotSpecified: (:) [Group-Object], FormatException
FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.GroupObjectCommand
InvocationInfo        : 
    MyCommand        : Group-Object
    ScriptLineNumber : 1
    OffsetInLine     : 7
    HistoryId        : 9
    Line             : '}' | Group-Object -NoElement;
    Statement        : Group-Object -NoElement
    PositionMessage  : At line:1 char:7
                       + '}' | Group-Object -NoElement;
                       +       ~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Group-Object
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

@cschneegans cschneegans added the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 17, 2023
@mawosoft
Copy link
Contributor

mawosoft commented Nov 19, 2023

Seems to have been introduced in #20608.
This accidentally reverts the use of StringBuilder.Append(CultureInfo, $"{interpolated}") (added in #18978) back to StringBuilder.AppendFormat(), but keeps the interpolated string instead of using "{0}", value, i.e. the value becomes the format string.

mawosoft added a commit to mawosoft/PowerShell that referenced this issue Nov 21, 2023
Fixes regression from PowerShell#20608 which mixed up use of `StringBuilder.AppendFormat` and interpolated strings, thereby passing the actual value to be formatted as the format string.

Fixes PowerShell#20711.
@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR Indicates that a PR is out for the issue label Nov 21, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 30, 2023
Copy link
Contributor

microsoft-github-policy-service bot commented Nov 30, 2023

📣 Hey @christoph-schneegans, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://forms.office.com/r/P926k48jRJ

@sba923
Copy link
Contributor

sba923 commented Jan 7, 2024

We'll be able to test the fix in the first 7.4.1 preview, correct?

@iSazonov
Copy link
Collaborator

iSazonov commented Jan 7, 2024

@sba923 Yes. And it is already in daily build (link is on main page).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR Indicates that a PR is out for the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants