asdf icon indicating copy to clipboard operation
asdf copied to clipboard

Automatically warn/install versions on enter directory when mismatch with current

Open tomsquest opened this issue 4 years ago • 7 comments

Is your feature request related to a problem?

I am frustrated when I enter a directory containing a tool-version that is not installed and thus not used by asdf, then I don't use the expected tools. I am not warned about the mismatch between required tool (defined in .tool-versions and the one in asdf current).

Given a directory with a .tool-versions When the user enters that directory or one of its children Then asdf warns and/ask for installation of the tools versions defined in .tool-versions

Describe the solution you'd like

I would like that asdf detects that the current tools are not the one defined in the .tool-versions and install them automatically (or ask for install).

Describe similar asdf features and why they are not sufficient

asdf correctly use the tools but it did not warn/install them automatically

Describe workarounds you've considered

A shell plugin could detect that the current tools and the one in .tool-versions (in the hierarchy) do not match. This plugin can warn the user that something is wrong and ask if it can install the tools.

Nvm (Node Version Manager) has some instructions to do it. This snippet will use or install (depending of the versions of node installed and declared).

Additional context

$ asdf version
v0.7.8-4a3e3d6

tomsquest avatar Jun 04 '20 11:06 tomsquest

We've avoided doing this because as you can see it requires aliasing cd to some other function in Bash which is 1). something that is frowned upon (for good reason) and 2). can have a significant affect on performance as it will run asdf code on all directory changes.

I'm not opposed to having this as a possible feature for some users to setup on their own, but I don't think this should be something we force on users.

Having an asdf command that performs the necessary check would allow us users to run it manually if they wished (without the cd hook) or configure their shell to run it on directory change (if they want the feature you are describing).

What do you think?

Stratus3D avatar Aug 07 '20 13:08 Stratus3D

No problem. If there is a performance impact and moreover it does not bother users, then definitively the feature should be optional.

For the "implementation", this could be documented as NVM does in this section of the Readme.

As I am on holidays and don't like having issues opened, I am closing this issue until I propose a PR.

tomsquest avatar Aug 08 '20 06:08 tomsquest

I re-opened this issue so others can more easily find it. I think this would be a good feature if it is optional. A PR is welcome but no hurry.

Stratus3D avatar Aug 11 '20 13:08 Stratus3D

I'd like to propose an asdf check subcommand. A way to verify that the current shims of commands meet the requirements of the .tool-versions file.

Scripted automation can then run something like:

asdf check || asdf install

ghostsquad avatar Dec 24 '20 00:12 ghostsquad

@ghostsquad Agreed. And while I agree that we want asdf to avoid polluting cd, pairing the proposed check command with a tool that is supposed to perform actions on directory entry, such as direnv, would allow for something fairly magical:

.envrc
if has asdf; then
  asdf check || asdf install
fi

jameslai avatar Feb 27 '21 15:02 jameslai

I'm using this script in my .zshrc, until there is a better way. It hooks into cd and also works with autojump

cd() {
  local target="$1"
  if [[ -z "$target" || "$target" == "~" ]]; then
    target="$HOME"
  elif [[ "$target" == ~/[^/]* ]]; then
    target="${target/#\~/$HOME}"
  fi
  builtin cd "$target" || return
  if [[ -f .tool-versions ]]; then
    asdf install
  fi
}

Mehdi-Hp avatar Oct 24 '23 09:10 Mehdi-Hp

I don't think checking/installing when entering a directory makes sense. Tried with direnv and it works, but I think the use case for this is:

  • cd git-repository
  • update code
  • use tools

so a check on cd would be too early. I think it would make much more sense to create a shell function like this (not tested yet, but should work):

function tf {
    asdf install
    terraform $@
}

and just run tf instead of terraform. For cases where asdf install is too slow it could be scoped to just terraform.

sblask avatar Nov 17 '23 05:11 sblask