elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.67k stars 300 forks source link

Track and report where a var was last set #1228

Open krader1961 opened 3 years ago

krader1961 commented 3 years ago

There was a recent question on Gitter/IM about how to track where an env var was last set. The execution tracing change I'm working on will provide one way to answer that question. For example, consider if ~/.x.elv contains these lines:

E:WTF = hello
set-env WTF goodbye
put $E:WTF

Then this is what tracing the execution of that script would output:

> elvish -trace=cmd ~/x.elv
  0.000000 [cmd     ] @ /Users/krader/x.elv, line 1
       ... [cmd     ] E:WTF = hello
       ... [cmd     ] RHS VALS: hello
  0.000116 [cmd     ] @ /Users/krader/x.elv, line 2
       ... [cmd     ] set-env WTF goodbye
       ... [cmd     ] CMD ARGS: WTF goodbye
  0.000059 [cmd     ] @ /Users/krader/x.elv, line 3
       ... [cmd     ] put $E:WTF
       ... [cmd     ] CMD ARGS: goodbye
▶ goodbye

However, it would be nice if Elvish supported something like ViM's tracking where vars are set so that the information could be directly queried as in this example:

:verbose set shiftwidth
  shiftwidth=4
        Last set from ~/Dropbox/dotfiles/config/vim/pack/tpope/start/sleuth/plugin/sleuth.vim line 134

Tracking where each var is set is (relatively) straightforward. The question is how to expose that information. One possibility is to leverage the recently introduced set command by adding an option that would output that information. Something like this hypothetical example (building on the previous ~/x.elv script):

> set &where E:WTF
▶ Users/krader/x.elv, line 3
krader1961 commented 3 years ago

Obviously there are details that need discussion. Such as what to report for the output of set &where when the environment variable was inherited and not explicitly set by Elvish. The output for that case should probably be something like "inherited" (without a line number).

krader1961 commented 2 years ago

A user recently asked on IM why they were getting this error even though up until recently their Elvish shell had no trouble finding the volta external command:

Exception: exec: "volta": executable file not found in $PATH

It turned out they had modified their ~/.config/elvish/rc.elv to include this line:

set paths = [/opts/bin /bin /usr/bin]

They figured it out, with hints from me, but it would have been easier to resolve the issue if there were a way to report where the var had most recently been set.