Skip to content

Build Error in Source Generator Test Project with TUnit 0.25.21+ #3668

@nandor23

Description

@nandor23

Issue

I'm encountering a build error in a source generator test project. The source generator uses System.Text.Json, and to make it work, all transitive dependencies of System.Text.Json must be packaged with the source generator.
The problem is most likely something internal to TUnit, since building the project with TUnit 0.25.21 succeeds, but any newer version produces a build error. The build also succeeds with XUnit.

One of the build errors is the following:

0>UaDetector_SourceGenerator_Tests_Tests_HintSourceGeneratorTests_ReportDiagnostic_WhenJsonIsInvalid.g.cs(8,53): 
Error CS0433 : The type 'IAsyncEnumerable<T>' exists in both 'Microsoft.Bcl.AsyncInterfaces, Version=9.0.0.10, 
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'System.Runtime, Version=10.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a'

It seems like there is some reference collision in the classes generated by TUnit.

Steps to Reproduce:

  1. Clone the UaDetector repository
  2. Update TUnit package version from 0.25.21 to the latest version (Central Package Management is used)
  3. Navigate to the tests/UaDetector.SourceGenerator.Tests project
  4. Build the project

csproj files

UaDetector.SourceGenerator.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <IsRoslynComponent>true</IsRoslynComponent>
    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="All" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="All" />
    <PackageReference Include="PolySharp" PrivateAssets="All" />
  </ItemGroup>

  <ItemGroup Label="Package References">
    <PackageReference Include="System.Text.Json" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Text.Encodings.Web" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Text.Encoding.CodePages" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="Microsoft.Bcl.HashCode" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Buffers" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Memory" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Runtime.CompilerServices.Unsafe" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Threading.Tasks.Extensions" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Numerics.Vectors" PrivateAssets="All" GeneratePathProperty="true" />
    <PackageReference Include="System.Reflection.Metadata" PrivateAssets="All" GeneratePathProperty="true" />
  </ItemGroup>

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

  <Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Text_Encodings_Web)\lib\netstandard2.0\System.Text.Encodings.Web.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Text_Encoding_CodePages)\lib\netstandard2.0\System.Text.Encoding.CodePages.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_Bcl_HashCode)\lib\netstandard2.0\Microsoft.Bcl.HashCode.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgMicrosoft_Bcl_AsyncInterfaces)\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Buffers)\lib\netstandard2.0\System.Buffers.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Memory)\lib\netstandard2.0\System.Memory.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Runtime_CompilerServices_Unsafe)\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Threading_Tasks_Extensions)\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Numerics_Vectors)\lib\netstandard2.0\System.Numerics.Vectors.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Reflection_Metadata)\lib\netstandard2.0\System.Reflection.Metadata.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
  </Target>
</Project>

UaDetector.SourceGenerator.Tests.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
    <PackageReference Include="TUnit" />
    <PackageReference Include="System.Formats.Asn1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\src\UaDetector.SourceGenerator\UaDetector.SourceGenerator.csproj" />
  </ItemGroup>
</Project>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions