Skip to content

Commit

Permalink
perf(shell/zsh.rs): avoid hook-env execution on Enter without command (
Browse files Browse the repository at this point in the history
…#2861)

* 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.

* 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.

* 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.

* fix(shell/zsh.rs): deactivate _mise_hook_cmd

* fix(shell/zsh.rs): deactivate precmd_functions

* test: updated snapshots

---------

Co-authored-by: jdx <[email protected]>
  • Loading branch information
powerman and jdx authored Nov 5, 2024
1 parent c4b1628 commit a1cdc4b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions src/shell/snapshots/mise__shell__zsh__tests__activate.snap
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/shell/snapshots/mise__shell__zsh__tests__deactivate.snap
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions src/shell/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ impl Shell for Zsh {
_mise_hook() {{
eval "$({exe} hook-env{flags} -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
Expand Down Expand Up @@ -80,8 +85,9 @@ 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
unset -f mise
unset MISE_SHELL
Expand Down

0 comments on commit a1cdc4b

Please sign in to comment.