You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Local exported (set -lx) variables are not exported to functions #1091
begin set -lx VERBOSE 1; make; end #it works
begin set -lx VERBOSE 1; nice make; end #it works too
but:
function my_nice #made from bash alias
nice -n 20 $argv;
end
begin set -lx VERBOSE 1; my_nice make; end #it doesn't work
begin set -lx MYKEY MYVAL ; env | grep MYKEY ; end
function my_func
env | grep MYKEY2
end
begin; set -lx MYKEY2 MYVAL2; my_func; end
This outputs MYKEY=MYVAL but not MYKEY2=MYVAL2
This is by design. The -l flag in set -lx means "local to the current block." When you call a function, local variables are not available to that function, as in C++ or Python. Perhaps surprisingly, this applies to local exported variables: they are un-exported before invoking the function, and re-exported after the function returns.
This is problematic because it breaks encapsulation. If foo is a command, then it sees local exported variables; if foo is a wrapper function around command foo then it does not. I wonder if we should adjust the behavior of local exports in some way. Let's use this issue to track that.
Perhaps the fix could work like Perl 4 "lexical" variables (Perl 5 variables activated using my are proper lexical variables) that simply disappeared when you left the block. In my opinion Perl 4-like lexicals should use separate flag called --temp (see temp in Perl 6).
In manpage is written: "Non-global variables disappear when the block they belong to ends",
but it works weird for me...
$ begin
begin
begin
set TEST_VAR asd; #is not global
end;
echo 1st: $TEST_VAR;
end;
echo 2nd: $TEST_VAR;
if set -q TEST_VAR
set -e TEST_VAR;
end
end
1st: asd
2nd: asd
$
I thought that TEST_VAR variable will be visible in inside scopes and will be erased automatically on end of its own scope.
About -l parameter - you have right, like in C local stacked variable is not visible in inside called function.
@xdudi you are hitting the difference between local variables and function-scope variables. The documentation says:
If a variable is not explicitly set to be either universal, global or local and has never before been defined, the variable will be local to the currently executing function. Note that this is different from using the -l or –local flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function. If no function is executing, the variable will be global.
The intent is to allow code like this to work:
if foo
set var val1
else
set var val2
end
# do something with var
If variables were local-scoped by default, var would not be available outside of the block it was defined.
Needless to say, this is very confusing and any improvements you can suggest are welcome.
changed the title [-]Wrong work of set -lx during cmake compilation[/-][+]Local exported (`set -lx`) variables are not exported to functions[/+]on Aug 24, 2016
Activity
ridiculousfish commentedon Nov 3, 2013
Alternatively:
This outputs MYKEY=MYVAL but not MYKEY2=MYVAL2
This is by design. The -l flag in
set -lxmeans "local to the current block." When you call a function, local variables are not available to that function, as in C++ or Python. Perhaps surprisingly, this applies to local exported variables: they are un-exported before invoking the function, and re-exported after the function returns.This is problematic because it breaks encapsulation. If
foois a command, then it sees local exported variables; iffoois a wrapper function aroundcommand foothen it does not. I wonder if we should adjust the behavior of local exports in some way. Let's use this issue to track that.@xdudi thank you for your nice test cases.
sugar700 commentedon Nov 3, 2013
Perhaps the fix could work like Perl 4 "lexical" variables (Perl 5 variables activated using
myare proper lexical variables) that simply disappeared when you left the block. In my opinion Perl 4-like lexicals should use separate flag called--temp(seetempin Perl 6).This outputs following result, as expected.
xdudi commentedon Nov 3, 2013
In manpage is written: "Non-global variables disappear when the block they belong to ends",
but it works weird for me...
I thought that TEST_VAR variable will be visible in inside scopes and will be erased automatically on end of its own scope.
About -l parameter - you have right, like in C local stacked variable is not visible in inside called function.
ridiculousfish commentedon Nov 4, 2013
@xdudi you are hitting the difference between local variables and function-scope variables. The documentation says:
If a variable is not explicitly set to be either universal, global or local and has never before been defined, the variable will be local to the currently executing function. Note that this is different from using the -l or –local flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function. If no function is executing, the variable will be global.
The intent is to allow code like this to work:
If variables were local-scoped by default,
varwould not be available outside of the block it was defined.Needless to say, this is very confusing and any improvements you can suggest are welcome.
[-]Wrong work of set -lx during cmake compilation[/-][+]Local exported (`set -lx`) variables are not exported to functions[/+]faho commentedon Aug 24, 2016
@ridiculousfish: I've had some time to think about this, and I believe the cleanest thing to do would be to make a local copy of -lx variables.
So it would work like this:
The latter is basically what happens for external commands - if we export a variable, they get a copy of it. If they modify it, we don't see it.
ridiculousfish commentedon Aug 28, 2016
Yep, makes sense @faho
krader1961 commentedon Jul 20, 2017
Merged into the major branch for inclusion in our fish 3.0 release.