From 2715a35b77281b7e4b637e09b9ab9ac55a36e88e Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Tue, 29 Oct 2024 11:14:09 +0200 Subject: [PATCH 1/6] perf(shell/zsh.rs): avoid hook-env execution on Enter without command Some zsh configuration are intentionally optimized to work really fast and it's important to keep it feels this way. And while `mise hook-env` is really fast (0.008 sec on my system) and slowdown isn't noticeable when running any command (even builtin one like `echo` or `true`) it still result in _noticeable_ slowdown when just pressing Enter without any command. At a glance proposed optimization shouldn't break any functionality, and it fixes mentioned above noticeable slowdown. --- src/shell/zsh.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index d65ef5031..4eaa4c896 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -42,7 +42,9 @@ impl Shell for Zsh { }} _mise_hook() {{ - eval "$({exe} hook-env{flags} -s zsh)"; + if [[ -n "$ZLAST_COMMANDS" ]]; then + eval "$({exe} hook-env{flags} -s zsh)"; + fi }} typeset -ag precmd_functions; if [[ -z "${{precmd_functions[(r)_mise_hook]+1}}" ]]; then From 98ae0118cf513f3b93cc586d19ff8081990f00c7 Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Tue, 29 Oct 2024 13:42:10 +0200 Subject: [PATCH 2/6] fix(shell/zsh.rs): always trigger hook on cd Fixes an issue when directory is changed after empty command (just Enter) using keyboard shortcut (e.g. Ctrl-PgUp/Down) instead of `cd` command - in this case hook wasn't triggered which result in showing other project's env variables to first command executed in non-project dir. --- src/shell/zsh.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 4eaa4c896..25fdd225c 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -42,13 +42,16 @@ impl Shell for Zsh { }} _mise_hook() {{ + eval "$({exe} hook-env{flags} -s zsh)"; + }} + _mise_hook_cmd() {{ if [[ -n "$ZLAST_COMMANDS" ]]; then - eval "$({exe} hook-env{flags} -s zsh)"; + _mise_hook; fi }} typeset -ag precmd_functions; - if [[ -z "${{precmd_functions[(r)_mise_hook]+1}}" ]]; then - precmd_functions=( _mise_hook ${{precmd_functions[@]}} ) + if [[ -z "${{precmd_functions[(r)_mise_hook_cmd]+1}}" ]]; then + precmd_functions=( _mise_hook_cmd ${{precmd_functions[@]}} ) fi typeset -ag chpwd_functions; if [[ -z "${{chpwd_functions[(r)_mise_hook]+1}}" ]]; then From 6df7f53c258c3b02778df0b35278e419c138666e Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Tue, 29 Oct 2024 20:42:29 +0200 Subject: [PATCH 3/6] fix(shell/zsh.rs): ensure hook-env will run on zsh start This fixes two cases: - Optimization bug: first command in a new shell won't get right env. - Adds support for `zsh -i -c '...'` which also won't get right env. --- src/shell/zsh.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 25fdd225c..7442ec7d3 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -20,6 +20,8 @@ impl Shell for Zsh { export MISE_SHELL=zsh export __MISE_ORIG_PATH="$PATH" + eval "$({exe} hook-env{flags} -s zsh)" + mise() {{ local command command="${{1:-}}" From fe67426bb92d0f49250ef36e8d67c7d2aa98bcae Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Wed, 30 Oct 2024 08:54:46 +0200 Subject: [PATCH 4/6] fix(shell/zsh.rs): deactivate _mise_hook_cmd --- src/shell/zsh.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 7442ec7d3..1194d7c15 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -89,6 +89,7 @@ impl Shell for Zsh { formatdoc! {r#" precmd_functions=( ${{precmd_functions:#_mise_hook}} ) chpwd_functions=( ${{chpwd_functions:#_mise_hook}} ) + unset -f _mise_hook_cmd unset -f _mise_hook unset -f mise unset MISE_SHELL From a25daa589e6e053f272ca0da09386cf7e71d3781 Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Wed, 30 Oct 2024 08:58:28 +0200 Subject: [PATCH 5/6] fix(shell/zsh.rs): deactivate precmd_functions --- src/shell/zsh.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 1194d7c15..f9911fa84 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -87,7 +87,7 @@ impl Shell for Zsh { fn deactivate(&self) -> String { formatdoc! {r#" - precmd_functions=( ${{precmd_functions:#_mise_hook}} ) + precmd_functions=( ${{precmd_functions:#_mise_hook_cmd}} ) chpwd_functions=( ${{chpwd_functions:#_mise_hook}} ) unset -f _mise_hook_cmd unset -f _mise_hook From 1034f3acb6fe7cc3dc755890fa3aea293b1eb785 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Tue, 5 Nov 2024 16:14:05 -0600 Subject: [PATCH 6/6] test: updated snapshots --- .../mise__cli__deactivate__tests__deactivate-2.snap | 4 +++- .../snapshots/mise__shell__zsh__tests__activate.snap | 10 ++++++++-- .../snapshots/mise__shell__zsh__tests__deactivate.snap | 4 +++- src/shell/zsh.rs | 2 -- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/cli/snapshots/mise__cli__deactivate__tests__deactivate-2.snap b/src/cli/snapshots/mise__cli__deactivate__tests__deactivate-2.snap index ac9a1130d..80a78dfa6 100644 --- a/src/cli/snapshots/mise__cli__deactivate__tests__deactivate-2.snap +++ b/src/cli/snapshots/mise__cli__deactivate__tests__deactivate-2.snap @@ -1,10 +1,12 @@ --- source: src/cli/deactivate.rs expression: output +snapshot_kind: text --- export PATH='$PATH' -precmd_functions=( ${precmd_functions:#_mise_hook} ) +precmd_functions=( ${precmd_functions:#_mise_hook_cmd} ) chpwd_functions=( ${chpwd_functions:#_mise_hook} ) +unset -f _mise_hook_cmd unset -f _mise_hook unset -f mise unset MISE_SHELL diff --git a/src/shell/snapshots/mise__shell__zsh__tests__activate.snap b/src/shell/snapshots/mise__shell__zsh__tests__activate.snap index 519352327..ad57a0a06 100644 --- a/src/shell/snapshots/mise__shell__zsh__tests__activate.snap +++ b/src/shell/snapshots/mise__shell__zsh__tests__activate.snap @@ -1,6 +1,7 @@ --- source: src/shell/zsh.rs expression: "zsh.activate(exe, \" --status\".into())" +snapshot_kind: text --- export MISE_SHELL=zsh export __MISE_ORIG_PATH="$PATH" @@ -29,9 +30,14 @@ mise() { _mise_hook() { eval "$(/some/dir/mise hook-env --status -s zsh)"; } +_mise_hook_cmd() { + if [[ -n "$ZLAST_COMMANDS" ]]; then + _mise_hook; + fi +} typeset -ag precmd_functions; -if [[ -z "${precmd_functions[(r)_mise_hook]+1}" ]]; then - precmd_functions=( _mise_hook ${precmd_functions[@]} ) +if [[ -z "${precmd_functions[(r)_mise_hook_cmd]+1}" ]]; then + precmd_functions=( _mise_hook_cmd ${precmd_functions[@]} ) fi typeset -ag chpwd_functions; if [[ -z "${chpwd_functions[(r)_mise_hook]+1}" ]]; then diff --git a/src/shell/snapshots/mise__shell__zsh__tests__deactivate.snap b/src/shell/snapshots/mise__shell__zsh__tests__deactivate.snap index 65c266ee7..bdb7d2b79 100644 --- a/src/shell/snapshots/mise__shell__zsh__tests__deactivate.snap +++ b/src/shell/snapshots/mise__shell__zsh__tests__deactivate.snap @@ -1,9 +1,11 @@ --- source: src/shell/zsh.rs expression: replace_path(&deactivate) +snapshot_kind: text --- -precmd_functions=( ${precmd_functions:#_mise_hook} ) +precmd_functions=( ${precmd_functions:#_mise_hook_cmd} ) chpwd_functions=( ${chpwd_functions:#_mise_hook} ) +unset -f _mise_hook_cmd unset -f _mise_hook unset -f mise unset MISE_SHELL diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index c137a2c66..1952b0e3e 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -20,8 +20,6 @@ impl Shell for Zsh { export MISE_SHELL=zsh export __MISE_ORIG_PATH="$PATH" - eval "$({exe} hook-env{flags} -s zsh)" - mise() {{ local command command="${{1:-}}"