Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xamlg] improve error for Xamarin.Forms namespace #19683

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Controls/src/SourceGen/CodeBehindGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ static void GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation
if (!TryParseXaml(text, uid, compilation, caches, context.CancellationToken, projItem.TargetFramework, out var accessModifier, out var rootType, out var rootClrNamespace, out var generateDefaultCtor, out var addXamlCompilationAttribute, out var hideFromIntellisense, out var XamlResourceIdOnly, out var baseType, out var namedFields, out var parseException))
{
if (parseException != null)
context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError, null, parseException.Message));
{
var location = projItem.RelativePath is not null ? Location.Create(projItem.RelativePath, new TextSpan(), new LinePositionSpan()) : null;
context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError, location, parseException.Message));
}
return;
}
var sb = new StringBuilder();
Expand Down Expand Up @@ -260,6 +263,14 @@ static bool TryParseXaml(SourceText text, string uid, Compilation compilation, A
return false;
}

#pragma warning disable CS0618 // Type or member is obsolete
if (xmlDoc.DocumentElement.NamespaceURI == XamlParser.FormsUri)
{
exception = new Exception($"{XamlParser.FormsUri} is not a valid namespace. Use {XamlParser.MauiUri} instead");
return false;
}
#pragma warning restore CS0618 // Type or member is obsolete

cancellationToken.ThrowIfCancellationRequested();

// if the following xml processing instruction is present
Expand Down
5 changes: 4 additions & 1 deletion src/Controls/src/Xaml/XamlParser.Namespaces.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#nullable disable
using System;

namespace Microsoft.Maui.Controls.Xaml
{
static partial class XamlParser
{
[Obsolete("Should not be used except for migration/error message purposes")]
public const string FormsUri = "http://xamarin.com/schemas/2014/forms";
public const string MauiUri = "http://schemas.microsoft.com/dotnet/2021/maui";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like reintroducing the old namespace in the source code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I put [Obsolete] on it? and just ignore the warning in one place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

public const string MauiDesignUri = "http://schemas.microsoft.com/dotnet/2021/maui/design";
public const string X2006Uri = "http://schemas.microsoft.com/winfx/2006/xaml";
Expand Down
3 changes: 2 additions & 1 deletion src/Controls/src/Xaml/XmlTypeXamlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ static class XmlTypeXamlExtensions
potentialTypes.Add(new(typeName, xmlnsDefinitionAttribute.ClrNamespace, xmlnsDefinitionAttribute.AssemblyName));

// As a fallback, for assembly=mscorlib try assembly=System.Private.CoreLib
if (xmlnsDefinitionAttribute.AssemblyName == "mscorlib" || xmlnsDefinitionAttribute.AssemblyName.StartsWith("mscorlib,", StringComparison.Ordinal))
if (xmlnsDefinitionAttribute.AssemblyName is string assemblyName &&
(assemblyName == "mscorlib" || assemblyName.StartsWith("mscorlib,", StringComparison.Ordinal)))
{
potentialTypes.Add(new(typeName, xmlnsDefinitionAttribute.ClrNamespace, "System.Private.CoreLib"));
}
Expand Down
Loading