From 096daa436d3dca1e7359a2ccb3af2a0b62ca69b8 Mon Sep 17 00:00:00 2001 From: ziboh <42881496+ziboh@users.noreply.github.com> Date: Tue, 25 Feb 2025 20:22:18 +0800 Subject: [PATCH] Fix the fnm bug of Nushell reloading (#1058) ![image](https://github.com/user-attachments/assets/a528e891-1be3-4e87-ba5b-6f3f989177a6) Opening a terminal in nvim results in the error shown above. --- modules/fnm/fnm.nu | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/modules/fnm/fnm.nu b/modules/fnm/fnm.nu index acfdafd..ed60fc9 100644 --- a/modules/fnm/fnm.nu +++ b/modules/fnm/fnm.nu @@ -23,17 +23,25 @@ export-env { if not (which fnm | is-empty) { fnm-env | load-env - if (not ($env | default false __fnm_hooked | get __fnm_hooked)) { - $env.__fnm_hooked = true - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change)) - $env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD)) - $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append { |before, after| + $env.config = ( + $env.config? + | default {} + | upsert hooks { default {} } + | upsert hooks.env_change { default {} } + | upsert hooks.env_change.PWD { default [] } + ) + let __fnm_hooked = ( + $env.config.hooks.env_change.PWD | any { try { get __fnm_hook } catch { false } } + ) + if not $__fnm_hooked { + $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append { + __fnm_hook: true, + code: {|before, after| if ('FNM_DIR' in $env) and ([.nvmrc .node-version] | path exists | any { |it| $it }) { - (^fnm use); (fnm-env | load-env) + (^fnm use); (fnm-env | load-env) + } } - })) + }) } } }