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

{Core} Entry script: Use @args to tolerate incorrectly passing list with $ #23802

Closed
wants to merge 1 commit into from

Conversation

jiasli
Copy link
Member

@jiasli jiasli commented Sep 7, 2022

Close #23797

#23514 changed the entry script az.cmd to az.ps1 when executed in PowerShell.

& "$PSScriptRoot\..\python.exe" -IBm azure.cli $args

Consider we have an array

> $mylist = "a", "b"

Some user passes the array to az.ps1 as $mylist:

> az --debug $mylist
cli.knack.cli: Command arguments: ['--debug', 'a b']

This is incorrect, as $args will become an array of array. Using $args to call a native exe will only splat the outer array and concatenate the inner array's items.

The correct way to a pass a list to az is to pass space-separated arguments, so in PowerShell, user should use @ splatting when calling az.ps1:

> az --debug @mylist
cli.knack.cli: Command arguments: ['--debug', 'a', 'b']

Meanwhile, when calling native exe with @args, it will even splat internal arrays. We can use a test script test.ps1 to verify this behavior:

echo $args.Length $args.GetType()
echo $args[0].GetType()
python -c "import sys; print(sys.argv)" $args
python -c "import sys; print(sys.argv)" @args
> ./test.ps1 $mylist $mylist
2

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
True     True     Object[]                                 System.Array
['-c', 'a b', 'a b']
['-c', 'a', 'b', 'a', 'b']

> ./test.ps1 @mylist @mylist
4

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
True     True     String                                   System.Object
['-c', 'a', 'b', 'a', 'b']
['-c', 'a', 'b', 'a', 'b']

Interesting thing is that even @args can only splat 2 layers and the 3rd layer was concatenated:

> ./test.ps1 ($mylist, $mylist)
1

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
True     True     Object[]                                 System.Array
['-c', 'System.Object[] System.Object[]']
['-c', 'a b', 'a b']

We can go even deeper:

> ./test.ps1 (($mylist, $mylist),($mylist, $mylist))
1

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
True     True     Object[]                                 System.Array
['-c', 'System.Object[] System.Object[]']
['-c', 'System.Object[] System.Object[]', 'System.Object[] System.Object[]']

@ghost ghost added Auto-Assign Auto assign by bot Core CLI core infrastructure labels Sep 7, 2022
@ghost ghost requested a review from yonzhan September 7, 2022 08:39
@ghost ghost assigned jiasli Sep 7, 2022
@ghost ghost added this to the Sep 2022 (2022-10-12) - For Ignite milestone Sep 7, 2022
@jiasli
Copy link
Member Author

jiasli commented Sep 7, 2022

CI task "Check the Format of Pull Request Title and Content" failed because of some example commands in the PR description @wangzelin007:

https://dev.azure.com/azure-sdk/public/_build/results?buildId=1835012&view=logs&j=a8943ac2-38d7-5792-f2a7-5f4fd06db24e&t=3ed51913-4dd7-564f-a8d8-fda07de13946&l=115

['-c', 'a b', 'a b']: missing space after ]
                   ↑

@@ -1,217 +1,217 @@
$env:AZ_INSTALLER="MSI"
& "$PSScriptRoot\..\python.exe" -IBm azure.cli $args
& "$PSScriptRoot\..\python.exe" -IBm azure.cli @args
Copy link
Member Author

Choose a reason for hiding this comment

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

The disadvantage of using @args is also obvious, as we can't pass "a b" string as ("a", "b") now.

@wangzelin007
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Assign Auto assign by bot Core CLI core infrastructure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot pass PowerShell array as $mylist to Azure CLI
3 participants