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

Update environment variables when changed #12689

Conversation

dchristensen
Copy link
Contributor

@dchristensen dchristensen commented Aug 8, 2021

Summary of the Pull Request

What is this about:
Update Launcher's environment variables when they are changed.

What is include in the PR:

How does someone test / validate:

  1. Run Launcher and search for %TEST_ENV_VAR% (results should be empty)
  2. Set environment variable using .NET API via PowerShell powershell.exe -c "[System.Environment]::SetEnvironmentVariable('TEST_ENV_VAR', 'C:\', [System.EnvironmentVariableTarget]::User)"
  3. Run Launcher again, and retype the search query %TEST_ENV_VAR% (should see a folder result now)

Quality Checklist

Contributor License Agreement (CLA)

A CLA must be signed. If not, go over here and sign the CLA.

@ghost
Copy link

ghost commented Aug 8, 2021

CLA assistant check
All CLA requirements met.

@github-actions
Copy link

github-actions bot commented Aug 8, 2021

@check-spelling-bot Report

Unrecognized words, please review:

  • shellexecute
  • textslashplain
Previously acknowledged words that are now absent coc djsoref dogancelik Fody itsme mfreadwrite mfuuid Nefario nitroin null ulazy
To accept these unrecognized words as correct (and remove the previously acknowledged and now absent words), run the following commands

... in a clone of the git@github.com:dchristensen/PowerToys.git repository
on the 6367_launcher_update_env_variables branch:

update_files() {
perl -e '
my @expect_files=qw('".github/actions/spell-check/expect.txt"');
@ARGV=@expect_files;
my @stale=qw('"$patch_remove"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spell-check/expect.txt";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$patch_add"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'
}

comment_json=$(mktemp)
curl -L -s -S \
  --header "Content-Type: application/json" \
  "https://api.github.com/repos/microsoft/PowerToys/issues/comments/894858024" > "$comment_json"
comment_body=$(mktemp)
jq -r .body < "$comment_json" > $comment_body
rm $comment_json

patch_remove=$(perl -ne 'next unless s{^</summary>(.*)</details>$}{$1}; print' < "$comment_body")
  

patch_add=$(perl -e '$/=undef;
$_=<>;
s{<details>.*}{}s;
s{^#.*}{};
s{\n##.*}{};
s{(?:^|\n)\s*\*}{}g;
s{\s+}{ }g;
print' < "$comment_body")
  
update_files
rm $comment_body
git add -u
If you see a bunch of garbage

If it relates to a ...

well-formed pattern

See if there's a pattern that would match it.

If not, try writing one and adding it to the patterns.txt file.

Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

Note that patterns can't match multiline strings.

binary-ish string

Please add a file path to the excludes.txt file instead of just accepting the garbage.

File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

@dchristensen dchristensen marked this pull request as ready for review August 9, 2021 03:14
Copy link
Contributor

@yuyoyuppe yuyoyuppe left a comment

Choose a reason for hiding this comment

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

generally LGTM, I guess we are ok to use undocumented api in this case. what do you think @jaimecbernardo?

@jaimecbernardo
Copy link
Collaborator

Regarding the undocumented API used in this PR: [DllImport("shell32.dll", EntryPoint = "RegenerateUserEnvironment")]

It seems to have been referred to on at least another Microsoft project, like https://github.com/microsoft/winfile/blob/799daab99e30b8835037bbf32069339f8e684b69/src/winfile.c#L917
But it seems like the code had to be #if 0ed.
It's likely that it's not in any publicly available headers.

On one hand, the API seems to have been around for long and I wouldn't expect it to break.
On the other hand, accepting its use on a Microsoft's project would incentivize other projects to use it as well, where there might not be plans to have this API accessible in the future, since it's undocumented.

@dedavis6797 @DHowett , what's the policy for PowerToys regarding use of private APIs such as this one?
It seems like it might be useful for Terminal as well : microsoft/terminal#9741 (comment)

@DHowett
Copy link
Member

DHowett commented Aug 10, 2021

Unfortunately, we cannot take a dependency on a private API unless we get explicit permission from the team who owns it and, perhaps, a commitment to document it and make it public in the future.

@dchristensen
Copy link
Contributor Author

thanks @DHowett I'll update with an implementation that doesn't use the undocumented API

@DHowett
Copy link
Member

DHowett commented Aug 10, 2021

thanks @DHowett I'll update with an implementation that doesn't use the undocumented API

Thanks, and thanks for the contribution to begin with! 😄

@dchristensen dchristensen force-pushed the 6367_launcher_update_env_variables branch from fb92715 to 8e053a3 Compare August 16, 2021 04:48
Copy link
Contributor

@yuyoyuppe yuyoyuppe left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution!

@jaimecbernardo jaimecbernardo merged commit e87e06b into microsoft:master Aug 18, 2021
@dchristensen dchristensen deleted the 6367_launcher_update_env_variables branch August 18, 2021 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants