dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.2k stars 1.75k forks source link

Publish MAUI Project with project reference To Class library produces errors. #6717

Closed danies8 closed 2 years ago

danies8 commented 2 years ago

Description

Publish MAUI Project with project reference To Class library produces errors.

Steps to Reproduce

  1. Create Maui Project.
  2. Add Class library A.
  3. Add Class library B.
  4. Add project reference from MAUI projects for two class librariles.
  5. Publish according to this documents: https://docs.microsoft.com/en-us/dotnet/maui/windows/deployment/overview
  6. I used this commands:
    New-SelfSignedCertificate -Type Custom -Subject "CN=Dani" -KeyUsage DigitalSignature -FriendlyName "My temp dev cert" -CertStoreLocation "Cert:\CurrentUser\My" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")
    Get-ChildItem "Cert:\CurrentUser\My" | Format-Table Subject, FriendlyName, Thumbprint
    msbuild  /restore /t:Publish /p:TargetFramework=net6.0-windows10.0.19041 /p:configuration=release /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackageCertificateThumbprint="44CF7D6F1FD0B7CF538085CBEA22A1B0ACDBB541" 
  7. Get error in build phase, please see Relevant log output

Version with bug

Release Candidate 1

Last version that worked well

Release Candidate 1

Affected platforms

Windows

Affected platform versions

...

Did you find any workaround?

No

Relevant log output

"C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj" (Restore target) (1) ->
(Restore target) ->
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-android31.0 (.NETCoreApp,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-ios15.4 (.NETCoreApp,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-maccatalyst15.4 (.NETCoreApp,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-android31.0 (.NETCoreApp,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-ios15.4 (.NETCoreApp,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
  C:\Users\danis\Desktop\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-maccatalyst15.4 (.NETCoreApp,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)

    0 Warning(s)
    6 Error(s)
Redth commented 2 years ago

@mattleibow is this a matter of setting some msbuild properties?

@jfversluis maybe you have some input here too?

jfversluis commented 2 years ago

Looks like a duplicate of #5833

danies8 commented 2 years ago

Reproduce also in VS Version 17.2.0 Preview 5.0,

danies8 commented 2 years ago

When I add this to csproj of class library: <TargetFramework Condition="'$(BuildWindowsOnly)' == 'true'">net6.0-windows10.0.19041</TargetFramework> and comment <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks> on MAU project csproj file. and-p:BuildWindowsOnly=true to build command it work. But you have to fix it :))

Dbquity commented 2 years ago

FYI, with earlier previews, I was able to work around this issue by creating nuget packages for the .NET 6 assemblies and letting the MAUI project refer to those packages rather than using project references.

With VS Community 2022 - preview version 17.2.0 Preview 6.0, the issue is still present and the nuget workaround seems to work, although I get the below error

C:\Program Files\dotnet\sdk\6.0.300-preview.22204.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5):
error NETSDK1005: Assets file 'C:\...\obj\project.assets.json' doesn't have a target for 'net6.0-windows10.0.19041'.
Ensure that restore has run and that you have included 'net6.0-windows10.0.19041' in the TargetFrameworks for your project.

which is reported as a separate issue here: https://github.com/dotnet/maui/issues/6831

mattleibow commented 2 years ago

This may be because you are publishing a net6.0-windows10.0.19041 TFM, but the app TFM is the one with an extra .0 suffix: net6.0-windows10.0.19041.0

mattleibow commented 2 years ago

Duplicate of #6831

I was able to repro the error with the CLI of:

dotnet publish -f net6.0-windows10.0.19041 -c Release

But all is well with the extra .0:

dotnet publish -f net6.0-windows10.0.19041.0 -c Release

I also get another error when I try publish the solution and that is unsupported. If you publish with the solution, you instruct the tooling to publish each project in the solution - so effectively you instruct the CLI to publish a net6.0 class library as a windows app - which is probably not what you are wanting.

The correct way to publish is using the project file - using a TFM that matches the one in the csproj as it is basically a selector of the items in there.

jfversluis commented 2 years ago

Added a note about the sln publishing to the docs: https://github.com/dotnet/docs-maui/pull/508 as well as the TFM that need to match up

Dbquity commented 2 years ago

@mattleibow a quick test on my devbox shows that the present issue (#6717) with project references to .NET 6 class libraries still exist in MAUI RC2 / VS 2022 17.2 Preview 6 - did you mean to close this issue?

mattleibow commented 2 years ago

@Dbquity can you attach a binlog? Maybe our projects don't quite match... or a repro case so I can test.

Dbquity commented 2 years ago

@mattleibow Quick reply is the repro I created earlier at https://github.com/Dbquity/MauiApp1 Its .csproj files are of cause out-of-date by bow, but the issue reproes by a dependency chain similar to this (which is what I have in my Dbquity solution): MauiApp --> MauiLib --> .NET6Lib The present issue suggests that even MauiApp --> .NET6Lib fails.

Dbquity commented 2 years ago

@Dbquity can you attach a binlog? Maybe our projects don't quite match... or a repro case so I can test.

@mattleibow created a simple repro here: https://github.com/Dbquity/Repro6717

mattleibow commented 2 years ago

Thanks! Looking now and will try find the issue and reopen if it is a bug or reply here with anything that is dodgy.

mattleibow commented 2 years ago

OK, so some interesting news and this feels like either a bug or a limitation of msbuild.

If you switch to dotnet build you get an error about TFMs, but this is easily fixed by adding a .0 to the end of the windows TFM:

dotnet build MauiApp1 /p:TargetFramework=net6.0-windows10.0.19041

However, for msbuild, it appears the single command of msbuild /restore throws off the part with the TFM. If you do it in 2 steps then it works:

msbuild /t:restore MauiApp1
msbuild MauiApp1 /p:TargetFramework=net6.0-windows10.0.19041.0

I think the fact that you specify a TFM during the restore somehow throws of the restore process. This can be repro-ed with just the restore target:

msbuild /t:restore MauiApp1 /p:TargetFramework=net6.0-windows10.0.19041.0

There is another workaround for this and it is probably due to the fact that the CLI arguments OVERWRITE the properties in the csproj. The workaround is to just change the singular <TargetFramework> in the class library to the plural version <TargetFrameworks>.

As a result, the CLI argument of /p:TargetFramework=net6.0-windows10.0.19041.0 no longer OVERWRITES the property but instructs the build to SELECT the best matching TFM.

Dbquity commented 2 years ago

@mattleibow Thanks for the explanation! Now I can ditch my hefty workaround of going via nuget packages for my 10ish .NET 6 class libraries :-)

Dbquity commented 2 years ago

@davidbritch @jfversluis Maybe @mattleibow 's point about splitting out the restore step when you have dependencies on .NET 6 class libraries warrants a mention in the documentation at https://github.com/dotnet/docs-maui/issues/352

jfversluis commented 2 years ago

We'll be looking forward to you PR @Dbquity 😝

Dbquity commented 2 years ago

@jfversluis that smells like work - I'll PO (pull out) instead ;-)

Dbquity commented 2 years ago

@jfversluis Jokes aside, since the subtlety of handling .NET 6 class library project references only pertains to msbuild and MAUI is using dotnet publish, I guess a PR on the docs would be less relevant. If you think otherwise, let me know, and I'll be happy to do my first github PR as an exercise :-)

danies8 commented 2 years ago

So the publish will be like this document ? ( Publish the MAUI project using this link: https://docs.microsoft.com/en-us/dotnet/maui/windows/deployment/overview )

mattleibow commented 2 years ago

That is correct.

danies8 commented 2 years ago

Great:) in which version I can see this fix ?

mattleibow commented 2 years ago

@danies8 fix? The fix is to either use dotnet build or to use 2 separate restore and then publish commands. Unless I am misreading your question?

https://github.com/dotnet/maui/issues/6717#issuecomment-1118415277

danies8 commented 2 years ago
  1. I used Version 17.2.0 Preview 5.0.
  2. I used this document:https://docs.microsoft.com/en-us/dotnet/maui/windows/deployment/overview
  3. In last phase I used dotnet build and get this error: dotnet build /restore /t:Publish /p:TargetFramework=net6.0-windows10.0.19041.0 /p:configuration=release /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackageCertificateThumbprint="5F1A782863655040EC3AE2E0C2132A1A04AC6618"
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-android31.0 (.NET
CoreApp,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-ios15.4 (.NETCore
App,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-maccatalyst15.4 (
.NETCoreApp,Version=v6.0). Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-android31.0 (.NET
CoreApp,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-ios15.4 (.NETCore
App,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI\AutomationClient.MAUI.csproj : error NU1201: Project AutomationClient.Shared is not compatible with net6.0-maccatalyst15.4 (
.NETCoreApp,Version=v6.0) / win10-x64. Project AutomationClient.Shared supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
    0 Warning(s)
    6 Error(s)
  1. I tried also 2 separate restore and then publish commands and I got these errors:
    
    PS C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI> msbuild /t:restore AutomationClient.MAUI
    Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist. Switch: AutomationClient.MAUI PS C:\Users\danis\Desktop\11_05_2022_unc client check\AutomationClient\AutomationClient.MAUI>



What do I miss ?
danies8 commented 2 years ago

Update when I use 4 -2 separate restore and then publish commands it works-:) Need to add .csproj msbuild /t:restore AutomationClient.MAUI.csproj with dotnet build is not working

danies8 commented 2 years ago

Another issue: On window server 2012 R2 the MSIX is not open ? Is it a bug or need to do something else ? This is my app manifest in Window platform:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="AutomationClient.MAUI.WinUI.app"/>

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <!-- The combination of below two tags have the following effect:
           1) Per-Monitor for >= Windows 10 Anniversary Update
           2) System < Windows 10 Anniversary Update
      -->
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </windowsSettings>
  </application>
</assembly>