PackageReference added under Target is not displayed in Dependency Tree
Visual Studio Version
17.9
Summary
I have a Target which calculates PackageReferences to be added and it runs right before CollectPackageReferences target.
Newly added packages are successfully restored and the project is successfully built on MSBuild and dotnet cli.
But they are not displayed under Dependency Tree.
Here is the code;
<ItemGroup>
<CustomProjectReference Include="..\ProjectName.MaterialBalance\ProjectName.MaterialBalance.csproj" PackageName="ProjectName.MaterialBalance" PackageVersion="$(AllVersions)" BuildConfigurations="Debug,Release,Prod,MaterialBalance"/>
<CustomProjectReference Include="..\ProjectName.Pricing\ProjectName.Pricing.csproj" PackageName="ProjectName.Pricing" PackageVersion="$(AllVersions)" BuildConfigurations="Debug,Release,Prod,Pricing"/>
</ItemGroup>
<Target Name="SetPackageReferences" BeforeTargets="CollectPackageReferences">
<ItemGroup>
<PackageReference Include="@(CustomProjectReference->'%(PackageName)')" Version="%(CustomProjectReference.PackageVersion)" Condition="!$([System.String]::new('%(CustomProjectReference.BuildConfigurations)').Contains('$(Configuration)'))" />
</ItemGroup>
</Target>
The idea is to pull referenced project as package if the active/current build configuration is not specified in BuildConfigurations metadata. Otherwise it will be added as ProjectReference. In this case, MaterialBalance and Pricing should be pulled as package.
As you can see from screenshot of ResolvePackageDependenciesDesignTime target, added package references are presenting;
It presents on Package Manager screen as well;
But it is not displayed under Dependency Tree;
Expected Behavior
Added PackageReference under Target should be displayed on Packages Dependency Tree.
Actual Behavior
Added PackageReference under Target is not displayed on Packages Dependency Tree while the packages are restored and the project is built successfully.
I have provided a repo that the issue is reproduced with minimum steps.
PackageReference items need to be available in project evaluation in order to display in the dependencies tree. You cannot synthesize them in targets, in the way you are doing, and have it work in VS.
What are you trying to achieve with your CustomProjectReference items? Can you use ProjectReference with some metadata instead?
It's unclear what steps we could take here, so closing this. If you can provide more info we can revisit.