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

Visual StudioMsbuild

Visual Studio Problem Overview


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?

Visual Studio Solutions


Solution 1 - Visual Studio

The Private tag maintains the user-override to the "Copy Local" checkbox in the Visual Studio References folder. This controls whether the reference is used from the GAC or whether it will copy the referenced assembly to the build directory.

While I cannot find any MSDN documentation to this effect (quelle surprise), it is evident from behavior and from the comment in Microsoft.Common.CurrentVersion.targets:1742 where it is applied:

This is documented in MSDN > Common MSBuild project items, and is evident from behavior and from the comment in Microsoft.Common.CurrentVersion.targets:1742 where it is applied:

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

                                        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.
    ============================================================
    -->

Solution 2 - Visual Studio

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBilly ONealView Question on Stackoverflow
Solution 1 - Visual StudioMitchView Answer on Stackoverflow
Solution 2 - Visual StudioTeronekoView Answer on Stackoverflow