Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use documented item group in FindInvalidProjectReferences #10220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jkoritzinsky
Copy link
Member

Context

Today, GetReferenceTargetPlatformMonikers and FindInvalidProjectReferences use TargetPathWithTargetPlatformMoniker instead of the documented _ProjectReferenceTargetPlatformMonikers item group for the project reference gathering/validation.

This causes a violation in the ProjectReference protocol, leading to multiple items being returned from GetTargetPath and the default (Build) target. Returning multiple items from these targets can cause usability problems and unexpected behaviors.

Below is a simplified use case of the one that hit this bug:

A/A.csproj:

<Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
          <TargetFramework>net8.0</TargetFramework>
      </PropertyGroup>
</Project>

B/B.csproj:

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
       <FindInvalidProjectReferences>true</FindInvalidProjectReferences>
       <TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
       <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    </PropertyGroup>
   <ItemGroup>
       <ProjectReference Include="../A/A.csproj" />
   </ItemGroup>
</Project>

C/C.csproj

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
       <OutputType>Exe</OutputType>
       <TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
       <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    </PropertyGroup>
   <ItemGroup>
       <ProjectReference Include="../A/A.csproj" Aliases="A" />
       <ProjectReference Include="../B/B.csproj" Aliases="B" />
   </ItemGroup>
</Project>

C/Program.cs

extern alias A; // Csc errors here with "alias not found"
                       // because the reference to B.csproj added items for A.dll and B.dll with the B alias metadata
                       // which overrode the item returned by A.csproj, the A.dll with the A alias metadata
Console.WriteLine("Hello world");

Changes Made

Use the documented item group instead of reusing TargetPathWithTargetPlatformMoniker.

Testing

Local hacking. I'd like to add an end-to-end test, but I'm not sure where is the best place to do so here.

Notes

This was discovered in a project that uses the Microsoft.WindowsAppSdk, which sets FindInvalidProjectReferences to true in some scenarios.

The user was able to work around this bug by changing the order of the ProjectReference items in their equivalent of C.csproj.

Use the documented item group instead of reusing `TargetPathWithTargetPlatformMoniker`.
@Sergio0694
Copy link

For context, I hit this in Sergio0694/ComputeSharp#813, and it completely broke my build due to multiple projects failing due to this issue (in one case due to missing/incorrect reference aliases, in another case because ReferenceOutputAssembly was being ignored). Special thanks to Jeremy for investigating this and both figuring out a workaround as well as creating this PR 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants