Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
lambdageek opened this issue Apr 15, 2022 · 8 comments
Open
1 task done
Labels
area-compiler Umbrella for all compiler issues untriaged

Comments

@lambdageek
Copy link
Member

Is there an existing issue for this?

  • I have searched the existing issues

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
Copy link
Member Author

Related to dotnet/roslyn#39758

@lambdageek
Copy link
Member Author

lambdageek commented Apr 21, 2022

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
Copy link
Contributor

This is also very likely problematic for trimming warning analyzer

/cc @agocke

@agocke
Copy link
Member

agocke commented Apr 22, 2022

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
Copy link
Contributor

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
Copy link
Member

agocke commented Apr 22, 2022

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

@agocke
Copy link
Member

agocke commented Apr 22, 2022

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.

@chsienki chsienki transferred this issue from dotnet/razor-compiler Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues untriaged
Projects
None yet
Development

No branches or pull requests

6 participants