Forbid subcommand keywords in variables-as-commands#10249
Conversation
This stops you from doing e.g. ```fish set pager command less echo foo | $pager ``` Currently, it would run the command *builtin*, which can only do `--search` and similar, and would most likely end up printing its own help. That means it very very likely won't work, and the code is misguided - it is trying to defeat function resolution in a way that won't do what the author wants it to. The alternative would be to make the command *builtin* execute the command, *but* 1. That would require rearchitecting and rewriting a bunch of it and the parser 2. It would be a large footgun, in that `set EDITOR command foo` will only ever work inside fish, but $EDITOR is also used outside. I don't want to add a feature that we would immediately have to discourage.
(also an fmt)
There was a problem hiding this comment.
The change itself LGTM although I'm not sure how much this is warranted — but I assume you have seen a lot of this and this is in response?
The behavior this would intercept seems like a fundamental misunderstanding of what variables are and the difference between variable expansion and the use of eval. Which leads me to ask
- Do we want to point people to eval in cases like this?
- How do we feel about filling the codebase with workarounds for people that aren't (yet) understanding the fundamentals of shell architecture and behavior?
I'm fine with keywords not being seen as such in variable expansion. You wouldn't want to have My big problem here is that the error behavior is pretty terrible. We have a To clarify, you do And the best place I can find to improve that is directly in the variable-expansion code, because if you changed it in the command builtin it would have to answer how you even got there and explain how not to get there. And we would have to do that in the
I disagree that this specific case is all that fundamental. It's a weird and subtle edge case interaction between a number of different parts, exacerbated by our builtin/keyword duality. And yes, we have a bunch of tweaks to prevent common errors by new people - try |
|
This looks fine but I think we should consider removing the decorators from the ast and and make them proper builtins. |
That is why it's a footgun. A "footgun" is a feature that is easy to misuse. The metaphor is a gun that is, by default, aimed at your feet when you hold it. In this case we know that's the case because the use we have in front of us is, by the definition of $EDITOR, already buggy. You set it, it works for some things, and then later you find out that It does not appear to me that $EDITOR is an unusual use of variables-as-commands, I would assume that's the typical use. Basically: We would enable something that we would then always recommend people don't use because it is only ever correct in edge cases. Think about it in terms of teaching - if we enabled it and you saw someone doing |
|
|
EDITOR is the typical example for variables-as-commands. $EDITOR, $PAGER and a few like this are widely set and used.
I don't really mind if internally these things have the keyword/builtin duality or not. I can tell you that having them as decorators makes some parts cleaner - like asking "what's the command for this commandline". Note that this PR also touches on
The difference there is that "set" is a thing on its own. It does something, it sets variables. By contrast And tbh I question how often anyone would stumble over |
interesting
I was talking about I don't feel great about making half-informed comments, probably not a good use of everyone's time. |
Commit bdfbdaa (Forbid subcommand keywords in variables-as-commands (fish-shell#10249), 2024-02-06) banned "set x command; $x foo" because the parser will not recognize "$x" as decorator. That means that we would execute only the builtin stub, which usually exist only for the --help argument. This scenario does not apply for keywords that are quoted or contain line continuations. We should not treat «"command"» differently from «command». Fix this inconsistency to reduce confusion.
Commit bdfbdaa (Forbid subcommand keywords in variables-as-commands (fish-shell#10249), 2024-02-06) banned "set x command; $x foo" because the parser will not recognize "$x" as decorator. That means that we would execute only the builtin stub, which usually exist only for the --help argument. This scenario does not apply for keywords that are quoted or contain line continuations. We should not treat «"command"» differently from «command». Fix this inconsistency to reduce confusion.
This stops you from doing e.g.
Currently, it would run the command builtin, which can only do
--searchand similar, and would most likely end up printing its own help.That means it very very likely won't work, and the code is misguided - it is trying to defeat function resolution in a way that won't do what the author wants it to.
The alternative would be to make the command builtin execute the command, but
set EDITOR command foowill only ever work inside fish, but $EDITOR is also used outside.I don't want to add a feature that we would immediately have to discourage.
TODOs: