Skip to content

Set status to 124 also for set with unmatched globs #2791

Open

Description

@faho

It might be nice to have set foo *.foo return failure if the glob is unmatched to make

if set -l foo *.foo
    # something
else
   # something else
end

work.

(Note that currently set foo (command1) (command2) will return command2's status, so it should work that way for the globs as well or be changed for everything)

Activity

  1. krader1961 commented on Mar 3, 2016

    @krader1961
    Contributor

    Doesn't this boil down to set returning 1 if there are no values and 0 if at least one value is assigned? In other words, while the glob expansion scenario is one example of how it could be used it is equally applicable to set foo (command1) (command2)? In fact, I don't understand why the exit status of the command substitution bubbles up. If you replace "set" with "echo" that doesn't happen. Try it:

    $ set foo (true) (false)
    $ echo $status
    1
    $ echo (true) (false)
    $ echo $status
    0
    

    That is fubar.

  2. faho commented on Mar 3, 2016

    @faho
    MemberAuthor

    Currently, set just returns the status from before:

    false
    set foo bar
    echo $status
    true
    set foo bar
    echo $status

    This is, AFAIK, to enable just that if set idiom. Returning if a value was assigned doesn't quite have the same semantics - in fact we have a ton of set -l var calls, and most of those aren't meant to be false. Though I haven't found a use of that idiom where there's no value if it's true (there might be values if it's false).

    Anyway, we currently have that behavior and maybe we want to change it, but the question at hand here is how that should apply to globs, and for those either interpretation would lead to the same outcome.

  3. krader1961 commented on Mar 3, 2016

    @krader1961
    Contributor

    Yes, set returns the previous status and I'm arguing that is wrong. What other command behaves that way? Certainly not any external command. I'm also perplexed by your argument about set -l var statements since it doesn't seem to be relevant to the question at hand. Why would anyone write something like the following:

    set -l var; or die
    
    if not set -l var
        die
    end

    I'm worried that having glob expansion altering $status is not consistent with the design of fish or users expectations. It's probably also going to be hard to document how it would interact with other constructs like command substitution. Lastly, if a failed glob expansion is going to set $status to a non-zero value then that implies echo *.foo should behave like set -l foo *.foo if the expansion fails: both should leave $status non-zero but at the moment echo explicitly sets $status to zero.

  4. faho commented on Mar 3, 2016

    @faho
    MemberAuthor

    at the moment echo explicitly sets $status to zero.

    It doesn't - like everything but count, for and set, it exits with 124 and prints an error. This is since #2719.

    What I'm arguing for is that all three of those designated special commands (when it comes to globbing) should fail without the error - for won't execute, count will return 1 and set will return something non-zero (I don't care too much about the specific value, 124 is just the default).

    Yes, set returns the previous status and I'm arguing that is wrong.

    I think we have to think further about the implications of that - would that make any of our code fail? Would that make other code fail? Would that be worth it?

    I believe, at a cursory glance, that in our code it could work because I don't think the commands we're using in that way print something to stdout if they fail.

    However this would be an additional break (on top of the glob change) and I'm not sure we want to add it when we're preparing a new release.

  5. krader1961 commented on Mar 3, 2016

    @krader1961
    Contributor

    It doesn't - like everything but count, for and set, it exits with 124 and prints an error.

    Right. I got side-tracked by your observation about set passing through the status of the most recent command (which includes the last command substitution if any in that statement). But your assertion isn't completely true. The echo command is not exiting with status 124. The echo command isn't even run. It's the parser that's doing that and the parser special cases set and count, https://github.com/anordal/fish-shell/blob/0f025ec318582bfa5e3bba902a38ce435ff50bc4/src/parse_execution.cpp#L940, and the for command, https://github.com/anordal/fish-shell/blob/0f025ec318582bfa5e3bba902a38ce435ff50bc4/src/parse_execution.cpp#L1612. If you don't try to echo a glob that fails to expand then echo does in fact force the status to zero -- it does not pass thru the status of the most recent command like set does.

    count will return 1 and set will return something non-zero

    Count already returns one in this scenario because it's being invoked with zero arguments. There isn't any easy way to have set return STATUS_UNMATCHED_WILDCARD (124) in this scenario because it has no knowledge of how its arguments were specified; e.g., as literals, globs, command substitution, or var substitution.

    If we were somehow to modify the code to pass the status of glob expansion to the set command what should happen in the following examples (assuming *.foo fails to expand):

    set foo *.foo (false)
    

    Should the status be 124 or 1?

    set foo (false) *.foo
    

    Should the status be 1 or 124?

    set foo *.foo (true)
    

    Should the status be 124 or 0?

    echo (echo *.foo)
    

    At present you get an error message but $status is zero, not 124. Which deserves a whiskey tango foxtrot exclamation-mark.

  6. anordal commented on Mar 10, 2016

    @anordal
    Contributor

    I'm a little worried about the viability of strict mode #805 if we put too much entropy in $status in success cases. That also goes for count returning 1.

    The use for "bubbling up" the exit status is to be able to check it while also capturing output:

    if not set tmpfile (mktemp)
        die
    end

    Of course, strict mode would make many of these checks unnecessary.

  7. krader1961 commented on Mar 10, 2016

    @krader1961
    Contributor

    The use for "bubbling up" the exit status is to be able to check it while also capturing output

    Yeah, but it only works that way for the set command and even then it's only useful if you have a single command substitution. Also, that example breaks if the command substitution produces valid (not error) output but also, for whatever reason, exits with a non-zero status. From an architecture viewpoint that is not ideal. Better would be a means of checking the exit status of any command substitution in the most recently executed statement independent of the status of the primary command.

  8. anordal commented on Mar 11, 2016

    @anordal
    Contributor

    @krader1961: If bash has $PIPESTATUS, maybe we could have something like $stmt_status, to also include statuses of command substitutions and wildcards (in order of appearance)?

    set wtfs (grep -i wtf src/*.{cpp,h} | string trim)
    echo $stmt_status[1] # status of set
    echo $stmt_status[2] # status of grep
    echo $stmt_status[3] # status of src/*.cpp
    echo $stmt_status[4] # status of src/*.h
    echo $stmt_status[5] # status of string

    In case of commands that were not executed (e.g. due to unmatched wildcards), we need to fill in a value. As things are right now, I suppose we could generalize 124 to mean "not executed".

    It doesn't address the desire for short ifs though.

  9. added a commit that references this issue on Aug 3, 2021
  10. scorphus commented on Aug 3, 2021

    @scorphus
    Contributor

    Any update on this? Any plans for it? Thanks. 🐠🐚

  11. faho commented on Aug 3, 2021

    @faho
    MemberAuthor

    @scorphus No plans for this, currently.

    Simply see if the variable has an element:

    set something *.png
    set -q something[1]
    or echo "Oops, no matches!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions