Open ading2210 opened 3 months ago
Indeed, $RAISE_SUBPROC_ERROR
is to raise error in any case when any command fails but set -e
treat the line as whole:
Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists of Commands), or a compound command (see Compound Commands) returns a non-zero status.
So we need additional setting e.g. $XONSH_RAISE_SUBPROC_PIPELINE_ERROR
to do the same as bash. PR is welcome!
JFYI. For this:
set -e
ls nonono || true # false || true
echo "success"
the xonsh equvalent for now is this:
$RAISE_SUBPROC_ERROR = True
try:
ls nonono
except: # OR except subprocess.CalledProcessError:
pass
echo "success"
You can create syntax sugar for this using macro call:
import subprocess
def ignore_error(cmd):
"""Ignore failing of the command."""
try:
execx(cmd)
except subprocess.CalledProcessError:
pass
$RAISE_SUBPROC_ERROR = False
ignore_error!(echo 1 and (ls nonono or echo 2))
echo success
# success
$RAISE_SUBPROC_ERROR = True
ignore_error!(echo 1 and (ls nonono or echo 2))
echo success
# success
I'm aware that I can do a try-except block to ignore errors, though that still doesn't help with the underlying problem of boolean logic being broken with subprocess return codes.
I figured this out the case.
What bash doing. In fact it implicitly overrides the execution of a logical expression and the execution of process. If we run echo 1 and ls nonono
the result of this logical will be treated as "return code" and if it's not 0 and set -e
the error will be raised.
In xonsh we have separation between process running and logical expression running. If we run echo 1 and ls nonono
we have 2 separate processes and logical expression and RAISE_SUBPROC_ERROR will work on process level.
In fact in this issue was requested an additional mode like RAISE_COMMAND_ERROR that will work for the whole logical expression.
Current behavior:
$RAISE_SUBPROC_ERROR=False
echo 1 and (ls no or echo 2)
# run logical: `echo 1`, `ls no`, `echo 2`.
__xonsh__.history[-1]
# rtn=0
echo 1 and (echo 2 or ls no)
# run logical: `echo 1`, `echo 2`.
__xonsh__.history[-1]
# rtn=0
$RAISE_SUBPROC_ERROR=True
echo 1 and (ls no or echo 2)
# run logical: `echo 1`, `ls no` (raise and stop).
__xonsh__.history[-1]
# rtn=2
echo 1 and (echo 2 or ls no)
# run logical: `echo 1`, `echo 2`.
__xonsh__.history[-1]
# rtn=0
xonfig
Expected Behavior
In bash, doing
cmd || true
will ignore any errors that the command generates ifset -e
is on. I would expect that the behavior is similar in Xonsh with$RAISE_SUBPROC_ERROR
, which is the equivalent setting.For example:
Current Behavior
A
subprocess.CalledProcessError
is called if any of the commands in the expression fail.Traceback (if applicable)
Steps to Reproduce
$RAISE_SUBPROC_ERROR = True
false || true
For community
⬇️ Please click the 👍 reaction instead of leaving a
+1
or 👍 comment