我可以在 VS 中构建这个项目,但不能用 msbuild [英] I can build this project in VS but not with msbuild

查看:28
本文介绍了我可以在 VS 中构建这个项目,但不能用 msbuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to build a ms test project in a nuget app

我的项目结构是这样的:

My project structure is this:

MyPackage
--> MyPackage
--> MyPackageTest

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

If I open VS and right click and build MyPackageTest it works If I go to cmd and type this I fails:

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"

to add, msbuild also throws this error: Could not locate the assembly "Microsoft.VisualStudio.QualityTools.UnitTestFramework"

推荐答案

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

Try running msbuild in a new cmd window everytime as a temporary workaround.

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

This works everytime for me and is what I currently do. I think running msbuild(with or without vsdevcmd) changes some environmental variables/settings which affects the subsequent msbuild runs.

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

I faced the same problem a while ago which greatly affected my productivity. I think it started after upgrading to VS2019. My solution didn't involve any nuget packages, but msbuild would fail randomly with these errors.

这篇关于我可以在 VS 中构建这个项目,但不能用 msbuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆