Skip to content

xUnit tests may not be discovered now that the .NET SDK trims packages from deps.json when they don't have any assets #49248

Closed

Description

@dsplaisted

The changes in #46218 can break xUnit tests, causing them to not be discovered at all.

This was found and documented here: dotnet/aspnetcore#62223

The issue is that xUnit looks for libraries nanmed "xunit" or "xunit.core" in the deps.json when trying to figure out whether an assembly is an xUnit test assembly. The xunit.core package doesn't include any runtime assets, only MSBuild .props and .targets files that are imported. This means that it may be removed from the deps.json file.

The xunit.core package depends on xunit.extensibility.core and xunit.extensibility.execution, both of which do have runtime assets. So if there are not references to those packages via another path, then the SDK will keep xunit.core in the deps.json. However, if there are other libraries that depend on xunit.extensibility.core and xunit.extensibility.execution, then the SDK will drop xunit.core from the deps.json and xUnit won't discover any tests.

Activity

  1. added
    untriaged
    on Jun 4, 2025
  2. dsplaisted commented on Jun 4, 2025

    @dsplaisted
    MemberAuthor

    Some options for fixing:

    • Revert Include only PackageReferences included at runtime in deps.json #46218
    • Add the ability to opt in or out of the new behavior
    • Hardcode "xunit.core" in DependencyContextBuilder to prevent it from being dropped
    • Change the graph algorithm in DependencyContextBuilder so that we don't drop libraries with transitive dependencies on runtime assets, even if those runtime assets are referred to via a different path.
  3. dsplaisted commented on Jun 4, 2025

    @dsplaisted
    MemberAuthor

    @ericstj @jasonmalinowski @ViktorHofer @agocke FYI, The most recent deps.json changes (#46218) have the potential to prevent xUnit from discovering any tests. See above.

    Feedback is welcome on how we should address this, I lean towards the last option.

  4. baronfel commented on Jun 4, 2025

    @baronfel
    Member

    Another option for fixing would be to make changes to the xunit detection to be more flexible - I don't know that we have any support around the schema or structure of this file so I'm not sure we should encourage the use of it in external libraries.

  5. ViktorHofer commented on Jun 4, 2025

    @ViktorHofer
    Member

    Yes, the last option sounds best to me.

  6. Youssef1313 commented on Jun 4, 2025

    @Youssef1313
    Member
  7. Youssef1313 commented on Jun 4, 2025

    @Youssef1313
    Member

    Another option for fixing would be to make changes to the xunit detection to be more flexible - I don't know that we have any support around the schema or structure of this file so I'm not sure we should encourage the use of it in external libraries.

    There is a risk with that if the user didn't notice their tests are not running and they didn't update the xunit dependency though. The failure here can be silent in many cases, as seen in aspnetcore repo.

  8. baronfel commented on Jun 4, 2025

    @baronfel
    Member

    @Youssef1313 I tend to agree with you, just mentioning that option for completeness 👍

  9. bradwilson commented on Jun 4, 2025

    @bradwilson
    Contributor

    The code you linked is no longer current. Have you tried using a 3.x build of xunit.runner.visualstudio? Those use xunit.v3.runner.utility, which relies on Cecil to do the assembly probing rather than looking for the .deps.json file: https://github.com/xunit/xunit/blob/0024bf168314d7f5db0ee07da0c517f7bd377ca9/src/xunit.v3.runner.common/Utility/AssemblyUtility.cs#L41-L47

    (And yes, the 3.x builds of xunit.runner.visualstudio are still able to run both v1 and v2 test projects, in addition to adding support for v3 test projects.)

  10. Youssef1313 commented on Jun 4, 2025

    @Youssef1313
    Member

    Updating aspnetcore to xunit.runner.visualstudio 3.x in dotnet/aspnetcore#62250. This issue is still relevant though for users of xunit.runner.visualstudio 2.x

  11. bradwilson commented on Jun 4, 2025

    @bradwilson
    Contributor

    This issue is still relevant though for users of xunit.runner.visualstudio 2.x

    Is there any reason anybody should be stuck on this old version? If there are compatibility issues, we'd like to know about them. We currently have no plans to release any more 2.x builds of xunit.runner.visualstudio, so we would generally call this issue "won't fix", since there exists a fairly simple workaround (use a current build).

  12. bradwilson commented on Jun 4, 2025

    @bradwilson
    Contributor

    (I'm aware of the one potentially significant issues: net472 minimum. Unfortunately, the fact that netstandard2.0 can be broken on .NET Framework < 4.7.2 mandates this change for us.)

  13. dsplaisted commented on Jun 5, 2025

    @dsplaisted
    MemberAuthor

    Given that it seems like a workaround for the issue is to update to v3 of xunit.runner.visualstudio, I'm now leaning towards adressing this by adding a property to opt out of the new behavior where libraries without runtime assets are trimmed from the deps.json.

    That means this issue could still be hit but it sounds like it may not have a huge impact, and if it did affect you, you could work around it by updating the xunit package or by setting a property to revert to the old behavior.

    Feedback welcome.

  14. Youssef1313 commented on Jun 5, 2025

    @Youssef1313
    Member

    and if it did affect you, you could work around it by updating the xunit package or by setting a property to revert to the old behavior.

    My problem here is "how" it is affecting me. It could be silently causing my tests to not run, and I could notice very late. A week of silent failures have already introduced few failing tests in aspnetcore repo. I wouldn't mind the breaking change here if it was obvious (i.e, the user is getting some error message and he will look for how to fix it)

  15. Evangelink commented on Jun 5, 2025

    @Evangelink
    Member

    IMO we should either:

    • list the breaking change on .NET 10 announcements and ask @bradwilson if he can update his documentation to mention this issue. (that would be my pick)
    • undo the breaking change

    I am failing to see how the opt-out mechanism will help because I won't notice my tests are not run and when I notice, I can update to the new xunit runner that is not affected by the error.

    Also note that this is affecting xunit but this could affect other libraries that are relying on the same behavior hence being explicit and calling out the "break" in .NET 10 SDK notes seems the best to me.

  16. bradwilson commented on Jun 5, 2025

    @bradwilson
    Contributor

    I could, in theory, replace the .deps.json code with Cecil code (like from v3) but it wouldn't help people realize they need to upgrade (and it would only be for people stuck on .NET Framework < 4.7.2, which I suppose if you're sticking with a 9 year old framework and don't move on, you're probably not upgrading all your dependencies on a regular basis 😂).

  17. bradwilson commented on Jun 5, 2025

    @bradwilson
    Contributor

    I also don't think we're going to document our way out of a problem that isn't obvious to begin with.

  18. ericstj commented on Jun 5, 2025

    @ericstj
    Member

    Opt-out might be a nice thing to have in the back pocket in case we have anyone else who hits this. I do think updating the runner is a reasonable thing to do since the new runner is backward compatible with old tests.

    Just do double-check here - the problem isn't that xunit needs the app to list assemblies that it doesn't deploy, right? It's only these empty packages that it probes for?

    Is there anything we can see in what Xunit is doing here and try to extrapolate that to a pattern we can search for to understand if others might do it?

  19. bradwilson commented on Jun 5, 2025

    @bradwilson
    Contributor

    It's only these empty packages that it probes for?

    Right, it looks for those package references rather than the assemblies, because at least with v2, adding package xunit.core means a unit test vs. adding package xunit.extensibility.core which means an extension project.

    FWIW, checking for assembly references is what we do in xunit.v3.runner.utility with Cecil, since we couldn't check for NuGet packages, so moving to that in a new 2.x release of xunit.runner.visualstudio would be parallel functionally, and the only reason to do that would be so people could continue to test projects linked with .NET Framework 4.6.2, 4.7, and 4.7.1.

  20. dsplaisted commented on Jun 6, 2025

    @dsplaisted
    MemberAuthor

    Thanks for the feedback so far folks. Here's what I'm currently planning to do:

    • Special case "xunit.core" so that it's never trimmed from the deps.json file
    • Add an opt-out property to disable the new deps.json trimming logic entirely.

    With these changes we won't break xUnit v2, and if there are other tools out there that rely on deps.json in a similar way there will be a workaround of setting a property to avoid the trimming.

    We'll also add a breaking change notice to .NET 10 so that the change is documented.

  21. added a commit that references this issue on Jun 8, 2025
  22. removed
    untriaged
    on Jun 9, 2025
  23. added a commit that references this issue on Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions