Skip to content

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

Closed

Description

@buyaa-n

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

Activity

  1. ghost added
    untriaged
    on Mar 8, 2021
  2. jeffhandley commented on Mar 9, 2021

    @jeffhandley
    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?
  3. buyaa-n commented on Mar 9, 2021

    @buyaa-n
    ContributorAuthor
    • 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.

  4. jeffhandley commented on Mar 9, 2021

    @jeffhandley
    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.

  5. buyaa-n commented on Mar 9, 2021

    @buyaa-n
    ContributorAuthor

    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.

  6. buyaa-n commented on Mar 12, 2021

    @buyaa-n
    ContributorAuthor

    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

  7. safern commented on Mar 12, 2021

    @safern
    Member

    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.

  8. buyaa-n commented on Mar 12, 2021

    @buyaa-n
    ContributorAuthor

    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

  9. safern commented on Mar 12, 2021

    @safern
    Member

    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?

  10. buyaa-n commented on Mar 12, 2021

    @buyaa-n
    ContributorAuthor

    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

  11. ViktorHofer commented on Mar 15, 2021

    @ViktorHofer
    Member

    Seems MSBuild not allowing version less TFM at all

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

  12. buyaa-n commented on Mar 15, 2021

    @buyaa-n
    ContributorAuthor

    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.

  13. removed
    untriaged
    on Mar 18, 2021
  14. added this to the 6.0.0 milestone on Mar 24, 2021
  15. ViktorHofer commented on Mar 24, 2021

    @ViktorHofer
    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?

  16. ghost added
    in-pr
    on Mar 24, 2021
  17. buyaa-n commented on Mar 24, 2021

    @buyaa-n
    ContributorAuthor

    @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

  18. ghost removed
    in-pr
    on Mar 31, 2021
  19. ghost locked as resolved and limited conversation to collaborators on Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions