Skip to content
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

fix: Introduce ASDF_FORCE_PREPEND variable on POSIX entrypoint #1560

Merged
merged 7 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Introduce ASDF_FORCE_PREPEND variable on POSIX entrypoint
This variable forces the prepending of the asdf directories to the PATH
variable. It does this by removing existing asdf entries in PATH,
including an optimization for Bash and Zsh shells.
  • Loading branch information
hyperupcall committed May 31, 2023
commit 6e6de80584e8391d312a23189f733a46abb9dcd1
30 changes: 30 additions & 0 deletions asdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ fi
_asdf_bin="$ASDF_DIR/bin"
_asdf_shims="${ASDF_DATA_DIR:-$HOME/.asdf}/shims"

# If ASDF_FORCE_PREPEND is set, remove any existing instances of asdf from PATH so
# the prepending done after is always at the frontmost part of the PATH.
if [ -n "${ASDF_FORCE_PREPEND+x}" ]; then
if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC3060
case ":$PATH:" in
*":${_asdf_bin}:"*) PATH=${PATH//$_asdf_bin:/} ;;
esac
# shellcheck disable=SC3060
case ":$PATH:" in
*":${_asdf_shims}:"*) PATH=${PATH//$_asdf_shims:/} ;;
esac
else
_path=${PATH}:
_new_path=
while [ -n "$_path" ]; do
_part=${_path%%:*}
_path=${_path#*:}

if [ "$_part" = "$_asdf_bin" ] || [ "$_part" = "$_asdf_shims" ]; then
continue
fi

_new_path="$_new_path${_new_path:+:}$_part"
done
PATH=$_new_path
unset -v _path _new_path _part
fi
fi

case ":$PATH:" in
*":$_asdf_bin:"*) : ;;
*) PATH="$_asdf_bin:$PATH" ;;
Expand Down
8 changes: 7 additions & 1 deletion docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ We highly recommend using the official `git` method.

## 3. Install asdf

There are many different combinations of Shells, OSs & Installation methods all of which affect the configuration here. Expand the selection below that best matches your system:
There are many different combinations of Shells, OSs & Installation methods all of which affect the configuration here. Expand the selection below that best matches your system.

**macOS users, be sure to read the warning about `path_helper` at the end of this section.**

::: details Bash & Git

Expand Down Expand Up @@ -359,6 +361,10 @@ export ASDF_DIR="/opt/asdf-vm"

`asdf` scripts need to be sourced **after** you have set your `$PATH` and **after** you have sourced your framework (oh-my-zsh etc).

::: warning
On macOS, starting a Bash or Zsh shell automatically calls a utility called `path_helper`. `path_helper` has poor logic and rearranges items in `PATH` (and `MANPATH`) in a bad way. To workaround this, set the `ASDF_FORCE_PREPEND` variable before sourcing `asdf`, like so: `ASDF_FORCE_PREPEND= . "<path-to-asdf-directory>/asdf.sh"`.
jthegedus marked this conversation as resolved.
Show resolved Hide resolved
:::

Restart your shell so that `PATH` changes take effect. Opening a new terminal tab will usually do it.

## Core Installation Complete!
Expand Down