You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set status to 124 also for set with unmatched globs #2791
(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)
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:
Currently, set just returns the status from before:
falseset foo bar
echo$statustrueset 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.
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
ifnotset-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.
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.
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.
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:
ifnotset tmpfile (mktemp)
die
end
Of course, strict mode would make many of these checks unnecessary.
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.
@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 setecho$stmt_status[2] # status of grepecho$stmt_status[3] # status of src/*.cppecho$stmt_status[4] # status of src/*.hecho$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.
Activity
krader1961 commentedon Mar 3, 2016
Doesn't this boil down to
setreturning 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 toset 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:That is fubar.
faho commentedon Mar 3, 2016
Currently,
setjust returns the status from before:This is, AFAIK, to enable just that
if setidiom. Returning if a value was assigned doesn't quite have the same semantics - in fact we have a ton ofset -l varcalls, 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.
krader1961 commentedon Mar 3, 2016
Yes,
setreturns 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 aboutset -l varstatements since it doesn't seem to be relevant to the question at hand. Why would anyone write something like the following:I'm worried that having glob expansion altering
$statusis 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 impliesecho *.fooshould behave likeset -l foo *.fooif the expansion fails: both should leave $status non-zero but at the moment echo explicitly sets $status to zero.faho commentedon Mar 3, 2016
It doesn't - like everything but
count,forandset, 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 -
forwon't execute,countwill return 1 andsetwill return something non-zero (I don't care too much about the specific value, 124 is just the default).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.
krader1961 commentedon Mar 3, 2016
Right. I got side-tracked by your observation about
setpassing 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. Theechocommand 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 theforcommand, https://github.com/anordal/fish-shell/blob/0f025ec318582bfa5e3bba902a38ce435ff50bc4/src/parse_execution.cpp#L1612. If you don't try toechoa glob that fails to expand thenechodoes in fact force the status to zero -- it does not pass thru the status of the most recent command likesetdoes.Count already returns one in this scenario because it's being invoked with zero arguments. There isn't any easy way to have
setreturn 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
setcommand what should happen in the following examples (assuming*.foofails to expand):Should the status be 124 or 1?
Should the status be 1 or 124?
Should the status be 124 or 0?
At present you get an error message but
$statusis zero, not 124. Which deserves a whiskey tango foxtrot exclamation-mark.anordal commentedon Mar 10, 2016
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
countreturning 1.The use for "bubbling up" the exit status is to be able to check it while also capturing output:
Of course, strict mode would make many of these checks unnecessary.
krader1961 commentedon Mar 10, 2016
Yeah, but it only works that way for the
setcommand 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.anordal commentedon Mar 11, 2016
@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)?
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.
Set `paths` var according to its value
scorphus commentedon Aug 3, 2021
Any update on this? Any plans for it? Thanks. 🐠🐚
faho commentedon Aug 3, 2021
@scorphus No plans for this, currently.
Simply see if the variable has an element:
Update for Fish 2.3 and add more/current cmd directories (#5)