levitali / CompiledBindings

MIT License
279 stars 14 forks source link

CompiledBindings for MAUI disables nullable checks #12

Closed Rybasum closed 2 years ago

Rybasum commented 2 years ago

In a MAUI project with nullable checks enabled, including the CompiledBindings.MAUI nuget seems to override this setting and disables the nullable checks. This results in tonnes of warnings, for every .cs file that uses the ? suffix (like string? myString). FYI, an example library project could be like this:

    <PropertyGroup>
        <TargetFrameworks>net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</TargetPlatformMinVersion>
        <AssemblyName>Gs.$(MSBuildProjectName)</AssemblyName>
        <RootNamespace>Gs.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
        <SingleProject>true</SingleProject>
        <UseMaui>true</UseMaui>
        <LangVersion>10.0</LangVersion>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="CompiledBindings.MAUI" Version="1.0.11" />
    </ItemGroup>

Thank you for creating this fantastic project, I hope this can be resolved easily!

levitali commented 2 years ago

Are you sure the warnings come only if CompiledBindings.MAUI nuget is included?

Maybe you receive this warning:

warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Than you should set

nullable enable

in the files where you use ? suffix.

You receive this warning, of cause, also without CompiledBindings.MAUI nuget

Otherwise, can you upload a project to reproduce the issue?

Rybasum commented 2 years ago

Yes, without including CompiledBindings.MAUI there are no warnings. Yes, the warnings I get are the ones you mentioned. I don't write #nullable enable at the top of every .cs file, because this option is set globally in the projects, with the <Nullable>enable</Nullable> line. It's hard to now go on now and add #nullable enable in every file, as the project has several hundreds of files.

I can't give you the whole solution (it has 23 projects), I'll try to create a separate small one to investigate this. Thanks for replying.

Rybasum commented 2 years ago

Steps to reproduce:

  1. Create a new MAUI project (I'm using VS 17.4.0 Preview 1.0)
  2. In the project's properties, Build section, set Nullable option to Enable
  3. In MainPage.xaml.cs add an empty class and add a nullable reference to that class in the MainPage class, like this:
    
    namespace BindingsTest;

class Test { }

public partial class MainPage : ContentPage { readonly Test? m_test; int count = 0;

public MainPage()
{
    InitializeComponent();
}

private void OnCounterClicked(object sender, EventArgs e)
{
          ...
}

}


Then build the solution. No warnings.
4. Add the CompiledBindings.MAUI nuget and build again.  There are several warnings warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

I hope this helps.
levitali commented 2 years ago

Thank you for reporting the issue! It will be fixed in the next version.

Rybasum commented 2 years ago

It works now (version 1.0.12), thank you for fixing it. Also, thanks for this amazing package!