VS 和 Rider 忽略源生成的文件

huangapple go评论75阅读模式
英文:

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

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;

    &lt;PropertyGroup&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
    
        &lt;IsPackable&gt;false&lt;/IsPackable&gt;
        &lt;IsTestProject&gt;true&lt;/IsTestProject&gt;
    &lt;/PropertyGroup&gt;

    &lt;ItemGroup&gt;
        &lt;PackageReference Include=&quot;Microsoft.NET.Test.Sdk&quot; Version=&quot;17.5.0&quot; /&gt;
        &lt;PackageReference Include=&quot;NUnit&quot; Version=&quot;3.13.3&quot; /&gt;
        &lt;PackageReference Include=&quot;NUnit3TestAdapter&quot; Version=&quot;4.4.2&quot; /&gt;
        &lt;PackageReference Include=&quot;NUnit.Analyzers&quot; Version=&quot;3.6.1&quot; /&gt;
        &lt;PackageReference Include=&quot;coverlet.collector&quot; Version=&quot;3.2.0&quot; /&gt;
        &lt;ProjectReference Include=&quot;..\TaskReward.Expander\TaskReward.Expander.csproj&quot; OutputItemType=&quot;Analyzer&quot; ReferenceOutputAssembly=&quot;false&quot; /&gt;
    &lt;/ItemGroup&gt;

    &lt;ItemGroup&gt;
      &lt;AdditionalFiles Include=&quot;StateMachineTests\Text.xstate&quot; /&gt;
    &lt;/ItemGroup&gt;

&lt;/Project&gt;

And this is the csproj of my generator

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;

  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
    &lt;LangVersion&gt;Latest&lt;/LangVersion&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
	&lt;EnforceExtendedAnalyzerRules&gt;true&lt;/EnforceExtendedAnalyzerRules&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
	&lt;PackageReference Include=&quot;Microsoft.CodeAnalysis.Analyzers&quot; Version=&quot;3.3.4&quot; PrivateAssets=&quot;all&quot; /&gt;
    &lt;PackageReference Include=&quot;Microsoft.CodeAnalysis.CSharp&quot; Version=&quot;4.6.0&quot; PrivateAssets=&quot;all&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;ItemGroup&gt;
    &lt;!-- Generator dependencies --&gt;
    &lt;PackageReference Include=&quot;Newtonsoft.Json&quot; Version=&quot;12.0.2&quot; PrivateAssets=&quot;all&quot; GeneratePathProperty=&quot;True&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;PropertyGroup&gt;
    &lt;GetTargetPathDependsOn&gt;$(GetTargetPathDependsOn);GetDependencyTargetPaths&lt;/GetTargetPathDependsOn&gt;
  &lt;/PropertyGroup&gt;

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

&lt;/Project&gt;

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.

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


       &quot;...\GeneratorUnitTestProject\GeneratorUnitTestProject.csproj&quot; (build Ziel) (1) -&gt;
       (CoreCompile Ziel) -&gt; 
         ...\GeneratorUnitTestProject\StateMachineTests\MachineTest.cs(1,7): error CS0246: Der Typ- oder Namespacename &quot;gen&quot; wurde nicht gefunden (m&#246;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 能够跟上。

&lt;PackageReference Include=&quot;Microsoft.CodeAnalysis.CSharp&quot; Version=&quot;4.5.0&quot; PrivateAssets=&quot;all&quot; /&gt;

作为附注,每当更新 Microsoft.CodeAnalysis.CSharp 时,您可能还需要更新您的 dotnet SDK。例如,4.5.07.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.

&lt;PackageReference Include=&quot;Microsoft.CodeAnalysis.CSharp&quot; Version=&quot;4.5.0&quot; PrivateAssets=&quot;all&quot; /&gt;

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.

huangapple
  • 本文由 发表于 2023年5月21日 10:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定