英文:
VS and Rider ignore source generator generated files
问题
C#源文件生成的文件被忽略或显示错误,但当我将文件复制出来时,一切都正常。
state.Text.g.cs
using System.Collections.Generic;
using System.Linq;
#nullable enable
namespace GeneratorUnitTestProject.StateMachineTests
{
//[...]
}
它报错 CS1001:需要标识符。
如果我复制生成的文件并移除生成器,它能够正常工作,我的单元测试也能够成功。
这是我的单元测试解决方案的 csproj:
<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>
这是我的生成器的 csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<!-- Generator dependencies -->
<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>
如果我在命名空间前添加 "gen.",错误将更改为未找到命名空间 (CS0246),但IDE(Rider或VS)没有显示任何UI错误,就好像被忽略了,但错误日志没有提到它。
英文:
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
答案1
得分: 4
This is a recurring issue any time Microsoft updates their C# analyzer libraries.
将您的分析器包降级到 4.5.0,直到 IDE 能够跟上。
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all" />
作为附注,每当更新 Microsoft.CodeAnalysis.CSharp
时,您可能还需要更新您的 dotnet SDK。例如,4.5.0
在 7.0.201
上运行良好,但升级到 4.6.0
需要 SDK 版本 7.0.302
,否则生成似乎根本没有运行源生成器。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论