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

Equivalent of bash set -e #3415

Closed
pauldambra opened this issue Mar 24, 2017 · 12 comments
Closed

Equivalent of bash set -e #3415

pauldambra opened this issue Mar 24, 2017 · 12 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-Fixed The issue is fixed. WG-Language parser, language semantics
Milestone

Comments

@pauldambra
Copy link

pauldambra commented Mar 24, 2017

I asked this question on twitter and it was suggested I post here.

Many people consider it good practice to start a bash script.

#! /bin/bash
set -eu

set -e tells the script to stop on first error
set -u tells the script to not allow undeclared variables

Although not everyone agrees

I think set -u and Set-StrictMode -Version 1.0 are equivalent.

I just wrote a set of Powershell scripts for managing an elasticsearch server. Then a few top-level scripts to tie them together.

It would have been really useful to be able to write something equivalent to...

set -e

$someValue = .\server-task-1.ps1
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1

... but I couldn't find how to and ended up with something more like...


$someValue = .\server-task-1.ps1
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

.\server-task-2.ps1  -using $someValue
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

.\task-that-requires-everything-else-suceeded.ps1
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

Apologies if this is already possible and I've missed it...

@SteveL-MSFT
Copy link
Member

$erroractionpreference = "stop" is similar to set -e, except it only works against cmdlets and not native commands

@SteveL-MSFT SteveL-MSFT added the Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif label Mar 25, 2017
@pauldambra
Copy link
Author

As someone fresh to Powershell that's a confusing distinction.

So would $erroractionpreference = "stop" work for my example above..?

i.e.

$erroractionpreference = "stop"

$someValue = .\server-task-1.ps1

# does not run the next two commands if server-task-1.ps1 exits with code != 0
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1

but

$erroractionpreference = "stop"

$someValue = .\server-task-1.exe

# does run the next two commands _even though_ server-task-1.exe exits with code != 0
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1

@SteveL-MSFT
Copy link
Member

$erroractionpreference = "stop" only works if Write-Error is called (such as an exception is raised). It doesn't do any checks against $lastexitcode. We can certainly look into adding another preference variable to throw an exception if $lastexitcode != 0.

@mopadden
Copy link

mopadden commented Mar 29, 2017

Would probably want to throw the exception only in a context where Powershell also sets $? to false as a result.

Throwing an exception in other situations where lastexitcode != 0 would not give a result equivalent to "set -e" in bash.

https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
Edit: the idea is to throw an error on failure where the success/failure of an application is not tested.

@SteveL-MSFT SteveL-MSFT added the Issue-Enhancement the issue is more of a feature request than a bug label Mar 29, 2017
@SteveL-MSFT SteveL-MSFT added this to the 6.1.0 milestone Mar 29, 2017
@SteveL-MSFT SteveL-MSFT added Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime WG-Language parser, language semantics and removed Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif WG-Engine core PowerShell engine, interpreter, and runtime labels Mar 29, 2017
@mopadden
Copy link

mopadden commented Apr 7, 2017

Hi, i've done some work on this, can I ask for an assignment?

mopadden pushed a commit to mopadden/PowerShell that referenced this issue Apr 10, 2017
Add preference to throw exception on $lastexitcode!=0, like bash ’set -e’.
Only throw with commands *not* used in an expression/if/loop value.

$NativeCommandPipeFailPreference=“continue”: like ‘set -e;set +o pipefail’.
An exception is thrown for a native command giving a non-zero exit code,
if it is the the last command in the pipeline.

$NativeCommandPipeFailPreference=“silentlycontinue”: like ‘set +e’.
No exception is thrown on non-zero exit codes. This is the default.

$NativeCommandPipeFailPreference=“stop”: like ‘set -e;set -o pipefail’.
An exception is thrown for a native command giving a non-zero exit code.

With $NativeCommandExceptionPreference "continue" (the default),
a RuntimeException is thrown, handing off to PowerShell error handling.

With $NativeCommandExceptionPreference “stop”, an ExitException is thrown,
terminating the shell regardless of other PowerShell error handling.
@SteveL-MSFT
Copy link
Member

@mopadden consider it assigned, GitHub doesn't allow assigning to people without write access

@SteveL-MSFT
Copy link
Member

Assigning to @daxian-dbw who's assigned to the PR so no one else duplicates this work

@SteveL-MSFT
Copy link
Member

Anyone who's interested in this should review the RFC: https://github.com/PowerShell/PowerShell-RFC/pull/88/files

@choovick
Copy link

+1, very inconvenient to check $LastExitCode, especially when using PS for build scripts with multiple steps...

@SteveL-MSFT SteveL-MSFT removed the Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors label Jul 11, 2018
@b-rad15
Copy link

b-rad15 commented Jun 29, 2021

Since this is the first search result, linking the most recent thread on this RFC that I can find for anyone interested in implementing it PowerShell/PowerShell-RFC#277

@daxian-dbw
Copy link
Member

The PR that implements the RFC was already merged as an experimental feature: #15897
So, closing this issue.

@nhooyr
Copy link

nhooyr commented Dec 16, 2022

Is the equivalent of set -eu then:

Enable-ExperimentalFeature PSNativeCommandErrorActionPreference
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-Fixed The issue is fixed. WG-Language parser, language semantics
Projects
Developer Experience/SDK
  
Awaiting triage
Development

Successfully merging a pull request may close this issue.

8 participants