Skip to content

Copy local-exported variables - #4149

Closed
faho wants to merge 7 commits into
fish-shell:masterfrom
faho:copy-local-exported-vars
Closed

faho wants to merge 7 commits into
fish-shell:masterfrom
faho:copy-local-exported-vars

Conversation

@faho

@faho faho commented Jun 20, 2017 •

Copy link
Copy Markdown
Member

This is a quick implementation of #1091 to get the discussion going.


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

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.

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:

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.

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 just set a variable, since this would break if no global variable was defined previously. So it should always be set -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:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.md

i.e. nothing yet.

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.
@faho faho added enhancement release notes Something that is or should be mentioned in the release notes labels Jun 20, 2017
@faho faho added this to the fish 2.7.0 milestone Jun 20, 2017
@faho
faho requested a review from ridiculousfish June 20, 2017 23:15
@krader1961

Copy link
Copy Markdown
Contributor

This is backwards-incompatible, since...

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 set var in this scenario modify the local copy of the localized var from the enclosing scope rather than a global by the same name is reasonable, even correct, behavior. At least with respect to exported variables since expectations for how those behave is fundamentally different from that for non-exported vars.

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.

@faho

faho commented Jun 21, 2017

Copy link
Copy Markdown
Member Author

We should, however, start communicating this change in behavior the instant this is merged.

I've actually started right now. I've posted the following message to the fisherman slack and oh-my-fish gitter:

Hi all! We at fish-shell upstream would like to change how local-exported (set -lx) variables interact with functions. We think this is much better (in particular it enables begin; set -lx something something; somecommandorfunction; end to also work correctly with functions), but it can break some functions - those that expect to set a global var without defining it explicitly as global. Could y'all test #4149 and see if it works with your functions?

Any other channels I should post it to?

@krader1961

Copy link
Copy Markdown
Contributor

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.

@faho faho added the RFC label Jun 21, 2017
@krader1961

Copy link
Copy Markdown
Contributor

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.

@jorgebucaran

jorgebucaran commented Jun 21, 2017 •

Copy link
Copy Markdown

@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. 🤔

@faho

faho commented Jun 21, 2017

Copy link
Copy Markdown
Member Author

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.

A few more examples would help a ton.

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; end

works like

begin; set -lx PAGER cat; command git log; end

and

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" before

Back 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 x prints "banana" since it's reading the copy, the second prints "wurst" since the copy was changed, and the echo outside prints "banana" since it's reading the original again.

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 (set -el var) or set the variable to global/universal explicitly.

Consider:

function set-pager
    set PAGER $argv
end

set -gx PAGER less
set -lx PAGER
set-pager most

Previously, the "lx" variable here would be irrelevant - the function would not receive it, so the unqualified set PAGER would access the global one.

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?

@jorgebucaran

jorgebucaran commented Jun 21, 2017 •

Copy link
Copy Markdown

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:

(banana)

EDIT: Corrected output after receiving input from faho.

@faho

faho commented Jun 21, 2017

Copy link
Copy Markdown
Member Author

Does it work when calling multiple functions like this too?

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

In >=2.7.0 the output is?

(banana)

Yes.

I encourage everyone to build this and try it out for yourself!

faho added 5 commits June 21, 2017 16:46
This used to create copies even then, which meant it couldn't modify them.
This is a bit minimal, but I'm not sure how often it should be mentioned.
@faho

faho commented Jun 21, 2017

Copy link
Copy Markdown
Member Author

I've added a few commits:

@krader1961

Copy link
Copy Markdown
Contributor

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.

@faho faho modified the milestones: fish-3.0, fish 2.7.0 Jun 22, 2017
@faho

faho commented Jun 22, 2017

Copy link
Copy Markdown
Member Author

Note: 2f739a7 doesn't just apply to "--no-scope-shadowing", but also blocks (like begin; end) in general. Without it:

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" again

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

@krader1961

Copy link
Copy Markdown
Contributor

Merged into the major branch for inclusion in our fish 3.0 release.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
@faho
faho deleted the copy-local-exported-vars branch November 22, 2022 09:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement release notes Something that is or should be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants