Skip to content

Commit

Permalink
Minor tweaks to host tracing around registered install location (#68843)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored May 4, 2022
1 parent e109bbd commit 5992145
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static AndConstraint<CommandResultAssertions> HaveUsedDotNetRootInstallLo
return assertion.HaveStdErrContaining($"Using environment variable {expectedEnvironmentVariable}=[{installLocation}] as runtime location.");
}

public static AndConstraint<CommandResultAssertions> HaveUsedConfigFileInstallLocation(this CommandResultAssertions assertion, string installLocation)
public static AndConstraint<CommandResultAssertions> HaveUsedRegisteredInstallLocation(this CommandResultAssertions assertion, string installLocation)
{
return assertion.HaveStdErrContaining($"Using install location '{installLocation}'.");
return assertion.HaveStdErrContaining($"Found registered install location '{installLocation}'.");
}

public static AndConstraint<CommandResultAssertions> HaveUsedGlobalInstallLocation(this CommandResultAssertions assertion, string installLocation)
Expand All @@ -53,7 +53,7 @@ public static AndConstraint<CommandResultAssertions> HaveLookedForDefaultInstall

public static AndConstraint<CommandResultAssertions> HaveLookedForArchitectureSpecificInstallLocation(this CommandResultAssertions assertion, string installLocationPath, string architecture)
{
return assertion.HaveStdErrContaining($"Looking for architecture specific install_location file in '{Path.Combine(installLocationPath, "install_location_" + architecture.ToLowerInvariant())}'.");
return assertion.HaveStdErrContaining($"Looking for architecture-specific install_location file in '{Path.Combine(installLocationPath, "install_location_" + architecture.ToLowerInvariant())}'.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void EnvironmentVariable_DotnetRootPathExistsButHasNoHost()
}

[Fact]
public void InstallLocationFile_ArchSpecificLocationIsPickedFirst()
public void RegisteredInstallLocation_ArchSpecificLocationIsPickedFirst()
{
var fixture = sharedTestState.PortableAppFixture
.Copy();
Expand Down Expand Up @@ -178,7 +178,9 @@ public void InstallLocationFile_ArchSpecificLocationIsPickedFirst()
.HaveLookedForArchitectureSpecificInstallLocation(registeredInstallLocationOverride.PathValueOverride, arch2);
}

result.Should().HaveUsedGlobalInstallLocation(path2);
result.Should()
.HaveUsedRegisteredInstallLocation(path2)
.And.HaveUsedGlobalInstallLocation(path2);
}
}

Expand All @@ -204,7 +206,7 @@ public void InstallLocationFile_ReallyLongInstallPathIsParsedCorrectly()
.DotNetRoot(null)
.Execute()
.Should().HaveLookedForDefaultInstallLocation(registeredInstallLocationOverride.PathValueOverride)
.And.HaveUsedConfigFileInstallLocation(reallyLongPath);
.And.HaveUsedRegisteredInstallLocation(reallyLongPath);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public void GetHostFxrPath_GlobalInstallation(bool explicitLoad, bool useAssembl
.Execute();
}

result.Should().HaveStdErrContaining("Using global installation location");
result.Should().HaveUsedGlobalInstallLocation(installLocation);
if (useRegisteredLocation)
result.Should().HaveUsedRegisteredInstallLocation(installLocation);

if (isValid)
{
Expand Down Expand Up @@ -213,7 +215,7 @@ public void GetHostFxrPath_InstallLocationFile(string value, bool shouldUseArchS
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
Expand All @@ -233,7 +235,7 @@ public void GetHostFxrPath_InstallLocationFile(string value, bool shouldUseArchS
if (shouldPass)
{
result.Should().Pass()
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveUsedRegisteredInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
else
Expand Down Expand Up @@ -262,7 +264,7 @@ public void GetHostFxrPath_GlobalInstallation_HasNoDefaultInstallationPath()
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
Expand All @@ -272,7 +274,7 @@ public void GetHostFxrPath_GlobalInstallation_HasNoDefaultInstallationPath()
.And.HaveLookedForArchitectureSpecificInstallLocation(
registeredInstallLocationOverride.PathValueOverride,
sharedState.RepoDirectories.BuildArchitecture)
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveUsedRegisteredInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
}
Expand All @@ -293,7 +295,7 @@ public void GetHostFxrPath_GlobalInstallation_ArchitectureSpecificPathIsPickedOv
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
Expand All @@ -303,7 +305,7 @@ public void GetHostFxrPath_GlobalInstallation_ArchitectureSpecificPathIsPickedOv
.And.HaveLookedForArchitectureSpecificInstallLocation(
registeredInstallLocationOverride.PathValueOverride,
sharedState.RepoDirectories.BuildArchitecture)
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveUsedRegisteredInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/native/corehost/hostmisc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ namespace pal

// Returns the globally registered install location (if any)
bool get_dotnet_self_registered_dir(string_t* recv);
// Returns name of the global registry location (for error messages)

// Returns name of the config location for global install registration (for example, registry key or file path)
string_t get_dotnet_self_registered_config_location();

// Returns the default install location for a given platform
Expand Down
4 changes: 2 additions & 2 deletions src/native/corehost/hostmisc/pal.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
pal::string_t install_location_path = get_dotnet_self_registered_config_location();
pal::string_t arch_specific_install_location_file_path = install_location_path;
append_path(&arch_specific_install_location_file_path, (_X("install_location_") + to_lower(get_arch())).c_str());
trace::verbose(_X("Looking for architecture specific install_location file in '%s'."), arch_specific_install_location_file_path.c_str());
trace::verbose(_X("Looking for architecture-specific install_location file in '%s'."), arch_specific_install_location_file_path.c_str());

pal::string_t install_location;
bool file_found = false;
Expand All @@ -523,7 +523,7 @@ bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
}

recv->assign(install_location);
trace::verbose(_X("Using install location '%s'."), recv->c_str());
trace::verbose(_X("Found registered install location '%s'."), recv->c_str());
return true;
}

Expand Down
26 changes: 22 additions & 4 deletions src/native/corehost/hostmisc/pal.windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ namespace
*sub_key = dotnet_key_path + pal::string_t(_X("\\Setup\\InstalledVersions\\")) + get_arch();
*value = _X("InstallLocation");
}

pal::string_t registry_path_as_string(const HKEY& key_hive, const pal::string_t& sub_key, const pal::char_t* value)
{
assert(key_hive == HKEY_CURRENT_USER || key_hive == HKEY_LOCAL_MACHINE);
return (key_hive == HKEY_CURRENT_USER ? _X("HKCU\\") : _X("HKLM\\")) + sub_key + _X("\\") + value;
}
}

pal::string_t pal::get_dotnet_self_registered_config_location()
Expand All @@ -334,7 +340,7 @@ pal::string_t pal::get_dotnet_self_registered_config_location()
const pal::char_t* value;
get_dotnet_install_location_registry_path(&key_hive, &sub_key, &value);

return (key_hive == HKEY_CURRENT_USER ? _X("HKCU\\") : _X("HKLM\\")) + sub_key + _X("\\") + value;
return registry_path_as_string(key_hive, sub_key, value);
}

bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
Expand All @@ -355,13 +361,24 @@ bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
const pal::char_t* value;
get_dotnet_install_location_registry_path(&hkeyHive, &sub_key, &value);

if (trace::is_enabled())
trace::verbose(_X("Looking for architecture-specific registry value in '%s'."), registry_path_as_string(hkeyHive, sub_key, value).c_str());

// Must use RegOpenKeyEx to be able to specify KEY_WOW64_32KEY to access the 32-bit registry in all cases.
// The RegGetValue has this option available only on Win10.
HKEY hkey = NULL;
LSTATUS result = ::RegOpenKeyExW(hkeyHive, sub_key.c_str(), 0, KEY_READ | KEY_WOW64_32KEY, &hkey);
if (result != ERROR_SUCCESS)
{
trace::verbose(_X("Can't open the SDK installed location registry key, result: 0x%X"), result);
if (result == ERROR_FILE_NOT_FOUND)
{
trace::verbose(_X("The registry key ['%s'] does not exist."), sub_key.c_str());
}
else
{
trace::verbose(_X("Failed to open the registry key. Error code: 0x%X"), result);
}

return false;
}

Expand All @@ -370,7 +387,7 @@ bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
result = ::RegGetValueW(hkey, nullptr, value, RRF_RT_REG_SZ, nullptr, nullptr, &size);
if (result != ERROR_SUCCESS || size == 0)
{
trace::verbose(_X("Can't get the size of the SDK location registry value or it's empty, result: 0x%X"), result);
trace::verbose(_X("Failed to get the size of the install location registry value or it's empty. Error code: 0x%X"), result);
::RegCloseKey(hkey);
return false;
}
Expand All @@ -380,13 +397,14 @@ bool pal::get_dotnet_self_registered_dir(pal::string_t* recv)
result = ::RegGetValueW(hkey, nullptr, value, RRF_RT_REG_SZ, nullptr, &buffer[0], &size);
if (result != ERROR_SUCCESS)
{
trace::verbose(_X("Can't get the value of the SDK location registry value, result: 0x%X"), result);
trace::verbose(_X("Failed to get the value of the install location registry value. Error code: 0x%X"), result);
::RegCloseKey(hkey);
return false;
}

recv->assign(buffer.data());
::RegCloseKey(hkey);
trace::verbose(_X("Found registered install location '%s'."), recv->c_str());
return true;
}

Expand Down

0 comments on commit 5992145

Please sign in to comment.