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

.NET 10: PlatformCompatibilityAnalyzer not working #7525

Open
jonathanpeppers opened this issue Jan 9, 2025 · 2 comments
Open

.NET 10: PlatformCompatibilityAnalyzer not working #7525

jonathanpeppers opened this issue Jan 9, 2025 · 2 comments

Comments

@jonathanpeppers
Copy link
Member

Analyzer

Diagnostic ID: CA1416: Validate platform compatibility

Analyzer source

Describe the bug

For Android, we have an API such as:

[SupportedOSPlatform("android22.0")]
public virtual int AccessibilityTraversalAfter { get; set; }

Using the API with SupportedOSPlatformVersion=21.0 does not result in a CA1416 warning.

We first noticed this here, getting the Android workload on a .NET 10 SDK:

In the following integration test:

Steps To Reproduce

  • dotnet new android
  • Use button.AccessibilityTraversalAfter
  • Observe no warnings

Here is a .binlog if that is helpful: supportedosplatformversion.zip

Expected behavior

Using the API with SupportedOSPlatformVersion=21.0 results in a CA1416 warning.

Actual behavior

Using the API with SupportedOSPlatformVersion=21.0 does not result in a CA1416 warning.

Additional context

I'm guessing the code here would not work as expected for TargetFramework=net10.0, as it has 1 more digit:

var tfmString = options.GetMSBuildPropertyValue(MSBuildPropertyOptionNames.TargetFramework, compilation);
if (tfmString?.Length >= 4 &&
tfmString.StartsWith(Net, StringComparison.OrdinalIgnoreCase) &&
int.TryParse(tfmString[3].ToString(), out var major) &&
major >= 5)

@ViktorHofer
Copy link
Member

ViktorHofer commented Jan 9, 2025

Yes the code that you shared looks faulty. Just wanted to add that a component should never parse and interpret the TargetFramework property. Instead, the deferred properties (i.e. TFI and TFV) should be read.

Why? Because with the eventually coming TFM aliasing feature, this analyzer will stop working:

<PropertyGroup>
  <TargetFrameworks>vs17.12;vs17.13</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
  <TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
  <TargetFrameworkVersion>10.0</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
  <RoslynAnalyzerVersion Condition="'$(TargetFramework)' == 'vs17.12'">4.12</RoslynAnalyzerVersion>
  <RoslynAnalyzerVersion Condition="'$(TargetFramework)' == 'vs17.13'">4.13</RoslynAnalyzerVersion>
</PropertyGroup>

@ericstj
Copy link
Member

ericstj commented Jan 9, 2025

cc @jeffhandley @dotnet/dotnet-analyzers -- this is going to be a blocking issue for .NET 10.0. We should get it fixed ASAP.

I think the fix should be to pass in the parsed values for TargetFrameworkIdentifier and TargetFrameworkVersion as @ViktorHofer suggests and use those instead (rather than taking a NuGet dependency to parse the framework value). Looks to me like that might mean adding to

internal static class MSBuildPropertyOptionNames
{
public const string TargetFramework = nameof(TargetFramework);
public const string TargetPlatformMinVersion = nameof(TargetPlatformMinVersion);
public const string UsingMicrosoftNETSdkWeb = nameof(UsingMicrosoftNETSdkWeb);
public const string ProjectTypeGuids = nameof(ProjectTypeGuids);
public const string InvariantGlobalization = nameof(InvariantGlobalization);
public const string PlatformNeutralAssembly = nameof(PlatformNeutralAssembly);
public const string EnforceExtendedAnalyzerRules = nameof(EnforceExtendedAnalyzerRules);
}

jonpryor pushed a commit to dotnet/android that referenced this issue Jan 10, 2025
Changes: dotnet/sdk@5b9d9d4...a93a592

	% git diff --shortstat 5b9d9d4677...a93a592ce9
	 2336 files changed, 57809 insertions(+), 28665 deletions(-)

Changes: dotnet/runtime@226c034...4b02c51

	% git diff --shortstat 226c0347b9...4b02c51f71
	 6343 files changed, 214693 insertions(+), 177501 deletions(-)

Changes: dotnet/emsdk@8e660ff...953fd74

	% git diff --shortstat 8e660ff41e...953fd74cd2
	 44 files changed, 844 insertions(+), 521 deletions(-)

Changes: dotnet/cecil@9c94433...9e8bd52

	% git diff --shortstat 9c9443396f8...9e8bd520939
	 20 files changed, 317 insertions(+), 205 deletions(-)

Changes: a8cd27e...4b20432

	% git diff --shortstat a8cd27e...4b20432
	 1522 files changed, 302553 insertions(+), 40811 deletions(-)

Context: dotnet/msbuild#11237
Context: dotnet/roslyn-analyzers#7525
Context: dotnet/maui#27040

Bump to:

  * .NET SDK 10.0.100-alpha.1.25056.1
  * .NET Runtime 10.0.0-alpha.1.25056.1
  * .NET Android 35.0.24

Note that the `Xamarin.Android-PR (MAUI Tests MAUI Integration)`
lane will fail with:

> Workload installation failed: Could not find workload 'microsoft-net-runtime-android-net9'
> extended by workload 'android' in manifest 'microsoft.net.sdk.android'

until we get dotnet/maui on .NET 10 (see e.g. dotnet/maui#27040).

Regressions discovered:

  * MSBuild: dotnet/msbuild#11237
  * Roslyn analyzers: dotnet/roslyn-analyzers#7525

I temporarily disabled tests that hit these issues, with a code
comment to restore them in the future.

Other changes:

  * Add the `dotnet10` NuGet feeds.

  * `$(DotNetTargetFramework)` is `net10.0`

  * Update project templates to say `net10.0-android`.

  * Update `WorkloadManifest.json` to support .NET 10 and able to
    build `net9.0-android` projects.

  * Replace the contents of `AutoImports.props`, so we shouldn't need
    to modify this file each year.

  * Various tests should target `net9.0-android` for the "previous"
    .NET Android version and `net10.0-android` for the current.

  * `workload-dependencies.csproj` needs `$(RollForward)=Major` to
    allow it to run on a .NET 10 runtime.

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Peppers <[email protected]>
Co-authored-by: Jonathan Pobst <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants