question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling the CLI from the outside with an empty command string (-c '') crashes PowerShell

See original GitHub issue

From within PowerShell, running pwsh -c '' (pwsh -Command '') provides a helpful error message (followed by the CLI syntax):

Cannot process the command because of a missing parameter. A command must follow -Command.

By contrast, when pwsh is called from the outside, it crashes.

Steps to reproduce

Run from either cmd.exe or bash:

pwsh -c ""

Expected behavior

The following should print to stderr and a nonzero exit code should be reported, analogous to what happens when calling from PowerShell.

Cannot process the command because of a missing parameter. A command must follow -Command.

(CLI syntax diagram)

Actual behavior

The pwsh process crashes as follows:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at System.String.IndexOf(String value, Int32 startIndex, Int32 count, StringComparison comparisonType)
   at System.String.IndexOf(String value, StringComparison comparisonType)
   at Microsoft.PowerShell.CommandLineParameterParser.MatchSwitch(String switchKey, String match, String smallestUnambiguousMatch)
   at Microsoft.PowerShell.CommandLineParameterParser.EarlyParse(String[] args)
   at Microsoft.PowerShell.UnmanagedPSEntry.Start(String consoleFilePath, String[] args, Int32 argc)
   at Microsoft.PowerShell.ManagedPSEntry.Main(String[] args)

Environment data

PowerShell Core 7.0.0-preview.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Oct 19, 2020

Agreed, @iSazonov.

On further reflection, I think I was wrong about the expected behavior in the OP:

Since an argument is passed to -c in this case, the syntax requirements are met, so we shouldn’t treat this the same as neglecting to pass an argument to -c.

After the "" are stripped by PowerShell’s CLI, a verbatim empty string remains (as opposed to a string literal representing the empty string). This is effectively the absence of any statement.

As a no-op, it is by definition successful, and the exit should be 0. This is consistent with how POSIX-compatible shells handle this case.

Since the current preview already exhibits this behavior, I think we can close this issue.

Note that the only reason it behaves differently when called from PowerShell is the broken handling of quoted arguments as described in #1995, which causes empty-string literals not to be passed at all, so that pwsh -c "" is unexpectedly the same as pwsh -c

1reaction
iSazonovcommented, Oct 3, 2019

@chuanjiao10 I think it is one issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell: escaping rules for CLI calls
tl;dr. On PowerShell's command line only, use \" to pass a " through to the code that -c ( -Command ) should execute....
Read more >
Setting and using variable within same command line in ...
In Bash, I can do EDITOR=vim command and command will be run with EDITOR set to vim , but this won't affect the...
Read more >
Troubleshooting Windows Subsystem for Linux
To change the default user to root use this command in PowerShell: C:\> lxrun /setdefaultuser root and then run Bash.exe to log in:...
Read more >
Debugging PowerShell script in Visual Studio Code – Part 1
One advantage of this approach is that a crash of the PowerShell Editor Services process doesn't cause Visual Studio Code to crash.
Read more >
Troubleshoot AWS CLI errors - AWS Command Line Interface
Diagnose and fix common AWS Command Line Interface (AWS CLI) errors. ... Here's an example of a command run with and without the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found