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

Platform names correctness analyzer fails for OSPlatform attributes added in AssemblyInfo.cs file #49323

Closed
buyaa-n opened this issue Mar 8, 2021 · 14 comments · Fixed by #50193

Comments

@buyaa-n
Copy link
Member

buyaa-n commented Mar 8, 2021

Related to #45851

Recently we added new analyzer which checks platform name/version correctness for SupportedOSPlatform and UnsupportedOSPlatform attributes usage, testing the analyzer with runtime repo produced warnings for generated files XYZ.AssemblyInfo.cs, see logs below

D:\dotnet\runtime\artifacts\obj\Microsoft.Win32.Primitives\net6.0-Unix-Debug\Microsoft.Win32.Primitives.AssemblyInfo.cs(33,12): error CA1418: The platform 'Unix' is not a known platform name [D:\dotnet\runtime\src\libraries\Microsoft.Win32.Primitives\src\Microsoft.Win32.Primitives.csproj]
D:\dotnet\runtime\artifacts\obj\Microsoft.Win32.Primitives\net6.0-Browser-Debug\Microsoft.Win32.Primitives.AssemblyInfo.cs(33,12): error CA1418: Version '1.0' is not valid for platform 'Browser'. Do not use versions for this platform. [D:\dotnet\runtime\src\libraries\Microsoft.Win32.Primitives\src\Microsoft.Win32.Primitives.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.FileSystem.Watcher\net6.0-Linux-Debug\System.IO.FileSystem.Watcher.AssemblyInfo.cs(35,12): error CA1418: Version '1.0' is not valid for platform 'Linux'. Do not use versions for this platform. [D:\dotnet\runtime\src\libraries\System.IO.FileSystem.Watcher\src\System.IO.FileSystem.Watcher.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.FileSystem.Watcher\net6.0-OSX-Debug\System.IO.FileSystem.Watcher.AssemblyInfo.cs(35,12): error CA1418: The platform 'OSX' is not a known platform name [D:\dotnet\runtime\src\libraries\System.IO.FileSystem.Watcher\src\System.IO.FileSystem.Watcher.csproj]
D:\dotnet\runtime\artifacts\obj\System.Net.NetworkInformation\net6.0-illumos-Debug\System.Net.NetworkInformation.AssemblyInfo.cs(35,12): error CA1418: The platform 'illumos' is not a known platform name [D:\dotnet\runtime\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.Ports\netstandard2.0-windows-Debug\System.IO.Ports.AssemblyInfo.cs(13,12): error CA1418: The platform 'browser' is not a known platform name [D:\dotnet\runtime\src\libraries\System.IO.Ports\src\System.IO.Ports.csproj]

It is basically because the SDK adding the attribute for the target platform into XYZ.AssemblyInfo.cs file, and that platform is not exist in the MSBuild supported platforms list or not expected to have a version part.

I can fix it easily by updating the analyzer to not analyze a generated files, but not sure if we want to do any other way of fix instead as i assume it is only runtime specific issue

CC @jeffhandley @terrajobst @safern @ViktorHofer @Anipik

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 8, 2021
@ghost
Copy link

ghost commented Mar 8, 2021

Tagging subscribers to this area: @safern, @ViktorHofer, @Anipik
See info in area-owners.md if you want to be subscribed.

Issue Details

Related to #45851

Recently we added new analyzer which checks platform name/version correctness for SupportedOSPlatform and UnsupportedOSPlatform attributes usage, testing the analyzer with runtime repo produced warnings for generated files XYZ.AssemblyInfo.cs, see logs below

D:\dotnet\runtime\artifacts\obj\Microsoft.Win32.Primitives\net6.0-Unix-Debug\Microsoft.Win32.Primitives.AssemblyInfo.cs(33,12): error CA1418: The platform 'Unix' is not a known platform name [D:\dotnet\runtime\src\libraries\Microsoft.Win32.Primitives\src\Microsoft.Win32.Primitives.csproj]
D:\dotnet\runtime\artifacts\obj\Microsoft.Win32.Primitives\net6.0-Browser-Debug\Microsoft.Win32.Primitives.AssemblyInfo.cs(33,12): error CA1418: Version '1.0' is not valid for platform 'Browser'. Do not use versions for this platform. [D:\dotnet\runtime\src\libraries\Microsoft.Win32.Primitives\src\Microsoft.Win32.Primitives.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.FileSystem.Watcher\net6.0-Linux-Debug\System.IO.FileSystem.Watcher.AssemblyInfo.cs(35,12): error CA1418: Version '1.0' is not valid for platform 'Linux'. Do not use versions for this platform. [D:\dotnet\runtime\src\libraries\System.IO.FileSystem.Watcher\src\System.IO.FileSystem.Watcher.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.FileSystem.Watcher\net6.0-OSX-Debug\System.IO.FileSystem.Watcher.AssemblyInfo.cs(35,12): error CA1418: The platform 'OSX' is not a known platform name [D:\dotnet\runtime\src\libraries\System.IO.FileSystem.Watcher\src\System.IO.FileSystem.Watcher.csproj]
D:\dotnet\runtime\artifacts\obj\System.Net.NetworkInformation\net6.0-illumos-Debug\System.Net.NetworkInformation.AssemblyInfo.cs(35,12): error CA1418: The platform 'illumos' is not a known platform name [D:\dotnet\runtime\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation.csproj]
D:\dotnet\runtime\artifacts\obj\System.IO.Ports\netstandard2.0-windows-Debug\System.IO.Ports.AssemblyInfo.cs(13,12): error CA1418: The platform 'browser' is not a known platform name [D:\dotnet\runtime\src\libraries\System.IO.Ports\src\System.IO.Ports.csproj]

It is basically because the SDK adding the attribute for the target platform into XYZ.AssemblyInfo.cs file, and that platform is not exist in the MSBuild supported platforms list or not expected to have a version part.

I can fix it easily by updating the analyzer to not analyze a generated files, but not sure if we want to do any other fix instead as i assume it is only runtime specific issue

CC @jeffhandley @terrajobst @safern @ViktorHofer @Anipik

Author: buyaa-n
Assignees: -
Labels:

area-Infrastructure-libraries, untriaged

Milestone: -

@jeffhandley
Copy link
Member

It is basically because the SDK adding the attribute for the target platform into XYZ.AssemblyInfo.cs file, and that platform is not exist in the MSBuild supported platforms list or not expected to have a version part.

I can fix it easily by updating the analyzer to not analyze a generated files, but not sure if we want to do any other way of fix instead as i assume it is only runtime specific issue

Instead of ignoring generating files, I'd prefer to ensure our build (or the SDK) injects the correct assembly-level attributes, that we use the <SupportedPlatforms> (or is it <SupportedPlatform>? I can't ever remember) item where appropriate, or that we fix the analyzer logic.

  • The platform 'Unix' is not a known platform name
    • We didn't create an OperatingSystem.IsUnix() check -- I don't know what the right thing is for this one
  • Version '1.0' is not valid for platform 'Browser'. Do not use versions for this platform.
    • What is leading to us producing "Browser1.0"?
  • Version '1.0' is not valid for platform 'Linux'. Do not use versions for this platform.
    • What is leading to us producing "Linux1.0"?
  • The platform 'OSX' is not a known platform name
    • OSX is an alias for macos. We should consider having the analyzer recognize this alias.
  • The platform 'illumos' is not a known platform name
    • What do you recommend for this one, @buyaa-n?
  • The platform 'browser' is not a known platform name
    • This should be recognized, right? Is CA1418 accidentally case-sensitive?

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 9, 2021

  • The platform 'Unix' is not a known platform name
    • We didn't create an OperatingSystem.IsUnix() check -- I don't know what the right thing is for this one

Yeah, but we have Unix target, I don't have any solution except disabling the analyzer for generated files

  • Version '1.0' is not valid for platform 'Browser'. Do not use versions for this platform.
    • What is leading to us producing "Browser1.0"?
  • Version '1.0' is not valid for platform 'Linux'. Do not use versions for this platform.
    • What is leading to us producing "Linux1.0"?

We are defaulting the version to 1.0:

<PropertyGroup Condition="'$(TargetFrameworkSuffix)' != '' and '$(TargetFrameworkSuffix)' != 'windows'">
<TargetPlatformIdentifier>$(TargetFrameworkSuffix)</TargetPlatformIdentifier>
<TargetPlatformVersion>1.0</TargetPlatformVersion>

  • The platform 'OSX' is not a known platform name
    • OSX is an alias for macos. We should consider having the analyzer recognize this alias.

That is a good point, i forgot about that

  • The platform 'illumos' is not a known platform name
    • What do you recommend for this one, @buyaa-n?

Nothing except disabling it for generated files 😛

  • The platform 'browser' is not a known platform name
    • This should be recognized, right? Is CA1418 accidentally case-sensitive?

No, that is found in netstandard2.0-windows;netstandard2.0-Linux;netstandard2.0-OSX;netstandard2.0;net461-windows;netstandard2.0-FreeBSD targeted builds for which we are adding assembly level UnsupportedOSPlatform("browser") attribute, not sure if we want to exclude adding UnsupportedOSPlatform("browser") assembly attribute for lower versions. Found 22 more

Overall there is 92 warnings, here i only showed non repeated scenarios.

Forgot to add one more interesting scenario which is found on src builds (not in generated files):

D:\dotnet\runtime\src\libraries\System.Private.CoreLib\src\System\Diagnostics\Tracing\CounterGroup.cs(18,6): error CA1418: The platform 'browser' is not a known platform name [D:\dotnet\runtime\src\libraries\Microsoft.Diagnostics.Tracing.EventSource.Redist\src\Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj]
D:\dotnet\runtime\src\libraries\System.Private.CoreLib\src\System\Diagnostics\Tracing\DiagnosticCounter.cs(23,6): error CA1418: The platform 'browser' is not a known platform name [D:\dotnet\runtime\src\libraries\Microsoft.Diagnostics.Tracing.EventSource.Redist\src\Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj]
D:\dotnet\runtime\src\libraries\System.Private.CoreLib\src\System\Diagnostics\Tracing\EventCounter.cs(27,6): error CA1418: The platform 'browser' is not a known platform name [D:\dotnet\runtime\src\libraries\Microsoft.Diagnostics.Tracing.EventSource.Redist\src\Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj]

These are all warnings found in src, but somehow suppressed after disabling the analyzer for generated files even it is not generated.

@jeffhandley
Copy link
Member

Yeah, but we have Unix target, I don't have any solution except disabling the analyzer for generated files

If Unix is a valid target for us, couldn't we use the <SupportedPlatform> item to get CA1418 to recognize it as valid? Is this the type of scenario that we added that extensibility hook for?

We are defaulting the version to 1.0:

Ah, I see; thanks. I recommend we add a condition to <TargetPlatformVersion> to not specify that for Browser or Linux then.

The platform 'illumos' is not a known platform name

Nothing except disabling it for generated files

Disabling it for generated files seems like a drastic step if the platform is valid for us. Maybe we could use <SupportedPlatform> to indicate illumos is also a supported platform in our build?

  • The platform 'browser' is not a known platform name

    • This should be recognized, right? Is CA1418 accidentally case-sensitive?

No, that is found in netstandard2.0-windows;netstandard2.0-Linux;netstandard2.0-OSX;netstandard2.0;net461-windows;netstandard2.0-FreeBSD targeted builds for which we are adding assembly level UnsupportedOSPlatform("browser") attribute, not sure if we want to exclude adding UnsupportedOSPlatform("browser") assembly attribute for lower versions.

Oh I think I understand. Is this happening because for the down-level targets (netstandard2.0 and net461), we try looking into the OperatingSystem type and we don't see the IsBrowser() method because it wasn't added until 5.0?

If that's the case, then I think the 2 options would be:

  1. Don't include the [UnsupportedOSPlatform("browser")] attribute on lower versions, or
  2. Use the <SupportedPlatform> hook to ensure Browser is in the list, even on lower versions

Forgot to add one more interesting scenario which is found on src builds (not in generated files):

I don't think we should disable the analyzer on generated files as a result of these warnings. These so far all look like legitimate scenarios that we should have the analyzer checking for us to ensure we have our loose ends tied up on our build.

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 9, 2021

If Unix is a valid target for us, couldn't we use the item to get CA1418 to recognize it as valid? Is this the type of scenario that we added that extensibility hook for?

Yes, I was thinking to use that if I see a warning in src, didn't felt it worth for generated files 😛, but of course we can add that

Ah, I see; thanks. I recommend we add a condition to to not specify that for Browser or Linux then.

The versions might added on purpose cc @Anipik, I am wondering if we could use version 0.0 instead

Oh I think I understand. Is this happening because for the down-level targets (netstandard2.0 and net461), we try looking into the OperatingSystem type and we don't see the IsBrowser() method because it wasn't added until 5.0?

Right

If that's the case, then I think the 2 options would be:

Don't include the [UnsupportedOSPlatform("browser")] attribute on lower versions, or
Use the hook to ensure Browser is in the list, even on lower versions

That assembly doesn't have any 5.0 or higher target though, so i am guess we still wanted the attribute for lower versions, if that is the case i will try the hook

I don't think we should disable the analyzer on generated files as a result of these warnings. These so far all look like legitimate scenarios that we should have the analyzer checking for us to ensure we have our loose ends tied up on our build.

Copy that, but for those src files we might could not add the <SupportedPlatform>, because those are in CoreLib and adding <SupportedPlatform Include="browser"> will reveal many many warnings, which i don't think were valid, from what i saw i think there is no browser target and no clear separation of non browser APIs in CoreLib.

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 12, 2021

I was not able to handle the warnings related to versions:

Version '1.0' is not valid for platform 'Browser'. Do not use versions for this platform.
Version '1.0' is not valid for platform 'Linux'. Do not use versions for this platform.

As i mentioned before we are defaulting the version to 1.0:

<PropertyGroup Condition="'$(TargetFrameworkSuffix)' != '' and '$(TargetFrameworkSuffix)' != 'windows'">
<TargetPlatformIdentifier>$(TargetFrameworkSuffix)</TargetPlatformIdentifier>
<TargetPlatformVersion>1.0</TargetPlatformVersion>
<TargetPlatformMoniker>$(TargetFrameworkSuffix),Version=$(TargetPlatformVersion)</TargetPlatformMoniker>

I have tried to set it to version 0.0, got build error:

error NU1012: Platform version is not present for one or more target frameworks, even though they have specified a platform: net6.0-browser, net6.0-android, net6.0-ios, net6.0-tvos [D:\dotnet\runtime\Build.proj]

Tried to not set version at all for browser or Linux build, got same error:

 error NU1012: Platform version is not present for one or more target frameworks, even though they have specified a platform: net6.0-browser [D:\dotnet\runtime\Build.proj]

Seems MSBuild requires a version with non 0.0 value

@safern
Copy link
Member

safern commented Mar 12, 2021

Oh I think I understand. Is this happening because for the down-level targets (netstandard2.0 and net461), we try looking into the OperatingSystem type and we don't see the IsBrowser() method because it wasn't added until 5.0?

Right

I guess these warnings have a unique diagnostic ID. Can we disable them in dotnet/runtime for downlevel targets where the IsBrowser() or other APIs are not available? That is what we did for nullable annotations given that the down level tfms didn't have enough nullable annotations for the flow analysis to be simple to annotate in those tfms. I don't know if this could have an impact on the customers though.

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 12, 2021

Yes we can do that, or as @jeffhandley mentioned:

  1. Don't include the [UnsupportedOSPlatform("browser")] attribute on lower versions, or
  2. Use the hook to ensure Browser is in the list, even on lower versions

the most problematic one right now is the versions added for Browser and Linux targets, where they are not expected to have versions and the analyzer not allow versions for those platforms

@safern
Copy link
Member

safern commented Mar 12, 2021

the most problematic one right now is the versions added for Browser and Linux targets, where they are not expected to have versions and the analyzer not allow versions for those platforms

I see. Have you tried conditioning our setting of TargetPlatformVersion to when TargetFrameworkSuffix != Browser and != Linux?

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 12, 2021

Yes, i have tried with a condition to not add versions conditionally, build error because it is used in the below statement
<TargetPlatformMoniker>$(TargetFrameworkSuffix),Version=$(TargetPlatformVersion)</TargetPlatformMoniker>

Also tried conditionally set the entire TFM logic, including not set <TargetPlatformVersionSupported>true</TargetPlatformVersionSupported> and all other version related logic when it is browser or Linux got build error:

error NU1012: Platform version is not present for one or more target frameworks, even though they have specified a platform: net6.0-browser [D:\dotnet\runtime\Build.proj]

Seems MSBuild not allowing version less TFM at all

@ViktorHofer
Copy link
Member

Seems MSBuild not allowing version less TFM at all

This is necessary for our custom RIDs to work. @Anipik can share more details.

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 15, 2021

Got it thanks, we resolved the issue using SupportedOSPlatformVersion property, when this version set to 0.0 the MSBuild generating an unversioned SupportedOSPlatform attribute for the assembly.

@buyaa-n buyaa-n removed the untriaged New issue has not been triaged by the area owner label Mar 18, 2021
@ViktorHofer ViktorHofer added this to the 6.0.0 milestone Mar 24, 2021
@ViktorHofer
Copy link
Member

@buyaa-n what's left to do here? Do we have a subscription that makes sure that the latest analyzer package is always used in dotnet/runtime?

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Mar 24, 2021
@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 24, 2021

@buyaa-n what's left to do here?

@ViktorHofer I have resolved the warning and was waiting for analyzer updates built for dogfood, now that is available so raised a PR, please review

Do we have a subscription that makes sure that the latest analyzer package is always used in dotnet/runtime?

I don't think so, as far as i know we update it manually, but i think that is better

@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 31, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants