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

Conversation

brycegbrazen
Copy link
Contributor

Adds failing test for #1800.

After digging into this there are three important Powershell automatic variables to take note of.

  • $LASTEXITCODE
  • $?
  • $ERROR

The first two, $LASTEXITCODE and $? are used in the code currently.

However, after testing the issue in PowerShell directly, it seems that the issue stems from an issue stemming from PowerShell itself.

When using aliases in PowerShell, if the command being called is not found, and error is printed to stderr, but neither the $LASTEXITCODE or $? variables are filled out correct to represent this.

For example, in PowerShell I ran:

PS C:\Users\bryce.gattis> hython
hython : The term 'hython' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ hython
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (hython:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\bryce.gattis> $?
False
PS C:\Users\bryce.gattis> $LASTEXITCODE

Then I ran the following that does the same thing but using aliases:

PS C:\Users\bryce.gattis> function python() { hython @args }
PS C:\Users\bryce.gattis> python
hython : The term 'hython' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:21
+ function python() { hython @args }
+                     ~~~~~~
    + CategoryInfo          : ObjectNotFound: (hython:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\bryce.gattis> $?
True
PS C:\Users\bryce.gattis> $LASTEXITCODE

Interestingly, it seems like the $ERROR automatic variable, correctly captures the error message in both cases.

PS C:\Users\bryce.gattis> $ERROR
hython : The term 'hython' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:21
+ function python() { hython @args }
+                     ~~~~~~
    + CategoryInfo          : ObjectNotFound: (hython:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Not sure what the implications would be of additionally relying on the presence of an error in the $ERROR variable, but seems to be something that is slipping through the cracks right now. Perhaps there's a better fix for this?

Signed-off-by: brycegbrazen <[email protected]>
Signed-off-by: Jean-Christophe Morin <[email protected]>
@JeanChristopheMorinPerso JeanChristopheMorinPerso force-pushed the powershell_alias_non_existent_command_exit_code branch from 9b7d8ba to 774963e Compare October 14, 2024 19:39
@JeanChristopheMorinPerso
Copy link
Member

Rebased to fix conflict

Copy link

codecov bot commented Nov 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.28%. Comparing base (491497f) to head (9b59dd2).
Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1801      +/-   ##
==========================================
- Coverage   59.30%   59.28%   -0.02%     
==========================================
  Files         126      126              
  Lines       17210    17217       +7     
  Branches     3015     3017       +2     
==========================================
+ Hits        10206    10207       +1     
- Misses       6319     6325       +6     
  Partials      685      685              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@brycegbrazen
Copy link
Contributor Author

brycegbrazen commented Nov 12, 2024

Had to change some -ErrorAction flags to Ignore so that expected errors would not be caught in the $Error array. Based on unit tests, it doesn't look like this change affects anything, but does fix the issue this PR is concerned with. Changing those flags fixed this error I was getting:

FAIL: test_rex_code (rez.tests.test_shells.TestShells)
Test that Rex code run in the shell creates the environment variable
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Code_forks\rez\.venv\lib\site-packages\rez\tests\util.py", line 271, in wrapper
    func(self, shell=shell)
  File "D:\Code_forks\rez\.venv\lib\site-packages\rez\tests\test_shells.py", line 502, in test_rex_code
    _execute_code(_rex_appending, expected_output)
  File "D:\Code_forks\rez\.venv\lib\site-packages\rez\tests\test_shells.py", line 371, in _execute_code
    self.assertEqual(p.returncode, 0)
AssertionError: 1 != 0 (in shell 'powershell')

Let me know what you think @JeanChristopheMorinPerso!

@brycegbrazen brycegbrazen changed the title PowerShell alias calling non-existent command returns 0 exit code failing test PowerShell alias calling non-existent command returns 0 exit code Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants