-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: hint shell builtins in command-not-found error #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kimjune01
wants to merge
4
commits into
sharkdp:master
Choose a base branch
from
kimjune01:fix/shell-builtin-hint-1944
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0ce7cbb
fix: hint shell builtins in command-not-found error (#1944)
kimjune01 1fd9e70
changelog: add shell builtin hint entry for #1944
kimjune01 28f15a9
fix(clippy): use slice::contains for shell builtin lookup
kimjune01 10f0c97
Merge remote-tracking branch 'origin/master' into pr-1994
kimjune01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,13 +98,37 @@ pub fn execute_commands<I: Iterator<Item = io::Result<Command>>>( | |
| ExitCode::Success | ||
| } | ||
|
|
||
| /// Common shell builtins that typically do not exist as standalone executables. | ||
| /// When fd encounters a "command not found" error for one of these, it hints | ||
| /// that the user may be trying to use a shell builtin. | ||
| const SHELL_BUILTINS: &[&str] = &[ | ||
| ".", "alias", "bg", "bind", "cd", "command", "declare", "dirs", "eval", "exec", "exit", | ||
| "export", "fg", "hash", "help", "history", "jobs", "let", "local", "logout", "popd", "pushd", | ||
| "read", "readonly", "return", "set", "shift", "shopt", "source", "suspend", "times", "trap", | ||
| "type", "typeset", "unalias", "unset", "wait", | ||
| ]; | ||
|
|
||
| fn is_shell_builtin(program: &str) -> bool { | ||
| SHELL_BUILTINS.iter().any(|&b| b == program) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does |
||
| } | ||
|
|
||
| fn command_not_found_message(program: &str) -> String { | ||
| if is_shell_builtin(program) { | ||
| format!( | ||
| "Command not found: {program}. Note: {program} is a shell builtin, \ | ||
| not a standalone program. To run shell builtins, invoke a shell explicitly, \ | ||
| e.g. fd -x sh -c '{program} ... \"$1\"' sh {{}}", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for getting the quoting/escaping right here :) |
||
| ) | ||
| } else { | ||
| format!("Command not found: {program}") | ||
| } | ||
| } | ||
|
|
||
| pub fn handle_cmd_error(cmd: Option<&Command>, err: io::Error) -> ExitCode { | ||
| match (cmd, err) { | ||
| (Some(cmd), err) if err.kind() == io::ErrorKind::NotFound => { | ||
| print_error(format!( | ||
| "Command not found: {}", | ||
| cmd.get_program().to_string_lossy() | ||
| )); | ||
| let program = cmd.get_program().to_string_lossy(); | ||
| print_error(command_not_found_message(&program)); | ||
| ExitCode::GeneralError | ||
| } | ||
| (_, err) => { | ||
|
|
@@ -113,3 +137,44 @@ pub fn handle_cmd_error(cmd: Option<&Command>, err: io::Error) -> ExitCode { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod builtin_tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn detects_known_builtins() { | ||
| assert!(is_shell_builtin("cd")); | ||
| assert!(is_shell_builtin("export")); | ||
| assert!(is_shell_builtin("source")); | ||
| assert!(is_shell_builtin("eval")); | ||
| assert!(is_shell_builtin(".")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_non_builtins() { | ||
| assert!(!is_shell_builtin("grep")); | ||
| assert!(!is_shell_builtin("ls")); | ||
| assert!(!is_shell_builtin("")); | ||
| assert!(!is_shell_builtin("CD")); | ||
| // These typically exist as standalone executables | ||
| assert!(!is_shell_builtin("echo")); | ||
| assert!(!is_shell_builtin("printf")); | ||
| assert!(!is_shell_builtin("test")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn builtin_message_includes_hint() { | ||
| let msg = command_not_found_message("cd"); | ||
| assert!(msg.starts_with("Command not found: cd.")); | ||
| assert!(msg.contains("shell builtin")); | ||
| assert!(msg.contains("sh -c")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_builtin_message_is_plain() { | ||
| let msg = command_not_found_message("nonexistent"); | ||
| assert_eq!(msg, "Command not found: nonexistent"); | ||
| assert!(!msg.contains("shell builtin")); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a
HashSetpersonally