I have the following project structure (the full repo can be found at https://github.com/asarkar/functional-csharp-buonanno)
root
├── Directory.Packages.props
├── src
│ └── Proj1
│ └── Proj1.csproj
└── tests
├── Directory.Packages.props
└── Proj1.Tests
├── Proj1.Tests.csproj
└── Proj1Tests.cs
root/Directory.Packages.props
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>
root/tests/Directory.Packages.props
<Project>
<!-- Import the root Directory.Packages.props file -->
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Packages.props, $(MSBuildThisFileDirectory)..))" />
<ItemGroup>
<!-- Global test packages for all test projects -->
<GlobalPackageReference Include="xunit.v3" Version="3.1.0" />
<GlobalPackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
<GlobalPackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
</ItemGroup>
</Project>
Proj1.Tests.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Proj1/Proj1.csproj" />
</ItemGroup>
</Project>
But when I run dotnet build/test, I get the following error. Why?
For reference, I am using .NET 9.
/home/.nuget/packages/xunit.v3.core/3.1.0/_content/DefaultRunnerReporters.cs(3,20): error CS0400: The type or namespace name 'Xunit' could not be found in the global namespace (are you missing an assembly reference?)
I asked ChatGPT, and it said:
xUnit v3 (the new major version) changed the way its assemblies are structured. GlobalPackageReference restores the package, but does not automatically reference the assemblies in the project. In other words, the compiler sees the NuGet package folder, but your project doesn’t actually reference the xunit.core assembly — so using Xunit fails.
Why it worked with v2
With xunit v2.*, the assemblies were structured so that GlobalPackageReference was enough for test projects, because the SDK implicitly added references from the package. v3 separates packages (xunit.core, xunit.abstractions, xunit.runner.visualstudio) and doesn’t wire up references automatically, even under CPM.
It recommends a fix as I myself figured out by trial-and-error. I don't know about .NET enough to assess whether ChatGPT is telling the truth or not.
Using PackageVersion in tests/Directory.Packages.props (with version) and PackageReference in tests/Directory.Build.props (without version) seems to work, but that still requires listing the packages twice.
Reference: https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management
dotnet build.