msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Public reference from project reference is copied twice when included with a Link

Open SeSamSeg opened this issue 1 year ago • 3 comments

Issue Description

A library included as an item with a LinkBase, is copied twice if it is also referenced with Private=False in a project reference. It is copied both to the root of the output directory as to the Link path.

Ratioanle: I have a library that uses Private=False references as these need to be referenced on runtime by the already installed libraries of another application. I have a Console application that references this library. In here, I want to copy these external references to a folder, so they are not automatically linked when running and I can decide on runtime to either use these copies or the ones that are already installed . I also have other projects, using this LibUsingExternalLib library, that don't need the copy to the folder. One of them is packaged with the external app

Steps to Reproduce

ConsoleApplication:

  <ItemGroup>
    <Content Include="..\Libs\*" LinkBase="libs\">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\LibUsingExternalLib\LibUsingExternalLib.csproj" />
  </ItemGroup>

LibUsingExternalLib:

  <ItemGroup>
    <Reference Include="SomeExternalLib">
      <HintPath>..\Libs\SomeExternalLib.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>

PublicReferenceFromProjectReference.zip

Expected Behavior

Only have the reference in the libs\ folder

Actual Behavior

The reference is both in the root of the output directory as in the libs\ folder

Analysis

It is the expected behavior if I:

  • also add a Private=False reference to SomeExternalLib in ConsoleApplication or
  • add the include content to LibUsingExternalLib instead of ConsoleApplication

So it seems include items that are referenced are somehow also added to the output folder, but the Private=False is somehow lost?

Versions & Configurations

No response

SeSamSeg avatar Jan 23 '25 10:01 SeSamSeg

By removing the reference to SomeExternalLib with Private=False in the project ConsoleApplication provided in the Steps to Reproduce, the issue was reproduced. Here is the binlog Rerpo.zip.

Since the reference SomeExternalLib in the project LibUsingExternalLib is set with Private=False, it is expected that the assembly SomeExternalLib.dll will not be copied to the root of ConsoleApplication's output directory. Actual result was the opposite. The content <Content Include="..\Libs\*" LinkBase="libs\"> in the ConsoleApplication caused this behavior. According to https://github.com/dotnet/msbuild/blob/04b6e1b59df1004aeedc4703757a5c407c06c139/src/Tasks/Microsoft.Common.CurrentVersion.targets#L2431, CandidateAssemblyFilesResolver included the assembly ..\Libs\SomeExternalLib.dll and would resolve the ConsoleApplication's transitive reference SomeExternalLib with the path from the content. It led to enter into if branch in the following code https://github.com/dotnet/msbuild/blob/04b6e1b59df1004aeedc4703757a5c407c06c139/src/Tasks/AssemblyDependency/ReferenceTable.cs#L1333-L1355, rather than else branch that would set the reference's property IsUnresolvable to be true and eventually set its CopyLocal state with NoBecauseUnresolved https://github.com/dotnet/msbuild/blob/04b6e1b59df1004aeedc4703757a5c407c06c139/src/Tasks/AssemblyDependency/Reference.cs#L972-L976. It's not reasonable to remove the item Content from the parameter CandidateAssemblyFiles of task ResolveAssemblyReference.

@rainersigwald do you have any idea to fix it?

GangWang01 avatar Feb 12 '25 10:02 GangWang01

@rainersigwald what do you think about it?

OvesN avatar May 13 '26 09:05 OvesN

Summary

Private=false never propagates across a project boundary, so downstream projects only avoid the copy by accident. So this is by design but unexpected.

Workaround

Set AssemblySearchPath_UseCandidateAssemblyFiles=false, or add  Private=False reference  to  ConsoleApplication too, or move the  Content  include into LibUsingExternalLib.

Potential fix

Do not force copy-local for a dependency resolved solely via {CandidateAssemblyFiles}. There is a risk to the fix, as someone might rely on RAR copying a loose Content DLL to the output root. @rainersigwald do you think implementing this fix is worth it?

AlesProkop avatar Jul 07 '26 11:07 AlesProkop