Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet build fails when WindowsAppSDK is referenced in console application. #3548

Closed
ianeuman opened this issue Mar 30, 2023 · 17 comments
Closed
Labels
area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration bug Something isn't working

Comments

@ianeuman
Copy link

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

@bpulliam bpulliam added area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration bug Something isn't working labels Apr 5, 2023
@wjk
Copy link

wjk commented May 9, 2023

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!

@inforithmics
Copy link

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

@TopperDEL
Copy link

TopperDEL commented May 31, 2023

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!

@ianeuman
Copy link
Author

ianeuman commented Jul 7, 2023

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?

Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
I guess I was too smart for my own good

Move GenerateLibraryOutput down to relevant block

Add workaround for microsoft/WindowsAppSDK#3548

Does this work?

Ok now it will work
Cheesebaron added a commit to MvvmCross/MvvmCross that referenced this issue Jul 12, 2023
* Add properties to easily check Target Platform

* Add MacCatalyst plugins

I guess I was too smart for my own good

Move GenerateLibraryOutput down to relevant block

Add workaround for microsoft/WindowsAppSDK#3548

Does this work?

Ok now it will work
@ThaDaVos
Copy link

ThaDaVos commented Sep 7, 2023

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: dotnet/maui#5886

@abdes
Copy link

abdes commented Jan 29, 2024

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

This works.
Thanks @inforithmics

@jeromelaban
Copy link

jeromelaban commented Mar 20, 2024

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.

@abdes
Copy link

abdes commented Mar 20, 2024

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)?

@jeromelaban
Copy link

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

@Eonasdan
Copy link

Eonasdan commented Apr 3, 2024

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.

@lindexi
Copy link

lindexi commented May 16, 2024

    <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
Copy link

lindexi commented May 16, 2024

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

As #3939 (comment)

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.

@evelynwu-msft
Copy link
Contributor

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.

@lindexi
Copy link

lindexi commented Jun 11, 2024

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

@evelynwu-msft
Copy link
Contributor

@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>

@lindexi
Copy link

lindexi commented Jun 12, 2024

@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

@evelynwu-msft
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration bug Something isn't working
Projects
None yet
Development

No branches or pull requests