-
Notifications
You must be signed in to change notification settings - Fork 5
Fix .NET runtime version mismatched (.NET 9 support, safer runtime detection)) #130
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
42 changes: 42 additions & 0 deletions
42
src/Microsoft.Agents.A365.DevTools.Cli/Exceptions/DotNetSdkVersionMismatchException.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,42 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.A365.DevTools.Cli.Constants; | ||
|
|
||
| namespace Microsoft.Agents.A365.DevTools.Cli.Exceptions; | ||
|
|
||
| /// <summary> | ||
| /// Thrown when there is a .NET SDK version mismatch. | ||
| /// </summary> | ||
| public sealed class DotNetSdkVersionMismatchException : Agent365Exception | ||
| { | ||
| public override bool IsUserError => true; | ||
| public override int ExitCode => 1; | ||
|
|
||
| public DotNetSdkVersionMismatchException( | ||
| string requiredVersion, | ||
| string? installedVersion, | ||
| string projectFilePath) | ||
| : base( | ||
| errorCode: ErrorCodes.DotNetSdkVersionMismatch, | ||
| issueDescription: $"The project targets .NET {requiredVersion}, but the required .NET SDK is not installed.", | ||
| errorDetails: new List<string> | ||
| { | ||
| $"Project file: {projectFilePath}", | ||
| $"TargetFramework: net{requiredVersion}", | ||
| $"Installed SDK version: {installedVersion ?? "Not found"}" | ||
| }, | ||
| mitigationSteps: new List<string> | ||
| { | ||
| $"Install the .NET {requiredVersion} SDK from https://dotnet.microsoft.com/download", | ||
| "Restart your terminal after installation", | ||
| "Re-run the a365 deploy command" | ||
| }, | ||
| context: new Dictionary<string, string> | ||
| { | ||
| ["TargetDotNetVersion"] = requiredVersion, | ||
| ["ProjectFile"] = projectFilePath | ||
| }) | ||
| { | ||
| } | ||
| } |
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
81 changes: 81 additions & 0 deletions
81
src/Microsoft.Agents.A365.DevTools.Cli/Services/Helpers/DotNetProjectHelper.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,81 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Microsoft.Agents.A365.DevTools.Cli.Services.Helpers; | ||
|
|
||
| /// <summary> | ||
| /// Helper class for inspecting .NET project metadata. | ||
| /// </summary> | ||
| public static class DotNetProjectHelper | ||
| { | ||
| /// <summary> | ||
| /// Detects the target .NET runtime version (e.g. "8.0", "9.0") from a .csproj file. | ||
| /// Supports both TargetFramework and TargetFrameworks. | ||
| /// </summary> | ||
mengyimicro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <param name="projectFilePath">The full path to the .csproj file</param> | ||
| /// <param name="logger">Logger for diagnostic messages</param> | ||
| /// <returns> | ||
| /// The detected .NET version (e.g., "8.0", "9.0"), or null if: | ||
| /// - The file doesn't exist | ||
| /// - No TargetFramework element is found | ||
| /// - The TFM format is not recognized (only supports "netX.Y" format) | ||
| /// When multiple TFMs are specified, returns the first one. | ||
| /// It does NOT support legacy or library-only TFMs and Unsupported TFMs return null and fall back to default runtime selection. | ||
| /// </returns> | ||
| public static string? DetectTargetRuntimeVersion(string projectFilePath, ILogger logger) | ||
| { | ||
| if (!File.Exists(projectFilePath)) | ||
| { | ||
| logger.LogWarning("Project file not found: {Path}", projectFilePath); | ||
| return null; | ||
| } | ||
|
|
||
| var content = File.ReadAllText(projectFilePath); | ||
|
|
||
| // Match <TargetFramework> or <TargetFrameworks> | ||
| var tfmMatch = Regex.Match( | ||
| content, | ||
| @"<TargetFrameworks?>\s*([^<]+)\s*</TargetFrameworks?>", | ||
| RegexOptions.IgnoreCase); | ||
mengyimicro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!tfmMatch.Success) | ||
| { | ||
| logger.LogWarning("No TargetFramework(s) found in project file: {Path}", projectFilePath); | ||
| return null; | ||
| } | ||
|
|
||
| // If multiple TFMs are specified, pick the first one | ||
| // (future improvement: pick highest) | ||
| var tfms = tfmMatch.Groups[1].Value | ||
| .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
|
|
||
| var tfm = tfms.FirstOrDefault(); | ||
| if (string.IsNullOrWhiteSpace(tfm)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Match net8.0, net9.0, net9.0-windows, etc. | ||
| var verMatch = Regex.Match( | ||
| tfm, | ||
| @"net(\d+)\.(\d+)", | ||
mengyimicro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RegexOptions.IgnoreCase); | ||
|
|
||
| if (!verMatch.Success) | ||
| { | ||
| logger.LogWarning("Unrecognized TargetFramework format: {Tfm}", tfm); | ||
| return null; | ||
| } | ||
|
|
||
| var version = $"{verMatch.Groups[1].Value}.{verMatch.Groups[2].Value}"; | ||
| logger.LogInformation( | ||
| "Detected TargetFramework: {Tfm} → .NET {Version}", | ||
| tfm, | ||
| version); | ||
|
|
||
| return version; | ||
| } | ||
| } | ||
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.