Disable transitive PackageReference dependency for a specific MsBuild project

本秂侑毒 提交于 2021-02-04 19:36:08

问题


I am migrating an old style MsBuild csproj project to using PackageReference format and have run into a problem with transitive dependencies.

Consider the following Project A reference NuGet package B and C, each containing one single assembly using PackageReference. On Build Project A uses IL merge to incorporate B as public symbols in the A assembly and C as internalized symbols. Project D have a project reference to A.

The transitive dependencies case D to reference A, B and C. When building D, compile errors of the type error CS0433: The type 'X' exists in both 'A' and 'B' occur.

Is there any way to force D to not add an explicit reference to B or C in the scenario above?


回答1:


I ended up using a workaround to move the transitive dependency to an alias to get around the compiler error.

<Target Name="ChangeAliasesOfBNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
  <ItemGroup>
    <ReferencePath Condition="'%(FileName)' == 'B'">
      <Aliases>nonmerged</Aliases>
    </ReferencePath>
  </ItemGroup>
</Target>

I tried to use private assets but couldn't get the compiler error to go away in that way.




回答2:


Disable transitive PackageReference dependency for a specific MsBuild project

If I understand you correct, you can try to use property <PrivateAssets>all</PrivateAssets>or PrivateAssets="all" for the PackageReference. If you have a package that's marked with private assets it merely prevents it from flowing to parent project or getting packed.

<PackageReference Include="B" Version="1.0.0" PrivateAssets="all">
<PackageReference Include="C" Version="1.0.0" PrivateAssets="all">

You can check the document Controlling dependency assets and this thread for some details.

Hope this helps.



来源:https://stackoverflow.com/questions/52207524/disable-transitive-packagereference-dependency-for-a-specific-msbuild-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!