Repository navigation
Conversation
When executing a function, local-exported (`set -lx`) variables
previously were not accessible at all. This is weird e.g. in case of
aliases, since
```fish
set -lx PAGER cat
git something # which will call $PAGER
```
would not work if `git` were a function, even if that ends up calling
`command git`.
Now, we copy these variables, so functions get a local-exported copy.
```fish
function x
echo $var
set var wurst
echo $var
end
set -lx var banana
x # prints "banana" and "wurst"
echo $var # prints "banana"
```
One weirdness here is that, if a variable is both local and global,
the local-copy takes precedence:
```fish
set -gx var banana
set -lx var pineapple
echo $var # prints "pineapple"
x # from above, prints "pineapple" and "wurst"
echo $var # still prints "pineapple"
set -el var # deletes local version
echo $var # "banana" again
```
I don't think there is any more consistent way to handle this - the
local version is the one that is accessed first, so it should also be
written to first.
Global-exported variables are _not_ copied, instead they still offer
full read-write access.
Yes, but as you go on to explain that pattern is already ambiguous because the behavior depends on whether or not a global by that name already exists. I think having a bare If we actually had a roadmap that said fish 3.0 will be released in X months (where X is no more than 18 or perhaps 24) I would defer this change to that release. But since we don't, and the current behavior is the source of bugs, I'm comfortable including it in the next minor release. We should, however, start communicating this change in behavior the instant this is merged. E.g., by sending a message to the fish-users mailing list and posting messages on Gitter and IRC. And repeating the warning every month until the next release is published. |
I've actually started right now. I've posted the following message to the fisherman slack and oh-my-fish gitter:
Any other channels I should post it to? |
The fish Gitter, IRC and mailing list since the majority of our users aren't monitoring Github but are (more) likely to be watching one of those three channels. Also, good job thinking to reach out to those two project communities. |
|
See also #2611: "Defining a local variable in a function can remove export-bit outside of the function". It would be nice if a change like this also addressed that issue. |
|
@faho A few more examples would help a ton. x # prints "banana" and "wurst"Do you mean it does after your changes or before your changes? fish 2.6.0 does not print banana on my shell. 🤔 |
Yes, after my changes. That's the difference.
So, the idea here is that for any lx variable outside of a function, we define a copy of it that is also lx. This makes functions behave like external commands regarding those variables. That means function git-log
command git log
end
begin; set -lx PAGER cat; git-log; endworks like begin; set -lx PAGER cat; command git log; endand function test-var
set -qlx var; and echo "Is set locally"
or echo "Is not set"
end
set -lx var something
test-var # prints "Is set locally" after this change, and "Is not set" beforeBack to my original example: function x
echo $var
set var wurst
echo $var
end
set -lx var banana
x # prints "banana" and "wurst"
echo $var # prints "banana"This prints an empty line, "wurst" and "banana" now. After this change, the first echo in Now, as I've stated in the commit message, this has some slight weirdness - if there is both an lx variable and a global (or universal) one, the lx-copy takes precedence, so to make changes inside the function propagate to the outside, you need to either delete the local-copy ( Consider: function set-pager
set PAGER $argv
end
set -gx PAGER less
set -lx PAGER
set-pager mostPreviously, the "lx" variable here would be irrelevant - the function would not receive it, so the unqualified After this change, the function has a local copy of $PAGER (the empty one, in this case), so it will set that, and when it ends that will go out of scope. The global version won't be changed. Does that help? Does it make sense? Does it break any of your stuff? |
Yes, this makes sense. Don't think anything will break. Definitely a step in the right direction since this is how things should work IMO. 🎉 Does it work when calling multiple functions like this too? EDIT: Yes. function third
echo "($var)"
end
function second
third
end
function first
set -lx var "banana"
second
end
first In <=2.6.0 the output is: In >=2.7.0 the output is: EDIT: Corrected output after receiving input from faho. |
Yes, it stacks. Though note that the different copies are independent (e.g. if you change the var in third, that change won't propagate back to second or first).
Yes. I encourage everyone to build this and try it out for yourself! |
This used to create copies even then, which meant it couldn't modify them.
I.e. the fix for fish-shell#2611.
This is a bit minimal, but I'm not sure how often it should be mentioned.
|
I've added a few commits:
|
|
FWIW I'm now using this full-time and have not observed any problems. However, I have zero OMF or Fisherman plugins -- only a dozen or so functions I've personally written. I've opened #4154 to discuss whether we should target this and some other changes for a fish 3.0 release in the near future. |
|
Note: 2f739a7 doesn't just apply to "--no-scope-shadowing", but also blocks (like set -lx var foo
begin
echo $var # a copy of $var, set to "foo"
set var bar
echo $var # bar
end
echo $var # the original $var, "foo" againThat would be highly unintuitive and means that commit is even more important than I had assumed. I'll add a comment and test to that effect. |
Seems important.
|
Merged into the |
This is a quick implementation of #1091 to get the discussion going.
When executing a function, local-exported (
set -lx) variablespreviously were not accessible at all. This is weird e.g. in case of
aliases, since
would not work if
gitwere a function, even if that ends up callingcommand git.Now, we copy these variables, so functions get a local-exported copy.
One weirdness here is that, if a variable is both local and global,
the local-copy takes precedence:
I don't think there is any more consistent way to handle this - the
local version is the one that is accessed first, so it should also be
written to first.
This is backwards-incompatible, since a function that expects to change the global environment without a previously-defined variable (a bare
set var) will now no longer function if there is a local-exported variable. I'm unsure how to work around this. Also, it's sloppy to justseta variable, since this would break if no global variable was defined previously. So it should always beset -g.Anyway, I expect this to be the main point of contention. One possibility is to push this to fish 3.0 and formally declare a backwards-compatibility break.
Global-exported variables are not copied, instead they still offer
full read-write access.
TODOs:
i.e. nothing yet.