2

I'm trying to build a ms test project in a nuget app.

My project structure is this:

MyPackage
--> MyPackage
--> MyPackageTest

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"

I get errors like this:

"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]

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

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392
  • Does this answer your question? [Visual Studio build successful, MSBuild fails](https://stackoverflow.com/questions/24458379/visual-studio-build-successful-msbuild-fails) – panoskarajohn Dec 27 '19 at 17:38
  • 1
    there are no selected answers for that question. trying out some of the suggestions did not work. Im building from a fresh clone too so obj and bin dirs are empty/dont exist yet – red888 Dec 27 '19 at 19:17
  • In VS you compile a solution, while at command prompt you compile a single project. I don't think that's a fair test. Can you try to compile the solution file instead Also there is no need to use quotes here. – Lex Li Dec 27 '19 at 23:05
  • in VS I was compiling the _specific_ project not the entire solution and it worked. Also, running msbuild against the sln results in the same error – red888 Dec 27 '19 at 23:07
  • MSbuild cannot locate the test-related assemblies. What does 'I go to cmd' do exactly? Do you start the right Visual Studio Tools command prompt which matches with the VS version you use, or just plain cmd? The latter is not ok. – stijn Dec 28 '19 at 09:16
  • the latter is obviously necessary for a build server, msbuild should be able to build my project – red888 Dec 28 '19 at 09:29
  • It can (VS invokes MSBuild), but it might require certain environment variables to be set correctly (which VS dos under the hood, and which is what the VS command prompt batch files will do as well). As far as I'm aware CI systems/build servers will invoke msbuild from a VS command prompt, not a plain one. Anyway, question is: does it work when invoked from a VS command prompt? – stijn Dec 28 '19 at 11:04
  • Anyway I just tried to reproduce this and cannot, a new test project created by VS2017 builds fine from a plain command line (invoking MSBuild with it's full path so it can be found). So: more information needed. Versions, build options, ... – stijn Dec 28 '19 at 11:09
  • Hi red888, did your issue still persists? One more question, where did he encounter the error when running msbuild, local machine or remote server? If your issue still bothers you, please feel free to let us know. – Mr Qian Jan 02 '20 at 10:18

2 Answers2

0

I can build this project in VS but not with msbuild

First of all, if you use CMD to call MSBuild, please make sure that you set the right path for the path of the Environment Variables. Just like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin(VS2017 Community version and the MSBuild.exe exists in this path).

Besides, l assume you have a local project called MyPackageTest which created by the local VS and then you migrate it to the build server without VS.

Actually, the issue could be caused by the missing packages which lost during migration to build server. Meanwhile,MSBuild with command line will not restore the missing packages since you have migrated it into the Build Server. However, when you use VS IDE, the build process will restore the missing packages automatically during the build process.(Tools-->Options-->Nuget Package Manager-->General-->Packages Restore).

Suggestion

  1. So please run the msbuild -t:restore first to restore the required packages.(Go to the project path first and do this).

  2. Then type this below:

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

In addition, you would better use Build Tool for VS2019 or Build Tool for VS2017 and please make sure that the build tool version is the same as the VS version which creates the project.

Second, when you use Build Tool, also make certain that you have installed the Testing tools core features in the individual components of vs installer.

enter image description here

Hope it could help you.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Sorry I should have added the I run `nuget.exe restore` before building with msbuild as well- it always completes without issue – red888 Dec 30 '19 at 15:12
  • How did you reference the `Microsoft.VisualStudio.QualityTools.UnitTestFramework dll`,did you use `Reference assembly`? l assume you configured the path of the msbuild migrated from VS into the environment variable and then invoked it by CMD. Besides, l think one possible issue is that when you migrate the project into buil server, it miss some reference assemblies for Test. – Mr Qian Dec 31 '19 at 09:29
  • In addition, If you use the old version msbuild which did not have the related framework version, the error will still occur. So please use Build Tool for VS to build your project and also remember to install the related framework version which is the same as your project. – Mr Qian Dec 31 '19 at 09:29
  • I doubt that whether you have installed the build tool or call the `Build.exe` by mistake. If you use the cmd to call MSBuild, please use the right path to call it like `C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin`. Besides, please make sure that you have installed the `Microsoft.VisualStudio.QualityTools.UnitTestFramework dll` in this path ` C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PublicAssemblies`. If not, you can copy it from local agent. – Mr Qian Jan 06 '20 at 10:24
0

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

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.

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.