Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 1358372 - zsh command_not_found_handler should behave exactly the same way as its bash counterpart
Summary: zsh command_not_found_handler should behave exactly the same way as its bash ...
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: zsh
Version: 7.2
Hardware: All
OS: Linux
low
low
Target Milestone: rc
: ---
Assignee: Kamil Dudka
QA Contact: BaseOS QE - Apps
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-07-20 15:03 UTC by Tim Speetjens
Modified: 2016-07-21 08:58 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-07-21 08:58:17 UTC


Attachments (Terms of Use)


Links
System ID Priority Status Summary Last Updated
Red Hat Knowledge Base (Solution) 2398991 None None None 2016-07-20 15:09:11 UTC

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.


Note You need to log in before you can comment on or make changes to this bug.