it would also appear that it is important to differentiate file dependencies, where the dependency references a dll assembly file and project dependencies (i.e. what I'm asking about), where the dependency references a project and implicitly the output file of that project.
Not really, no.
MSBuild doesn't really care if the reference points to another project in the solution or to a DLL.
If ProjectA
depends on ProjectB
to build ProjectA
ProjectB
must be already built (and up-to-date), MSBuild will then pull its DLL (not its C# code) and link it to ProjectA
.
Adding a project reference instead of a DLL is "syntactic sugar" for your convenience: this way MSBuild knows it must pick the output of the referenced project, whatever the output is.
Otherwise, you'll have to manually pre-build the dependency, find its DLL and link it to the project, repeating the process whenever you switch build configuration, move or rename things. Not really practical.
Will the other two dlls also be copied to the output directory?
If any kind of element from a dependency is used directly from the project where the assembly is referenced, that reference will be copied.
An example could be this solution layout:
- MySolution
- MySolution.ConsoleApplication
- MySolution.FirstDependency
- MySolution.SecondDependency
- MySolution.ThirdDependency
- MySolution.FourthDependency
With this dependency chain:
- MySolution.ConsoleApplication
- MySolution.FirstDependency
- MySolution.SecondDependency
- MySolution.ThirdDependency
- MySolution.FourthDependency
If you build this solution you'll notice that in MySolution.ConsoleApplication
output directory there will be the DLLs for MySolution.FirstDependency
, MySolution.SecondDependency
and MySolution.ThirdDependency
but no DLL for MySolution.FourthDependency
.
Why is it so? When MSBuild builds MySolution.SecondDependency
it notices that there's a dependency declared to MySolution.FourthDependency
, but since it can't find any usage of any kind of element from MySolution.FourthDependency
in MySolution.SecondDependency
code it decides to perform some "optimization" and omits MySolution.FourthDependency
assembly from the output.
This same issue bit me in the past when I added through NuGet AutoMapper to a "deep dependency": adding AutoMapper adds two assembly references, AutoMapper
and AutoMapper.Net4
, where the second assembly is loaded by the first through reflection when it needs to perform certain kind of action on the new collection objects introduced by the .NET Framework 4. Since the second assembly is loaded through reflection MSBuild thinks it's unused and doesn't bother to copy it around.
So, yes, they will be copied as long as you're using them directly and not through reflection.
Is this documented somewhere?
This behavior seems to be a "feature" of MSBuild, I managed to find a blog post by some folks from Microsoft back when I experienced this issue, but I can't find it again at the moment.