tunnelvisionlabs / ReferenceAssemblyAnnotator

IL weaver to add nullability annotations to .NET reference assemblies
MIT License
72 stars 8 forks source link

Nullability attributes are not included in WPF temp projects which are part of the WPF build process #81

Closed jnm2 closed 4 years ago

jnm2 commented 4 years ago

Usage of any nullability attributes in a WPF project result in this:

CS0122 'NotNullWhenAttribute' is inaccessible due to its protection level

The error is reported as coming from a project named YourProjectName_*_wpftmp.csproj.

The reason is that the NullableAttributes.cs file is not included in such a way that this WPF build task can see it:

https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFx.targets#L401 https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/GenerateTemporaryTargetAssembly.cs#L90

Repro:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[3.1.0]" />
    <PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.154" PrivateAssets="all" />
  </ItemGroup>
</Project>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        _ = typeof(NotNullWhenAttribute);
        this.InitializeComponent();
    }
}
chucker commented 4 years ago

Would it be possible to optionally expose the attributes as public? Can we perhaps take advantage of InternalsVisibleToAttribute?

jnm2 commented 4 years ago

@chucker Public is dangerous. Rather than adding a new option that is a workaround, I think it would be better for WPF projects for now to opt out with <GenerateNullableAttributes>false</GenerateNullableAttributes> and manually add a source file with public attributes. Especially since the WPF team is considering fixing this problem: https://github.com/dotnet/wpf/issues/810#issuecomment-640919316

jnm2 commented 4 years ago

I figured out how to fix it.