Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow erasing in multiple scopes in one go #9280

Merged
merged 4 commits into from
Oct 20, 2022

Conversation

mqudsi
Copy link
Contributor

@mqudsi mqudsi commented Oct 15, 2022

While I was working on fish_config I found myself trying to execute set -egl ... a few times, and I couldn't come up with a good reason for why this wasn't allowed.

This patch extends set -e to allow unsetting a variable from multiple scopes in one go. (The docs and changelog would also need to be updated.)

Closes #7711.

// `set -e` is allowed to be called with multiple scopes.
for (int bit = 0; 1<<bit <= ENV_USER; ++bit) {
int scope = scopes & 1<<bit;
if (scope == 0 || (scope == ENV_USER && scopes != ENV_USER)) continue;
Copy link
Contributor

@krobelus krobelus Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supporting set -elgU sounds great!

@faho
Copy link
Member

faho commented Oct 16, 2022

Fixes #7711

@faho faho added this to the fish 3.6.0 milestone Oct 16, 2022
int scope = scopes & 1<<bit;
if (scope == 0 || (scope == ENV_USER && scopes != ENV_USER)) continue;
for (int i = 0; i < argc; i++) {
auto split = split_var_and_indexes(argv[i], scope, parser.vars(), streams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming with indexes this works like:

set -g foo 1 2 3
set -l foo a b c

set -egl foo[2]

makes the global foo "1 3" and the local foo "a c"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed how it works, and it's probably a good test case.

Variable events are also fired for each scope, which is probably okay for now.

(tbh I'm still of the opinion local variables shouldn't fire events - you can't read them in an event handler anyway - but last time that turned out to be unpopular)

Copy link
Contributor Author

@mqudsi mqudsi Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed how it works, and it's probably a good test case.
Variable events are also fired for each scope, which is probably okay for now.

Yes, what I had in mind the entire time was that set -elg foo should behave exactly as if set -el foo; set -eg foo (except for the return codes, which are already somewhat non-sensical and need to be fixed later), side effects and all. We can always change that behavior later if we have a good reason to, but I wanted to minimize the scope of the changes for this PR to make this an easier yes/no decision.

(tbh I'm still of the opinion local variables shouldn't fire events - you can't read them in an event handler anyway - but last time that turned out to be unpopular)

I must have missed that discussion. Do you happen to remember what issue this was in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have missed that discussion. Do you happen to remember what issue this was in?

#8384, which asked about for-loop variables.

In that case, we chose to keep it because someone could try to do something with --no-scope-shadowing.

@mqudsi
Copy link
Contributor Author

mqudsi commented Oct 16, 2022

Fixes #7711

When I was drafting the PR, I originally wrote "A team member recently mentioned in the chat that this either is possible or should be possible" but then I couldn't find it in the chat and was left confused where I had seen it. Thanks for linking it!

@mqudsi
Copy link
Contributor Author

mqudsi commented Oct 18, 2022

Added tests and updated docs.

@ridiculousfish
Copy link
Member

Nice fix, LGTM! Please changelog before merging.

@mqudsi mqudsi merged commit a3ad5d6 into fish-shell:master Oct 20, 2022
@mqudsi mqudsi deleted the set_erase_multi branch October 28, 2022 19:06
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: 'set --erase' for multiple/all scopes
4 participants