Search code examples
c#c#-9.0sourcegenerators

Disabling a specific C# 9 source generator


Is there any way to disable a specific C# 9 source generator? Or alternatively disable them all?

the package in question is https://github.com/Husqvik/GraphQlClientGenerator#c-9-source-generator which is mean to be able to be used as both a lib and a source generator. but those are mutually exclusive, ie the majority of use cases it make no sense to gen code both by executing code and by code gen


Solution

  • seems this will disable all

    <Target Name="DisableAnalyzers" 
            BeforeTargets="CoreCompile">
      <ItemGroup>
        <Analyzer Remove="@(Analyzer)" />
      </ItemGroup>
    </Target>
    

    removing a named one uses the file path

    <Target Name="DisableAnalyzers"
            BeforeTargets="CoreCompile">
      <ItemGroup>
        <Analyzer Remove="D:\nugets\nugetx\0.9.2\analyzers\dotnet\cs\NugetXAnalizer.dll" />
      </ItemGroup>
    </Target>
    

    ok and finally u can remove based on filename

    <Target Name="DisableAnalyzers"
            BeforeTargets="CoreCompile">
      <ItemGroup>
        <Analyzer Remove="@(Analyzer)"
                  Condition="'%(Filename)' == 'NugetXAnalizer'"/>
      </ItemGroup>
    </Target>