Source generator not working correctly in Visual Studio 2022
Asked Answered
R

1

9

I am writing a source generator that automatically writes JsonObject classes, that translantes JSON to run-time objects.

enter image description here

As shown in the screenshot, you can see red wavy lines. These are CS0246. However, the project can be successfully built without any error. That means the source generator works.

Is it possible to let the editor recognize the work of source generator?

Furthermore, in the solution explorer, it shows "This generator is not generating files." This is obviously incorrect.

enter image description here

Redivivus answered 7/7, 2022 at 1:55 Comment(10)
documentation says you have to restart VS after a change on the SGShimmy
@PatrickBeynio I restarted many times already.Redivivus
You can attach your debugger to the Visual Studio process to debug it. Otherwise is your generator doing anything tricky about assuming it can read other files from disk or something else like that?Venegas
@JasonMalinowski sorry for misusing the comments, but I will need a onetime DB request when the SG is initiated, will we get a way to add additional sources/providers to ISGs?Shimmy
@JasonMalinowski even more important... will we get any useable documentation soon? -.- sometimes i feel like SGs are already deadShimmy
Yeah, we're working on the documentation, but yeah, we're definitely behind on that.Venegas
Agreed. SG does not seem to be in a useable stage. I got excited looking at the code samples and thought it was a better alternate to T4. However, the execution makes SG impractical. So far I have noticed that I need to restart VS for each line of code I update.Oligopoly
@JasonMalinowski I found this Q&A after running into the same problem. The generator works. The target project builds. I can step through the generator and watch it call AddSource. If I deliberately introduce an error in the generated source, the target project reports that error and no longer builds. But the dependency tree in the Solution Explorer says the generator is not generating source, which is clearly wrong.Jemmie
@KevinKrumwiede: if you're making changes to your generator, it's possible VS has already cached the generator that wasn't generating files. If you're not doing that (or have already restarted VS), then raise this on gthub.com/dotnet/roslyn and we can investigate further.Venegas
@JasonMalinowski It wasn't a caching issue. I'm quite familiar with the change-rebuild-restart ritual. This bug was triggered by the generator having nuget dependencies, which still require another arcane ritual as shown in my answer. But the problem was fixed (apparently accidentally) in VS 17.8.4, which released a few days before I found this thread.Jemmie
J
2

This was finally fixed in Visual Studio 17.8.4. It did not work in 17.8.3.

This is the .csproj for my generator project:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
        <PackageReference Include="Scriban" Version="5.9.1" GeneratePathProperty="true" />
    </ItemGroup>

    <PropertyGroup>
        <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
    </PropertyGroup>

    <Target Name="GetDependencyTargetPaths">
        <ItemGroup>
            <TargetPathWithTargetPlatformMoniker Include="$(PKGScriban)\lib\netstandard2.0\Scriban.dll" IncludeRuntimeDependency="false" />
        </ItemGroup>
    </Target>

</Project>

Contrary to various tutorials, I did not find it necessary to use PrivateAssets="all" here.

My consumer resides in the same solution and references the generator with a ProjectReference:

    <ItemGroup>
        <ProjectReference Include="..\TestGenerator\TestGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>
Jemmie answered 15/1, 2024 at 20:33 Comment(5)
So looking at the full set of changes between 17.8.3 and 17.8.4...not much jumps out that would have impacted this. There was definitely no change to Roslyn. So something makes me think whatever you're seeing is actually flaky and you got lucky now.Venegas
@JasonMalinowski I don't know if the upvotes represent confirmation that it's actually fixed. I can't very well report it if I can't repro it anymore. It's scary that the developers didn't know it was broken or how it got fixed. No reflection on your or any other individual developer. I can smell the corporate rot on this. Microsoft rushed source generators, didn't allocate sufficient resources to design them properly or see the project through, and apparently pencil-whipped it as "done." All these years later, source generators remain half-finished and barely usable.Jemmie
@KevinKrumwiede Currently people use incremental source generator instead and it works fine.Redivivus
@Redivivus We're talking about incremental source generators, and no they don't.Jemmie
Visual Studio version 17.9.4 and I have issues till this day with Incremental Source generators, and its random... sometimes works, sometimes dont, sometimes restarting VS works, sometimes doesntChrotoem

© 2022 - 2025 — McMap. All rights reserved.