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 #7361

Closed drauch closed 1 year 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

drauch commented 1 year ago

Is there something wrong with this issue?

It does now exist 4 times:

7307

7323

7347

7361

In all four instances, it shows:

This issue is being transferred. Timeline may not be complete until it finishes.

What is going on here? :-)

Best regards, D.R.

davidwengier commented 1 year ago

@drauch The transfer hit a bug in GitHub, which resulted in duplicates, which were manually closed. https://github.com/dotnet/razor/issues/7286 is the open issue tracking this, which has all of the history and comments correctly transferred. You can ignore the closed issues :)