WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

dotnet build fails when WindowsAppSDK is referenced in console application.

Open ianeuman opened this issue 2 years ago • 12 comments
trafficstars

Describe the bug

Building a blank console application with .NET 6 through dotnet cli dotnet build fails with the following error.

MSBuild version 17.5.0+6f08c67f3 for .NET
  Determining projects to restore...
  Restored D:\Repos\WindowsAppSDKDotnet\WindowsAppSDKDotnetConsole\WindowsAppSDKDotnetConsole.csproj (in 216 m
  s).
C:\Users\user\.nuget\packages\microsoft.windowsappsdk\1.2.230313.1\buildTransitive\MrtCore.PriGen.targets(911,5): e
rror MSB4062: The "Microsoft.Build.Packaging.Pri.Tasks.ExpandPriContent" task could not be loaded from the assembly C:\
Program Files\dotnet\sdk\7.0.202\\Microsoft\VisualStudio\v17.0\AppxPackage\\Microsoft.Build.Packaging.Pri.Tasks.dll. Co
uld not load file or assembly 'C:\Program Files\dotnet\sdk\7.0.202\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.B
uild.Packaging.Pri.Tasks.dll'. The system cannot find the path specified. Confirm that the <UsingTask> declaration is c
orrect, that the assembly and all its dependencies are available, and that the task contains a public class that implem
ents Microsoft.Build.Framework.ITask. [D:\Repos\WindowsAppSDKDotnet\WindowsAppSDKDotnetConsole\WindowsAppSDKDo
tnetConsole.csproj]

Build FAILED.

C:\Users\user\.nuget\packages\microsoft.windowsappsdk\1.2.230313.1\buildTransitive\MrtCore.PriGen.targets(911,5): e
rror MSB4062: The "Microsoft.Build.Packaging.Pri.Tasks.ExpandPriContent" task could not be loaded from the assembly C:\
Program Files\dotnet\sdk\7.0.202\\Microsoft\VisualStudio\v17.0\AppxPackage\\Microsoft.Build.Packaging.Pri.Tasks.dll. Co
uld not load file or assembly 'C:\Program Files\dotnet\sdk\7.0.202\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.B
uild.Packaging.Pri.Tasks.dll'. The system cannot find the path specified. Confirm that the <UsingTask> declaration is c
orrect, that the assembly and all its dependencies are available, and that the task contains a public class that implem
ents Microsoft.Build.Framework.ITask. [D:\Repos\WindowsAppSDKDotnet\WindowsAppSDKDotnetConsole\WindowsAppSDKDo
tnetConsole.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.38

Note: I tried with both latest and the preview version. 1.2.230313.1 1.3.230228005-preview1

Steps to reproduce the bug

  1. Create new console application project through visual studio.
  2. Add WindowsAppSDK nuget package as a dependency.
  3. Open the location for the project through powershell.
  4. Build the project through 'dotnet build'.
  5. The build fails.

Expected behavior

The build succeeds.

Screenshots

No response

NuGet package version

Windows App SDK 1.2.5: 1.2.230313.1

Packaging type

Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

ianeuman avatar Mar 30 '23 00:03 ianeuman

Yeah, this is weird. From what I can tell while examining the targets files, the modern (dotnet build-compatible) resource packaging code is only enabled if MSIX packaging is turned on. Otherwise, the build falls back to a different file that assumes it is being built through Visual Studio. To fix this, you need to define a EnableCoreMrtTooling property with the value false. However, this disables the other (non-MSIX) code that makes creating PRI files possible. I don’t know of any middle ground. Hope this helps!

wjk avatar May 09 '23 00:05 wjk

I set following property in the winui project and it compiles now in visual studio and on the dotnet build command line. <EnableCoreMrtTooling Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">false</EnableCoreMrtTooling> as a workaround for now

inforithmics avatar May 28 '23 09:05 inforithmics

~~I have the same problem, I assume. I get this error during a Github action:~~

C:\Users\runneradmin\.nuget\packages\microsoft.windowsappsdk\1.1.3\buildTransitive\MrtCore.PriGen.targets(911,5): error MSB4062: The "Microsoft.Build.Packaging.Pri.Tasks.ExpandPriContent" task could not be loaded from the assembly C:\Program Files\dotnet\sdk\7.0.302\\Microsoft\VisualStudio\v17.0\AppxPackage\\Microsoft.Build.Packaging.Pri.Tasks.dll. Could not load file or assembly 'C:\Program Files\dotnet\sdk\7.0.302\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.Build.Packaging.Pri.Tasks.dll'. The system cannot find the path specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

~~I've added the EnableCoreMrtTooling-Property to the Windows-WinUI-Head, but the error seems to happen in a Uno-Cross-Platform library. Any hints on how to overcome this issue?~~

Just found out that adding the property as provided by @inforithmics to the other project solves the issue in my end. Thank you!

TopperDEL avatar May 31 '23 06:05 TopperDEL

I can confirm that it works on my projects too, thanks for the help! Is this the expected workaround? And if so, could we improve the documentation around this?

ianeuman avatar Jul 07 '23 00:07 ianeuman

Had the same issue as in I couldn't build the standalone UI project and added @inforithmics workaround and confirm it works - getting build errors though but at least it's trying to build now!

~But if I try to build my class-library which references the UI project I get an almost same error as @ianeuman :~

%USERPROFILE%\.nuget\packages\microsoft.windowsappsdk\1.3.230602002\buildTransitive\MrtCore.PriGen.targets(380,5): error MSB4062: The "Microsoft.Build.AppxPackage.GetSdkFileFullPath" task could not be loade
d from the assembly %PROGRAM_FILES%\dotnet\sdk\8.0.100-preview.7.23376.3\\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.Build.AppxPackage.dll. Could not load file or assembly '%PROGRAM_FILES%\dotnet\s 
dk\8.0.100-preview.7.23376.3\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.Build.AppxPackage.dll'. Het systeem kan het opgegeven pad niet vinden. Confirm that the <UsingTask> declaration is correct, tha 
t the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [%PROJECT_PATH%\Library 
.csproj]

~Anyone know how to solve this?~

EDIT: Removed namespace error and added error corresponding to the issue EDIT2: Can be solved by: https://github.com/dotnet/maui/issues/5886

ThaDaVos avatar Sep 07 '23 07:09 ThaDaVos

<EnableCoreMrtTooling Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">false</EnableCoreMrtTooling>

This works. Thanks @inforithmics

abdes avatar Jan 29 '24 12:01 abdes

It is important to note that setting EnableCoreMrtTooling to false does not produce valid class libraries layouts when the project contains XAML files.

When disabled, MrtCore.targets is not included. While the app builds, XAML files are not retargeted properly to be placed in an AssemblyName folder in the final app layout, causing any ms-appx:// resolution to fail.

jeromelaban avatar Mar 20 '24 14:03 jeromelaban

It is important to note that setting EnableCoreMrtTooling to false does not produce valid class libraries layouts.

When disabled, MrtCore.targets is not included. While the app builds, XAML files are not retargeted properly to be placed in an AssemblyName folder in the final app layout, causing any ms-appx:// resolution to fail.

Thanks for the clarification. Does that matter if the library has no XAML files (i.e WinAppSdk is being used but no XAML)?

abdes avatar Mar 20 '24 17:03 abdes

@abdes only if there are XAML files indeed, I've adjusted my comment above.

jeromelaban avatar Mar 21 '24 15:03 jeromelaban

I resolved the PRI DLL issue by modifying the targets file like this %userprofile%\.nuget\packages\microsoft.windowsappsdk\1.5.240311000\buildTransitive\MrtCore.PriGen.targets

 <AppxMSBuildToolsPath Condition="'$(AppxMSBuildToolsPath)' == ''">C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Microsoft\VisualStudio\v17.0\AppxPackage\</AppxMSBuildToolsPath>

For me, the DLL was located in this folder, not from the dotnet folder as indicated by the error.

However, I still couldn't get dotnet run to work, nor would did it run from Rider. I can get it to run in VS 2022, but it's not worth it to me at the moment.

Eonasdan avatar Apr 03 '24 13:04 Eonasdan

    <AppxMSBuildToolsPath Condition="'$(AppxMSBuildToolsPath)' == ''">$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\AppxPackage\</AppxMSBuildToolsPath>
    <PriProjTaskAssembly Condition="'$(PriProjTaskAssembly)' == ''">$(AppxMSBuildToolsPath)\Microsoft.Build.Packaging.Pri.Tasks.dll</PriProjTaskAssembly>
    <AppxMSBuildTaskAssembly Condition="'$(AppxMSBuildTaskAssembly)' == ''">$(AppxMSBuildToolsPath)Microsoft.Build.AppxPackage.dll</AppxMSBuildTaskAssembly>

AppxMSBuildToolsPath = C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\AppxPackage\

PriProjTaskAssembly = C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.Build.Packaging.Pri.Tasks.dll

AppxMSBuildTaskAssembly = C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\AppxPackage\Microsoft.Build.AppxPackage.dll

But the TargetFramework of Microsoft.Build.Packaging.Pri.Tasks.dll is .NET Framework 4.7.2 and it may cause the dotnet fails to load the assembly.

lindexi avatar May 16 '24 12:05 lindexi

Can we degraded it to .NET Standard, or copy as .NET Core version?

As https://github.com/microsoft/WindowsAppSDK/issues/3939#issuecomment-1793743107

First, when you don't use EnableMsixTooling, the build process will use the ExpandPriContent task, and this task is backed by Microsoft.Build.Packaging.Pri.Tasks.dll. This is in the Visual Studio directory. IIRC, MSBuild still uses .NET Framework 4. There can be issues loading assemblies built for different versions of .NET into a process running a different version. When you use EnableMsixTooling, this will redirect the makepri related tasks to the WinAppSdkGenerateProjectPriFile task. This task is backed by Microsoft.Build.Msix.dll, and can be found in the Windows App SDK NuGet package. What's more, there is a version for .NET 4.72 and a version for .NET 5. As a complete guess, there is some functionality difference that makes the .NET 4 version incompatible with .NET 5 and newer, so when you use donet build, which I assume is using the .NET 8 runtime in your case, it is unable to use the .NET 4 assembly and requires a version explicitly for .NET 5 and newer. I would guess that this workaround will always be needed while Visual Studio and MSBuild provides no alternative.

lindexi avatar May 16 '24 12:05 lindexi

It is expected that in order to enable this scenario you need to add the following property to your project file: <EnableMsixTooling>true</EnableMsixTooling>. MRT Core's targets rely on the build tasks included in the Windows SDK and those are, as you've realized, not compatible with dotnet build. Single-project MSIX Packaging, however, includes both build tasks that are compatible with dotnet build and MSBuild targets that supersede those of MRT Core. So by using the EnableMsixTooling property to bring in Single-project MSIX Packaging (even if you are creating an unpackaged application or a class library) you allow your project to be compatible with dotnet build.

evelynwu-msft avatar Jun 11 '24 01:06 evelynwu-msft

@evelynwu-msft <EnableMsixTooling>true</EnableMsixTooling> is helpless... At least I didn't pass the test.

lindexi avatar Jun 11 '24 02:06 lindexi

@evelynwu-msft <EnableMsixTooling>true</EnableMsixTooling> is helpless... At least I didn't pass the test.

Please provide more details and a binlog. dotnet build successfully compiled a blank console application project with a reference to Windows App SDK 1.5.4 after I made the following changes:

  1. Set TargetFramework to net8.0-windows10.0.19041.0
  2. Added <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
  3. Added <EnableMsixTooling>true</EnableMsixTooling>

evelynwu-msft avatar Jun 12 '24 00:06 evelynwu-msft

@evelynwu-msft The error output:

C:\lindexi\Code\lindexi_gd\Temp\BeregemnallchunufeRojergalbu>dotnet build
MSBuild version 17.9.8+610b4d3b5 for .NET
  Determining projects to restore...
C:\Users\lindexi\.nuget\packages\microsoft.windowsappsdk\1.5.240607001\buildTransitive\MrtCore.PriGen.targets(913,5): error MSB4062: The "Microsoft
.Build.Packaging.Pri.Tasks.ExpandPriContent" task could not be loaded from the assembly C:\Program Files\dotnet\sdk\8.0.206\\Microsoft\VisualStudio
\v17.0\AppxPackage\\Microsoft.Build.Packaging.Pri.Tasks.dll. Could not load file or assembly 'C:\Program Files\dotnet\sdk\8.0.206\Microsoft\VisualS
tudio\v17.0\AppxPackage\Microsoft.Build.Packaging.Pri.Tasks.dll'. The system cannot find the path specified. Confirm that the <UsingTask> declaration is correct, that the assembl
y and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\lindexi\Co
de\lindexi_gd\Temp\BeregemnallchunufeRojergalbu\BeregemnallchunufeRojergalbu.csproj]

Build FAILED.

C:\Users\lindexi\.nuget\packages\microsoft.windowsappsdk\1.5.240607001\buildTransitive\MrtCore.PriGen.targets(913,5): error MSB4062: The "Microsoft
.Build.Packaging.Pri.Tasks.ExpandPriContent" task could not be loaded from the assembly C:\Program Files\dotnet\sdk\8.0.206\\Microsoft\VisualStudio
\v17.0\AppxPackage\\Microsoft.Build.Packaging.Pri.Tasks.dll. Could not load file or assembly 'C:\Program Files\dotnet\sdk\8.0.206\Microsoft\VisualS
tudio\v17.0\AppxPackage\Microsoft.Build.Packaging.Pri.Tasks.dll'. The system cannot find the path specified. Confirm that the <UsingTask> declaration is correct, that the assembl
y and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\lindexi\Co
de\lindexi_gd\Temp\BeregemnallchunufeRojergalbu\BeregemnallchunufeRojergalbu.csproj]
    4 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.83

Build command: dotnet build

My csproj code:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWinUI>true</UseWinUI>
    <Platforms>x86;x64</Platforms>
    <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
    <EnableMsixTooling>true</EnableMsixTooling>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240607001" />
  </ItemGroup>
 
</Project>

My program file:

// See https://aka.ms/new-console-template for more information

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace BeregemnallchunufeRojergalbu;

internal class Program
{
    static void Main(string[] args)
    {
        global::WinRT.ComWrappersSupport.InitializeComWrappers();
        global::Microsoft.UI.Xaml.Application.Start((p) =>
        {
            var app = new App();
            app.Launched += (sender, e) =>
            {
                var window = new Window()
                {
                    Title = "Create Window from console"
                };
                window.Content = new Grid()
                {
                    Children =
                    {
                        new TextBlock()
                        {
                            Text = "ConsoleApplication",
                            HorizontalAlignment = HorizontalAlignment.Center,
                            VerticalAlignment = VerticalAlignment.Center
                        }
                    }
                };
                window.Activate();
            };
        });
    }
}

public class App : Application
{
    public event EventHandler<LaunchActivatedEventArgs>? Launched;

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        Launched?.Invoke(this, args);
    }
}

You can find my demo code in: https://github.com/lindexi/lindexi_gd/tree/7fb1a1db6e329d55ff803944c7a606c870f2dd34/BeregemnallchunufeRojergalbu

The version of dotnet SDK is 8.0.206

lindexi avatar Jun 12 '24 01:06 lindexi

My VS (17.10) and .NET SDK (8.0.300) are slightly newer than yours but I don't think that matters. dotnet build had no issues compiling the repro solution.

evelynwu-msft avatar Jun 12 '24 02:06 evelynwu-msft