Skip to content

MSBuild failed to handle duplicate ProjectReference entries correctly when TargetFrameworks is used #12971

@FrediKats

Description

@FrediKats

Issue Description

MSBuild can't build project if all conditions are met:

  • Project has duplicate ProjectReference entries to another project.
  • Project uses TargetFrameworks instead of TargetFramework.
  • Project references another project with different TargetFramework (net8 vs netstandard)

Steps to Reproduce

dotnet new sln --name ProjectReferencesRepro

# Create projects
dotnet new classlib --name NetStandard --framework netstandard2.0
dotnet new classlib --name SingleTargetWithDuplicates --framework net8.0
dotnet new classlib --name MultipleTargets --framework net8.0
dotnet new classlib --name MultipleTargetWithDuplicates --framework net8.0

# Add projects to solution
dotnet sln add NetStandard/
dotnet sln add SingleTargetWithDuplicates
dotnet sln add MultipleTargets
dotnet sln add MultipleTargetWithDuplicates

# Add project references
dotnet add SingleTargetWithDuplicates/SingleTargetWithDuplicates.csproj reference NetStandard/NetStandard.csproj
dotnet add MultipleTargets/MultipleTargets.csproj reference NetStandard/NetStandard.csproj
dotnet add MultipleTargetWithDuplicates/MultipleTargetWithDuplicates.csproj reference NetStandard/NetStandard.csproj

# Run `dotnet build` to confirm that everything works
dotnet build

# Add duplicate ProjectReference to SingleTargetWithDuplicates.csproj, build should still work
# Change `TargetFramework` to `TargetFrameworks` in MultipleTargets.csproj, build should still work
# Add duplicate ProjectReference to MultipleTargetWithDuplicates.csproj and change `TargetFramework` to `TargetFrameworks`, build should fail
dotnet build

Expected Behavior

Build finished without any error.

Actual Behavior

NetStandard net8.0 failed with 1 error(s) (0.0s)
    C:\Program Files\dotnet\sdk\10.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(266,5): error NETSDK1005: Assets file 'D:\Temp\ProjectReferencesReproduce\NetStandard\obj\project.assets.json' doesn't have a target for 'net8.0'. Ensure that restore has run and that you have included 'net8.0' in the TargetFrameworks for your project.

Workaround

Remove duplicate ProjectReference entries from project files.

Additional notes

I believe that the described problem isn't a common case, but we faced it and spent significant time investigating.
The reported issue isn't evident from the error message and it may also have an impact on other scenarios where TargetFrameworks is used.

Versions & Configurations

λ dotnet --version
10.0.101

λ dotnet msbuild -version
18.0.6.57005

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions