首页 > 解决方案 > 单元测试在 VSTS Build 上查找 roslyn 二进制文件的位置错误

问题描述

我们有一个 Web 应用程序,它大量使用动态代码编译。我的任务是添加 C#6 功能并使用 roslyn 编译器。

这样做非常简单。只需添加 nuget 包

对所有使用动态代码编译并更改一行代码的项目

private static System.CodeDom.Compiler.CodeDomProvider Compiler => compiler 
?? (compiler =                                                                             
new 
Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider());

到目前为止一切正常。将 nuget 包添加到使用动态代码编译的单元测试后,它们也可以工作(本地化)。

但是当我在 VSTS/TFS 服务器上查询一个新的构建时,一些单元测试失败了。经过一番研究,我发现了问题:

System.IO.DirectoryNotFoundException:找不到路径“D:\Agents\agent_work\1\a\roslyn\csc.exe”的一部分。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.get_CompilerName() at Microsoft.CodeDom.Providers.DotNetCompilerPlatform 上的 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.FromFileBatch(CompilerParameters options, String[] fileNames)。

好吧,项目根目录是查找 roslyn 二进制文件的错误位置。

在相关单元测试的 app.config 中:

<appSettings>
    <add key="aspnet:RoslynCompilerLocation" value="roslyn" />
  </appSettings>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

我还尝试了 nuget 包Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix

没有成功。我找到了很多信息,如何修复 /bin/roslyn 错误,但没有关于构建服务器、单元测试和 roslyn 的特殊组合。有人有更多信息吗?

编辑:

我尝试了 Jayendran 建议的解决方案。我发现变量 $(CscToolPath) 和 $(WebProjectOutputDir) 是空的,如下所示:

<Target Name="CheckVariables" AfterTargets="AfterBuild" Condition="'$(OutDir)' != '$(OutputPath)'">
    <Message Text="$(OutDir)" Importance="high"/>
    <Message Text="$(OutputPath)" Importance="high"/>
    <Message Text="$(CscToolPath)" Importance="high"/>
    <Message Text="$(WebProjectOutputDir)" Importance="high"/>
  </Target>

$(CscToolPath) 和 $(WebProjectOutputDir) 没有输出。

EDIT2: 作为一种解决方法,我编辑了单元测试的 app.configs,如下所示:

<appSettings>
    <add key="aspnet:RoslynCompilerLocation" value="_PublishedWebsites\*OneOfTheWebApps*\bin\roslyn" />
  </appSettings>

单元测试现在可以找到二进制文件,并且似乎此更改对我的本地计算机没有影响。单元测试现在在本地和构建服务器上通过。然而,这不能解决这个问题。

标签: c#unit-testingazure-devopsroslynazure-pipelines

解决方案


推荐阅读