jorgebucaran / nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
https://git.io/nvm.fish
MIT License
2.06k stars 69 forks source link

Node is unavailable in new shells #196

Closed maxdevjs closed 1 year ago

maxdevjs commented 1 year ago
fish, version 3.5.1
fisher, version 4.4.3
nvm, version 2.2.11

I currently experience the issue in a freshly installed system, but not in another system with the same versions of fish and plugins. In both systems I use the same personal configuration.

Similar to https://github.com/jorgebucaran/nvm.fish/issues/168 , but I have already run set --universal nvm_default_version latest:

echo $nvm_default_version
latest

However, every time I spawn a new shell:

I tried to update, uninstall and reinstall configuration and plugins, with no success.

Any suggestions? What should I look for? I am actually confused by this behavior.

P.S.: as a workaround, I currently replicate https://github.com/jorgebucaran/nvm.fish/pull/186 which is, in turn, a very nice addition to my configuration

jorgebucaran commented 1 year ago

Works as expected here.

$ nvm install latest
$ node -v
v19.0.1
$ set --universal nvm_default_version latest

In a new shell:

$ node -v
v19.0.1

The code that sets your preferred default Node version is here:

https://github.com/jorgebucaran/nvm.fish/blob/9db8eaf6e3064a962bca398edd42162f65058ae8/conf.d/nvm.fish#L26-L28

Perhaps you are inadvertently inhibiting that code from running in your config.fish.

Let me see your configuration.

maxdevjs commented 1 year ago

Thank you for the reply.

Indeed, it should work: I have one system with no issue, one featuring the issue.

I restored an old configuration that I knew surely worked, but the issue is still present. I guess it could be something outside fish configuration: I noticed the issue in a "freshly" installed Clear Linux, tested in Kitty, (gnome) Terminal, Console

config is the same for the two systems: config.fish

I did not push the https://github.com/jorgebucaran/nvm.fish/pull/186 "fix", but it is

function __nvm_auto --on-variable PWD
  nvm use --silent 2>/dev/null
end
__nvm_auto

and with it, works also in the system presenting the issue.

jorgebucaran commented 1 year ago

Did you try with an empty config.fish?

maxdevjs commented 1 year ago

Yes

same behavior.

Apparently, all the other plugins that I use work flawlessly :thinking:

Updated my other system plugins and, apparently, everything is fine...

I played a bit testing something similar to https://github.com/jorgebucaran/nvm.fish/blob/9db8eaf6e3064a962bca398edd42162f65058ae8/conf.d/nvm.fish#L26 and I'm puzzled now.

I am aware that it is a long shot, but do you have any suggestion on how to test it?

jorgebucaran commented 1 year ago

Yes, you could directly edit nvm.fish and add some echo statements to see if nvm use is run or not. Sounds like it isn't, though.

I would also uninstall all other plugins to discard a possible bad actor.

Would you paste your whole config.fish here?

maxdevjs commented 1 year ago

TL;DR I actually guess it has nothing to do with fish and nvm.fish, but with how the system handles the shell (related to something in my config/fish or in general?). If I am not wrong, it actually works if I test for not interactive status... (will post from the not working system ASAP)

Test:

if status is-interactive && set --query nvm_default_version && ! set --query nvm_current_version
    nvm use --silent $nvm_default_version
    echo default
end

Note: the following is probably irrelevant

fish_plugins

jorgebucaran/fisher
rafaelrinaldi/pure
acomagu/fish-async-prompt
jorgebucaran/spark.fish
joseluisq/gitnow
jorgebucaran/autopair.fish
gazorby/fish-abbreviation-tips
jethrokuan/z
PatrickF1/fzf.fish
jorgebucaran/nvm.fish

Common config (I also tried with a newly created, minimal, configuration and only fisher and nvm.fish):

# pure prompt settings
set -g async_prompt_functions _pure_prompt_git

function fish_right_prompt_loading_indicator
    echo (set_color '#aaa')' … '(set_color normal)
end

# universal variable are stored in $HOME/.config/fish/fish_variables

function set_universal_nvm_default_version
    #Ex.: set --universal nvm_default_version v18.9.0
    set --universal nvm_default_version $argv
    # Packages to install every change of version
    #Ex.: set --universal nvm_default_packages typescript yarn
end

# Question: Make nvm use usable for my own auto-switching? #186
# https://github.com/jorgebucaran/nvm.fish/pull/186
# Node is unavailable in new shells #196
# https://github.com/jorgebucaran/nvm.fish/issues/196
function __nvm_auto --on-variable PWD
  nvm use --silent 2>/dev/null
end
__nvm_auto

abbr -a c clear
abbr -a flutter-run 'docker run --rm -e UID=$(id -u) -e GID=$(id -g) --workdir /project -v "$PWD":/project --device /dev/bus/usb matspfeiffer/flutter:beta run'
abbr -a flutter-run-emulator 'xhost local:$USER && docker run --rm -ti -e UID=(id -u) -e GID=(id -g) -p 42000:42000 --workdir /project --device /dev/kvm --device /dev/dri:/dev/dri -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY -v "$PWD":/project --entrypoint flutter-android-emulator  matspfeiffer/flutter:beta'
# https://github.com/fish-shell/fish-shell/issues/3907
abbr -a yd 'youtube-dl ""'

alias ks="$XDG_CONFIG_HOME/kitty/sessions/kitty-startup"

if type -q n exa
  #alias la="exa -la"
  alias la="exa -l -g --icons"
  alias lg="exa --git -l"
  alias lt="exa --tree -D -L 3"
end

# vi mode
fish_vi_key_bindings

# Set the cursor shapes for the different vi modes.
set fish_cursor_default     block      blink
set fish_cursor_insert      line       blink
set fish_cursor_replace_one underscore blink
set fish_cursor_visual      block

# Reset padding/margin to 0 when opening nvim
# TODO: search for fish hook to use in all applications
# ANSWER: does not exist
alias kmp0='kitty @ set-spacing padding=0 margin=0'
# Reset padding/margin to config values when closing nvim
# TODO: store/retrieve from...?
alias kmpV='kitty @ set-spacing padding=20 margin=10'

# bat
if type -q bat
  alias cat=bat
end

# nvim 🦾
if type -q nvim
  set -gx EDITOR 'nvim -u NONE'
  set -gx VISUAL nvim
  set -gx MANPAGER "nvim +Man!"

  # Padding 0
  alias n='kmp0 && nvim '
  alias vimdiff="n -d"
  alias ncf="n $XDG_CONFIG_HOME/fish/config.fish"
  alias ncn="n $XDG_CONFIG_HOME/nvim/init.lua"
  #alias ncx="n $HOME/System/nixos-config/"
end

set -gx FZF_DEFAULT_COMMAND "rg --files --hidden --follow -g \"!.git/\" 2> /dev/null"
set -gx FZF_CTRL_T_COMMAND $FZF_DEFAULT_COMMAND

set -gx XDG_CONFIG_HOME "$HOME/.config"

# https://github.com/haslersn/any-nix-shell
#any-nix-shell fish --info-right | source

set DENO_INSTALL "/home/maxdevjs/.deno"

set -x PATH $HOME/.guix-profile/bin $HOME/.local/bin $DENO_INSTALL/bin $PATH

# https://github.com/jarun/nnn/tree/master/plugins#configuration
#"NNN_FIFO=/tmp/nnn.fifo NNN_PLUG='p:preview-tui' nnn "
set -gx NNN_FIFO /tmp/nnn.fifo

# https://github.com/jarun/nnn/tree/master/plugins#skip-user-confirmation-after-command-execution-
# https://github.com/jarun/nnn/tree/master/plugins#page-non-interactive-command-output-
set NNN_PLUG_COM 't:-!|tree -ps;l:-!|ls -lah --group-directories-first'
set -gx NNN_COLORS '1234' '#0a1b2c3d''#0a1b2c3d;1234'
set NNN_PLUG_DEV 'g:-!git diff*;l:-!git log*;o:!dev-git-open-repo-in-browser*'
set NNN_PLUG_MEDIA 'm:-!|mediainfo $nnn;w:!&mpv $nnn*'
set NNN_PLUG_NNN 'p:preview-tui;u:nmount'
set -gx NNN_PLUG "$NNN_PLUG_DEV;$NNN_PLUG_MEDIA;$NNN_PLUG_NNN"
alias nnn="nnn -i -o -U" 

# https://github.com/Olical/dotfiles/blob/master/stowed/.config/fish/config.fish
if type -q direnv
#   eval (direnv hook fish)
  direnv hook fish | source
end

# error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override
alias nix='nix --extra-experimental-features nix-command --extra-experimental-features flakes '
maxdevjs commented 1 year ago

(on the not working system) it echoes if:

if ! status is-interactive && ! set --query nvm_default_version && set --query nvm_current_version
    nvm use --silent $nvm_default_version
    echo default
end

still it does not set node version

/usr/share/defaults/etc/profile has this section, never knows...


# Ensure the interactive terminal has rows and columns
if [ -n "$PS1" ] && tty > /dev/null; then # Only for interactive shells
  tty_rows=$(stty size | cut -d' ' -f1)
  tty_columns=$(stty size | cut -d' ' -f2)
  if [ $((${tty_rows} + 0)) -le 0 -o $((${tty_columns} + 0)) -le 0 ]; then
    [ -x /usr/bin/setterm ] &&  setterm --resize
    # fail safe if size still not set
    tty_rows=$(stty size | cut -d' ' -f1)
    tty_columns=$(stty size | cut -d' ' -f2)
    if [ $((${tty_rows} + 0)) -le 0 -o $((${tty_columns} + 0)) -le 0 ]; then
      stty rows 24 columns 80
    fi
  fi
  unset tty_rows tty_columns
fi
jorgebucaran commented 1 year ago

Please uninstall all your plugins, empty your config.fish, and try again.

maxdevjs commented 1 year ago

I already did it at the beginning (and repeated later): started several times with empty config, added only fisher and nvm.fish, until I got confused and opened this issue.

Both systems use fish, version 3.5.1. I found out that if fish is the users shell and (?) /usr/share/defaults/etc/profile is not sourced (still not sure if it can be a cause, but can be a clue). I can not test to set the default shell to bash, exec fish in .bashrc and then verify if something changes, as I can not log in anymore if doing it.

Another workaround is to set

if status is-interactive && ! set --query nvm_default_version
  nvm use --silent $nvm_default_version
end

in config.fish.

Note that to set

if status is-interactive && set --query nvm_default_version && ! set --query nvm_current_version
    nvm use --silent $nvm_default_version
end

does not work, actually. Perhaps this can be another clue.

Temporarily fixing as showed. If I find what is the actual cause, I will report here.

Thank you for the help and patience :smile:

jorgebucaran commented 1 year ago

If you did and it worked then it's not really nvm.fish's fault, and there's nothing we need to fix with our code is essentially what I was trying to get at.

If you are doing a lot of stuff in your config.fish, try to do it only if interactive.

maxdevjs commented 1 year ago

Forgot to ask: do you have an idea why if status is-interactive && ! set --query nvm_default_version works and if status is-interactive && set --query nvm_default_version && ! set --query nvm_current_version does not?