Skip to content

NetAnalyzers should probably have flatter layout #54350

Closed

Description

@jjonescz

See discussion under dotnet/dotnet#6573:

##[error]CSC(0,0): error CS8032: (NETCORE_ENGINEERING_TELEMETRY=Build) An instance of analyzer Microsoft.NetCore.CSharp.Analyzers.Usage.CSharpMissingShebangInFileBasedProgram cannot be created from D:\a_work\1\s\src\runtime\artifacts.packages\microsoft.codeanalysis.netanalyzers\11.0.100-preview.5.26257.113\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll : System.TypeLoadException: Could not load type 'Microsoft.NetCore.Analyzers.Usage.MissingShebangInFileBasedProgram' from assembly 'Microsoft.CodeAnalysis.NetAnalyzers, Version=11.0.100.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

@jjonescz:

It seems this is caused by the roslyn repo build using analyzers from SDK, and runtime repo build using analyzers from a newer nuget package. The latter have the new analyzer, the former don't. And the mismatch is likely caused like this:

When building roslyn repo, analyzers are loaded from the sdk:

.dotnet/sdk/11.0.100-preview.5.26227.104/Sdks/Microsoft.NET.Sdk/analyzers/
  Microsoft.CodeAnalysis.NetAnalyzers.dll
  Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll

When building runtime repo, it requests the CSharp analyzers

runtime/artifacts/.packages/.../11.0.100-preview.5.26257.113/analyzers/dotnet/cs/
  Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll

Which depends on Microsoft.CodeAnalysis.NetAnalyzers.dll analyzer but that one is not in the same folder, so it's not handled by the compiler's consistency check and instead the already loaded one (in compiler server) is happily loaded (it also has the same assembly version, 11.0.100.0), causing that mismatch (because the old one doesn't have the base type that the new one depends on).

It really looks like a bug in runtime's config then - it should be loading the analyzers from one directory to ensure consistency. Or maybe the NetAnalyzers should be packaged differently. Or maybe the assembly version of the analyzer should be bumped.

Or I guess you could work around this by making sure both repos use different compiler server (by setting SharedCompilationId).

@jaredpar:

This is essentially a cross analyzer dependency which is an unsupported area. The fix should be to put them into the same directory.

@jjonescz:

Yes, ideally, but runtime seems to just be using NetAnalyzers nuget package, so we would need to change how that's packed on the sdk side. Also it seems to be a fairly common pattern to do what NetAnalyzers package is doing - have common analyzers in the main folder and then /cs/ and /vb/ subfolders for the C#/VB specific analyzers.

Activity

  1. added theissue type on May 18, 2026
  2. jjonescz commented on May 18, 2026

    @jjonescz
    MemberAuthor

    For starters I put this workaround into VMR: dotnet/dotnet@549080b

  3. pavelsavara commented on May 18, 2026

    @pavelsavara
    Member

    I'm not sure if it's related or not, I saw

    error CS8032: An instance of analyzer Microsoft.CodeAnalysis.Razor.Compiler.Analyzers.ComponentParameterNullableWarningSuppressor 
    cannot be created from D:\runtime2\.dotnet\sdk\11.0.100-preview.5.26227.104\Sdks\Microsoft.NET.Sdk.Razor\source-generators\Microsoft.CodeAnalysis.Razor.Compiler.dll : 
    System.IO.FileNotFoundException: Could not load file or assembly 'System.Collections.Immutable
    

    during runtime build on my local machine today. It was resolved by deleting stale d:\runtime2\.dotnet folder.

    Image Image

    I still have the binlog if you want it.

  4. kg commented on May 19, 2026

    @kg

    This maybe also caused dotnet/runtime#128070

  5. akoeplinger commented on May 19, 2026

    @akoeplinger
    Member

    the layout changed somewhere between version 9.0.0 and 10.0.100 of the package:

    Before:
    Image

    After:

    Image

    According to @jjonescz Roslyn's contract is that one analyzer and its dependencies need to be in one folder.

  6. kg commented on May 20, 2026

    @kg

    This should be fixed once #54372 flows through the VMR into dotnet/runtime, do I understand correctly?

  7. jjonescz commented on May 20, 2026

    @jjonescz
    MemberAuthor

    I think this should be already closed, it wasn't auto-closed because the PR was merged into non-main branch.

  8. kg commented on May 20, 2026

    @kg

    I think this should be already closed, it wasn't auto-closed because the PR was merged into non-main branch.

    Sorry, what I meant is, this issue (addressed by 54372, I think) is causing breakage for some CI flows on dotnet/runtime. I'm guessing for that to be fixed, 54372 will need to flow from here into the VMR and then from the VMR into dotnet/runtime, yeah?

  9. jjonescz commented on May 21, 2026

    @jjonescz
    MemberAuthor

    is causing breakage for some CI flows on dotnet/runtime

    What issues? I put a workaround to one VMR PR: dotnet/dotnet@549080b, perhaps it needs to be cherry-picked to other branches. Although if you can wait for this fix to flow as you say, then yeah, that's the ideal fix.

  10. akoeplinger commented on May 21, 2026

    @akoeplinger
    Member

    This one: dotnet/runtime#128070 (comment)

    And yes right now the flow that needs to happen is sdk 10.0.1xx -> ... -> sdk main (via automatic inter-branch merge) -> VMR main -> runtime main.

    If we need it faster (the sdk inter-branch merge can sometimes be blocked, same applies to the VMR->runtime backflow) we'd need to cherry-pick

  11. jjonescz commented on May 22, 2026

    @jjonescz
    MemberAuthor

    As a temporary workaround you could add NoWarn for CA2266 and that should disable the analyzer.

  12. kg commented on Jun 4, 2026

    @kg

    Still seeing this randomly crop up, did it just not flow yet? Is there some sort of bug that would cause it to intermittently break builds only on certain CI configurations? For example, on a Wasm-only PR, I saw the shebang error fail an OS X build but all the linux and windows lanes were fine:
    https://dev.azure.com/dnceng-public/public/_build/results?buildId=1448683&view=logs&jobId=2710d842-fa61-5104-0e86-cc29dd313ead&j=2710d842-fa61-5104-0e86-cc29dd313ead&t=0b292913-8361-5e62-8106-16cdc9499d26

  13. jjonescz commented on Jun 4, 2026

    @jjonescz
    MemberAuthor

    did it just not flow yet?

    Depends on where you mean. It didn't flow to 10.0.4xx and newer yet, stuck here it seems: #54459

  14. MichalStrehovsky commented on Jun 10, 2026

    @MichalStrehovsky
    Member

    We are still seeing this in dotnet/runtime:

    2026-06-10T06:28:45.4413270Z CSC : error CS8032: An instance of analyzer Microsoft.NetCore.CSharp.Analyzers.Usage.CSharpMissingShebangInFileBasedProgram cannot be created from /Users/runner/work/1/s/.packages/microsoft.codeanalysis.netanalyzers/11.0.100-preview.6.26302.118/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll : System.TypeLoadException: Could not load type 'Microsoft.NetCore.Analyzers.Usage.MissingShebangInFileBasedProgram' from assembly 'Microsoft.CodeAnalysis.NetAnalyzers, Version=11.0.100.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. [/Users/runner/work/1/s/src/tools/illink/src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj]

    preview.6.26302.118 seems pretty recent (June 2nd build of dotnet/dotnet main). @jjonescz is that one expected to already have the fix? The workaround mentioned above dotnet/dotnet@549080b seems to have merged into dotnet/dotnet main three weeks ago.

  15. MichalStrehovsky commented on Jun 10, 2026

    @MichalStrehovsky
    Member

    dotnet/runtime#129031 collects these on the dotnet/runtime side.

  16. jjonescz commented on Jun 10, 2026

    @jjonescz
    MemberAuthor

    preview.6.26302.118 seems pretty recent (June 2nd build of dotnet/dotnet main). @jjonescz is that one expected to already have the fix?

    No, the fix was stuck in code flow it seems; the PR #54459 carrying it merged only today.

    The workaround mentioned above dotnet/dotnet@549080b seems to have merged into dotnet/dotnet main three weeks ago.

    That was a workaround for VMR builds only, that wouldn't affect runtime or any other builds just using the SDK.

  17. akoeplinger commented on Jun 13, 2026

    @akoeplinger
    Member

    11.0.100-preview.6.26312.114 has the fix now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions