Skip to content

fix: else branch missing for when cd is aliased e.g. oh-my-bash#539

Open
allen-munsch wants to merge 1 commit into
moovweb:masterfrom
allen-munsch:master
Open

fix: else branch missing for when cd is aliased e.g. oh-my-bash#539
allen-munsch wants to merge 1 commit into
moovweb:masterfrom
allen-munsch:master

Conversation

@allen-munsch
Copy link
Copy Markdown

hello

i've experienced an issue with gvm for a couple years now, and i was never really sure what was the cause

i tried to outline as best i could the issue, it only ever reared its head when i had both oh-my-bash and gvm installed at the same time

i think it has to do with the lack of an alias branch check

  $ bash test-cd-fix.sh /path/to/gvm                                                                                                                                                         
                                                                                                                                                                                             
  === BROKEN (original) ===                                                                                                                                                                  
  cd before gvm: cd is aliased to `_omb_plugin_nvm_cd'                                                                                                                                       
  cd after gvm:  cd is aliased to `_omb_plugin_nvm_cd'                                                                                                                                       
  PWD before: /some/dir                                                                                                                                                                      
  PWD after:  /some/dir  (EXPECTED /tmp/gvm-cd-test-XXXXX)                                                                                                                                   
  FAIL                                                                                                                                                                                       
                                                                                                                                                                                             
  === FIXED ===                                                                                                                                                                              
  cd before gvm: cd is aliased to `_omb_plugin_nvm_cd'                                                                                                                                       
  cd after gvm:  cd is aliased to `_omb_plugin_nvm_cd'                                                                                                                                       
  PWD before: /some/dir                                                                                                                                                                      
  PWD after:  /tmp/gvm-cd-test-XXXXX                                                                                                                                                         
  PASS                                                                                                                                                                                       

test-cd-fix.sh :

#!/usr/bin/env bash
# ------------------------------------------------------------------
# Reproduce: when another shell framework (e.g. oh-my-bash) aliases
# cd, gvm's cd() wrapper silently drops the directory change because
# its save-logic only handles cd-as-function and cd-as-builtin, but
# not cd-as-alias.  __gvm_oldcd is never created, so the `cd` in the
# wrapper is a no-op.
#
# Fixed by adding an `else` branch that falls back to builtin cd.
#
# Usage:
#   git -C /path/to/gvm checkout -- scripts/env/cd   # apply the fix
#   bash test-cd-fix.sh /path/to/gvm
# ------------------------------------------------------------------
set -uo pipefail  # no -e: gvm's cd() may return non-zero if no Go installed

GVM_REPO="${1:?Usage: $0 /path/to/gvm/repo}"

export GVM_ROOT="$GVM_REPO"
export GVM_DEBUG=0

# Minimal gvm bootstrap (env/cd depends on these helpers)
set +u  # gvm internals reference ZSH_VERSION bare
# shellcheck source=/dev/null
. "$GVM_ROOT/scripts/functions"
. "$GVM_ROOT/scripts/function/_bash_pseudo_hash" 2>/dev/null || true
. "$GVM_ROOT/scripts/function/_shell_compat"     2>/dev/null || true

# Shims: we don't have a Go installation, but cd() will try to resolve
# .go-version / .go-pkgset.  Make those paths quiet.
display_error()                { :; }
__gvm_munge_path()             { echo "$PATH"; }
__gvm_read_environment_file()  { return 1; }
__gvmp_find_closest_dot_go_version() { return 1; }
__gvmp_find_closest_dot_go_pkgset()  { return 1; }
__gvmp_read_dot_go_version()   { return 1; }
__gvmp_read_dot_go_pkgset()    { return 1; }

# ------------------------------------------------------------------
# Step 1: alias cd like oh-my-bash's nvm plugin does
# ------------------------------------------------------------------
shopt -s expand_aliases
_omb_plugin_nvm_cd() { command cd "$@" || return "$?"; }
alias cd='_omb_plugin_nvm_cd'

echo "cd before gvm: $(type cd)"

# ------------------------------------------------------------------
# Step 2: source gvm's env/cd (discard noise from go-version scan)
# ------------------------------------------------------------------
. "$GVM_ROOT/scripts/env/cd" 2>/dev/null
echo "cd after gvm:  $(type cd | head -1)"

# ------------------------------------------------------------------
# Step 3: does cd actually change directory?
# ------------------------------------------------------------------
START_DIR="$PWD"
TMPDIR="/tmp/gvm-cd-test-$$"
mkdir -p "$TMPDIR"
trap 'cd / 2>/dev/null; rm -rf "$TMPDIR"' EXIT

echo ""
echo "PWD before: $START_DIR"
cd "$TMPDIR" 2>/dev/null

if [[ "$PWD" == "$TMPDIR" ]]; then
    echo "PWD after:  $PWD"
    echo ""
    echo "PASS"
    exit 0
else
    echo "PWD after:  $PWD  (EXPECTED $TMPDIR)"
    echo ""
    echo "FAIL"
    exit 1
fi

Signed-off-by: allen-munsch <james.a.munsch@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant