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

Xonsh returns zero exit code when a command fails in a script #3309

Closed
certik opened this issue Sep 9, 2019 · 10 comments · Fixed by #3465
Closed

Xonsh returns zero exit code when a command fails in a script #3309

certik opened this issue Sep 9, 2019 · 10 comments · Fixed by #3465
Labels

Comments

@certik
Copy link

certik commented Sep 9, 2019

I haven't had time to triage it yet, but here is what I executed:

$ xonsh ci/build.xsh && echo "OK"
...
Interactive Fortran. Experimental prototype, not ready for end users.
  * Use Ctrl-D to exit
  * Use Enter to submit
Try: function f(); f = 42; end function
>>> Input:
fxunction f(); f = 42; end function
input:1:11 syntax error: token type 'identifier' is unexpected here
fxunction f(); f = 42; end function
          ^
terminate called after throwing an instance of 'LFortran::ParserError'
  what():  syntax error
Aborted (core dumped)
OK

The C++ code exited with an uncaught exception which generates a non-zero exit code as can be verified by:

$ ./lfortran < ../../../src/bin/example_input.txt  && echo "OK"
Interactive Fortran. Experimental prototype, not ready for end users.
  * Use Ctrl-D to exit
  * Use Enter to submit
Try: function f(); f = 42; end function
>>> Input:
fxunction f(); f = 42; end function
input:1:11 syntax error: token type 'identifier' is unexpected here
fxunction f(); f = 42; end function
          ^
terminate called after throwing an instance of 'LFortran::ParserError'
  what():  syntax error
Aborted (core dumped)
$

And yet Xonsh returns zero exit code and the "OK" gets printed.

This is a serious issue as I use Xonsh on a CI and I need it to fail when an exception occurs in my C++ program.

Here is the ci/build.xsh (as you can see I enabled the equivalent of set -e):

#!/usr/bin/env xonsh

$RAISE_SUBPROC_ERROR = True
trace on

$lfortran_version=$(cat version).strip()
cd lfortran-$lfortran_version

cd test-bld
if $WIN == "1":
    ./src/lfortran/tests/Release/test_llvm -s
else:
    ./src/lfortran/tests/test_llvm -s
ctest --output-on-failure
src/bin/lfortran < ../src/bin/example_input.txt
cd ..
@gforsyth
Copy link
Collaborator

Closed via #3307

@certik
Copy link
Author

certik commented Sep 21, 2019

#3307 couldn't have possibly fixed this problem. I can't reopen this issue, but the bug is most probably still there.

@gforsyth
Copy link
Collaborator

Whoops. Thanks @certik! I was confusing this with #3292

@gforsyth gforsyth reopened this Sep 21, 2019
@certik
Copy link
Author

certik commented Sep 22, 2019

@gforsyth no worries! I figured that was the case.

@scopatz scopatz added the bug label Oct 1, 2019
@scopatz
Copy link
Member

scopatz commented Oct 2, 2019

Hi @certik - I can't for the life of me reproduce this with a simpler example. Consider the following bad.xsh scripts:

#!/usr/bin/env xonsh

$RAISE_SUBPROC_ERROR = True

aliases['bad'] = lambda: 42
![bad]

print("You can't see me")

or

#!/usr/bin/env xonsh

$RAISE_SUBPROC_ERROR = True

bash -c "exit 42"

print("You can't see me")

or

#!/usr/bin/env xonsh

$RAISE_SUBPROC_ERROR = True

python -c "import sys; sys.exit(42)"

print("You can't see me")

At the command line, all of these produce:

scopatz@artemis ~/temp $ xonsh bad.xsh && echo OK
Traceback (most recent call last):
  File "/home/scopatz/.local/bin/xonsh", line 4, in <module>
    main()
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 24011, in main
    _failback_to_other_shells(args, err)
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23975, in _failback_to_other_shells
    raise err
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 24009, in main
    return main_xonsh(args)
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 24053, in main_xonsh
    args.file, shell.execer, glb=shell.ctx, loc=None, mode="exec"
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 3038, in run_script_with_cache
    run_compiled_code(ccode, glb, loc, mode)
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 2943, in run_compiled_code
    func(code, glb, loc)
  File "bad.xsh", line 9, in <module>
    python -c "import sys; sys.exit(42)"
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 22466, in subproc_captured_hiddenobject
    return run_subproc(cmds, captured="hiddenobject")
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 22429, in run_subproc
    command.end()
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 15671, in end
    self._end(tee_output=tee_output)
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 15690, in _end
    self._raise_subproc_error()
  File "/home/scopatz/.local/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 15816, in _raise_subproc_error
    raise subprocess.CalledProcessError(rtn, spec.args, output=self.output)
subprocess.CalledProcessError: Command '['python', '-c', 'import sys; sys.exit(42)']' returned non-zero exit status 42.
scopatz@artemis ~/temp $ _.returncode
1

Are you sure the lfortran actually has the proper returncode?

@certik
Copy link
Author

certik commented Oct 15, 2019

Here is how to reproduce this very important bug. Create this file a.cpp:

#include <iostream>

int main() {
    throw std::runtime_error("xx");
    return 0;
}

Compile using:

g++ a.cpp

And test that it returns a non-zero exit code:

$ ./a.out && echo "OK"
terminate called after throwing an instance of 'std::runtime_error'
  what():  xx
Aborted (core dumped)
$

Now call this as part of a Xonsh script:

#!/usr/bin/env xonsh

$RAISE_SUBPROC_ERROR = True
trace on

./a.out

as follows:

$ xonsh build.xsh && echo "OK"
build.xsh:6:./a.out
terminate called after throwing an instance of 'std::runtime_error'
  what():  xx
Aborted (core dumped)
OK
$

As you can see, Xonsh returns a zero exit value.

@scopatz
Copy link
Member

scopatz commented Mar 1, 2020

@certik - we figured this out, right? but did a fix go in?

@certik
Copy link
Author

certik commented Mar 1, 2020 via email

@scopatz
Copy link
Member

scopatz commented Mar 1, 2020

I found the fix on a branch! Just need to add tests

@scopatz scopatz mentioned this issue Mar 1, 2020
@scopatz
Copy link
Member

scopatz commented Mar 1, 2020

Potential fix in #3465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants