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