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

exit status 0 on syntax errors, when reading scrip from stdin #21784

Closed
5 tasks done
philcerf opened this issue May 9, 2024 · 9 comments
Closed
5 tasks done

exit status 0 on syntax errors, when reading scrip from stdin #21784

philcerf opened this issue May 9, 2024 · 9 comments
Labels
Resolution-By Design The reported behavior is by design. WG-Interactive-Console the console experience WG-Reviewed A Working Group has reviewed this and made a recommendation

Comments

@philcerf
Copy link

philcerf commented May 9, 2024

Prerequisites

Steps to reproduce

Hey.

This also seems to go back to the days of powershell.exe and seems particular disturbing.

When running a script with a deliberate syntax error (the -EQfoo):

$ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit 44}

like this:

$ pwsh -NonInteractive -NoProfile -Command '$ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit 44}' ; echo exit status: $?

The exit status returned by PowerShell is non-zero (as it should be):

ParserError: 
Line |
   1 |  $ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit …
     |                                              ~~~~~~
     | Unexpected token '-EQfoo' in expression or statement.
exit status: 1

When running it however via stdin like so:

$ { pwsh -NonInteractive -NoProfile -Command - <<'END'
$ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit 44}

END
} ; echo exit status: $?

The exit status is 0:

ParserError: 
Line |
   1 |  $ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit …
     |                                              ~~~~~~
     | Unexpected token '-EQfoo' in expression or statement.
exit status: 0

Expected behavior

use non-zero exit status on syntax error, even when command is read from stdin

Actual behavior

uses 0 as exit status

Error details

No response

Environment data

N/A

Visuals

No response

@philcerf philcerf added the Needs-Triage The issue is new and needs to be triaged by a work group. label May 9, 2024
@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented May 9, 2024

The good news is I can just refer back to 21808 for much of this.

My explanations often start with PowerShell is not a UNIX shell. UNIX programs are pipe/stream based, PowerShell is record based.

Two more details are significant

  • in record processing, the failure of one record does not automatically mean abort or fail the following records, each is treated on its own merits
  • PowerShell does not parse character by character looking for ends of statements then executing them, then going back for more. It is not a UNIX shell, it is record based and attempts to parse the whole record. If that parsing fails then none of the record is executed.

Lets see this in action

pwsh -NonInteractive -NoProfile -Command '-' <<'END'
$ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit 44}
$ErrorActionPreference
END

with the result

ParserError:
Line |
   1 |  $ErrorActionPreference = "Stop"; try{ $null -EQfoo $null} catch {exit …
     |                                              ~~~~~~
     | Unexpected token '-EQfoo' in expression or statement.
Continue

What you see is that none of the first record was executed. The assignment of $ErrorActionPreference never happened.
So that record failed and on to the next record which shows the current state of $ErrorActionPreference.

@mklement0
Copy link
Contributor

Fundamentally, a parser error cannot be handled with runtime constructs such as $ErrorActionPreference = "Stop" and try / catch.

It is the statement-by-statement, REPL-like processing exhibited by -Command - that isolates a parser error to a given statement, but the fact that a parser error in the last statement doesn't result in a nonzero exit code should indeed be considered a bug, as argued in detail in #21808 (comment)

@philcerf
Copy link
Author

@rhubarb-geek-nz Well I guess my reply would be mostly what I've wrote over in the other ticket, i.e. that it's at least undocumented, that - for -Command and -File make them behave completely differently than with values other than -... but more practically spoken: it seems not so good design with, to have a single option, e.g. -Command behave so dramatically different just by changing the source of the data.

If there'd really be a need for a mode of that semi-interactive parsing, it would IMO be better to have separate option for that and not for -Command which behaves just as expected with real commands given.

@mklement0 My main point here wasn't about the try not working (which I think is obvious in case of a syntax error), it was about 0 being returned despite the syntax error... so a caller has no chance of knowing that it didn't work.

@mklement0
Copy link
Contributor

so a caller has no chance of knowing that it didn't work.

Yes. That's why I said that what you've uncovered should indeed be considered a bug.

@theJasonHelmick theJasonHelmick added WG-NeedsReview Needs a review by the labeled Working Group WG-Interactive-Console the console experience labels May 15, 2024
@SeeminglyScience
Copy link
Collaborator

The Interactive WG discussed this. The feature -Command - is designed to imitate a REPL, so the behavior is by design. While we understand and agree that this feature should have been appropriately named to indicate the functionality it offers (which is unfortunately different than how most applications handle -), we cannot change this now.

@SeeminglyScience SeeminglyScience added Resolution-By Design The reported behavior is by design. WG-Reviewed A Working Group has reviewed this and made a recommendation and removed Needs-Triage The issue is new and needs to be triaged by a work group. WG-NeedsReview Needs a review by the labeled Working Group labels May 22, 2024
@mklement0
Copy link
Contributor

@philcerf, to bring closure to the specific aspect of the -Command - behavior that prompted this issue (the exit code):

As it turns out, the observed behavior is actually consistent with the pseudo-interactive, REPL-like behavior (which has just been declared to be by design), and the reason is now mentioned in the doc-update PR that @sdwheeler has since submitted:

  • An interactively submitted, syntactically broken command - i.e. one that cannot even be parsed - does not cause $? to be set: the previous (non-broken) command's $? value remains in effect.

  • However, with -Command it is the value of $? value after the last statement executed that translates into the process exit code ($true -> 0, $false -> 1); thus, if $? happened to be $true before executing the syntactically broken command, 0 ends up getting reported as the exit code.

A simple example:

@'
# If you uncomment the following statement, $LASTEXITCODE will be 1, because it is then
# this command's $? value that determines the exit code.
# 1 / 0

& {
  1 /  # This syntactically broken command has NO impact on the exit code.
}

'@ | pwsh -NoProfile -Command -; $LASTEXITCODE  # -> 0(!)

It follows that if you use the workaround to make -Command - behave like a script (enclose all code in & { ... } and append an extra newline), $LASTEXITCODE will always be 0 if the code is syntactically broken.


While the -Command behavior is consistent with the interactive experience it replicates, you could argue that it would make more sense to make parsing errors cause $? to reflect $false too, not just runtime errors.

While this wouldn't be much of an improvement in a true interactive session, it would help in the -Command - case when -Command - is used with the behave-like-a-script workaround.

@philcerf
Copy link
Author

TBH, I kinda stopped following these issues a bit, because the general way they seem to be dealt with is:
defining a completely broken behaviour as defective by design and thus allegedly valid.

I mean for me it's not really a big problem, since I simply pass my commands now as a multiline strings argument to -Command and with that, behaviour seems to be more or less what one would expect and what makes sense.

But I guess more people will fall into the traps involved when commands are read (non-interactively) from stdin with a behaviour that probably makes no sense in any situation.

Anyway, thanks for the workaround suggestions, but I rather go by my multiline string argument, because there's always the danger that there's more weird behaviour with the semi-interactive mode which no one can reasonably expect - I mean I found already a handful in just a few days.

Thanks,
Philippe.

Copy link
Contributor

This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Copy link
Contributor

microsoft-github-policy-service bot commented May 24, 2024

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

🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-By Design The reported behavior is by design. WG-Interactive-Console the console experience WG-Reviewed A Working Group has reviewed this and made a recommendation
Projects
None yet
Development

No branches or pull requests

5 participants