[Feature] Allow non-127 returns from fish_command_not_found
nekodjin opened this issue · comments
Proposal
Fish invokes the user-defined fish_command_not_found
function whenever a command isn't found. Implementing custom handlers allows a lot of great things, such as automatically searching for a package to provide a command (which is an ootb feature of Fish on many platforms). However, it has one small limitation - no matter what the function returns, the $status
after any command-not-found event is always 127. This causes issues for the following non-inclusive list of varyingly hacky uses of the function:
- Correcting typos
A customfish_command_not_found
could search for binaries in$PATH
that are a low edit distance away and automatically re-run the command. - Installing packages
A customfish_command_not_found
could install the required package and automatically re-run the command. - Executing files in CWD
A customfish_command_not_found
could allow you to run binaries likea.out
instead of./a.out
. - Zoxide integration
Fish supports "implicitcd
" allowing you to navigate to directories without actually typing outcd
. The good news is that because it still executescd
under the hood, navigating in this way still updates the zoxide "frecency" database. The bad news is that because Fish only does this when it recognizes the directory, you can't actually use zoxide without typing outcd.
A customfish_command_not_found
could enable this behavior.
In all of these cases, a $status
of 127 is confusing in the case of success. In the case of failure, a $status
of 127 may actually be hiding information - specifically, it is hiding the actual failure code of the command.
Admittedly, these are probably not the intended use cases, and some of these might be better served by a dedicated API instead of hijacking fish_command_not_found
. But in the spirit of customizability and extensibility, I still think we should consider allowing non-127 returns.
Considerations
I'd bet cash money that most existing fish_command_not_found
just implicitly return 0. This is a problem - if this feature is implemented in an upcoming version of Fish, a lot of people may suddenly find that command-not-found events "succeed" instead of failing with 127 like they're used to. If we're willing to get hacky, this function could be special-cased so that its implicit return value is 127 instead of 0. Regardless of the specifics, the addition of this feature should not negatively impact existing uses of fish_command_not_found
.
Duplicate of #7902