From d33325a89f8e13f42f597af126095eb0dfd71711 Mon Sep 17 00:00:00 2001 From: Weile Zhang Date: Mon, 2 Feb 2026 06:01:26 +0800 Subject: [PATCH 1/2] Refactor asdf_update_java_home function for better handling --- set-java-home.nu | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/set-java-home.nu b/set-java-home.nu index 55f1ec79..3d6762f2 100644 --- a/set-java-home.nu +++ b/set-java-home.nu @@ -1,11 +1,21 @@ -def asdf_update_java_home [] { - let $java_path = (asdf which java) - - if $java_path { - let $full_path = (realpath $java_path | lines | nth 0 | str trim) - - let $java_home = ($full_path | path dirname | path dirname) - $env.JAVA_HOME = $java_home - $env.JDK_HOME = $java_home +def --env asdf_update_java_home [] { + let result = (do -i { asdf which java } | complete) + + match $result { + { exit_code: 0, stdout: $s } if ($s | str trim | is-not-empty) => { + let java_home = ( + $s + | lines | first | str trim + | path expand + | path dirname | path dirname + ) + $env.JAVA_HOME = $java_home + $env.JDK_HOME = $java_home + } + _ => { hide-env -i JAVA_HOME JDK_HOME } } } + +$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | default [] | append { + asdf_update_java_home +}) From 48f5fb8f9429a9a22aecfe2f162f3c7eea1d1798 Mon Sep 17 00:00:00 2001 From: Weile Zhang Date: Mon, 2 Feb 2026 23:46:26 +0800 Subject: [PATCH 2/2] Optimize refresh by updating only after mutating asdf commands or directory changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of recomputing the Java environment on every prompt, the update now happens only after running asdf commands excluding asdf which and asdf where, or when the working directory changes. The purpose is to reduce unnecessary prompt-time overhead while keeping the environment variables correct and consistent with asdf’s state. --- set-java-home.nu | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/set-java-home.nu b/set-java-home.nu index 3d6762f2..d631f5bc 100644 --- a/set-java-home.nu +++ b/set-java-home.nu @@ -1,6 +1,6 @@ def --env asdf_update_java_home [] { let result = (do -i { asdf which java } | complete) - + match $result { { exit_code: 0, stdout: $s } if ($s | str trim | is-not-empty) => { let java_home = ( @@ -16,6 +16,33 @@ def --env asdf_update_java_home [] { } } -$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | default [] | append { - asdf_update_java_home -}) +def is-asdf-mutating []: string -> bool { + let s = ($in | str trim) + + if not ($s like '^\^?asdf(\s|$)') { return false } + if ($s like '^\^?asdf\s+(which|where)(\s|$)') { return false } + + true +} + +def --env pre_execution_hook [] { + let line = (commandline) + + if ($line | is-asdf-mutating) { + $env._ASDF_REFRESH_NEEDED = true + } else { + hide-env -i _ASDF_REFRESH_NEEDED + } +} + +def --env pre_prompt_hook [] { + if ($env._ASDF_REFRESH_NEEDED? | default false) { + asdf_update_java_home + hide-env -i _ASDF_REFRESH_NEEDED + } +} + +$env.config.hooks = $env.config.hooks + | upsert pre_execution { $in | default [] | append { pre_execution_hook } } + | upsert pre_prompt { $in | default [] | append { pre_prompt_hook } } + | upsert env_change.PWD { $in | default [] | append { asdf_update_java_home } }