dotnet / dotnet/msbuild

Difference when building VS solution in VS and msbuild

Open
#417 8 comments 1 reaction 0 assignees View on GitHub
triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 6h
Merged PRs (30d)
139

Description

I'm having an issue building some projects. I´ve found differences between building my solutions in VS (it work fine), and building the solutions with msbuild through command line (it fails).

I've managed to make a small sample that illustrates the problem. [BuildError.zip](https://github.com/Microsoft/msbuild/files/77337/BuildError.zip)

Basically I've got 2 solutions `SolA.sln` and `SolAandB.sln`. `SolA.sln` only contains project `A.csproj` while `SolAandB.sln` contains both `A.csproj` and `B.csproj`. The project `A.csproj` is just a class library, while `B.csproj` is a command line project which references project `A.csproj`. The **subtle peculiarity** about this projects, is that `A.csproj` is configured to not be built in `SolAandB.sln`. The reason of this, is that I've got a lot of projects (more than 300, some of which are C# and others are C++ managed and unmanaged), and some projects are included in more than one solution (mainly to allow adding references to the project), but I only want to build the project once.

When I build `SolAandB.sln` in VS (I'm using `VS2015` with update 1, but don't believe that changes anything), I can see that the invocation to `csc.exe` for `B.csproj` is as follows (I've added line breaks between arguments for clarity):

```
C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe
/noconfig
/nowarn:1701,1702,2008
/nostdlib+
/platform:anycpu32bitpreferred
/errorreport:prompt
/warn:4
/define:DEBUG;TRACE
/errorendlocation
/preferreduilang:en-US
/highentropyva+
/reference:"F:\Visual Studio 2015\Projects\BuildError\A\bin\Debug\A.dll"
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll"
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll"
/debug+
/debug:full
/filealign:512
/optimize-
/out:obj\Debug\B.exe
/ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset"
/subsystemversion:6.00
/target:exe
/utf8output
Program.cs
Properties\AssemblyInfo.cs
"C:\Users\Fede\AppData\Local\Temp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
obj\Debug\\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
obj\Debug\\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
obj\Debug\\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
```

Note the `/reference:"F:\Visual Studio 2015\Projects\BuildError\A\bin\Debug\A.dll"` which points correctly to the output of `A.csproj` for the selected build configuration. If I had built in `Release`, the reference would have correctly pointed to the release build output of `A.csproj`.

The result of that build depends on having `A.csproj` already built, which I have. You can see a sample build script in `BuildSolutions.proj` which first builds `SolA.sln` and then `SolAandB.sln`.

Now if I build `SolAandB.sln` through the command line, or as a result of building `BuildSolutions.proj`, I can see that the invocation to `csc.exe` for `B.csproj` is as follows:

```
C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe
/noconfig
/nowarn:1701,1702
/nostdlib+
/platform:anycpu32bitpreferred
/errorreport:prompt
/warn:4
/define:DEBUG;TRACE
/highentropyva+
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll"
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll"
/debug+
/debug:full
/filealign:512
/optimize-
/out:obj\Debug\B.exe
/ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset"
/subsystemversion:6.00
/target:exe
/utf8output
Program.cs
Properties\AssemblyInfo.cs
"C:\Users\Fede\AppData\Local\Temp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
```

As you can see, beside several apparently subtle differences, the biggest difference is in that the argument `/reference:"F:\Visual Studio 2015\Projects\BuildError\A\bin\Debug\A.dll"` is missing, and thus the project fails to build with the error `error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)`.

How can I build the solution, in a way that when `csc.exe` is invoked, it's invoked the same way as when I'm building the solution inside visual studio?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.