Skip to content

Commit

Permalink
fix: Fixes infinite loop in auto install not found bash function (#4094)
Browse files Browse the repository at this point in the history
* Fixes infinite loop in auto install not found bash function

This infinite loop bug is triggered when mise itself becomes not found, e.g., by deleting the directory mise is in.

* Updates test snap

* Attempts other shells and matches binaries starting with `mise-`
  • Loading branch information
bnorick authored Jan 15, 2025
1 parent 78bb4fd commit fcbe2c4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/shell/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Shell for Bash {
{chpwd_load}
chpwd_functions+=(_mise_hook)
_mise_hook
"#,
"#,
chpwd_functions = include_str!("../assets/bash_zsh_support/chpwd/function.sh"),
chpwd_load = include_str!("../assets/bash_zsh_support/chpwd/load.sh")
});
Expand All @@ -71,7 +71,7 @@ impl Shell for Bash {
fi
command_not_found_handle() {{
if {exe} hook-not-found -s bash -- "$1"; then
if [[ "$1" != "mise" && "$1" != "mise-"* ]] && {exe} hook-not-found -s bash -- "$1"; then
_mise_hook
"$@"
elif [ -n "$(declare -f _command_not_found_handle)" ]; then
Expand Down
5 changes: 3 additions & 2 deletions src/shell/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Shell for Fish {

if !opts.no_hook_env {
out.push_str(&formatdoc! {r#"
function __mise_env_eval --on-event fish_prompt --description {description};
{exe} hook-env{flags} -s fish | source;
Expand Down Expand Up @@ -92,7 +92,8 @@ impl Shell for Fish {
end
function fish_command_not_found
if {exe} hook-not-found -s fish -- $argv[1]
if test "$argv[1]" != "mise" -a "$argv[1]" != "mise-"*
and {exe} hook-not-found -s fish -- $argv[1]
{exe} hook-env{flags} -s fish | source
else if functions -q __mise_fish_command_not_found
__mise_fish_command_not_found $argv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ if [ -z "${_mise_cmd_not_found:-}" ]; then
fi

command_not_found_handle() {
if /some/dir/mise hook-not-found -s bash -- "$1"; then
if [[ "$1" != "mise" && "$1" != "mise-"* ]] && /some/dir/mise hook-not-found -s bash -- "$1"; then
_mise_hook
"$@"
elif [ -n "$(declare -f _command_not_found_handle)" ]; then
Expand Down
3 changes: 2 additions & 1 deletion src/shell/snapshots/mise__shell__fish__tests__activate.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ if functions -q fish_command_not_found; and not functions -q __mise_fish_command
end

function fish_command_not_found
if /some/dir/mise hook-not-found -s fish -- $argv[1]
if test "$argv[1]" != "mise" -a "$argv[1]" != "mise-"*
and /some/dir/mise hook-not-found -s fish -- $argv[1]
/some/dir/mise hook-env --status -s fish | source
else if functions -q __mise_fish_command_not_found
__mise_fish_command_not_found $argv
Expand Down
2 changes: 1 addition & 1 deletion src/shell/snapshots/mise__shell__zsh__tests__activate.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if [ -z "${_mise_cmd_not_found:-}" ]; then
[ -n "$(declare -f command_not_found_handler)" ] && eval "${$(declare -f command_not_found_handler)/command_not_found_handler/_command_not_found_handler}"

function command_not_found_handler() {
if /some/dir/mise hook-not-found -s zsh -- "$1"; then
if [[ "$1" != "mise" && "$1" != "mise-"* ]] && /some/dir/mise hook-not-found -s zsh -- "$1"; then
_mise_hook
"$@"
elif [ -n "$(declare -f _command_not_found_handler)" ]; then
Expand Down
4 changes: 2 additions & 2 deletions src/shell/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Shell for Zsh {

if !opts.no_hook_env {
out.push_str(&formatdoc! {r#"
_mise_hook() {{
eval "$({exe} hook-env{flags} -s zsh)";
}}
Expand All @@ -71,7 +71,7 @@ impl Shell for Zsh {
[ -n "$(declare -f command_not_found_handler)" ] && eval "${{$(declare -f command_not_found_handler)/command_not_found_handler/_command_not_found_handler}}"
function command_not_found_handler() {{
if {exe} hook-not-found -s zsh -- "$1"; then
if [[ "$1" != "mise" && "$1" != "mise-"* ]] && {exe} hook-not-found -s zsh -- "$1"; then
_mise_hook
"$@"
elif [ -n "$(declare -f _command_not_found_handler)" ]; then
Expand Down

0 comments on commit fcbe2c4

Please sign in to comment.