maui icon indicating copy to clipboard operation
maui copied to clipboard

Can't publish a Maui app that references a class library on Windows without modifying the project

Open javiercn opened this issue 3 years ago • 30 comments

Description

A Maui app that references a class library can't be published without modifying the project file using the following command:

msbuild /restore /t:Publish /p:TargetFramework=net6.0-windows10.0.19041 /p:TargetFrameworks="net6.0-windows10.0.19041;net6.0" /p:configuration=release /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackageCertificateThumbprint="YOUR_CERT_THUMBPRINT_HERE" /p:PackageCertificatePassword="AnyTestPasswordYouLike"

Steps to reproduce

  1. Create a Maui application.
  2. Create a class library.
  3. Reference the class library from the Maui application.
  4. Open a developer command prompt.
  5. Navigate to the Maui app folder.
  6. Run the command above to publish the app.

Workaround

  • Update the project file with the following changes
    -               <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
    -               <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
    +               <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    
  • Update the command to use the following:
    msbuild /restore /t:Publish /p:configuration=Release /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackageCertificateThumbprint="<<YOURTHUMBPRINTHERE>>" /p:PackageCertificatePassword="AnyTestPasswordYouLike"
    

javiercn avatar Jun 24 '22 11:06 javiercn

I believe if you remove the following from the command line (without modifying the project file), this should work:

/p:TargetFrameworks="net6.0-windows10.0.19041;net6.0"

dsplaisted avatar Jun 24 '22 16:06 dsplaisted

@dsplaisted unfortunately it doesn't because it also has the ios and android TFMs

javiercn avatar Jun 24 '22 16:06 javiercn

I suppose there is a lot of same, or it's the same same problem, I have.

  1. Open Visual Studio 2022, version 17.3.0 Preview 3.0.
  2. Using VS, create a Maui project.
  3. Add a .NET 6 class library the the solution using VS.
  4. Reference this newly created .NET 6 class library in MauiProgram.

The class library project reference can be added to the Maui project. Using VS it is possible to navigate to it (<Ctrl+F12>). However when compiling an error is generated:

error CS0246: The type or namespace name 'TestClassLibrary' could not be found.

Here TestClassLibrary being whatever one used when generating the project.

The class library .csproj is as follows

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Different issues come if I add net6.0 to Maui program, change the library to netstandard2.1 or do something else.

veikkoeeva avatar Jul 13 '22 13:07 veikkoeeva

@javiercn This issue does not repro when change the class library .csproj is <TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>, but there is a warning Path to mspdbcmf.exe could not be found. And the https://github.com/aspnet/AspNetCore-ManualTests/pull/1519 has update the Blazor Hybrid Sample Project TFM. So I want to confirm with you, should I need file a new bug to track this warning and close this issue? image "C:\Users\v-xinyfe\source\repos\MauiApp1\MauiApp1\MauiApp1.csproj" (Publish target) (1:10) -> (_EnsurePdbCmfExeFullPath target) -> C:\Users\v-xinyfe.nuget\packages\microsoft.windowsappsdk\1.1.3\buildTransitive\Microsoft.Build.Msix.Packaging.target s(341,5): warning : Path to mspdbcmf.exe could not be found. A symbols package will not be generated. Review https:// aka.ms/windowsappsdkdocs and ensure that all prerequisites for Windows App SDK development have been installed. [C:\Use rs\v-xinyfe\source\repos\MauiApp1\MauiApp1\MauiApp1.csproj]

EmilyFeng97 avatar Aug 02 '22 10:08 EmilyFeng97

@EmilyFeng97 Just to be very clear, the issue is that you should be able to have a class library with just <TargetFramework>net6.0</TargetFramework>. What you are describing is at best a workaround.

I can't speak to the warning that you are seeing because that comes from the Windows SDK. If this is for manual testing purposes, I would stick to the workaround that I provided above.

javiercn avatar Aug 02 '22 10:08 javiercn

I cross-reference https://github.com/dotnet/maui/issues/9091. I think this works for me now with the latest VS but there's similarish issue. That linked issue seem to be a show stopper if it is a supposed scenario, a bit similar to this and seem to be related around tooling with Maui and .NET 6.

veikkoeeva avatar Aug 02 '22 12:08 veikkoeeva

@javiercn as mentioned in the Docs for this

Attempting to publish a .NET MAUI solution will result in the dotnet publish command attempting to publish each project in the solution individually, which can cause issues when you've added other project types to your solution. Therefore, the dotnet publish command should be scoped to your .NET MAUI app project.

Try specifically to specify the .NET MAUI csproj to publish. Additionally, you should be able to use dotnet publish now for the Windows target as well.

Let us know if that worked!

jfversluis avatar Aug 02 '22 12:08 jfversluis

Hi @javiercn. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Aug 02 '22 12:08 ghost

@jfversluis I was not publishing the solution. I was publishing the Maui project. I do not think that calling MSBuild vs MSBuild Core (dotnet publish) makes a difference here.

javiercn avatar Aug 02 '22 13:08 javiercn

@javiercn the command you stated above does not reflect specifying the csproj. If you do not specify that explicitly it will try to publish the sln (or maybe each project in that folder individually, not really sure). You will have to specify the csproj in the command explicitly like so: msbuild MyApp.csproj /restore /t:Publish /p:TargetFramework=net6.0-windows10.0.19041 /p:configuration=release /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackageCertificateThumbprint="YOUR_CERT_THUMBPRINT_HERE" /p:PackageCertificatePassword="AnyTestPasswordYouLike"

jfversluis avatar Aug 02 '22 13:08 jfversluis

5. Navigate to the Maui app folder.

Are you saying that it will walk up the directory hierarchy and try to build the solution?

javiercn avatar Aug 02 '22 13:08 javiercn

Let's put it this way: I have never seen it work if you don't specify the csproj explicitly :)

This might be a good test case and if it appears to be that, let's find out where to log a but to maybe change that before for the dotnet CLI tooling

jfversluis avatar Aug 02 '22 13:08 jfversluis

@jfversluis I tried a few things and it does not make any difference.

  • Using dotnet publish vs MSBuild does not fix the problem.
  • Specifying the csproj file explicitly doesn't change the problem either.

The question then that I have is.

  • Given a class library that targets .NET 6.0
  • Given a Maui app created from the template that references that class library.

What command should a user run to produce a windows package that it can install in a machine

javiercn avatar Aug 02 '22 14:08 javiercn

Hm that's strange then. In that case I need to dive into it again for a bit! Thanks for the investigation on your part!

jfversluis avatar Aug 02 '22 15:08 jfversluis

@javiercn This issue also repro when publishing a Maui app that references a class library using the following command in Dev17.4 Preview 2 installed from PR412410. If I manually add "net6.0-android" to the SampleContent.csproj file and publish, this issue does not repro. Should I file a new bug to track this issue? dotnet publish -f:net6.0-android -c:Release image

EmilyFeng97 avatar Aug 19 '22 10:08 EmilyFeng97

installed from PR412410.

Does this still reproduce using the "normal" build of VS you utilize for validations.

installed from PR412410

We're still ironing out some details for that build, so it's not the best measure of whether or not a bug is still around 😄

TanayParikh avatar Aug 21 '22 01:08 TanayParikh

@TanayParikh This issue repro on Dev17.4 Preview 2 installed from main branch when the MAUI project is created via CLI. And this issue does not repro if the MAUI project is created in VS. In windows testing scenarios, the MAUI project is usually created in VS. I created the project via CLI due to issue https://github.com/aspnet/AspNetCore-ManualTests/issues/1565. So should we ignore this issue?

EmilyFeng97 avatar Aug 24 '22 04:08 EmilyFeng97

So should we ignore this issue?

Thanks for the clarification. No we shouldn't ignore this issue if it repros on Dev17.4 Preview 2 installed from main branch. I just wanted to confirm it wasn't specific to the PR branch 😄

TanayParikh avatar Aug 31 '22 22:08 TanayParikh

Thanks for the clarification. No we shouldn't ignore this issue if it repros on Dev17.4 Preview 2 installed from main branch. I just wanted to confirm it wasn't specific to the PR branch 😄

This issue is not specific to the PR branch, it also repro on VS installed from main branch when the MAUI project is created via CLI. So should I file a new issue now about publishing projects created via the CLI, or file a new issue when the issue still repro after #8304 is fixed?

EmilyFeng97 avatar Sep 01 '22 09:09 EmilyFeng97

Thanks for the details @EmilyFeng97. Let's use this issue to track that as well. If after this issue is closed, projects created via CLI but published using VS are still failing, please create a new issue.

TanayParikh avatar Sep 01 '22 20:09 TanayParikh

Any news about this issue? dotnet build -c Release -f net6.0-android failed when building a .NET MAUI containing several class libraries targeting net6.0 results in the same error.

marcelbeeker avatar Oct 12 '22 20:10 marcelbeeker

@marcelbeeker which version of the SDK are you on (dotnet --version).

I believe @jinzhao1127 may have mentioned this no longer reproduces.

TanayParikh avatar Oct 12 '22 22:10 TanayParikh

I believe @jinzhao1127 may have mentioned this no longer reproduces.

Yes, this issue does not repro anymore on both 6.0 and 7.0 Blazor Maui app that referenced razor class library app

jinzhao1127 avatar Oct 13 '22 06:10 jinzhao1127

The SDK version I use is version 6.0.402

marcelbeeker avatar Oct 13 '22 17:10 marcelbeeker

I create a traditional Maui (XAML) app (not Blazor).

I referenced a Class library project targeting .NET6 (net6.0).

marcelbeeker avatar Oct 13 '22 17:10 marcelbeeker

Finally I found out, the problem has something to do with the include of the Microsoft.NET.Test.Sdk in my Test project. When I remove this nuget I can build my projects without any problem.

I can't find out how I have to excute dotnet build -c Release -f net6.0-android (using in Azure Devops) for a solution a MAUI project which aslo contain a Xunit test project also.

marcelbeeker avatar Oct 14 '22 21:10 marcelbeeker

Have next steps:

  1. Create default .NET MAUI project
  2. Create .NET 6 class library (with just one static string field to use in MAUI project)
  3. Add lib reference to .NET MAUI project

Publish (via wizard): For Debug|Any CPU is ok For Release|Any CPU (only difference in projects): Cannot resolve Assembly or Windows Metadata file '...\MauiAppTestPublish\ClassLibrary\bin\Release\net6.0\ClassLibrary.dll'

Kazbek avatar Oct 23 '22 17:10 Kazbek

@Kazbek , Exactly that is my problem too. I cannot even add one .NET 6 project class library to my Maui Blazor app, and this exact same warning is presented to me. The build-solution even enters a state where it never completes.

j03ybh avatar Oct 29 '22 18:10 j03ybh

I'm curious but isn't the whole point of .NET to self contain everything in the one sdk? Shouldn't it all just work without any extra fiddling around and additional dependencies?

heron1 avatar Dec 11 '22 12:12 heron1

I am facing the same problem as https://github.com/dotnet/maui/issues/8768 - considered as duplicate here, when publish in iOS. My MAUI Blazor project, with a Blazor class library: Can run in Debug mode, for both simulator and real device. For iOS, can build success in Release mode, but gets errors on publish. For Android, haven't tried yet. Im using lasted VS2022 Version 17.4.3, Xcode 14.2 (the problem happend before I update VS2022 to the lastest version, I can't remember the old version)

build command dotnet publish -f:net7.0-ios -c:Release /p:ServerAddress=abc /p:ServerUser=abc /p:ServerPassword=abc /p:RuntimeIdentifier=ios-arm64 /p:TcpPort=58181 /p:ArchiveOnBuild=true /p:_DotNetRootRemoteDirectory=/Users/tranviettravel/Library/Caches/Xamarin/XMA/SDKs/dotnet/

error log

 Determining projects to restore...
  All projects are up-to-date for restore.
  Executing SayHello Task to establish a connection to a Remote Server.
                        Properties:
                                SessionId=79c3425643240xxxdeff14f87a3cf3c1443c1f155c3b556852dad0bbc62e6368,
                                Addresss=abc,
                                SshPort=22,
                                TcpPort=,
                                User=abc,
                                AppName=emTransViet,
                                VisualStudioProcessId=,
                                ContinueOnDisconnected=true
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\16.1.1481\tools\msbuild\iOS\Xamarin.Shared.targets(1738,3): error : Could not find Microsoft.iOS in /Users/tranviettravel/Library/Caches/Xamarin/XMA/SDKs/dotnet/packs/Microsoft.iOS.Sdk/16.1.1481/. [C:\EventManage\
emTransViet\emTransViet.csproj::TargetFramework=net7.0-ios]
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\16.1.1481\tools\msbuild\iOS\Xamarin.Shared.targets(1738,3): error :          [C:\EventManage\emTransViet\emTransViet.csproj::TargetFramework=net7.0-ios]

on MAUI Blazor app .csproj

 <PropertyGroup>
    <TargetFrameworks>;net7.0-ios;net7.0-android</TargetFrameworks>
..............
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">24.0</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
  <RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
</PropertyGroup>

on the class library .csproj, I tried to add ;net7.0-ios;net7.0-android, but do not work, it has only 1 TargetFramework net7.0 as default.

  <PropertyGroup>
    <TargetFrameworks>net7.0;net7.0-ios;net7.0-android</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

The sdk folder above /Users/tranviettravel/Library/Caches/Xamarin/XMA/SDKs/dotnet/packs/Microsoft.iOS.Sdk/16.1.1481/ exists in my Mac, with contents inside. Screenshot 2022-12-21 185700

TranNguyen17893 avatar Dec 21 '22 12:12 TranNguyen17893