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

Trim leading v from TargetFrameworkVersion #7544

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ private static bool PlatformAnalysisAllowed(AnalyzerOptions options, Compilation
string tfmVersion = options.GetMSBuildPropertyValue(MSBuildPropertyOptionNames.TargetFrameworkVersion, compilation) ?? "";

if (tfmIdentifier.Equals(NetCoreAppIdentifier, StringComparison.OrdinalIgnoreCase) &&
tfmVersion.StartsWith("v", StringComparison.OrdinalIgnoreCase) &&
Version.TryParse(tfmVersion[1..], out var version) &&
Version.TryParse(tfmVersion.TrimStart(['v', 'V']), out var version) &&
Copy link
Member Author

@tannergooding tannergooding Jan 23, 2025

Choose a reason for hiding this comment

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

An interesting "quirk" of this is that technically vvvvvvvv5.0 is also valid (and the SDK appears to "support" it as well since they also just TrimStart("vV")); but I don't think that's worth adding an explicit test around 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Worth noting that the reason this is TrimStart(['v', 'V']) and not TrimStart("vV") is because analyzers are .NET Standard 2.0 and the latter API returns a ROSpan<char>.

Since we're .NET Standard 2.0, there is no Version.TryParse(ROSpan<char>, out Version) API and we have to convert back to string anways. So I went with what I thought was the shorter/simpler option here.

version.Major >= 5)
{
// We want to only support cases we know are well-formed by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static IEnumerable<object[]> Create_DifferentTfms()

// Custom TFMs with valid user specified Identifier/Version
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=v11.0", true };
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=V11.0", true };
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=11.0", true };

// Custom TFMs with invalid user specified Identifier/Version
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=v5", false };
Expand Down
Loading