1

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

2
  • Which version of .NET are you using? Which IDE are you using? Commented Oct 18 at 7:57
  • @jraufeisen .NET 9.0 (I've edited the question to add this info). IDE VSCode, but I don't think the IDE has anything to do with this, since I get the error by running dotnet build. Commented Oct 18 at 8:26

1 Answer 1

2

XUnit v3 is indeed not compatible with GlobalPackageReference anymore. See discussion in this Github issue.

As a workaround, you can use Directory.Packages.Props and Directory.Build.Props to provide a global package reference to all your projects.

Directory.Packages.props

<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageVersion Include="xunit.v3" Version="3.1.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />

Directory.Build.props

<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I've made a comment on the issue asking this limitation to be documented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.