From 7cb452c7e72ed5aab4bbe1ad391f9e22d7b7ff49 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Wed, 27 May 2020 19:56:47 +0200 Subject: [PATCH] Computed variables are global Variables like $status and $history showed up in all scopes, including universal, when querying with `set -q` or `set -S`. This makes it so they all only count as set in global scope, because we already only allow assignment to electric variables in global scope. Fixes #7032 --- src/env.cpp | 7 ++++++- tests/checks/set.fish | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/env.cpp b/src/env.cpp index d7c526403..fd985c818 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -734,7 +734,12 @@ maybe_t env_scoped_impl_t::try_get_universal(const wcstring &key) con maybe_t env_scoped_impl_t::get(const wcstring &key, env_mode_flags_t mode) const { const query_t query(mode); - maybe_t result = try_get_computed(key); + maybe_t result; + // Computed variables are effectively global and can't be shadowed. + if (query.global) { + result = try_get_computed(key); + } + if (!result && query.local) { result = try_get_local(key); } diff --git a/tests/checks/set.fish b/tests/checks/set.fish index 89fc348b9..5c1096d1e 100644 --- a/tests/checks/set.fish +++ b/tests/checks/set.fish @@ -624,4 +624,15 @@ $FISH -c 'set __fish_test_universal_exported_var 2' env | string match -e __fish_test_universal_exported_var #CHECK: __fish_test_universal_exported_var=2 +# Test that computed variables are global. +# If they can be set they can only be set in global scope, +# so they should only be shown in global scope. +set -S status +#CHECK: $status: set in global scope, unexported, with 1 elements +#CHECK: $status[1]: |0| + +set -ql history +echo $status +#CHECK: 1 + true