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

Create til::env to help with refreshing environment variables #14839

Merged
merged 22 commits into from
Mar 13, 2023

Conversation

ianjoneill
Copy link
Contributor

@ianjoneill ianjoneill commented Feb 13, 2023

Adds a helper that replicates how the RegenerateUserEnvironment()
method in shell32.dll behaves.

Co-authored-by: Michael Niksa [email protected]

@github-actions

This comment has been minimized.

src/inc/til/env.h Outdated Show resolved Hide resolved
@ianjoneill
Copy link
Contributor Author

Hmm, the environment blocks produced by RegenerateUserEnvironment() and the new method aren't the same during the test run on Azure. The expected temp directory has the DOS short name for the username, whereas the environment class produces a path containing the full username. On my local machine the test passes - as my username is Ian. I'm guessing Michael's username was miniksa so he didn't see this either?

2023-02-13T14:09:08.3435350Z Error: Verify: AreEqual(expectedValue, actual.as_map()[expectedKey]) - Values (C:\Users\CLOUDT~1\AppData\Local\Temp, C:\Users\cloudtest\AppData\Local\Temp) [File: C:\a\_work\1\s\src\til\ut_til\EnvTests.cpp, Function: EnvTests::Generate, Line: 44]

@ianjoneill
Copy link
Contributor Author

So on my work PC, which has %USERPROFILE% as C:\Users\ian.oneill, RegenerateUserEnvironment() returns the following:

TEMP=C:\Users\IAN~1.ONE\AppData\Local\Temp
TEMPTEST1=C:\WINDOWS\TEMP\foo
TEMPTEST2=C:\WINDOWS\TEMP
TEMPTEST3=C:\Users\IAN~1.ONE\AppData\Local\Temp\foo
TEMPTEST4=C:\Users\IAN~1.ONE\AppData\Local\Temp
TMP=C:\Users\IAN~1.ONE\AppData\Local\Temp

With HKCU\Environment set to:
image

This suggests check_for_temp() needs a bit of tweaking.

src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
}
else
{
set_user_environment_var(valueName, std::wstring{ data });
Copy link
Member

Choose a reason for hiding this comment

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

this will expand it even during pass 1 for a REG_SZ; is that definitely what we want? I suspect it is, because the environment is strange

src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
static constexpr std::wstring_view os2LibPath{ L"Os2LibPath" };
bool is_path_var(std::wstring_view input)
{
return !_wcsicmp(input.data(), path.data()) || !_wcsicmp(input.data(), libPath.data()) || !_wcsicmp(input.data(), os2LibPath.data());
Copy link
Member

Choose a reason for hiding this comment

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

@lhecker may be able to recommend a cleaner way to do this

src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
@ianjoneill
Copy link
Contributor Author

All comments should now hopefully be addressed

@ianjoneill
Copy link
Contributor Author

Test failures are flaky feature tests.

Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

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

I'm not too happy about the amount of string copies, but I'd be happy to merge this anyways. I'd love to see some or all of my comments to be addressed though.

src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
src/inc/til/env.h Outdated Show resolved Hide resolved
// length will receive the number of bytes including trailing null byte. Convert to a number of wchar_t's.
// AdaptFixedSizeToAllocatedResult will then resize buffer to valueLengthNeededWithNull.
// We're rounding up to prevent infinite loops if the data isn't a REG_SZ and length isn't divisible by 2.
*valueLengthNeededWithNul = (length + sizeof(wchar_t) - 1) / sizeof(wchar_t);
Copy link
Member

Choose a reason for hiding this comment

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

This function is a little bit cheeky since RegQueryValueExW returns binary data, but this assumes the caller is trying to get a wide string. I think it's kind of fine though...

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

Assuming that the underlying algorithm that @miniksa wrote for regenerating the environment is correct (I have no reason to believe it isn't), then the rest of this looks good to me.

I'd love if @miniksa could give this another once over, but the Terminal ain't his day job anymore 😥

Sorry it took us so long to get around to this! You caught us at a particularly busy couple months 😅 Looking forward to shipping this (and the follow up 😉)

inline HRESULT GetComputerNameW(string_type& result) WI_NOEXCEPT
{
return wil::AdaptFixedSizeToAllocatedResult<string_type, initialBufferLength>(result,
[&](_Out_writes_(valueLength) PWSTR value, size_t valueLength, _Out_ size_t* valueLengthNeededWithNul) -> HRESULT {
Copy link
Member

Choose a reason for hiding this comment

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

holy indenting batman. This is totally a nit, but I'd pull that lambda out to a local, just for the sake of some sensible indenting

@ianjoneill
Copy link
Contributor Author

Looking forward to shipping this (and the follow up 😉)

That is waiting in the wings and is a grand total of 22 additional lines.

@zadjii-msft
Copy link
Member

@msftbot merge this at 7pm

(to give @miniksa and @DHowett a last chance to block)

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

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

block to review

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 3, 2023
Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

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

LET'S GO! YEAH!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants