Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] parameter ... set in enclosing scope #781

Open
ratijas opened this issue Nov 12, 2020 · 10 comments
Open

[BUG] parameter ... set in enclosing scope #781

ratijas opened this issue Nov 12, 2020 · 10 comments

Comments

@ratijas
Copy link

ratijas commented Nov 12, 2020

While sourcing zsh-syntax-highlighting.zsh, zsh encounter couple of warnings.

/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh:434: array parameter ZSH_HIGHLIGHT_HIGHLIGHTERS set in enclosing scope in function (anon)
_zsh_highlight_main__precmd_hook:1: array parameter _zsh_highlight_main__command_type_cache set in enclosing scope in function _zsh_highlight_main__precmd_hook

I have my WARN_CREATE_GLOBAL and WARN_NESTED_VAR zsh options turned on.

See also

#438

@ratijas
Copy link
Author

ratijas commented Nov 12, 2020

Also, when I cloned the repository, and loaded plugin from the latest master, errors slightly relocated:

zsh-syntax-highlighting.zsh:48: scalar parameter ZSH_HIGHLIGHT_REVISION set in enclosing scope in function (anon)
zsh-syntax-highlighting.zsh:584: array parameter ZSH_HIGHLIGHT_HIGHLIGHTERS set in enclosing scope in function (anon)

@phy1729
Copy link
Member

phy1729 commented Nov 12, 2020

All of the referenced variables are declared global before being set. The test framework also sets WARN_CREATE_GLOBAL to catch issues like these. Could you see if something else in your configuration is necessary to reproduce the issue?

@ratijas
Copy link
Author

ratijas commented Nov 12, 2020

Interesting. I'm using my dotfiles repository, branch zsh. Relevant options might be in 11.options-user.zsh.

https://github.com/ratijas/dotfiles/tree/zsh.

@ratijas
Copy link
Author

ratijas commented Nov 12, 2020

Looking briefly at the code, I don't even know what could be wrong with it, or how zsh imagines it should be.

# Set $0 to the expected value, regardless of functionargzero.
0=${(%):-%N}
if true; then
  # $0 is reliable
  typeset -g ZSH_HIGHLIGHT_VERSION=$(<"${0:A:h}"/.version)
  typeset -g ZSH_HIGHLIGHT_REVISION=$(<"${0:A:h}"/.revision-hash)
  if [[ $ZSH_HIGHLIGHT_REVISION == \$Format:* ]]; then
    # When running from a source tree without 'make install', $ZSH_HIGHLIGHT_REVISION
    # would be set to '$Format:%H$' literally.  That's an invalid value, and obtaining
    # the valid value (via `git rev-parse HEAD`, as Makefile does) might be costly, so:
    ZSH_HIGHLIGHT_REVISION=HEAD
  fi
fi

The problem is reported at ZSH_HIGHLIGHT_REVISION=HEAD line.

@ratijas
Copy link
Author

ratijas commented Nov 12, 2020

Oh, I'm sorry. I provided the wrong info. I have WARN_NESTED_VAR option on. Edited the top post.

On a second thought, maybe I shouldn't be using this option. It seems too restrictive without obvious benefits. What do you think?

@danielshahaf
Copy link
Member

Oh, I'm sorry. I provided the wrong info. I have WARN_NESTED_VAR option on. Edited the top post.

The error message under master is in the second post.

Given that the option exists, z-sy-h should support it. However, for ZSH_HIGHLIGHT_REVISION there is no reason why it would be set in the enclosing scope, and for ZSH_HIGHLIGHT_HIGHLIGHTERS, no reason why it would be set but empty in the enclosing scope. Which is to say, we're in need of a reproduction recipe.

On a second thought, maybe I shouldn't be using this option. It seems too restrictive without obvious benefits. What do you think?

It seems to me that the option does have bug-finding potential. Feel free to discuss this further on zsh's support channels.

@phy1729
Copy link
Member

phy1729 commented Nov 15, 2020

I agree it seems silly to set in interactive use, but as @danielshahaf said we still ought to support it.

I can't reproduce the interactive bug, but this avoids warnings when the test harness sets WARN_NESTED_VAR

diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh
index 8b564a8..cab2fe3 100755
--- a/tests/test-highlighting.zsh
+++ b/tests/test-highlighting.zsh
@@ -29,7 +29,7 @@
 # -------------------------------------------------------------------------------------------------
 
 
-setopt NO_UNSET WARN_CREATE_GLOBAL
+setopt NO_UNSET WARN_CREATE_GLOBAL WARN_NESTED_VAR
 
 # Required for add-zle-hook-widget.
 zmodload zsh/zle
@@ -85,6 +85,8 @@ else
   exit 1
 fi > >($results_filter | $root/tests/tap-colorizer.zsh)
 
+setopt NOWARN_NESTED_VAR
+
 # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
 _zsh_highlight_add_highlight()
 {
@@ -154,7 +156,9 @@ run_test_internal() {
     : ${CURSOR=$#BUFFER} ${PENDING=0} ${WIDGET=z-sy-h-test-harness-test-widget}
 
     # Process the data.
+    setopt WARN_NESTED_VAR
     _zsh_highlight
+    setopt NOWARN_NESTED_VAR
   }; [[ -z $RETURN ]] || return $RETURN
   unset ARG
 
diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh
index d20dc5b..2a45136 100644
--- a/zsh-syntax-highlighting.zsh
+++ b/zsh-syntax-highlighting.zsh
@@ -45,7 +45,7 @@ if true; then
     # When running from a source tree without 'make install', $ZSH_HIGHLIGHT_REVISION
     # would be set to '$Format:%H$' literally.  That's an invalid value, and obtaining
     # the valid value (via `git rev-parse HEAD`, as Makefile does) might be costly, so:
-    ZSH_HIGHLIGHT_REVISION=HEAD
+    typeset -g ZSH_HIGHLIGHT_REVISION=HEAD
   fi
 fi
 
@@ -130,7 +130,8 @@ _zsh_highlight()
   }
 
   # Probe the memo= feature, once.
-  (( ${+zsh_highlight__memo_feature} )) || {
+  (( ${+zsh_highlight__memo_feature} )) || (){
+    setopt localoptions nowarnnestedvar
     region_highlight+=( " 0 0 fg=red, memo=zsh-syntax-highlighting" )
     case ${region_highlight[-1]} in
       ("0 0 fg=red")
@@ -175,10 +176,10 @@ _zsh_highlight()
 
   # Reset region_highlight to build it from scratch
   if (( zsh_highlight__memo_feature )); then
-    region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
+    typeset -g region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
   else
     # Legacy codepath.  Not very interoperable with other plugins (issue #418).
-    region_highlight=()
+    typeset -g region_highlight=()
   fi
 
   # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
@@ -581,7 +582,7 @@ add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || {
 zmodload zsh/parameter 2>/dev/null || true
 
 # Initialize the array of active highlighters if needed.
-[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
+[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && typeset -g ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
 
 if (( $+X_ZSH_HIGHLIGHT_DIRS_BLACKLIST )); then
   print >&2 'zsh-syntax-highlighting: X_ZSH_HIGHLIGHT_DIRS_BLACKLIST is deprecated. Please use ZSH_HIGHLIGHT_DIRS_BLACKLIST.'

@danielshahaf
Copy link
Member

danielshahaf commented Nov 16, 2020 via email

@danielshahaf
Copy link
Member

I'm not sure I'm happy with this strategy.

Which is to say: why don't we just unset the option? Cross-referencing #758.

@FranklinYu
Copy link

FranklinYu commented Sep 14, 2021

update

Sorry for the spam. I wasn’t aware that this error message is about warn_nested_var instead of warn_create_global. z-sy-h works perfectly under warn_create_global; I guess I’ll simply disable warn_nested_var.

original comment

With my current Zsh configuration, I can reproduce the second bug:

_zsh_highlight_main__precmd_hook:1: array parameter _zsh_highlight_main__command_type_cache set in enclosing scope in function _zsh_highlight_main__precmd_hook

This is weird since I can clearly see the variable set as global in

typeset -gA _zsh_highlight_main__command_type_cache

I’ll try finding a minimal example to reproduce.

FranklinYu added a commit to FranklinYu/XDG-configuration that referenced this issue Sep 14, 2021
This reverts commit 42332c6.

Reason: maintainer of zsh-syntax-highlighting [doesn’t like][1]
`warn_nested_var`. The other option, `warn_create_global`, might be
fine, but more experiments should be done with it.

[1]: zsh-users/zsh-syntax-highlighting#781 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants