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

PowerShell alias calling non-existent command returns 0 exit code #1801

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/rez/tests/test_shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,22 @@ def _make_alias(ex):
out, _ = p.communicate()
self.assertEqual(1, p.returncode)

@per_available_shell()
def test_alias_non_existent_command_return_code(self, shell):
"""Ensure return codes are correct while using aliases."""
config.override("default_shell", shell)

def _make_alias(ex):
ex.alias('my_alias', 'a_non_existent_executable')

r = self._create_context([])
p = r.execute_shell(command='my_alias',
actions_callback=_make_alias,
stdout=subprocess.PIPE)

out, _ = p.communicate()
self.assertNotEqual(0, p.returncode)


if __name__ == '__main__':
unittest.main()
10 changes: 5 additions & 5 deletions src/rezplugins/shell/_utils/powershell_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _record_shell(ex, files, bind_rez=True, print_msg=False):
# only the bool $? var is set.
#
executor.command(
"if(! $? -or $LASTEXITCODE) {\n"
"if(! $? -or $LASTEXITCODE -or $error) {\n"
" if ($LASTEXITCODE) {\n"
" exit $LASTEXITCODE\n"
" }\n"
Expand Down Expand Up @@ -248,7 +248,7 @@ def prependenv(self, key, value):
# Be careful about ambiguous case in pwsh on Linux where pathsep is :
# so that the ${ENV:VAR} form has to be used to not collide.
self._addline(
'Set-Item -Path "Env:{0}" -Value ("{1}{2}" + (Get-ChildItem -ErrorAction SilentlyContinue "Env:{0}").Value)'
'Set-Item -Path "Env:{0}" -Value ("{1}{2}" + (Get-ChildItem -ErrorAction Ignore "Env:{0}").Value)'
.format(key, value, self.pathsep)
)

Expand All @@ -257,15 +257,15 @@ def appendenv(self, key, value):

# Be careful about ambiguous case in pwsh on Linux where pathsep is :
# so that the ${ENV:VAR} form has to be used to not collide.
# The nested Get-ChildItem call is set to SilentlyContinue to prevent
# The nested Get-ChildItem call is set to Ignore to prevent
# an exception of the Environment Variable is not set already
self._addline(
'Set-Item -Path "Env:{0}" -Value ((Get-ChildItem -ErrorAction SilentlyContinue "Env:{0}").Value + "{1}{2}")'
'Set-Item -Path "Env:{0}" -Value ((Get-ChildItem -ErrorAction Ignore "Env:{0}").Value + "{1}{2}")'
.format(key, os.path.pathsep, value))

def unsetenv(self, key):
self._addline(
'Remove-Item -ErrorAction SilentlyContinue "Env:{0}"'.format(key)
'Remove-Item -ErrorAction Ignore "Env:{0}"'.format(key)
)

def resetenv(self, key, value, friends=None):
Expand Down
Loading