Fix trailing slash handling in directory tests

Resolves issue where z mydir/ fails even when mydir exists

Shell built-in directory tests can fail when paths have trailing slashes. This fix strips trailing slashes before the directory test while preserving the original path for cd.

Changes: Fish, Bash, Zsh, POSIX, and Tcsh templates now strip trailing slashes before directory tests
This commit is contained in:
Michael Collard 2026-02-12 10:41:51 -06:00
parent ce46915901
commit 08dc03c65e
5 changed files with 23 additions and 7 deletions

View File

@ -111,7 +111,7 @@ function __zoxide_z() {
__zoxide_cd ~
elif [[ $# -eq 1 && $1 == '-' ]]; then
__zoxide_cd "${OLDPWD}"
elif [[ $# -eq 1 && -d $1 ]]; then
elif [[ $# -eq 1 && -d ${1%%+(/)} ]]; then
__zoxide_cd "$1"
elif [[ $# -eq 2 && $1 == '--' ]]; then
__zoxide_cd "$2"

View File

@ -79,7 +79,7 @@ function __zoxide_z
__zoxide_cd $HOME
else if test "$argv" = -
__zoxide_cd -
else if test $argc -eq 1 -a -d $argv[1]
else if test $argc -eq 1 -a -d (string replace -r '/+$' '' -- $argv[1])
__zoxide_cd $argv[1]
else if test $argc -eq 2 -a $argv[1] = --
__zoxide_cd -- $argv[2]

View File

@ -105,8 +105,18 @@ __zoxide_z() {
\command printf 'zoxide: $OLDPWD is not set'
return 1
fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then
__zoxide_cd "$1"
elif [ "$#" -eq 1 ]; then
__zoxide_arg="$1"
# Strip trailing slashes for directory test
while [ "${__zoxide_arg}" != "${__zoxide_arg%/}" ] && [ "${__zoxide_arg}" != / ]; do
__zoxide_arg="${__zoxide_arg%/}"
done
if [ -d "${__zoxide_arg}" ]; then
__zoxide_cd "$1"
else
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \command true)" -- "$@")" &&
__zoxide_cd "${__zoxide_result}"
fi
else
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \command true)" -- "$@")" &&
__zoxide_cd "${__zoxide_result}"

View File

@ -38,8 +38,14 @@ if ("$#__zoxide_args" == 0) then\
else\
if ("$#__zoxide_args" == 1 && "$__zoxide_args[1]" == "-") then\
cd -\
else if ("$#__zoxide_args" == 1 && -d "$__zoxide_args[1]") then\
cd "$__zoxide_args[1]"\
else if ("$#__zoxide_args" == 1) then\
set __zoxide_arg_tmp = `echo "$__zoxide_args[1]" | sed "s|/*$||"`\
if (-d "$__zoxide_arg_tmp") then\
cd "$__zoxide_args[1]"\
else\
set __zoxide_pwd = `{{ pwd_cmd }}`\
set __zoxide_result = "`zoxide query --exclude '"'"'$__zoxide_pwd'"'"' -- $__zoxide_args`" && cd "$__zoxide_result"\
endif\
else\
set __zoxide_pwd = `{{ pwd_cmd }}`\
set __zoxide_result = "`zoxide query --exclude '"'"'$__zoxide_pwd'"'"' -- $__zoxide_args`" && cd "$__zoxide_result"\

View File

@ -88,7 +88,7 @@ function __zoxide_z() {
__zoxide_doctor
if [[ "$#" -eq 0 ]]; then
__zoxide_cd ~
elif [[ "$#" -eq 1 ]] && { [[ -d "$1" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[-+][0-9]+$ ]]; }; then
elif [[ "$#" -eq 1 ]] && { [[ -d "${1%%/##}" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[-+][0-9]+$ ]]; }; then
__zoxide_cd "$1"
elif [[ "$#" -eq 2 ]] && [[ "$1" = "--" ]]; then
__zoxide_cd "$2"