3

C# Source generated files are ignored or show errors but when I copy the file out everything works.

state.Text.g.cs

using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace GeneratorUnitTestProject.StateMachineTests
{
//[...]
}

It says the error is CS1001 Identifier expected.

If I copy out the generated file and remove the generator it works perfectly fine and my Unit Test succeeds.

This is the csproj of my unit test solution

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

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    
        <IsPackable>false</IsPackable>
        <IsTestProject>true</IsTestProject>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
        <PackageReference Include="NUnit" Version="3.13.3" />
        <PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
        <PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
        <PackageReference Include="coverlet.collector" Version="3.2.0" />
        <ProjectReference Include="..\TaskReward.Expander\TaskReward.Expander.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>

    <ItemGroup>
      <AdditionalFiles Include="StateMachineTests\Text.xstate" />
    </ItemGroup>

</Project>

And this is the csproj of my generator

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>Latest</LangVersion>
    <Nullable>enable</Nullable>
    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <!-- Generator dependencies -->
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" PrivateAssets="all" GeneratePathProperty="True" />
  </ItemGroup>

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

  <Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
      <!-- Pack both our DLL and the dependencies into a generated Nuget Package -->
      <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
      <None Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>
  </Target>

</Project>

If I go ahead and add a "gen." infront of the namespace the error changes to namespace not found. (CS0246) But the IDE (Rider or VS) dont show any error in the UI. Its like it gets ignored but the error Log doesnt say anything about it.

Fehler beim Buildvorgang.

       "...\GeneratorUnitTestProject\GeneratorUnitTestProject.csproj" (build Ziel) (1) ->
       (CoreCompile Ziel) -> 
         ...\GeneratorUnitTestProject\...\....Gen.State.Statemachines\state.Text.g.cs(172,106): warning CS8669: Die Anmerkung für Nullable-Verweistypen darf nur in Code innerhalb eines #nullable-Anmerkungskontexts verwendet werden. Für automatisch generierten Code ist eine explizite #nullable-Anweisung in der Quelle erforderlich.


       "...\GeneratorUnitTestProject\GeneratorUnitTestProject.csproj" (build Ziel) (1) ->
       (CoreCompile Ziel) -> 
         ...\GeneratorUnitTestProject\StateMachineTests\MachineTest.cs(1,7): error CS0246: Der Typ- oder Namespacename "gen" wurde nicht gefunden (möglicherweise fehlt eine using-Direktive oder ein Assemblyverweis).

    1 Warnung(en)
    1 Fehler
  • As a quick update for some reason it now has a version of the source generated stored. So I changed the source and now the IDE shows an error with the old code but building works fine. Idk where the old file comes from. cleaning, rebuilding and deleting stuff doesnt work – Drachencheat May 21 '23 at 12:39

1 Answers1

4

This is a recurring issue any time Microsoft updates their C# analyzer libraries.

Downgrade your analyzer package to 4.5.0 until the IDEs catch up.

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all" />

As an additional note, any time Microsoft.CodeAnalysis.CSharp is updated, you likely also need to update your dotnet SDK. For example, 4.5.0 worked fine on 7.0.201, however upgrading to 4.6.0 requires SDK version 7.0.302, otherwise the build will appear to not have run the source generators at all.

Jeff
  • 12,085
  • 12
  • 82
  • 152
  • @Drachencheat The issue for 4.6.0 has been resolved on Rider's side. But there are also newer versions of the analyzer package now. – Jeff Aug 28 '23 at 11:39