dotnet / project-system

The .NET Project System for Visual Studio
MIT License
959 stars 386 forks source link

PackageReference added under Target is not displayed in Dependency Tree #9415

Open kabaogluemre opened 3 months ago

kabaogluemre commented 3 months ago

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 MSBuildand 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 BuildConfigurationsmetadata. Otherwise it will be added as ProjectReference. In this case, MaterialBalanceand Pricingshould be pulled as package.

As you can see from screenshot of ResolvePackageDependenciesDesignTime target, added package references are presenting; Capture

It presents on Package Manager screen as well; Capture2

But it is not displayed under Dependency Tree; Capture4

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.

drewnoakes commented 3 months ago

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?