Closed mattleibow closed 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.
We're hitting the same error when trying to unit test the .NET MAUI Community Toolkit.
/cc @nohwnd
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.
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:
[xUnit.net 00:00:00.33] Skipping: OptionalWorkloadsTests (could not find dependent assembly 'Microsoft.Maui.Controls, Version=1.0.0')
dotnet build
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"
}
]
}
}
dotnet test --no-build
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.
@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
.
Thanks, it looks like this fixes it: https://github.com/dotnet/maui/pull/3017
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>
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**"
}
]
}
}
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: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)
Further technical details