dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

setting GenerateAssemblyInfo to false [csproj] disables EnablePreviewFeatures with no warning [analyser?] #24700

Open ChrisBertrandIndus opened 2 years ago

ChrisBertrandIndus commented 2 years ago

Issue Description

When, in the csproj file, GenerateAssemblyInfo is false and EnablePreviewFeatures is true, the compiler/analyser does not warn that EnablePreviewFeatures is automatically ignored due to GenerateAssemblyInfo is false. A developer can struggle some time before finding the reason.

I first reported this to roslyn-analyzers, and buyaa-n suggested MSBuild might be in charge of such warning.

Steps to Reproduce

  1. Create a .NET 6 library.
  2. Create an interface in the project.
  3. Add a function to the interface : static abstract void A();
  4. Add <EnablePreviewFeatures>True</EnablePreviewFeatures> to the csproj.
  5. Compile. That should be OK.
  6. Add <GenerateAssemblyInfo>False</GenerateAssemblyInfo> to the csproj.
  7. Compilation fails with error CA2252.

Code:

    internal interface ITest
    {
        static abstract void A();
    }

ClassLibrary1 2022-04-04__09_01_23.zip

Expected Behavior

A warning explaining EnablePreviewFeatures is disabled when GenerateAssemblyInfo is false and what to do.

Actual Behavior

No warning at all.

Versions & Configurations

msbuild : 17.1.0.7609

analysers:

Visual Studio 17.1.2
C# 4.1.0-5.22165.10+e555772db77ca828b02b4bd547c318387f11d01f

.NET SDKs installed: 5.0.405 6.0.200 6.0.201

Also tried (with same error) NuGet package : Microsoft.CodeAnalysis.NetAnalyzers Version=6.0.0

rainersigwald commented 2 years ago

EnablePreviewFeatures is a .NET SDK concept, so moving this to dotnet/sdk. I agree that a warning would be nice here.

buyaa-n commented 2 years ago

Same issue happens for Platform Compatibility Analyzer, when a project targets a specific platform (Windows for example) assembly level SupportedOSPlatform("windows") attribute should be added to AssmeblyInfo.cs, but could not if GenerateAssemblyiInfo set to false. There has been a discussion to warn about it in the PC Analyzer but it is not issue with the analyzer. Also there is an advanced scenarios like Building a library that offers preview features where a library developer might not wanted/needed to have an attribute for entire assembly, instead to have the attribute only for APIs that requires it.

MagicAndre1981 commented 11 months ago

In a project with only .netx.0-windows target framework the warning CA1416 is confusing.

What is added via GenerateAssemblyInfo? I need to set it to false as I use Unclassified.NetRevisionTask to set version base on code revision which sometime caused duplicate AssemblyInfo data, so I turned GenerateAssemblyInfo off.

buyaa-n commented 11 months ago

What is added via GenerateAssemblyInfo?

As you know when GenerateAssemblyInfo is enabled MSBuild adds AssemblyInfo.cs file, with required assembly attributes like [assembly: System.Runtime.Versioning.SupportedOSPlatform("windows")] when the project targeting windows. This attribute which turns off the CA1416 when calling windows only APIs. If you are using custom AssemblyInfo.cs you can add the assembly level SupportedOSPlatform attribute with the target platform anywhere in your project

MagicAndre1981 commented 8 months ago

What is added via GenerateAssemblyInfo?

If you are using custom AssemblyInfo.cs you can add the assembly level SupportedOSPlatform attribute with the target platform anywhere in your project

this is what I did now. I created a new project targeting netX.0-windows and copied the generated SupportedOS and TargetPlatform entries to my custom AssemblyInfo.cs.