fnm env: Use fish_add_path in fish shell
fnm env should use the fish_add_path function when appropriate. This avoids appending multiple fnm binary paths to $PATH, which can happen in a child shell.
diff --git a/a.fish b/b.fish
index 3a9ba79..c299b09 100644
--- a/a.fish
+++ b/b.fish
@@ -1,4 +1,4 @@
-set -gx PATH "/tmp/fnm_multishell_16129_1619310866524/bin" $PATH;
+fish_add_path -g "/tmp/fnm_multishell_16129_1619310866524/bin";
set -gx FNM_MULTISHELL_PATH "/tmp/fnm_multishell_16129_1619310866524";
set -gx FNM_DIR "/home/username/.fnm";
set -gx FNM_LOGLEVEL "info";
This function was recently added in fish v3.2.0 (released March 1st, 2021) which will break on older versions of fish. If backwards compatibility with those versions is important this PR should probably be ignored until the minimum supported fish version is raised to v3.2.0.
Interesting. What is the difference? I guess we should have some grace period. The current solution does not work on latest fish?
The current solution works fine by modifying PATH which fish exposes for compatibility. The only advantage fish_add_path offers is to avoid duplicate entries in the PATH as well as modifying the fish_user_paths (which is automatically prepended to PATH).
This PR would just make the fish script more like an idiomatic fish script.
fish_add_path did not help to avoid the script to append a new fnm binary path in this situation. Because fnm will create a new binary path whenever you reload the terminal and the fnm env command will run. I think the best way is we need to check the PATH variable before it calls set function or erases it from PATH before adding a new one like the code below:
set -gx FNM_MULTISHELL_PATH "/var/folders/yb/v3z_zfmx1lj85yglm61wtqzc0000gn/T/fnm_multishells/14026_1640102023748";
set -gx FNM_DIR "/Users/rootblack45/.fnm";
set -gx FNM_LOGLEVEL "info";
set -gx FNM_NODE_DIST_MIRROR "https://nodejs.org/dist";
set -gx FNM_ARCH "x64";
echo $fish_user_paths | string match -qr 'fnm_multishells'
or fish_add_path $FNM_MULTISHELL_PATH
I'm working on a feature that will make less mess. Then, we can use fish_add_path and it will be the same path and not a random one.
Still, I wonder what's the adoption process for fish. Should we have some kind of if fish_version > 3.2.0 then ... else ...?
I think we can do it simply by using contains like the example from fish 2.0. It's gonna be like this:
if not contains $FNM_MULTISHELL_PATH/bin $PATH
set -gx PATH "$FNM_MULTISHELL_PATH/bin" $PATH
end
PRs welcome 😉