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
21.63k stars 1.62k forks source link

Fix <ApplicationTitle> encoding in maui-mobile template #22084

Open MiYanni opened 2 weeks ago

MiYanni commented 2 weeks ago

Summary

There is a bug where certain characters (in this case &) which are not valid in MSBuild/XML are being used as the <ApplicationTitle> in the maui-mobile template. To fix this, I made <ApplicationTitle> use a custom symbol instead of the default name parameter. This symbol uses xmlEncode to allow it to work properly in the .csproj file.

Issues Fixed

Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2045293

Before/After

Before (using the project name Cats&Dogs):

image

After (using the project name Cats&Dogs2):

image

jsuarezruiz commented 1 week ago

/azp run

azure-pipelines[bot] commented 1 week ago
Azure Pipelines successfully started running 1 pipeline(s).
rmarinho commented 1 week ago

Can we add a unit test? there's some msbuild project tests I think.

Thanks

MiYanni commented 1 week ago

@rmarinho

Can we add a unit test? there's some msbuild project tests I think.

Where? I looked at src\Templates\tests and don't see anything of value there.

FYI, I haven't built or done anything of actual coding in this repo. All I did for this fix was:

  1. Install maui workload via CLI.
  2. Created a new project using the name Cats&Dogs, tried to run the dotnet publish command (like in the issue), and saw it fails.
  3. Went into the template-packs folder and found the maui nupkg.
  4. Extracted the contents, modified the template.json and .csproj for maui-mobile, then dropped the modified files back into the nupkg again.
  5. Did the same thing as Step 2 with the name Cats&Dogs2, saw that ApplicationTitle was encoded properly, and observed that publishing no longer errored for the reported reason.
rmarinho commented 1 week ago

We have a couple of them here

https://github.com/dotnet/maui/blob/222ca3ca088d199ff22adde8ca3ae82d9dba0b35/src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs#L6

dotnet-policy-service[bot] commented 4 days ago

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

MiYanni commented 4 days ago

I don't have cycles to work on a unit test for this issue. I've had the DevOps work item assigned to the MAUI team.

Eilon commented 3 days ago

I tried the original repro and it seems still broken. I cloned this PR locally, built it, and ran dotnet new maui -n "𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞" (per the original bug), and the template was created, but then building it caused errors.

This is in the Build Output as an error:

1>C:\Users\elipton\.nuget\packages\microsoft.maui.resizetizer\8.0.7\buildTransitive\Microsoft.Maui.Resizetizer.After.targets(766,9): error : Could not find a part of the path 'E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\Platforms\Windows\Package.appxmanifest'.

And then another error shows up only in the Error List:

Severity    Code    Description Project File    Line    Suppression State   Details
Error (active)      Could not find a part of the path 'E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\Platforms\Android\AndroidManifest.xml'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   at System.Xml.XmlTextReaderImpl.FinishInitUriString()
   at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
   at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
   at Xamarin.Android.Tasks.GetJavaPlatformJar.RunTask()    𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-android) C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.95\tools\Xamarin.Android.Tooling.targets   100     
Eilon commented 3 days ago

And once I start opening other files in the project, such as XAML/C# files I get even more errors in the Error List:

Severity    Code    Description Project File    Line    Suppression State   Details
Error   XLS0414 The type 'maui:MauiWinUIApplication' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.   𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞  E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\Platforms\Windows\App.xaml    1       
Error   XLS0429 Undefined namespace. The 'using' URI refers to a namespace 'Microsoft.Maui' that could not be found.    𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞  E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\Platforms\Windows\App.xaml    5       
Error (active)  CS0246  The type or namespace name 'ContentPage' could not be found (are you missing a using directive or an assembly reference?)   𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  3       
Error (active)  CS0103  The name 'InitializeComponent' does not exist in the current context    𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  9       
Error (active)  CS0103  The name 'CounterBtn' does not exist in the current context 𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  17      
Error (active)  CS0103  The name 'CounterBtn' does not exist in the current context 𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  19      
Error (active)  CS0103  The name 'SemanticScreenReader' does not exist in the current context   𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  21      
Error (active)  CS0103  The name 'CounterBtn' does not exist in the current context 𫓧龦唉丂荳◎℉㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞ (net8.0-windows10.0.19041.0) E:\repos\BugRepros\maui22084encodeapptitle\𫓧龦唉丂荳◎℉%EE%80%B9㐁&ᠭᠤ སྲིད خەل ꌠ ᥕ᧞\MainPage.xaml.cs  21      

Also as a note, whatever fix we end up with needs to be applied to all the relevant templates: maui, maui-blazor, maui multi-project, and the net9-only maui-blazor-solution.

Eilon commented 3 days ago

For the case of dotnet new maui -n "dogs & cats" it builds fine for all platforms, but when I run it on Android Emulator I get this runtime exception:

Microsoft.Maui.Controls.Xaml.XamlParseException
  Message=Position 12:9. DataTemplateExtension: Could not locate type for local:MainPage.
  Source=Microsoft.Maui.Controls.Xaml
  StackTrace:
   at Microsoft.Maui.Controls.Xaml.DataTemplateExtension.ProvideValue(IServiceProvider serviceProvider) in D:\a\_work\1\s\src\Controls\src\Xaml\MarkupExtensions\DataTemplateExtension.cs:line 27
   at Microsoft.Maui.Controls.Xaml.DataTemplateExtension.Microsoft.Maui.Controls.Xaml.IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) in D:\a\_work\1\s\src\Controls\src\Xaml\MarkupExtensions\DataTemplateExtension.cs:line 32
   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.ProvideValue(Object& value, ElementNode node, Object source, XmlName propertyName) in D:\a\_work\1\s\src\Controls\src\Xaml\ApplyPropertiesVisitor.cs:line 286

And on Windows when I try to run it won't even let me and I get this error:

---------------------------
Microsoft Visual Studio
---------------------------
Unable to activate Windows Store app 'com.companyname.dogscats_9zz4h110yvjzm!App'. The activation request failed with error 'This application has failed to start because the application configuration is incorrect. Review the manifest file for possible errors. Reinstalling the application may fix this problem. For more details, please see the application event log'.

See help for advice on troubleshooting the issue.
---------------------------
OK   Help   
---------------------------