zsh-workers
 help / color / mirror / code / Atom feed
From: Philippe Altherr <philippe.altherr@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: "Lawrence Velázquez" <larryv@zsh.org>, zsh-workers@zsh.org
Subject: Re: Inconsistent behavior of ERR_EXIT with conditionals
Date: Wed, 9 Nov 2022 05:11:37 +0100	[thread overview]
Message-ID: <CAGdYchtyqMTAWn6yVJpJypOZ-Gyj1Fm6+kmsztm8hKXki5FSLg@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7ZkWP_O08ETh-UbOguXngACQPGS+Z9rwUCPVez8ZTq=gg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4100 bytes --]

>
> All of 1,3,4 are fixed by my patch in workers/50897


That's great! It's a small win but I actually (re-)stumbled on the problem
of conditional expressions because of one of these cases. So it's good to
see that they can be fixed.


My ultimate goal is to be able to run Zsh scripts with the guarantee that
if any command unexpectedly fails (i.e., if any command whose result is not
otherwise checked returns a non-zero exit status), then the whole *script* (not
just some subshells) stops immediately. Wouldn't you agree that this would
be a useful feature?

The question is how can this be achieved. On the surface, it looks like
enabling ERR_EXIT does the trick. However there are several cases where
ERR_EXIT fails to do the job. These cases are of two categories:

   1. *Non-triggering:* In some contexts, commands whose result is not
   otherwise checked don't trigger a shell exit when they return a non-zero
   exit status even when ERR_EXIT is enabled, e.g., the "false" command in
   "{false; true} && true" doesn't trigger a shell exit.
   2. *Non-propagation:* In some contexts, errors in subshells don't
   propagate to the parent shell, e.g., the "false" in "local var=$(false)"
   triggers an exit in the subshell of the command substitution but the
   assignment ignores the result of the command substitution and thus the
   parent shell fails to exit in turn.

I hoped that some of these cases could be "fixed" but I have now checked
the POSIX specification and as you both pointed out, for most of them POSIX
specifies that they have to work as they currently do (this doesn't include
Lawrence's example 1,3,4, which should indeed be fixed).

The first developer is wrong.  That's not what -e is for.  A script
> should be correct WITHOUT the use of -e ... the purpose of -e is to
> uncover cases where the developer made a mistake, not to be an
> integral part of the script function.


I can agree with that but consider that the developer's mistake was to use
a ";" instead of an "&&" in the "backup" function. My broader point was
that the same error (or developer mistake) in a function "foo" triggers an
exit if "foo" is called from a plain statement but not if it's called from
within a condition. Wouldn't you agree that it's unfortunate that the same
error/mistake may or may not trigger an exit depending on whether it's
executed outside or inside a condition?

Again, wrong.  "{ false; true }" is a single statement because of the
> braces.  When that statement is followed by || the result of the
> ENTIRE statement is considered to have been "checked".
> Similarly, in "if false; true; then" the conditional part is
> considered as a single statement whose result is "checked".


Indeed, POSIX states "The -e setting shall be ignored when executing the
compound list following the while, until, if, or elif reserved word, a
pipeline beginning with the ! reserved word, or any command of an AND-OR
list other than the last.", so there is unfortunately no way this can be
changed, at least in the context of ERR_EXIT.


Is all hope lost? Not necessarily. The non-propagation issues can be worked
around. That's what my zabort
<https://github.com/paltherr/zabort/blob/main/src/bin/zabort.zsh> does by
configuring a ZERR trap that forcibly kills all parent shells from within
the subshell where the error occurred. Unfortunately, I don't see how the
non-triggering issues could be worked around. For these some change is
needed in Zsh but I agree that changing the behavior of ERR_EXIT isn't the
way to go as it should remain POSIX compliant. What could work is to
implement a new shell option ERR_EXIT_STRICT, which triggers an exit on any
command that returns a non-zero exit status and whose result isn't checked
otherwise. Only one of ERR_EXIT and ERR_EXIT_STRICT could be enabled at any
given time.

*Would you agree to add a new shell option if it allows to run Zsh scripts
such that if any command unexpectedly fails the script immediately stops
(and its implementation doesn't require too complex changes)?* If yes, I
may look into implementing it.

Philippe

[-- Attachment #2: Type: text/html, Size: 5132 bytes --]

  reply	other threads:[~2022-11-09  4:12 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 16:37 Philippe Altherr
2022-11-06 20:45 ` Bart Schaefer
2022-11-07  3:50   ` Bart Schaefer
2022-11-07  5:35     ` [PATCH] " Bart Schaefer
2022-11-07  9:44       ` Peter Stephenson
2022-11-08  1:20         ` Bart Schaefer
2022-11-08  4:58     ` Philippe Altherr
2022-11-08  5:36       ` Bart Schaefer
2022-11-08  8:04         ` Lawrence Velázquez
2022-11-08 18:51           ` Philippe Altherr
2022-11-08 19:20             ` Lawrence Velázquez
2022-11-08 23:28             ` Bart Schaefer
2022-11-09  4:11               ` Philippe Altherr [this message]
2022-11-09  6:00                 ` Bart Schaefer
2022-11-09 14:22                   ` Philippe Altherr
2022-11-10  1:00                     ` Bart Schaefer
2022-11-10  5:09                       ` Bart Schaefer
2022-11-11  3:04                         ` Philippe Altherr
2022-11-11  4:06                           ` Lawrence Velázquez
2022-11-11  4:09                           ` Eric Cook
2022-11-08 23:11           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGdYchtyqMTAWn6yVJpJypOZ-Gyj1Fm6+kmsztm8hKXki5FSLg@mail.gmail.com \
    --to=philippe.altherr@gmail.com \
    --cc=larryv@zsh.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).