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
505 stars 195 forks source link

No platform compatability analyzer warnings in Razor @code sections #7250

Open lambdageek opened 2 years ago

lambdageek commented 2 years ago

Is there an existing issue for this?

Describe the bug

C# code in .razor files (in @code sections or elsewhere) should be analyzed by the platform compatibility analyzer in order to spot uses of APIs that are unsupported on the browser.

Currently, in a blazorwasm project, only .cs files (which may be partial classes for Razor components) are analyzed and the CA1416 warnings are produced.

Expected Behavior

CA1416 warnings are produced for @code sections in .razor files:

warning CA1416: This call site is reachable on: 'browser'. 'Thread.Start()' is unsupported on: 'browser'. [/Users/alklig/work/multi-blazy/multi-blazy.csproj]

Steps To Reproduce

  1. Install a .NET 7 preview SDK or .NET 6
  2. dotnet new blazorwasm
  3. edit Counter.razor and change IncrementCount to the following:
     private void IncrementCount() {
         new Thread(() => { Console.WriteLine("hello"); }).Start();
         currentCount++;
     }
  4. dotnet build

Expected result: CA1416 warning about use of Thread.Start().

Actual result: no warning.


Now add new Thread(() => { Console.WriteLine ("hi"); }).Start() somewhere in Program.cs and rebuild. Note that a CA1416 is generated:

/Users/alklig/work/multi-blazy/Program.cs(12,1): warning CA1416: This call site is reachable on all platforms. 'Thread.Start()' is unsupported on: 'browser'. [/Users/alklig/work/multi-blazy/multi-blazy.csproj]

Exceptions (if any)

No response

.NET Version

7.0.100-preview.2.22153.17

Anything else?

Also tested with .NET 6.0.x, same behavior

lambdageek commented 2 years ago

Related to https://github.com/dotnet/roslyn/issues/39758

lambdageek commented 2 years ago

This is also relevant to MAUI Blazor projects if a Razor component calls a platform-specific API that is not available on a particular platform.

marek-safar commented 2 years ago

This is also very likely problematic for trimming warning analyzer

/cc @agocke

agocke commented 2 years ago

Unless there's something specific to this analyzer, this sounds like a Roslyn bug. It might be marked as generated code, which will silence analyzer warnings by default, but in this case I think it should probably classify as user code.

cc @chsienki

chsienki commented 2 years ago

By default generator created code is marked as user code.

However, the generator author can choose to give a hint name ending in .g.cs or add /// <Autogenerated> to the top of the file. That will cause the regular analyzer heuristics to kick in and treat it as generated code, much the same as if a tool had added it outside the compilation pipeline.

agocke commented 2 years ago

@chsienki This bug is that warnings don't appear in razor code sections, though. Are either of those things added to razor compiled files?

chsienki commented 2 years ago

@agocke yes. https://github.com/dotnet/sdk/blob/4e35c47e93f5ac1b8b4d813fd4b3f69e2377afa7/src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs#L240

agocke commented 2 years ago

Got it, so then this looks like a Razor bug to me. If there's scaffolding code for Razor, that might be considered generated, but the stuff in @code looks like user-written code to me and shouldn't be marked as generated.