【问题标题】:I can build this project in VS but not with msbuild我可以在 VS 中构建这个项目,但不能使用 msbuild
【发布时间】:2019-12-27 17:20:49
【问题描述】:

我正在尝试在 nuget 应用程序中构建一个 ms 测试项目。

我的项目结构是这样的:

MyPackage
--> MyPackage
--> MyPackageTest

如果我打开 VS 并右键单击并构建 MyPackageTest 它可以工作。如果我去 cmd 并输入这个我会失败:

msbuild .\MyPackageTest\MyPackageTest.csproj /T:build "/p:Configuration=Release"

我收到这样的错误:

"C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj" (build target) (1) ->
(CoreCompile target) ->
  MyTests.cs(2,17): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(2,17): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(13,4): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(13,4): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(13,4): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(13,4): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(17,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(17,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(38,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(38,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(59,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(59,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(75,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(75,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(91,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyOtherTests.cs(91,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(16,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(16,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(36,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(36,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(50,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(50,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(65,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(65,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(81,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]
  MyTests.cs(81,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]

编辑:添加,msbuild 也会抛出这个错误:Could not locate the assembly "Microsoft.VisualStudio.QualityTools.UnitTestFramework"

【问题讨论】:

  • 这能回答你的问题吗? Visual Studio build successful, MSBuild fails
  • 该问题没有选定的答案。尝试了一些建议没有奏效。我也是从一个新的克隆构建的,所以 obj 和 bin 目录是空的/还不存在
  • 在 VS 中编译解决方案,而在命令提示符下编译单个项目。我不认为这是一个公平的测试。您可以尝试编译解决方案文件吗这里也不需要使用引号。
  • 在 VS 中,我正在编译 specific 项目而不是整个解决方案,并且它有效。此外,针对 sln 运行 msbuild 会导致相同的错误
  • MSbuild 找不到与测试相关的程序集。 'I go to cmd' 到底是做什么的?您是否启动了与您使用的 VS 版本匹配的正确 Visual Studio 工具命令提示符,或者只是简单的 cmd?后者不行。

标签: c# .net visual-studio msbuild


【解决方案1】:

我可以在 VS 中构建这个项目,但不能使用 msbuild

首先,如果使用CMD调用MSBuild,请确保Environment Variables的路径设置正确。就像C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin(VS2017社区版和MSBuild.exe在这个路径下)。

另外,我假设你有一个名为MyPackageTest的本地项目,它是由本地VS创建的,然后你在没有VS的情况下将它迁移到构建服务器。

其实,这个问题可能是由于迁移到构建服务器过程中丢失的包丢失造成的。同时,MSBuild with command line 不会恢复丢失的包,因为您已将其迁移到 Build Server 中。但是,当您使用 VS IDE 时,构建过程会在构建过程中自动恢复丢失的包。(Tools-->Options-->Nuget Package Manager-->General-->Packages Restore)。

建议

  1. 所以请先运行msbuild -t:restore 来恢复所需的包。(先到项目路径执行此操作)。

  2. 然后在下面输入:

    msbuild .\MyPackageTest\MyPackageTest.csproj /T:build "/p:Configuration=Release"

另外,您最好使用Build Tool for VS2019Build Tool for VS2017,并请确保构建工具版本与创建项目的VS版本相同。

其次,在你使用Build Tool的时候,还要确保你已经在vs安装器的individual components中安装了Testing tools core features

希望对你有帮助。

【讨论】:

  • 抱歉,我应该在使用 msbuild 构建之前添加我运行的 nuget.exe restore - 它总是可以毫无问题地完成
  • 你是怎么引用Microsoft.VisualStudio.QualityTools.UnitTestFramework dll的,你用的是Reference assembly吗? l 假设你配置了从VS迁移的msbuild的路径到环境变量中,然后通过CMD调用。此外,我认为一个可能的问题是,当您将项目迁移到构建服务器时,它会丢失一些用于测试的参考程序集。
  • 另外,如果您使用没有相关框架版本的旧版本msbuild,仍然会出现错误。所以请使用 Build Tool for VS 来构建您的项目,同时记得安装与您的项目相同的相关框架版本。
  • 我怀疑您是否安装了构建工具或错误地调用了Build.exe。如果使用cmd调用MSBuild,请使用正确的路径调用,如C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin。此外,请确保您已将Microsoft.VisualStudio.QualityTools.UnitTestFramework dll 安装在此路径`C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PublicAssemblies` 中。如果没有,您可以从本地代理复制。
【解决方案2】:

尝试每次在新的 cmd 窗口中运行 msbuild 作为临时解决方法。

这对我来说每次都有效,也是我目前所做的。我认为运行 msbuild(有或没有 vsdevcmd)会改变一些影响后续 msbuild 运行的环境变量/设置。

不久前我遇到了同样的问题,这极大地影响了我的工作效率。我认为它是在升级到 VS2019 后开始的。我的解决方案不涉及任何 nuget 包,但 msbuild 会随机失败并出现这些错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    相关资源
    最近更新 更多