-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Improve error messaging when tool requires unavailable runtime #52709
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
Draft
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/fix-tool-install-error-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+719
−24
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b32b6d
Initial plan
Copilot 6283dd7
Add runtime compatibility checking for tool installation
Copilot 6c77d86
Update tests for new runtime compatibility error messages
Copilot 2779e78
Fix redundant ".NET" in error message
Copilot 7230066
Address code review feedback
Copilot 0784c3b
Use hostfxr native library to enumerate runtimes
Copilot 84302f1
Use hostfxr_resolve_frameworks_for_runtime_config for compatibility c…
Copilot 70cb339
Address code review feedback
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
src/Cli/Microsoft.DotNet.Cli.Utils/InstalledRuntimeEnumerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.DotNet.NativeWrapper; | ||
| using NuGet.Frameworks; | ||
| using NuGet.Versioning; | ||
|
|
||
| namespace Microsoft.DotNet.Cli.Utils; | ||
|
|
||
| /// <summary> | ||
| /// Enumerates installed .NET runtimes on the system using hostfxr | ||
| /// </summary> | ||
| internal static class InstalledRuntimeEnumerator | ||
| { | ||
| private const string NetCoreAppFrameworkName = "Microsoft.NETCore.App"; | ||
|
|
||
| /// <summary> | ||
| /// Checks if a tool can run with the installed runtimes by using hostfxr to resolve frameworks | ||
| /// </summary> | ||
| /// <param name="runtimeConfigPath">Path to the tool's runtimeconfig.json file</param> | ||
| /// <returns>True if the tool can run with installed runtimes</returns> | ||
| public static bool CanResolveFrameworks(string runtimeConfigPath) | ||
| { | ||
| if (!File.Exists(runtimeConfigPath)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var bundleProvider = new NETBundlesNativeWrapper(); | ||
| return bundleProvider.CanResolveFrameworks(runtimeConfigPath); | ||
| } | ||
| catch | ||
| { | ||
| // If hostfxr call fails, return false | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets all installed .NET Core runtimes using hostfxr_get_dotnet_environment_info | ||
| /// </summary> | ||
| /// <returns>List of installed .NET Core runtime versions</returns> | ||
| public static IEnumerable<NuGetVersion> GetInstalledRuntimes() | ||
| { | ||
| var runtimes = new List<NuGetVersion>(); | ||
|
|
||
| try | ||
| { | ||
| // Get the dotnet executable directory to pass to hostfxr | ||
| var muxer = new Muxer(); | ||
| var dotnetPath = Path.GetDirectoryName(muxer.MuxerPath); | ||
|
|
||
| // Use hostfxr to enumerate runtimes | ||
| var bundleProvider = new NETBundlesNativeWrapper(); | ||
| var envInfo = bundleProvider.GetDotnetEnvironmentInfo(dotnetPath ?? string.Empty); | ||
|
|
||
| // Filter to only Microsoft.NETCore.App runtimes and convert to NuGetVersion | ||
| foreach (var runtime in envInfo.RuntimeInfo) | ||
| { | ||
| if (runtime.Name == NetCoreAppFrameworkName) | ||
| { | ||
| if (NuGetVersion.TryParse(runtime.Version.ToString(), out var version)) | ||
| { | ||
| runtimes.Add(version); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // If we fail to enumerate runtimes, return empty list | ||
| // This ensures the tool installation continues with default behavior | ||
| } | ||
|
|
||
| return runtimes; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if a compatible runtime is available for the given framework requirement | ||
| /// </summary> | ||
| /// <param name="requiredFramework">The framework required by the tool</param> | ||
| /// <param name="allowRollForward">Whether roll-forward is allowed</param> | ||
| /// <returns>True if a compatible runtime is available</returns> | ||
| public static bool IsCompatibleRuntimeAvailable(NuGetFramework requiredFramework, bool allowRollForward = false) | ||
| { | ||
| if (requiredFramework.Framework != FrameworkConstants.FrameworkIdentifiers.NetCoreApp) | ||
| { | ||
| // Only check .NET Core runtimes | ||
| return true; | ||
| } | ||
|
|
||
| var installedRuntimes = GetInstalledRuntimes(); | ||
| var requiredVersion = requiredFramework.Version; | ||
|
|
||
| foreach (var installedVersion in installedRuntimes) | ||
| { | ||
| // Exact match or higher minor version in same major | ||
| if (installedVersion.Major == requiredVersion.Major) | ||
| { | ||
| if (installedVersion.Minor >= requiredVersion.Minor) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| // If roll-forward is allowed, check for higher major versions | ||
| else if (allowRollForward && installedVersion.Major > requiredVersion.Major) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if allowing roll-forward would help find a compatible runtime | ||
| /// </summary> | ||
| /// <param name="requiredFramework">The framework required by the tool</param> | ||
| /// <returns>True if roll-forward would find a compatible runtime</returns> | ||
| public static bool WouldRollForwardHelp(NuGetFramework requiredFramework) | ||
| { | ||
| if (requiredFramework.Framework != FrameworkConstants.FrameworkIdentifiers.NetCoreApp) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var installedRuntimes = GetInstalledRuntimes(); | ||
| var requiredVersion = requiredFramework.Version; | ||
|
|
||
| // Check if there's any runtime with a higher major version | ||
| return installedRuntimes.Any(v => v.Major > requiredVersion.Major); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.