Pure: Use `typeset -g` for global variables in functions

Created on 10 Sep 2017  路  13Comments  路  Source: sindresorhus/pure

General information

  • Pure version: HEAD 4082418e4feffd876e0f666aa5cfbd7d3360ed16
  • ZSH version: 5.4.1
  • ZSH framework: -

Problem description

Please use declare -g ( or typeset -g, it's the same) to declaring global variables in functions. So, people who set setopt WARN_CREATE_GLOBAL won't get warning like such:

prompt_pure_check_cmd_exec_time:3: scalar parameter prompt_pure_cmd_exec_time created globally in function prompt_pure_check_cmd_exec_time
prompt_pure_check_cmd_exec_time:4: scalar parameter PURE_CMD_MAX_EXEC_TIME created globally in function prompt_pure_check_cmd_exec_time
prompt_pure_precmd:6: scalar parameter prompt_pure_cmd_timestamp created globally in function prompt_pure_precmd
prompt_pure_async_tasks:7: scalar parameter prompt_pure_async_init created globally in function prompt_pure_async_tasks
prompt_pure_preprompt_render:63: scalar parameter prompt_pure_last_prompt created globally in function prompt_pure_preprompt_render

Reproduction steps

  1. Wrote setopt WARN_CREATE_GLOBAL in .zshrc.
  2. Reload .zshrc.
  3. Errors appeared.

All 13 comments

Thanks, it's a good suggestion. I was in the process of changing this at some point, but it lies dormant in one of my git stashes 馃槢. I'll fix this soon!

Thanks! Please do that for setopt WARN_NESTED_VAR too.

I'll consider it, but I'm not making any promises. These warn options are mostly useful for development and shouldn't be enabled in day-to-day use (IMO). Sometimes setting nested vars is the wanted effect.

Well, you can still use nested vars if you want to with typeset -g / declare -g, and it won't make it global, it just suprese the error. Consider this example.

setopt WARN_NESTED_VAR

function toplevel() {
    local foo="in toplevel"
    nested
}

function nested() {
    echo $foo # "in toplevel"

    foo="in nested" # nested:3: scalar parameter foo set in enclosing scope in function nested in nested

    declare -g foo="in nested"
    declare -g bar="global"

    echo $foo # "in nested"
}

toplevel

echo $foo # ""
echo $bar # "global"

Just pushed #347, can you confirm that it fixes your issues with warn_create_global? I opted for doing nothing about warn_nested_var although I'm not totally against it, but I would need to understand / see how making the changes necessary improves the code.

Thanks! It looks okay now, all of my errors are gone.

Thanks for the report @kutsan, pushed a fix to master. Missed this one because I didn't have any unpushed/pulled commits when I tested this.

No problem! Thanks for quick fix! I really appreciate it.

By the way, the reason why it took so long to realize that it's because prompt was redrawing itself before I saw that error. Somehow, it appeared when I am messing with tmux panes. I would have told you if I saw that before. So, I'm sorry.

There鈥檚 absolutely no need to be sorry. It was good that this was caught now. 馃檪

And again.

I think you need to silence them with:

local match=
local mbegin=
local mend=

@kutsan thanks, fix is live on master!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tyrollins picture tyrollins  路  4Comments

ebsen picture ebsen  路  10Comments

julenpardo picture julenpardo  路  5Comments

lezuber picture lezuber  路  11Comments

bigH picture bigH  路  8Comments