What does the Private setting do on a ProjectReference in a MSBuild project file?
Asked Answered
K

2

173

I saw this in a project file the other day:

<ProjectReference Include="Foo\Bar\Baz.csproj">
    <Project>{A GUID HERE}</Project>
    <Name>Baz</Name>
    <Private>False</Private> <!-- ??? -->
    <ReferenceOutputAssembly>False</ReferenceOutputAssembly>
</ProjectReference>

Every node in a ProjectReference appears to be self explanatory (the referenced project file, GUID, name to show in the solution explorer, and whether or not the current project should link to the referenced project) except Private, and the Common MSBuild Project Items page doesn't document this value. (There's a Private setting documented for Reference rather than ProjectReference -- but it has Never, Always, and PreserveNewest settings, not true and false)

What does this setting do?

Kiger answered 7/10, 2014 at 18:38 Comment(4)
As far as MSBuild is concerned, ProjectReference is an item group (i.e., list) and Private is item metadata for the included item. The answer to your question lies in what any includes do with it. In more general terms, what specific type of project is it? Maybe tag your question with csharp.Buxton
I meant "Imports" not "includes".Buxton
@malexander: I think your answer was good if you'd undelete it...Kiger
@Tom: Sure, strictly speaking that's true. On the other hand, the ProjectReference item is recognized by (at least) the C# and C++ MSBuild supporting infrastructure; it looks like it is handled mostly in the Microsoft.Common.CurrentVersion.targets file.Kiger
S
175

Private metadata on a ProjectReference item corresponds to the "Copy Local" property on the reference node in Visual Studio's Solution Explorer.

It controls whether the reference should be copied to the output folder or not:

  • true means the reference should be copied
  • false means the reference should NOT be copied

This is documented in Common MSBuild project items, as well as the MSBuild source itself in Microsoft.Common.CurrentVersion.targets:

<!--
============================================================

                  ResolveAssemblyReferences

Given the list of assemblies, find the closure of all assemblies that they depend on. These are
what we need to copy to the output directory.

    [IN]
    @(Reference) - List of assembly references as fusion names.
    @(_ResolvedProjectReferencePaths) - List of project references produced by projects that this project depends on.

        The 'Private' attribute on the reference corresponds to the Copy Local flag in IDE.
        The 'Private' flag can have three possible values:
            - 'True' means the reference should be Copied Local
            - 'False' means the reference should not be Copied Local
            - [Missing] means this task will decide whether to treat this reference as CopyLocal or not.

    [OUT]
    @(ReferencePath) - Paths to resolved primary files.
    @(ReferenceDependencyPaths) - Paths to resolved dependency files.
    @(_ReferenceRelatedPaths) - Paths to .xmls and .pdbs.
    @(ReferenceSatellitePaths) - Paths to satellites.
    @(_ReferenceSerializationAssemblyPaths) - Paths to XML serialization assemblies created by sgen.
    @(_ReferenceScatterPaths) - Paths to scatter files.
    @(ReferenceCopyLocalPaths) - Paths to files that should be copied to the local directory.

============================================================
-->
Seniority answered 5/4, 2015 at 20:30 Comment(8)
If the <Private> is missing, then it's not equivalent to True. Search for "MSBuild CopyLocal bug". E.g. see stackoverflow.com/questions/1132243Grouper
@xmedeko, That's correct. I'm not sure where @GPR got "If it is absent then the default value of True is assumed" since the answer explicitly says "[Missing] means this task will decide whether to treat this reference as CopyLocal or not". Most of the logic is in msbuild\Reference.cs:949Seniority
Is it possible that even if <Private> is set to True, MSBuild still does not include the reference in the Output if its not used by the application? This is the current behavior that I am getting locally...Austen
@Ninja, this most frequently happens if MSBuild cannot locate the referenced assembly. If it is not directly used by the code, it may still compile successfully. You can troubleshoot with procmon or MSBuild detailed loggingSeniority
@Seniority Ah i see. Because this is what I am seeing, strange behavior... My build is succeeding but I don't see the DLL even when I set <Private> to True... I was thinking that this could be another MSBuild Optimization? I am not 100% sure its not used by the code - but it is just a guess since the build succeeds...Austen
@Ninja, My bet is still on the assembly not being directly used or found. MSBuild logging should be sufficient to identify which. Feel free to open a question specific to the issue and link it here.Seniority
Mitch, in answer to your point I'm not sure where @Mayapple got "If it is absent then the default value of True is assumed" since the answer explicitly says "[Missing] means this task will decide whether to treat this reference as CopyLocal or not". ...I just tried it out on a few references, and it always seemed to come out that way. You are, however, correct in pointing out that the actual logic when missing is somewhat more complex and if true is wanted, then explicit setting should be used, and leaving it out should not be relied apon.Mayapple
"These are my private files ... copy them all you like"Peta
D
2

I want just to state, that <Private>false</Private> (which you can apply to ProjectReferences) may not work when using <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Publish" Properties="$(_MSBuildProperties)" /> and project $(MSBuildProjectFullPath) have ProjectReferences that have <None><CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory></None> . I've read the source code around https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets and found the solution. You need to define _GetChildProjectCopyToPublishDirectoryItems=false so an example would be: <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Publish" Properties="TargetFramework=$(TargetFramework);_GetChildProjectCopyToPublishDirectoryItems=false" />

Decimalize answered 6/8, 2020 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.