microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.85k stars 323 forks source link

Unable to update C# ClassLibrary from v1.5 to v1.6-exp2 #4591

Closed acouvert-msft closed 3 months ago

acouvert-msft commented 4 months ago

Describe the bug

I have a project that creates a C# component with WinUI 3 controls that I consume from a C++/WinRT app that uses the Windows App SDK. I followed the instructions: Walkthrough—Create a C# component with WinUI 3 controls, and consume it from a C++/WinRT app that uses the Windows App SDK with some minor changes (targeting .net6, minimum Windows version 10.0.19041, x64 and ARM64 platforms).

Initially the C# component project is depending on Microsoft.WindowsAppSDK 1.5.240311000. It can build and be consumed as expected.

Updating the C# component project to Microsoft.WindowsAppSDK 1.6.240701003-experimental2 results in build issue.

Steps to reproduce the bug

Create a C# ClassLibrary project as bellow:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
    <RootNamespace>TestCSharpWinrRTComponent</RootNamespace>
    <RuntimeIdentifiers>win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
    <CsWinRTComponent>true</CsWinRTComponent>
    <Configurations>Debug;Release</Configurations>
    <Platforms>x64;ARM64</Platforms>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.8" />
      <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240311000" />
  </ItemGroup>
</Project>

Build successfully ✅

Update Microsoft.WindowsAppSDK to 1.6.240701003-experimental2. Build and get the following error 🚩

This version of the Windows App SDK requires Microsoft.Windows.SDK.NET.Ref 10.0.19041.35-preview;10.0.19041.35-preview or later, which can be added with:
<PropertyGroup>
  <WindowsSdkPackageVersion>10.0.19041.35-preview;10.0.19041.35-preview</WindowsSdkPackageVersion>
</PropertyGroup>
TestCSharpWinrRTComponent   C:\.tools\.nuget\packages\microsoft.windowsappsdk\1.6.240701003-experimental2\buildTransitive\Microsoft.WindowsAppSDK.targets   84

Address the error by adding <WindowsSdkPackageVersion>10.0.19041.35-preview</WindowsSdkPackageVersion>. .csproj is now:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
    <RootNamespace>TestCSharpWinrRTComponent</RootNamespace>
    <RuntimeIdentifiers>win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
    <CsWinRTComponent>true</CsWinRTComponent>
    <Configurations>Debug;Release</Configurations>
    <Platforms>x64;ARM64</Platforms>
    <WindowsSdkPackageVersion>10.0.19041.35-preview</WindowsSdkPackageVersion>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.8" />
      <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240701003-experimental2" />
  </ItemGroup>
</Project>

Build and get the following error 🚩

1>------ Build started: Project: TestCSharpWinrRTComponent, Configuration: Debug x64 ------
1>CSC : warning CS8785: Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'UnauthorizedAccessException' with message 'Access to the path 'Generated Files\\CsWinRT\' is denied.'.
1>CSC : warning CS8785: Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'UnauthorizedAccessException' with message 'Access to the path 'Generated Files\\CsWinRT\' is denied.'.
1>Failed to resolve WinRT.Runtime.dll.
1>TestCSharpWinrRTComponent -> C:\source\repos\TestCSharpWinrRTComponent\bin\x64\Debug\net6.0-windows10.0.19041.0\TestCSharpWinrRTComponent.dll
1>C:\.tools\.nuget\packages\microsoft.windows.cswinrt\2.0.8\build\Microsoft.Windows.CsWinRT.Authoring.targets(340,5): error MSB3030: Could not copy the file "Generated Files\\CsWinRT\\TestCSharpWinrRTComponent.winmd" because it was not found.
1>Done building project "TestCSharpWinrRTComponent.csproj" -- FAILED.

Expected behavior

I should be able to update to latest Microsoft.WindowsAppSDK without build issue.

Screenshots

No response

NuGet package version

Windows App SDK 1.6 Experimental 2: 1.6.240701003-experimental2

Packaging type

No response

Windows version

No response

IDE

Visual Studio 2022

Additional context

The C# project is only containing a simple XAML control:

<?xml version="1.0" encoding="utf-8" ?>
<UserControl
    x:Class="TestCSharpWinrRTComponent.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:TestCSharpWinrRTComponent"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <TextBlock Text="Hello World" />
    </Grid>
</UserControl>
using Microsoft.UI.Xaml.Controls;

namespace TestCSharpWinrRTComponent
{
    public sealed partial class MyUserControl : UserControl
    {
        public MyUserControl() => InitializeComponent();
    }
}
ghost1372 commented 4 months ago

There is some issues for class library, i also reported another issue here https://github.com/microsoft/WindowsAppSDK/issues/4567

I can bypass my issue with changing platform from anycpu to x86 or x64 so give it a try and change your class library platform to x64 or x86

acouvert-msft commented 4 months ago

I'm currently experiencing the issue when targeting x86 / x64 / ARM64. @ghost1372 could you share your project file so I could compare what you are doing different? Thanks.

For the records, I'm attaching my projects before and after the updates. OK_TestCSharpWinrRTComponent_v1.5.zip KO_TestCSharpWinrRTComponent_v1.6-exp2.zip

DarranRowe commented 3 months ago

Just to ask, have you tried this with CsWinRT 2.1.0 preview?

acouvert-msft commented 3 months ago

@DarranRowe I tried with CsWinRT 2.1.0-prerelease.240715.1 and latest WindowsAppSDK 1.6.240722000-experimental2 with the same result.

ghost1372 commented 3 months ago

@DarranRowe I tried with CsWinRT 2.1.0-prerelease.240715.1 and latest WindowsAppSDK 1.6.240722000-experimental2 with the same result.

I tested your sample and unfortunately I did not find any solution for it. Class library has many problems. Just a few moments ago, I reported another problem related to the publish single file

manodasanW commented 3 months ago

This is due to a change in experimental 2 where GeneratedFilesDir is getting set to a relative path rather than an absolute path.

Tracked internally via https://task.ms/52711016

JesseCol commented 3 months ago

The internal bug is named: 'wv2winrt.targets' incorrectly overrides 'GeneratedFilesDir' to be the top-level project folder

Until the WebView2 nupkg is published with this fix, you can workaround it by setting GeneratedFilesDir early in the project.

Closing for now, thanks!