Disabling a specific C# 9 source generator
Asked Answered
A

1

14

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

Acetaldehyde answered 10/2, 2021 at 2:51 Comment(10)
Pretty sure source generators are just fancy analyzers, and you can disable those.Mayamayakovski
what effort have you made?Lavaliere
Let’s ask a maintainer of several very important open-source projects (and with reasonable rep) if he has made an effort.Uppermost
@DanielA.White i spent ~45 min researching. nothing that even gave me a direction of what to try.Acetaldehyde
@JeremyLakeman i tried a few diff combos of exclude/include. the generator still ran. eg IncludeAssets="compile" ExcludeAssets="all"Acetaldehyde
Is the source generation a standalone package or part of another package?Triturate
@Triturate it is a lib and a source generator in one package. i updated the Q with the full contextAcetaldehyde
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.Mayamayakovski
Or can you "ExcludeAssets" the transitive analyzers dependency learn.microsoft.com/en-us/nuget/consume-packages/…?Mayamayakovski
@JeremyLakeman ExcludeAssets didnt work for meAcetaldehyde
A
18

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>
Acetaldehyde answered 10/2, 2021 at 3:19 Comment(4)
Hi, Where do add the target ? How Do I integrate the target into my solution ? ThxMatamoros
I added the target into my Directory.Build.targets file. If you don't have one you can create that file in your project or solution folder. See learn.microsoft.com/en-us/visualstudio/msbuild/…Columbite
I wanted to use the third flavor, to remove based on filename. But what worked for me was just a bit different. For the condition I had to use Analyzer.Filename instead of just Filename. E.g. <Target Name="DisableAnalyzers" BeforeTargets="CoreCompile"> <ItemGroup> <Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.Filename)' == 'NugetXAnalizer'"/> </ItemGroup> </Target>Columbite
Is it possible to disable running the analyzer, but keep dll? I'm trying to run other source generator AFTER my source generator using reflection. Without Remove statement there are namespace collisions and with Remove I cannot find the package with reflection.Smukler

© 2022 - 2024 — McMap. All rights reserved.