dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.68k stars 1.06k forks source link

Running `dotnet test` on a project that uses optional workloads can't find them #21845

Closed mattleibow closed 2 years ago

mattleibow commented 3 years ago

Describe the bug

Running dotnet test on a project that has an optional workload (or maybe we got a bad workload) results in an error:

Testhost process exited with error: It was not possible to find any compatible framework version 
The framework 'Microsoft.Maui.Core', version '**FromWorkload**' (x64) was not found. 

If I set an explicit version, the error is the same - just with **FromWorkload** replaced with <version>.

I get the same error in Visual Studio when running tests in the Test Explorer.

To Reproduce

The repo: https://github.com/mattleibow/OptionalWorkloadsTests The run: https://github.com/mattleibow/OptionalWorkloadsTests/runs/3828827544

Exceptions (if any)

Test run for D:\a\OptionalWorkloadsTests\OptionalWorkloadsTests\OptionalWorkloadsTests\bin\Debug\net6.0\OptionalWorkloadsTests.dll (.NETCoreApp,Version=v6.0) 
Microsoft (R) Test Execution Command Line Tool Version 17.0.0 
Copyright (c) Microsoft Corporation. All rights reserved. 

Starting test execution, please wait... 
A total of 1 test files matched the specified pattern. 
Testhost process exited with error: It was not possible to find any compatible framework version 
The framework 'Microsoft.Maui.Core', version '**FromWorkload**' (x64) was not found. 
 - No frameworks were found. 
You can resolve the problem by installing the specified framework and/or SDK. 
The specified framework can be found at: 
 - aka.ms/dotnet-core-applaunch?framework=Microsoft.Maui.Core&framework_version=6.0.101-preview.9.1843&arch=x64&rid=win10-x64. Please check the diagnostic logs for more information. 

Test Run Aborted.

Further technical details

> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.2.21505.1
 Commit:    a151058157

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22468
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.2.21505.1\

Host (useful for support):
  Version: 6.0.0-rc.2.21474.18
  Commit:  d1c6659c2d

.NET SDKs installed:
  2.1.816 [C:\Program Files\dotnet\sdk]
  3.1.411 [C:\Program Files\dotnet\sdk]
  5.0.205 [C:\Program Files\dotnet\sdk]
  5.0.302 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21458.32 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21463.6 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.2.21478.25 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.2.21505.1 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.2.21470.37 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.2.21475.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.7.21377.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21470.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21474.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.7.21378.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21470.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21475.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
drasticactions commented 2 years ago

We're hitting this issue trying to come up with Component tests for XAML Hot Reload for MAUI. If I try to reference specific versions of MAUI using the MauiVersion tag and run it through a test runner I get the same error.

brminnick commented 2 years ago

We're hitting the same error when trying to unit test the .NET MAUI Community Toolkit.

richlander commented 2 years ago

/cc @nohwnd

nohwnd commented 2 years ago

This is caused by the runtimeconfig.json content. We pick that up and pass that to testhost when we start it to ensure the testhost is using the same target framework as the test project.

The content is this:

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0-rc.2.21480.5"
      },
      {
        "name": "Microsoft.Maui.Core",
        "version": "**FromWorkload**"
      },
      {
        "name": "Microsoft.Maui.Controls",
        "version": "**FromWorkload**"
      },
      {
        "name": "Microsoft.Maui.Essentials",
        "version": "**FromWorkload**"
      }
    ]
  }
}

If you remove the Maui part and run without build it should succeed dotnet test --no-build. At least it did on the OptionalWorkload project above:

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0-rc.2.21480.5"
      }
    ]
  }
}

Not a great solution, but you could possibly add a post build step that will rewrite your runtimeconfig.json for your test until we have a more systematic solution.

brminnick commented 2 years ago

Thanks Jakub! After updating the reproduction sample to include a unit test referencing a .NET Workload (in this case, the workload is .NET MAUI), we found that removing the MAUI references from runtimeconfig.json doesn't solve the problem.

Removing the .NET MAUI references from runtimeconfig.json gives a new error:

Error

[xUnit.net 00:00:00.33] Skipping: OptionalWorkloadsTests (could not find dependent assembly 'Microsoft.Maui.Controls, Version=1.0.0')

Reproduction Steps

  1. Download/Clone the reproduction sample
  2. run dotnet build
  3. Remove the MAUI references in OptionalWorkloadsTests/bin/Debug/net6.0/OptionalWorkloadsTests.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net6.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0-rc.2.21480.5"
      }
    ]
  }
}
  1. run dotnet test --no-build image
nohwnd commented 2 years ago

Thanks, I can repro the same thing in a console app, just running with dotnet run (and also in mstest and xunit). This is still a maui issue / optional workloads issue, not a dotnet test issue.

The dependency Microsoft.Maui.Controls is not stated in .deps.json, and I guess it should come from the additional frameworks. But because we are removing those additional frameworks, and are not able to resolve them correctly we don't get the dependency.

To repro this, you can circumvent all the dotnet test and xunit infrastructure by adding:

        <GenerateProgramFile>false</GenerateProgramFile>

To your csproj.

And defining main, where you call your test method directly:

    public class Program {
        public static void Main () { 

            System.Console.WriteLine("Hello");
            new MauiTests().ShadowInitializesCorrectly();
            System.Console.WriteLine("Bye");

        }
    }

And running it with:

dotnet build 

# edit the runtimeconfig.json

dotnet run --project "OptionalWorkloadsTests\OptionalWorkloadsTests.csproj" --no-build

If this does not run, then dotnet test won't run because it is doing (almost) the same thing.

Hello
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
   at OptionalWorkloadsTests.MauiTests..ctor()
   at OptionalWorkloadsTests.Program.Main() in C:\p\OptionalWorkloadsTests\OptionalWorkloadsTests\MauiTests.cs:line 43

I'll be happy to help you with issues of dotnet test, but this needs to be addressed first.

dsplaisted commented 2 years ago

@jonathanpeppers I think a simplified version of this issue is that a console app targeting net6.0 and using UseMaui won't run.

I would guess that the solution would involve setting RuntimePackAlwaysCopyLocal to true on the KnownFrameworkReference or KnownRuntimePack.

jonathanpeppers commented 2 years ago

Thanks, it looks like this fixes it: https://github.com/dotnet/maui/pull/3017

rmarinho commented 2 years ago

I m having the same issue trying to add a MAUI to a WPF project.. in theory it should work ..

Here's a example

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <UseMaui>true</UseMaui>
    <EnableDefaultXamlItems>false</EnableDefaultXamlItems>
  </PropertyGroup>

</Project>
image

the runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0"
      },
      {
        "name": "Microsoft.WindowsDesktop.App",
        "version": "6.0.0"
      },
      {
        "name": "Microsoft.Maui.Core",
        "version": "**FromWorkload**"
      },
      {
        "name": "Microsoft.Maui.Controls",
        "version": "**FromWorkload**"
      },
      {
        "name": "Microsoft.Maui.Essentials",
        "version": "**FromWorkload**"
      }
    ]
  }
}