18
votes

I am trying to clean and rebuild a solution file with multiple projects from the command line using MSBUILD. For some reason my build fails (about 10% of the built projects fail) and I get multiple errors which all look like:

error CS0234: The type or namespace name 'foo' does not exist in the namespace 'bar' (are you missing an assembly reference?)

Now if I clean and rebuild the same solution file from Visual Studio 2010 with the exact same configurations it will build successfully with no errors.

Is there a difference in the setup or configuration of MSBuild from Visual Studio that needs to be changed that I am not aware of?

8

8 Answers

3
votes

I just had to deal with this issue and it turns out that msbuild likes to move built binaries into the binaries\release directory and reference those instead of the projects themselves when it builds things. After building it copies the files to this directory. This explains why it works in visual studio and not msbuild (I'm currently using TFS 1010).

In my case I had an old binary version of a dll being referenced by a project that was being built after the one that should have generated the correct file. The old one (binary) was overwriting the new one (built from source) as the solution referencing the binary one was being built later in the build.

2
votes

I assume there's a difference in how the project is built, because Visual Studio does not run MSBuild, as it rather hosts the build engine itself. This was answered here.

However, I've had similar problem.

In my case the project referenced an external library, which was placed in the project's child directory, unluckily named "packages".

After running MSBuild the folder's content was deleted, supposedly to be downloaded again by Nuget.

The obvious solution was to rename the folder and it worked.

1
votes

Try checking the paths to the references that MSBuild can't find in the non-building library's project file. Sometimes when you use VS or ReSharper to automatically add a reference for you the path ends up being to the \obj directory. VS seems able to cope with this, but MSBuild not so much.

1
votes

For me the problem was that the some projects in the solution were not included in the build configuration for the solution. Those projects were dependencies for the projects in the build configuration, so all the projects in the solution failed.

After marking the dependcies projects with build in the solution configuration the msbuild ran successfully.

1
votes

Summary: Set Debug/Release mode in Visual Studio to the same settings as MSBuild to check for compilation errors.

I encountered the same problem:

  • Tried deleting all "bin" and "obj" folders.

  • Made sure all related projects are indeed being referenced and not just liked to compiled dlls. ex. Project B references A. Remove A from solution. Then add again. B would then reference A but via compiled dll only. Remove reference and re-add the project.

Finally switched to "Release" in Visual Studio. Turns out I had conditional compilation in some code (eg. #if DEBUG). So what was running in MSBuild and Visual Studio were actually different hence the error "The type or namespace name 'foo' does not exist in the namespace 'bar' (are you missing an assembly reference?)"

1
votes

My issue was found in the solution file.

Remove all lines which reference 'ANY' for CPU and leave the 64 bit ones in.

0
votes

In my case I had one PCL referencing another PCL with different targets. Visual Studio showed me a warning in the references list of the first library, but compiled the solution, whereas MSBUILD refused to compile. I fixed the problem by retargeting the PCL. Hope this helps somebody.

0
votes

See my answer here.

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