dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
498 stars 191 forks source link

CS8669 in Razor generated code #7286

Closed drauch closed 6 months ago

drauch commented 2 years ago

Is there an existing issue for this?

Describe the bug

When switching to .NET 6 ReSharper suggested that some of my null checks in Razor files are not necessary because my Model is never null. That's not true though, which is why I changed the model declaration to a nullable one, e.g.:

@model string?

However, when I compile my solution with warnings as errors and use Razor runtime compilation I get the following errors when executing this Razor page:

tylng5v1.afl(32,109): error CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

I tried to circumvent this problem with the following idea:

@{
  #nullable enable
}
@model string?

However, it still doesn't work.

Any ideas on what I am doing wrong? I've found https://github.com/dotnet/aspnetcore/issues/37510 but it says everthing is fixed with 6.0.102 and I'm using an up-to-date VS 2022 with an SDK version of 6.0.201 (64bit).

Expected Behavior

Nullable models in Razor pages work without warnings.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0.201

Anything else?

Up-to-date ASP.NET Core 6.x Up-to-date VS 2022 Professional

mkArtakMSFT commented 2 years ago

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project (ideally a GitHub repo) that illustrates the problem.

ghost commented 2 years ago

Hi @drauch. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

drauch commented 2 years ago

@mkArtakMSFT : Here is a solution that demonstrates the problem:

https://www.dropbox.com/s/1iv79foz9luoa2r/ReproAspNetCore40866ModelNullabilityProblems.zip?dl=0

adams85 commented 2 years ago

I can confirm that the issue exists in the latest .NET release (6.0.3). I also ran into this when I was upgrading a project from .NET Core 3.1 to .NET 6.

It works fine when runtime compilation is off but I get the same error when I turn it on. In my project TreatWarningsAsErrors is enabled too. When I disable that, the compilation error goes away.

So it's clear that the issue is related to TreatWarningsAsErrors. After some digging in the sources, I found that the Razor compiler picks up this setting from the project.

I guess this warning is some recent addition to Roslyn, which blocks the runtime Razor compilation when TreatWarningsAsErrors is enabled as the Razor engine doesn't emit the explicit #nullable enable into the compilation output.

@drauch As a hotfix, you can disable TreatWarningsAsErrors. I didn't like this idea though, so I came up with another workaround. This approach emits a #pragma warning disable 8669 line in the compilation output and that solves the issue for me. (At first I tried #nullable enable but interestingly that didn't work...)

drauch commented 2 years ago

As far as I can see I do not have runtime compilation turned on in my sample above. Is it on by default?

adams85 commented 2 years ago

No, it's not on by default either in the dotnet new templates or in your repro project. Here it is how you enable it.

What's even more puzzling is that I tested the repro project you submitted and it works for me without errors, although I'm on SDK v6.0.201 too... I needed to make the following changes to produce the error: https://github.com/adams85/aspnetcore-40866-repro/commit/16e7371aa98abfb323fc403ab60d89154d786bfc

Edit: I'm on Windows 10 if that matters (probably not).

drauch commented 2 years ago

That's very weird. I'm also on Win10 for what it's worth. I think we will have to wait for officials...

alexdbkim commented 2 years ago

I've just hit this too. I'm with 6.0.540 with VS2022 latest. I couldn't figure out what code change I need to make. VS intellisense doesn't show me the suggested resolution.

alexdbkim commented 2 years ago

Never mind. I just removed nullable "?" on parameter and the warning went away... I guess Razor isn't as sensitive as the new .NET highlighting on nullable/non-nullable...

damianog commented 2 years ago

Same issue with my custom input Blazor component.

It happens only with string type

image

Value types no warnings

image

stap123 commented 1 year ago

We get this in .net 7 with @inherits InputComponentBase<DateTimeRange?> in a .razor file

LunicLynx commented 1 year ago

Just hit this one as well. Also related to <WarningsAsErrors>nullable</WarningsAsErrors>. Not possible to emit pragma statements before the component class is generated.

Only solution was to disable WarningsAsErrors.

sh-slfcu commented 9 months ago

Just hit this one as well. .net 8. Can't have nullable reference types inside Razor, while also having TreatWarningsAsErrors on. C'mon...

robledop commented 7 months ago

I've been having this problem since I migrated my Blazor project to .net 8. I get the errors on generated razor.g.cs files. Any updates?

StefanJanssen95 commented 7 months ago

Same issue here with dotnet 8.0.202. when trying to implement a generic interface in a razor component: @implements IDialogContent<bool, Student?>

Could this be put on the list for .net 9?

gran0123 commented 4 months ago

This is still an issue in dotnet 8.0.201 with <TreatWarningsAsErrors>true</TreatWarningsAsErrors> configured in Directory.Build.props and serviceCollection.AddControllersWithViews().AddRazorRuntimeCompilation();

void PrintKeyValue(string key, string? value)
{
    if (value is not { Length: > 0 })
    {
       return;
    }

   <li class="list-group-item">
      <strong>@key:</strong> @value
   </li>
}
jjonescz commented 4 months ago

@gran0123 to avoid a breaking change, the fix has been limited to Razor 9 which should be automatically enabled in .NET 9 or you can add <RazorLangVersion>9.0</RazorLangVersion> to you build props.

It likely won't work with AddRazorRuntimeCompilation though, that's a legacy system which is not updated with new features. Can you share why you are using that?

gran0123 commented 4 months ago

@jjonescz Yes, AddRazorRuntimeCompilation was a left over from a dotnet upgrade and will be removed now. Thanks for the answer.

0xced commented 3 months ago

On .NET 8, you can get rid of the CS8669 warning by using #pragma warning disable CS8669 instead of the more natural #nullable enable.

@{
#pragma warning disable CS8669
}

Trick found in https://github.com/dotnet/razor/issues/8720#issue-1713452475