13

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

Simon
  • 33,714
  • 21
  • 133
  • 202
  • Pretty sure source generators are just fancy analyzers, and you can disable those. – Jeremy Lakeman Feb 10 '21 at 02:53
  • 3
    what effort have you made? – Daniel A. White Feb 10 '21 at 02:56
  • 5
    Let’s ask a maintainer of several very important open-source projects (and with reasonable rep) if he has made an effort. – Matthias Feb 10 '21 at 03:02
  • 2
    @DanielA.White i spent ~45 min researching. nothing that even gave me a direction of what to try. – Simon Feb 10 '21 at 03:05
  • 1
    @JeremyLakeman i tried a few diff combos of exclude/include. the generator still ran. eg IncludeAssets="compile" ExcludeAssets="all" – Simon Feb 10 '21 at 03:05
  • Is the source generation a standalone package or part of another package? – JohanP Feb 10 '21 at 03:08
  • @JohanP it is a lib and a source generator in one package. i updated the Q with the full context – Simon Feb 10 '21 at 03:11
  • I think the generator needs to be included as an analyzer dependency... If it's being included as a transitive dependency, complain upstream or recompile packages yourself I guess. – Jeremy Lakeman Feb 10 '21 at 03:20
  • Or can you "ExcludeAssets" the transitive analyzers dependency https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets? – Jeremy Lakeman Feb 10 '21 at 03:24
  • @JeremyLakeman ExcludeAssets didnt work for me – Simon Feb 10 '21 at 03:40

1 Answers1

16

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>
Julian
  • 33,915
  • 22
  • 119
  • 174
Simon
  • 33,714
  • 21
  • 133
  • 202