Bug 1358372

Summary: zsh command_not_found_handler should behave exactly the same way as its bash counterpart
Product: Red Hat Enterprise Linux 7 Reporter: Tim Speetjens <tspeetje>
Component: zshAssignee: Kamil Dudka <kdudka>
Status: CLOSED NOTABUG QA Contact: BaseOS QE - Apps <qe-baseos-apps>
Severity: low Docs Contact:
Priority: low    
Version: 7.2CC: kdudka
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-07-21 08:58:17 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Tim Speetjens 2016-07-20 15:03:26 UTC
Description of problem:
The zsh command_not_found_handler has slighty different semantics, which make it less useful:
1 If the handler returns non-zero, the shell itself returns 127, regardless.
2 If the handler returns non-zero, the shell always prints an error message 'command not found', regardless.

This makes it impossible to differentiate an error with the handler (say, a user chose not to install the program with PackageKit), or an error code from the program. Also, this may print a command not found error, when the command actually launched.

In bash, this would be 0 if the handler installed the program, and it ran successfully, 127 if the handler didn't exist, or had an error, anything else, if the program exited with this error code.


Version-Release number of selected component (if applicable):
zsh-5.0.2

How reproducible:
100%


Steps to Reproduce / Actual results:
[user@host ~] $ zsh
[user@host] ~% not-found
zsh: command not found: not-found
[user@host] ~% echo $?
127
[user@host] ~% function command_not_found_handler() { return 0; }
[user@host] ~% not-found
[user@host] ~% echo $?
0
[user@host] ~% function command_not_found_handler() { return 3; }
[user@host] ~% not-found                                         
zsh: command not found: not-found
[user@host] ~% echo $?                                           
127
[user@host] ~% function command_not_found_handler() {
  echo "Command not found" >&2; return 127; }
[user@host] ~% not-found                                                                         
Command not found
zsh: command not found: not-found
[user@host] ~% echo $?                                                                           
127

Expected results:
[user@host ~] $ zsh
[user@host] ~% function command_not_found_handler() { return 0; }
[user@host] ~% not-found
[user@host] ~% echo $?
0
[user@host] ~% function command_not_found_handler() { return 3; }
[user@host] ~% not-found                                         
[user@host] ~% echo $?                                           
3
[user@host] ~% function command_not_found_handler() {
  echo "Command not found" >&2; return 127;
}
[user@host] ~% not-found                                                                       Command not found
[user@host] ~% echo $?                                                                           
127


Additional info:
For bash:

[user@host ~] $ unset command_not_found_handle
[user@host ~] $ not-found
bash: not-found: command not found
[user@host ~] $ echo $?
127
[user@host ~] $ function command_not_found_handle() { return 3; }
[user@host ~] $ not-found
[user@host ~] $ echo $?
3
[user@host ~] $ function command_not_found_handle() {
  echo "Command not found" >&2; return 127;
}
[user@host ~] $ not-found
Command not found
[user@host ~] $ echo $?
127

Comment 2 Kamil Dudka 2016-07-21 08:58:17 UTC
It works exactly as documented:

    If no external command is found but a function command_not_found_handler
    exists the shell executes this function with all command line arguments.
    The function should return status zero if it successfully handled the
    command, or non-zero status if it failed. In the latter case the standard
    handling is applied: `command not found' is printed to standard error and
    the shell exits with status 127. Note that the handler is executed in a
    subshell forked to execute an external command, hence changes to
    directories, shell parameters, etc. have no effect on the main shell.

This could be a topic for upstream discussion but we are not going to change the behavior during the life-time of RHEL-7 anyway.  From my point of view, there is no obvious benefit in passing the exit code out of PackageKit to an interactive shell session.

If your concern is about the error message being printed by zsh, I would suggest to just tweak the handler to always return zero if an action is taken.