Skip to content

Commit

Permalink
Hab launch returns the error code returned by the called alias
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Jan 15, 2024
1 parent 92409dd commit 5084337
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
10 changes: 6 additions & 4 deletions bin/hab.bat
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ if exist %temp_launch_file% (
)
)

:: the config scripts turn echo back on, turn it off again
@ECHO OFF
:: the config scripts turn echo back on, Make sure to prefix future commands
:: with @ to prevent printing them to the console

:: Remove the temp directory and its contents
RMDIR /S /Q %temp_directory%
@RMDIR /S /Q %temp_directory%

@ECHO ON
:: Ensure the errorlevel is reported to the calling process. This is needed
:: when calling hab via subprocess/QtCore.QProcess calls to receive errorlevel
@exit /b %ERRORLEVEL%
3 changes: 3 additions & 0 deletions bin/hab.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ elseif (Test-Path $temp_config -PathType Leaf)

# Remove the temp directory if it exists
Remove-Item $temp_directory -Force -Recurse -erroraction 'silentlycontinue'

# Ensure the exit-code is reported to the calling process.
exit $LASTEXITCODE
10 changes: 6 additions & 4 deletions hab/parsers/hab_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,13 @@ def shell_formats(cls, ext):
if ext in (".bat", ".cmd"):
ret["launch"] = 'cmd.exe /k "{path}"\n'
elif ext == ".ps1":
ret[
"launch"
] = 'powershell.exe{launch_args} -ExecutionPolicy Unrestricted . "{path}"\n'
ret["launch"] = (
'powershell.exe{launch_args} -ExecutionPolicy Unrestricted -File "{path}"\n'
"exit $LASTEXITCODE\n"
)
elif ext in (".sh", ""): # Assume no ext is a .sh file
ret["launch"] = 'bash --init-file "{path}"\n'
# `set -e` propagates the error code returned by bash to the caller
ret["launch"] = 'set -e\nbash --init-file "{path}"\n'

return ret

Expand Down
2 changes: 2 additions & 0 deletions hab/templates/config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ function {{ alias }}() {
{% if launch_info %}
# Run the requested command
{{ launch_info.key }}{{ launch_info.args }}
# Ensure the exit-code is reported to the calling process.
exit $LASTEXITCODE
{% endif %}
5 changes: 5 additions & 0 deletions hab/templates/config.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Customize the prompt
export PS1="[{{ hab_cfg.uri }}] $PS1"

# Exit immediately if a command exits with a non-zero status.
# This ensures that if any exit codes are encountered it gets propagated to
# whatever originally called hab.
set -e

# Setting global environment variables:
{% for key, value in hab_cfg.environment.items() %}
{% if value %}
Expand Down

0 comments on commit 5084337

Please sign in to comment.