Repository navigation
Closed
Milestone
Description
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
Activity
jeffhandley commentedon Mar 9, 2021
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.OperatingSystem.IsUnix()check -- I don't know what the right thing is for this one"Browser1.0"?"Linux1.0"?macos. We should consider having the analyzer recognize this alias.buyaa-n commentedon Mar 9, 2021
Yeah, but we have Unix target, I don't have any solution except disabling the analyzer for generated files
We are defaulting the version to 1.0:
runtime/eng/targetframeworksuffix.props
Lines 8 to 10 in 3eca9e1
That is a good point, i forgot about that
Nothing except disabling it for generated files 😛
No, that is found in
netstandard2.0-windows;netstandard2.0-Linux;netstandard2.0-OSX;netstandard2.0;net461-windows;netstandard2.0-FreeBSDtargeted builds for which we are adding assembly levelUnsupportedOSPlatform("browser")attribute, not sure if we want to exclude addingUnsupportedOSPlatform("browser")assembly attribute for lower versions. Found 22 moreOverall 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):
These are all warnings found in src, but somehow suppressed after disabling the analyzer for generated files even it is not generated.
jeffhandley commentedon Mar 9, 2021
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?Ah, I see; thanks. I recommend we add a condition to
<TargetPlatformVersion>to not specify that forBrowserorLinuxthen.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?Oh I think I understand. Is this happening because for the down-level targets (
netstandard2.0andnet461), we try looking into theOperatingSystemtype and we don't see theIsBrowser()method because it wasn't added until 5.0?If that's the case, then I think the 2 options would be:
[UnsupportedOSPlatform("browser")]attribute on lower versions, or<SupportedPlatform>hook to ensure Browser is in the list, even on lower versionsI 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 commentedon Mar 9, 2021
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
The versions might added on purpose cc @Anipik, I am wondering if we could use version 0.0 instead
Right
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
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 commentedon Mar 12, 2021
I was not able to handle the warnings related to versions:
As i mentioned before we are defaulting the version to 1.0:
runtime/eng/targetframeworksuffix.props
Lines 8 to 11 in 3eca9e1
I have tried to set it to version 0.0, got build error:
Tried to not set version at all for browser or Linux build, got same error:
Seems MSBuild requires a version with non
0.0valuesafern commentedon Mar 12, 2021
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 commentedon Mar 12, 2021
Yes we can do that, or as @jeffhandley mentioned:
the most problematic one right now is the versions added for
BrowserandLinuxtargets, where they are not expected to have versions and the analyzer not allow versions for those platformssafern commentedon Mar 12, 2021
I see. Have you tried conditioning our setting of
TargetPlatformVersionto whenTargetFrameworkSuffix!=Browserand !=Linux?buyaa-n commentedon 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:Seems MSBuild not allowing version less TFM at all
ViktorHofer commentedon Mar 15, 2021
This is necessary for our custom RIDs to work. @Anipik can share more details.
buyaa-n commentedon Mar 15, 2021
Got it thanks, we resolved the issue using
SupportedOSPlatformVersionproperty, when this version set to0.0the MSBuild generating an unversionedSupportedOSPlatformattribute for the assembly.ViktorHofer commentedon Mar 24, 2021
@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?
buyaa-n commentedon Mar 24, 2021
@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
I don't think so, as far as i know we update it manually, but i think that is better