Errors with dotnet publish - maui

I've used these resources to learn how to publish a MAUI app:
https://learn.microsoft.com/en-us/dotnet/maui/ios/deployment/overview
https://github.com/dotnet/maui/issues/4397
https://github.com/dotnet/sdk/issues/21877
Based on these, I tried different variants to publish, but none of them work:
dotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64
error : The RuntimeIdentifier 'ios-arm64' is invalid.
dotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 --no-restore
error NETSDK1032: The RuntimeIdentifier platform 'ios-arm64' and the
PlatformTarget 'x64' must be compatible.
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true
error : The RuntimeIdentifier 'ios-arm64' is invalid.
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true --no-restore
error NETSDK1032: The RuntimeIdentifier platform 'ios-arm64' and the
PlatformTarget 'x64' must be compatible.
Update
Environment:
Windows 10 Home - 21H2
VS 2022 Version 17.3.0 Preview 1.1
Update 2
Based on the answers I have now this csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0-ios</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>MonkeyFinder</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Display name -->
<ApplicationTitle>MonkeyFinder</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.testapp.monkeyfinder</ApplicationId>
<ApplicationIdGuid>E46570A0-D087-4FC2-ADFE-58FEAB0BEBB9</ApplicationIdGuid>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<!-- Required for C# Hot Reload -->
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
<CodesignKey>‌xxx</CodesignKey>
<CodesignProvision>Development Test Provisioning</CodesignProvision>
<ArchiveOnBuild>true</ArchiveOnBuild>
<TcpPort>58181</TcpPort>
<ServerAddress>xxx</ServerAddress>
<ServerUser>xxx</ServerUser>
<ServerPassword>xxx</ServerPassword>
<_DotNetRootRemoteDirectory>/Users/xxx/Library/Caches/Xamarin/XMA/SDKs/dotnet/</_DotNetRootRemoteDirectory>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-android|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-android|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-ios|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-ios|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-maccatalyst|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-maccatalyst|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-windows10.0.19041|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-windows10.0.19041|AnyCPU'">
<MtouchDebug>True</MtouchDebug>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
And then I tried out these commands based on more collected information since, with the following results:
dotnet publish -f:net6.0-ios -c:Release
A runtime identifier for a device architecture must be specified in order to publish this project. 'iossimulator-x64' is a
simulator architecture.
dotnet build -c:Release /p:BuildIpa=true
Successful, but there is no *.ipa file in bin/Release/net6.0-ios/ios-arm64/publish
I also removed the other PropertyGroup tags, but it made no difference. Expect when removing the first one, then the error is:
error NETSDK1013: The TargetFramework value '' was not recognized. It may be misspelled. If not, then the TargetFrameworkIdentifier and/or TargetFrameworkVersion properties must be specified explicitly.
And also the project doesn't load properly anymore.
Update 3:
When I create a brand new .NET MAUI project from the template it starts compiling with the command
dotnet publish -f:net6.0-ios -c:Release
But even when I copy the xml in the csproj file from the working to the not working project I get the error:
A runtime identifier for a device architecture must be specified in
order to publish this project. 'iossimulator-x64' is a simulator
I guess this indicates that the cause for the error is actually not to be found in the csproj file, but somewhere else?

My solution is to run it on a Mac. Don't run it on your Window machine.
At the moment, you may want to install Visual Studio 2022 Preview for Mac and Open Terminal of your project. Then, run the command with sudo.
Example:
sudo dotnet publish -f:net6.0-ios -c:Release
Note: I set all of my properties in PropertyGroup in the Maui Project
Worked like a champ!

FIX 1
Try including RuntimeIdentifier via a conditional property group in .csproj, and omit it from command line.
From Add code signing ....
.csproj:
<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
<CodesignKey>iPhone Distribution: John Smith (AY2GDE9QM7)</CodesignKey>
<CodesignProvision>MyMauiApp</CodesignProvision>
<ArchiveOnBuild>true</ArchiveOnBuild>
</PropertyGroup>
Command line:
dotnet publish -f:net6.0-ios -c:Release
FIX 2
For now, when publish for ios, remove other target frameworks from .csproj. NOTE: This shouldn't be needed, because you specify framework via -f, but its worth a try.
Change:
<TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
To:
<TargetFrameworks>net6.0-ios</TargetFrameworks>

Related

ERROR: NU1605 "Detected package downgrade" - how to enforce downgrade in .Net5

In the past (and according to current documentation) referencing downgraded nuget package eliminated the above NU1605 (warn/err).
Now, with dotnet SDK 5.0.302, it seems impossible. A sample solution demonstrating it:
MyLib reference 1 nuget package : Newtonsoft.Json 13.0.1
My empty solution has this csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="MyLyb" Version="1.0.0" />
</ItemGroup>
</Project>
Still, I get
error NU1605: Detected package downgrade: Newtonsoft.Json from 13.0.1 to 12.0.3. Reference the package directly from the project to select a different version.
(building from cli, no visual studio involved)
I also tried "excludeAssets" with no help.
How can I reference lower version and avoid this error (without hiding it using "NoWarn")

Substitute for $(SolutionDir) in Azure DevOps CI Pipeline

I have the following situation:
A SQL project in which i have a nuget package installed. This package (merely a PS script) is responsible for unpacking the DACPACs that the DB requires, using references to paths relative to the solution folder (to find the packages/ and dacpacs/ folders and to browse through the projects which it extracts from the .sln file). This is called as a pre-build event.
When building the entire solution, the $(SolutionDir) is defined, as expected (locally and ADO).
When building the test project, the $(SolutionDir) is either '' or '*Undefined*'. Again, as expected, because msbuild has no knowledge about the solution when building a single project. I can live with this caveat locally, no problem.
The question is this: is there something "magical" out there that i can use to make this work in Azure DevOps?
I can try various hacks, if anyone is aware of such methods, although i would like a clean solution.
Tried so far:
1) Adding the following PropertyGroup:
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*'">.\</SolutionDir>
</PropertyGroup>
to the test project.
2) Following these sugestions: Prebuild event in Visual Studio replacing $(SolutionDir) with *Undefined*
No effect.
If your folder structure is something like:
1.The Azure Devops Repos contains the solution folder(where exists the xx.sln file).
2.And those projects are under same solution folder.
You can try my script:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
</PropertyGroup>
The $(MySolutionDir) represents the path where your sln file and project folders exists. Same like $(SolutionDir), it also has the \. So it's format looks like SomePath\.
And it's recommended to insert my script above the script of PreBuild event. Something like:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
It's recommended to add my script and PreBuildEvent in same propertyGroup, and mine should be in the first.
<PreBuildEvent>echo $(MySolutionDir)</PreBuildEvent>
</PropertyGroup>
Edit1:
You can also add condition on that:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
<SolutionDir Condition="xxxx">$(MySolutionDir)</SolutionDir>
It's recommended to add my script and PreBuildEvent in same propertyGroup, and mine should be in the first.
<PreBuildEvent>echo $(MySolutionDir)</PreBuildEvent>
</PropertyGroup>
Edit2:
Hmm, I now can reproduce the issue on my side. It's quite a strange behavior and I'm not sure about the root cause of this one. But a quick workaround is to create a new PropertyGroup to insert our custom script instead inserting it into existing PropertyGroup from default template:
It used to be:
....
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
<ProjectFolder>$([System.IO.Directory]::GetParent($'(ProjectDir)'))</ProjectFolder>
<ParentFolder>$([System.IO.Directory]::GetParent($'(ProjectFolder)'))\</ParentFolder>
<SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
</ItemGroup>
<ItemGroup>
<Build Include="test.sql" />
</ItemGroup>
<PropertyGroup>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
</Project>
Now change it to be:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
</ItemGroup>
<ItemGroup>
<Build Include="test.sql" />
</ItemGroup>
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<ParentFolder>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</ParentFolder>
<SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
Also, remove the extra ' in $(ProjectDir). It should be $(ProjectDir) instead of $'(ProjectDir)' and $'(ProjectFolder)'. I also see you have two PreBuildEvent properties, just keep the one in our custom script. After above steps, you project now works well on my side:

Can't exclude *.dcproj from dotnet pack

I used Visual Studio 2017 to make a solution with docker-compose support. When I try to pack a nuget package (during a CI process) using dotnet pack MySolution.sln I getting the error MSB4057 saying that it cannot pack the docker-compose.dcproj project. So I tried to use <IsPackable>false</IsPackable> inside my docker-compose.dcproj with no success.
So I have to pack my projects one by one or use different solutions for CI and for debugging process - both solutions looks ugly to me.
Does anyone have an idea how to exclude .dcproj file from trying to been packed by dotnet pack?
In the CI script, remove the docker-compose project from the solution before the pack command with
dotnet sln MySol.sln remove docker-compose.dcproj
I had the same problem and I have solved it by adding a dummy "Pack" target to docker-compose.dcproj, so my project looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ImportDirectoryBuildProps>false</ImportDirectoryBuildProps>
<ImportDirectoryBuildTargets>false</ImportDirectoryBuildTargets>
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Windows</DockerTargetOS>
<ProjectGuid>0a1c7d45-3174-4d07-a025-4d5fd55042c0</ProjectGuid>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>
<PropertyGroup>
<DockerServiceName>app-name</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
</ItemGroup>
<Target Name="Pack">
</Target>
</Project>

Change C# language version to 7.2 in vs-code on Linux

I read that .NET Core 2.0 SDK support C# 7.2 by default but the features of C# 7.1 and 7.2 are disabled and we have to enable them.
I install both SDK and C# extension for vs-code, but when I compile my code I got this error:
Program.cs(118,2): error CS1513: } expected [/home/smn/Desktop/myTest.csproj]
The build failed. Please fix the build errors and run again.
I also add these line to my .csproj file:
<PropertyGroup">
<LangVersion>7.2</LangVersion>
</PropertyGroup>
try this too:
`<PropertyGroup">
<LangVersion>latest</LangVersion>
</PropertyGroup>`
and also try this too:
`<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>`
What should I do?!
If you have not been able to solve this yet, you should try to remove the erroneous quote (") in the PropertyGroup tag, which appears in all the examples you gave.
<PropertyGroup">
This works for me:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
</Project>

Migrating a build to VSTS - get warning messages and no artifacts in the drop folder

I have a legacy application where I want to migrate the build to VSTS.
In the application is a web project and a WCF API that I want to include in the build, and a WIN RT client project that should not be included.
So I instead of having a single Visual Studio task to build the sln file, I have 2 which build the web and WCF projects individually one after the other.
See
Although the build is successful, I get these warnings and I would like to know how to fix them;
The log files can be found here; https://1drv.ms/f/s!ArKf9AZKW_zWgRapkZ_IUT-DrhY0
The property groups in my csproj file are;
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
If I replace the build for 2 project csproj files with one with the overall sln file, I get these error messages;
C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(2049):C:\Program
Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(2049,5):
Error MSB3779: The processor architecture of the project being built
"Any CPU" is not supported by the referenced SDK "Bing.Maps.Xaml,
Version=1.313.0825.0". Please consider changing the targeted processor
architecture of your project (in Visual Studio this can be done
through the Configuration Manager) to one of the architectures
supported by the SDK: "x86, x64, ARM". C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(2049):C:\Program
Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(2049,5):
Error MSB3779: The processor architecture of the project being built
"Any CPU" is not supported by the referenced SDK "Microsoft.VCLibs,
Version=12.0". Please consider changing the targeted processor
architecture of your project (in Visual Studio this can be done
through the Configuration Manager) to one of the architectures
supported by the SDK: "x86, x64, ARM". C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets(2606):C:\Program
Files
(x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets(2606,5):
Error : Copying file Service
References\SurveyServiceReference\M.Survey.SurveyAdminApp.SurveyServiceReference.AssignedJobs.datasource
to obj\Release\Package\PackageTmp\Service
References\SurveyServiceReference\M.Survey.SurveyAdminApp.SurveyServiceReference.AssignedJobs.datasource
failed. Could not find file 'Service
References\SurveyServiceReference\M.Survey.SurveyAdminApp.SurveyServiceReference.AssignedJobs.datasource'.
Process 'msbuild.exe' exited with code '1'.
Make sure you add the parameters to the MsBuild Arguments and that these are a valid combination for your project, e.g.:
/p:Configuration=Release /p:Platform=AnyCPU
Or edit the project file and make sure the default Configuration and Platform are specified in the first PropertyGroup:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
</PropertyGroup>
It turned out it caused by the variable BuildPlatform = any cpu in your build definition.
You should change the variable BuildPlatform with the value anycpu (no blank space) to align with the value in PropertyGroups of your .csproj.
The Visual studio build task does have this weird behavior. When building a solution or .sln file, you have to provide the build platform in the following manner: With a space in between
However, when you are building a project or a .csproj file, you have to provide the build platform in the following manner: Without any space in between
If building the project individually doesn't work for you, and you need to build the solution, then this SO post will help you make solution level changes in Visual Studio so when you build in VSTS, the task will only build the desired projects. Hope this helps.