Skip to content

Fix warnings caused from build attributes for TFM target platforms added by SDK  #44231

@buyaa-n

Description

@buyaa-n

Related to the PR Previously we were stripping the target platforms during build of our csproj. Net5.0 sdk supports the targetPlatformMoniker string for net5.0 and above and hence we no longer need to strip it off for newer frameworks.

With the above change for the builds targeting a specific platform MSBuild will be adding assembly-level SupportedOSPlatform("platform") which is causing 2 types of CA1416 failures along shared files having some platform-specific annotations, see the failures here

  1. Warning caused from platform attributes inconsistency for APIs having annotations different than the target platform. When a project has some APIs annotated with SupportedOSPlatform("windows") but also has a Linux target: net5.0-Linux MSBuild would produce assembly level SupportedOSPlatform("Linux") and simple repro looks like:
    [assembly:SupportedOSPlatform("Linux")]  // MSBuild would produces assembly level attribute 
    public class TestClass
    {
        [SupportedOSPlatform("windows")] // This annotation ignored as child API couldn't extend parend
        public TestApi() 
        {
            Console.Beep(10, 20); // call for windows only API, warns here because the method annotation ignored from inconsistency
        }
    }
  2. When shared file has [UnsupportedOSPlatform("windows")] attribute for some of the APIs build fails for browser target (net5.0-Browser) because of [UnsupportedOSPlatform("windows")] APIs accessing some fields within the shared file. Example:
    [assembly:SupportedOSPlatform("Browser")]  // MSBuild would produces assembly-level attribute 
    public static class Console
    {
        ...
        private static readonly object s_syncObject = new object();
        private static TextReader? s_in;
        ...
    
        [UnsupportedOSPlatform("browser")]
        public static Encoding InputEncoding
        {
            get
            {
                Encoding? encoding = Volatile.Read(ref s_inputEncoding); // Warns: 'Console.s_inputEncoding' is supported on 'Browser'
                if (encoding == null)
                {
                    lock (s_syncObject) // Warns: 'Console.s_syncObject' is supported on 'Browser'
                    {
                        if (s_inputEncoding == null)
                        {
                            Volatile.Write(ref s_inputEncoding, ConsolePal.InputEncoding);
                        }
                        encoding = s_inputEncoding;
                    }
                }
                return encoding;
            }
            ...
        }

@Anipik added a rule to remove specified assembly level attributes. #44257
cc @jeffhandley @terrajobst

Metadata

Metadata

Labels

Cost:SWork that requires one engineer up to 1 weekPriority:0Work that we can't release withoutarea-Infrastructure-librariescode-analyzerMarks an issue that suggests a Roslyn analyzer

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions