From 1457082fd02b1798725baf4ea288cc4cc699f06d Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 17 Mar 2016 13:32:10 -0700 Subject: [PATCH] Fix powershell -c 'throw error' When executing a single command block, two bugs existed. 1) Exceptions were not reported, instead it core dumped 2) The error code was not set, instead 0 was returned Fixing 1) required calling `Execute` instead of `ExecuteHelper` as the latter handles exception through `ReportException`. Fixing 2) required setting the listener's error code when an exception was thrown, and resetting it for each command when an interactive session is running. --- src/Microsoft.PowerShell.Linux.Host/main.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Linux.Host/main.cs b/src/Microsoft.PowerShell.Linux.Host/main.cs index be52ea0715..2411e79d5c 100644 --- a/src/Microsoft.PowerShell.Linux.Host/main.cs +++ b/src/Microsoft.PowerShell.Linux.Host/main.cs @@ -233,7 +233,7 @@ OPTIONS // run the initial script if (initialScript != null) { - ExecuteHelper(initialScript, null); + Execute(initialScript); } } @@ -432,6 +432,9 @@ OPTIONS { if (e != null) { + // Return non-zero exit code if an exception is thrown + this.ExitCode = 1; + object error; IContainsErrorRecord icer = e as IContainsErrorRecord; if (icer != null) @@ -547,6 +550,9 @@ OPTIONS // the user calling "exit". while (!this.ShouldExit && this.myHost.Runspace != null) { + // Reset exit code for each command + this.ExitCode = 0; + // If the prompt function failed for any reason, use a sane default string prompt; try