Skip to content

WIP: Incorrect arg splitting when using .. syntax for git log - #20091

Open
James Truher (JamesWTruher) wants to merge 13 commits into
PowerShell:masterfrom
JamesWTruher:Issue15279_3
Open

James Truher (JamesWTruher) wants to merge 13 commits into
PowerShell:masterfrom
JamesWTruher:Issue15279_3

Conversation

@JamesWTruher

@JamesWTruher James Truher (JamesWTruher) commented Aug 7, 2023 •

Copy link
Copy Markdown
Collaborator

PR Summary

Fix #15279

PR Context

The current NativeParameterBinder (NPB) receives 2 distinct parameters from the parser which need to be combined, but does not as the following example shows:

PS> trace-command -PSHost -name param* { git diff $b..master } 
DEBUG: 2023-08-21 13:39:51.0417 ParameterBinding Information: 0 : BIND NAMED native application line args [/usr/local/bin/git]
DEBUG: 2023-08-21 13:39:51.0503 ParameterBinding Information: 0 :     BIND cmd line arg [diff] to position [0]
DEBUG: 2023-08-21 13:39:51.0503 ParameterBinding Information: 0 :     BIND cmd line arg [Issue15279_3] to position [1]
DEBUG: 2023-08-21 13:39:51.0504 ParameterBinding Information: 0 :     BIND cmd line arg [..master] to position [2]
DEBUG: 2023-08-21 13:39:51.0614 ParameterBinding Information: 0 : CALLING BeginProcessing

While investigating this, i discovered other behavior which makes it difficult to interact with native executables. There are native applications which allow parameters to be expressed as -parm=value which can be very useful if you can use variables: -param=${param}.

Unfortunately, this is currently converted to:

> trace-command -pshost -name param* { echoit -foo=${thing} }
DEBUG: 2023-08-21 13:43:40.7358 ParameterBinding Information: 0 : BIND NAMED native application line args [/Users/james/bin/echoit]
DEBUG: 2023-08-21 13:43:40.7360 ParameterBinding Information: 0 :     BIND cmd line arg [-foo=$] to position [0]
DEBUG: 2023-08-21 13:43:40.7360 ParameterBinding Information: 0 :     BIND cmd line arg [-encodedCommand] to position [1]
DEBUG: 2023-08-21 13:43:40.7361 ParameterBinding Information: 0 :     BIND cmd line arg [dABoAGkAbgBnAA==] to position [2]
DEBUG: 2023-08-21 13:43:40.7361 ParameterBinding Information: 0 :     BIND cmd line arg [-inputFormat] to position [3]
DEBUG: 2023-08-21 13:43:40.7361 ParameterBinding Information: 0 :     BIND cmd line arg [xml] to position [4]
DEBUG: 2023-08-21 13:43:40.7361 ParameterBinding Information: 0 :     BIND cmd line arg [-outputFormat] to position [5]
DEBUG: 2023-08-21 13:43:40.7361 ParameterBinding Information: 0 :     BIND cmd line arg [xml] to position [6]
DEBUG: 2023-08-21 13:43:40.7509 ParameterBinding Information: 0 : CALLING BeginProcessing

The parser recognizes the -foo=$ as the parameter because of the opening brace, and then the minish treatment converts the second argument (which looks like a scriptblock) to a base64 encoded string. After this minishell conversion the NPB is called with these newly constructed parameters.

There is a work-around: using double quotes "-foo=${thing}" will properly pass the argument to the NPB, but this is somewhat difficult to discover after the case because the behavior is so unexpected and in most cases, the error just looks like you've used the command incorrectly.

The change does two things:

  • It changes the parsing of parameters to treat = similarly to : (but retains the = as a part of the parameter)
  • it combines parameters and arguments which have no space between them

There is a breaking behavior here:
Currently, it is possible to support parameters (in scripts, not in compiled code) which look like param (${z=p}).

for the function function zap { param (${p=z}); "p=z:" + ${p=z}; "args: " + $args }, the current behavior is

PS> zap -p=z arg!
p=z: arg!
args: 

The new behavior is:

> zap -p=z arg!
p=z: -p=z
Args: arg!

the parameter is bound positionally rather than by the parameter name because -p=z is no longer recognized as a parameter, but an argument. Although I do not believe that this change will affect many (if any) current pwsh users.

Because e now re-parse a parameter with an = as an argument rather than parameter is the reason that the behavior when using a : is now:

> zap -p=z:arg!
p=z:-p=z:arg!
args: 

the entire -p=z:arg! string is seen as a non-positional argument and is bound to the p=z parameter.

We can see this by inspecting the ASTs as follows:

Present:

PS>  { echo -p=one=1 }.Ast.FindAll({$true},$true) | ft {$_.GetType()},Extent  

$_.GetType()                                                      Extent
------------                                                      ------
System.Management.Automation.Language.ScriptBlockAst              { echo -p=one=1 }
System.Management.Automation.Language.NamedBlockAst               echo -p=one=1
System.Management.Automation.Language.PipelineAst                 echo -p=one=1
System.Management.Automation.Language.CommandAst                  echo -p=one=1
System.Management.Automation.Language.StringConstantExpressionAst echo
System.Management.Automation.Language.CommandParameterAst         -p=one=1

and new:

PS>  { echo -p=one=1 }.Ast.FindAll({$true},$true) | ft {$_.GetType()},Extent  

$_.GetType()                                                      Extent
------------                                                      ------
System.Management.Automation.Language.ScriptBlockAst              { echo -p=one=1 }
System.Management.Automation.Language.NamedBlockAst               echo -p=one=1
System.Management.Automation.Language.PipelineAst                 echo -p=one=1
System.Management.Automation.Language.CommandAst                  echo -p=one=1
System.Management.Automation.Language.StringConstantExpressionAst echo
System.Management.Automation.Language.StringConstantExpressionAst -p=one=1

This PR behavior is accessible by enabling the PSNativeParameterParsing Experimental feature.

PR Checklist

@JamesWTruher
James Truher (JamesWTruher) force-pushed the Issue15279_3 branch 3 times, most recently from a1ba43e to eef18ed Compare August 21, 2023 17:36
@JamesWTruher James Truher (JamesWTruher) added Review - Committee The PR/Issue needs a review from the PowerShell Committee Breaking-Change breaking change that may affect users labels Aug 21, 2023
@JamesWTruher James Truher (JamesWTruher) changed the title WIP: fix for Issue15279 fix for Issue15279 Aug 21, 2023
@SteveL-MSFT Steve Lee (SteveL-MSFT) changed the title fix for Issue15279 Incorrect arg splitting when using .. syntax for git log Aug 23, 2023
@microsoft-github-policy-service microsoft-github-policy-service Bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Aug 28, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By setting sawColonAtEnd = true here, the resulted ParameterToken will have the public property UsedColon set to true, which is incorrect for this publicly exposed API.

## an example output of the ParameterToken

ParameterName : foo=
UsedColon     : True
Text          : -foo=
TokenFlags    : None
Kind          : Parameter
HasError      : False
Extent        : -foo=

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the code here doesn't consider InCommandMode() as it does in the default branch, which will result in different tokens when -f= is tokenized in the expression mode. But this is less concerned than the incorrect value for the public property UseColon.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems to change how ValueFromRemainingArguments works with strings. For example:

echo -d=test

Would previously return a single string of -d=test but now returns

-d=
test

This would be most impactful for commands that wrap native commands.

function Show-PowerShellWithBat {
    param(
        [Parameter()]
        [string] $Script,

        [Parameter(ValueFromRemainingArguments)]
        [string[]] $AdditionalArguments
    )
    end {
        $Script | bat --language powershell @AdditionalArguments
    }
}

Show-PowerShellWithBat '$testing' -r=1

That will cause bat to emit

[bat error]: Empty line range

due to the extra space

@JamesWTruher James Truher (JamesWTruher) Sep 7, 2023 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dongbo Wang (@daxian-dbw) Patrick Meinecke (@SeeminglyScience) thanks - I was trying to treat HasColon as a behavioral "no space between parameter and argument" (which likely wouldn't have considered = when it was created) rather than adding a new property. More disturbing to me is the ValidateFromRemainingArguments concern because it wasn't caught by our current validation. I'll certainly be adding that as a test (or 4) when this is ready.

all of my earlier attempts (without the parser change) couldn't handle the -foo=${a}..b case which is why i went down this path. I think I'll have to not have the parser change and handle it only in the native parameter binder which should still handle the original issue of git diff $a..$b . Without the parser change, it's likely to not even be a breaking change, and you must still use "-foo=${a}..bar" to get that into the native app.

back to the drawing board - marking this back to WIP

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a different way to do this with Patrick Meinecke (@SeeminglyScience), so I've removed the work in progress. The new solution works with the bat example as desired (solving the ValueFromRemainingArguments mishandling). Essentially, now when we see an = as part of a parameter, we re-parse the input as a generic token rather than a parameter. This will certainly work better for native applications.

I also added some additional tests for ValueFromRemainingArguments to be a little more exotic.

@JamesWTruher James Truher (JamesWTruher) changed the title Incorrect arg splitting when using .. syntax for git log WIP: Incorrect arg splitting when using .. syntax for git log Sep 7, 2023
@JamesWTruher James Truher (JamesWTruher) changed the title WIP: Incorrect arg splitting when using .. syntax for git log Incorrect arg splitting when using .. syntax for git log Sep 8, 2023
@StevenBucher98 Steven Bucher (StevenBucher98) added the PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed label Sep 25, 2023
@pull-request-quantifier-deprecated

Copy link
Copy Markdown

This PR has 128 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Medium
Size       : +121 -7
Percentile : 45.6%

Total files changed: 5

Change summary by file extension:
.cs : +52 -3
.ps1 : +69 -4

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@JamesWTruher James Truher (JamesWTruher) changed the title Incorrect arg splitting when using .. syntax for git log WIP: Incorrect arg splitting when using .. syntax for git log Dec 13, 2023
@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee discussed this and agree with the new behavior regarding .. and =:

  • .. within an arg value will be a single argument with this change instead of two (previously). This will be less surprising to users as there isn't a whitespace that would have indicated this would have been two args.
  • = previously script functions could use this character within a parameter name and with this change, the = character is treated as a delimiter separating the arg name and arg value. This also seems unlikely to be used by existing script functions and was never allowed for C# cmdlets.

Overall, although a breaking change, we believe this is bucket 3 and improves the command-line experience overall so worth taking.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Dec 13, 2023
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Dec 21, 2023
@pull-request-quantifier-deprecated

Copy link
Copy Markdown

This PR has 128 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Medium
Size       : +121 -7
Percentile : 45.6%

Total files changed: 5

Change summary by file extension:
.cs : +52 -3
.ps1 : +69 -4

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

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

Labels

Breaking-Change breaking change that may affect users Committee-Reviewed PS-Committee has reviewed this and made a decision Medium PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect arg splitting when using .. syntax for git log

6 participants