MS Fakes的GENERATEFAKES偶尔与CS1705一起失败



在我的机器上,单元测试项目构建几天后就失败了,但不是每次都失败。在我同事的机器上,它有时也会失败,但大多数时候他只需要重建并成功构建。

Assembly 'xx' with identity '...' uses 'System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' which has a higher version than referenced assembly 'System.ValueTuple' with identity 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' [PathToTestDirobjDebugFakesxxf.csproj]
3>  GENERATEFAKES : error : project compilation failed with exit code 1

我已经检查了我们所有项目的解决方案中的所有nuget引用(也在project.assests.json和packages.lock.json中(,它们都引用了System.ValueTuple包4.5.0版本(汇编版本4.0.3.0(。

在packages.lock.json中,我在这个json路径上看到了以下依赖项:

dependencies[".NETFramework,Version=v4.6.2"]["Microsoft.CodeAnalysis.Common"]

这是哪一个:

"Microsoft.CodeAnalysis.Common": {
"type": "Transitive",
"resolved": "2.8.0",...

这包含对的依赖

"System.ValueTuple": "4.3.0"

其具有程序集版本4.0.2.0。

在生成的f.csproj文件中,我还看到了以下提示路径:

<Reference Include="System.ValueTuple">
<HintPath>[PathToXX]binDebugSystem.ValueTuple.dll</HintPath>
<Aliases>svt</Aliases>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>

但是在这个路径上找到了正确的4.0.3.0版本。整体源和所有bin输出文件夹不包含4.0.2.0版本,除了在那些fakes生成的程序集的obj文件夹中。

对于我们引用为fakes的所有其他项目,MS fakes工具正在生成以下提示路径:

<Reference Include="System.ValueTuple">
<HintPath>C:Usersmyuser.nugetpackagessystem.valuetuple4.5.0refnet461System.ValueTuple.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference> 

我们的解决方案包含几个net462经典项目(Package Reference Style(和一些sdk风格的项目,我们的一些项目也引用了Asp.net core 2.1。多年来,我们的单元测试项目一直在使用Microsoft Fakes和MS test。我们正在使用VS 2022(17.1.6(。

到目前为止我尝试过的:

  • 关闭VS,删除所有bin和obj文件夹,删除nuget缓存,重新启动VS,重建(到目前为止,这对我来说只起了一次作用,但现在我又被困了好几天(
  • 将单元测试项目中的AutoGenerateBBindingRedirects和GenerateBindirectsOutputTypeto设置为True
  • 同样在.fakes文件中,我尝试设置这两个属性(即使GenerateBindirectsOutputType对fakes程序集没有多大意义(
  • 尝试了这个SO答案

所以在这些失败的尝试之后,我的fakes文件看起来是这样的:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
<Assembly Name="xx"/>
<StubGeneration>
<Clear/>
<Add Interfaces="true"/>
<Remove TypeName="xyz"/>
</StubGeneration>
<!--Shim do not make sense for interfaces-->
<ShimGeneration>
<Clear/>
</ShimGeneration>
<Compilation>
<Property Name="PlatformTarget">x64</Property>
<Property Name="AutoGenerateBindingRedirects">True</Property>
<Property Name="GenerateBindingRedirectsOutputType">True</Property>
</Compilation>
</Fakes>

如果有任何帮助或暗示,我将不胜感激,谢谢!

编辑:我还在输出窗口中看到这些警告:

C:Program FilesMicrosoft Visual Studio2022EnterpriseMSBuildCurrentBinamd64Microsoft.Common.CurrentVersion.targets(2301,5): warning MSB3277: Found conflicts between different versions of "System.ValueTuple" that could not be resolved. [[PathToMyTestProjectFolder]objDebugFakesxxf.csproj]
C:Program FilesMicrosoft Visual Studio2022EnterpriseMSBuildCurrentBinamd64Microsoft.Common.CurrentVersion.targets(2301,5): warning MSB3277: There was a conflict between "System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" and "System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51". [[PathToMyTestProjectFolder]objDebugFakesxxf.csproj]
C:Program FilesMicrosoft Visual Studio2022EnterpriseMSBuildCurrentBinamd64Microsoft.Common.CurrentVersion.targets(2301,5): warning MSB3277: "System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" was chosen because it was primary and "System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" was not. [[PathToMyTestProjectFolder]objDebugFakesxxf.csproj]

正如我在上面的评论中所说,我已经为我们的情况找到了一个解决方案:

在应该伪造的项目中,我们上周在接口方法签名中引入了一个c#元组。我已经将元组更改为一个类,所以现在接口不再需要c#元组了。构建成功了,但我在文章末尾的OP中提到的三个警告仍然存在。

最新更新