-
Notifications
You must be signed in to change notification settings - Fork 538
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
Change Namespace to _Microsoft.Android.Resource.Designer #7681
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs
Show resolved
Hide resolved
jonpryor
reviewed
Jan 11, 2023
src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs
Outdated
Show resolved
Hide resolved
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
2ec6d5e
to
d892d8e
Compare
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
39854be
to
8c4c56b
Compare
jonathanpeppers
approved these changes
Jan 17, 2023
Context: dc3ccf28cdbe9f8c0a705400b83c11a85c81a980
Context: https://github.com/dotnet/maui/pull/12520#issuecomment-1376431313
Context: https://discord.com/channels/732297728826277939/732297837953679412/1062137297438519296
Context: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0002
Consider this C# fragment:
namespace Android.Content {
public class Intent {
}
}
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (Android.Content.Intent intent) {}
}
}
Given a C# *namespace_or_type_name* such as `Android.Content.Intent`,
[ECMA 334 §7.8.1][0] will be used to determine what the type refers
to, resolving it to `global::Android.Content.Intent`.
All is good.
However, once dc3ccf28 enters the picture, we now have:
// via dc3ccf28 & _Microsoft.Android.Resource.Designer.dll
namespace Microsoft.Android.Resource.Designer {
public class Resource {
}
}
namespace Android.Content {
public class Intent {
}
}
namespace Microsoft.Maui.Example {
public class Helper {
// CS0234 on the following line:
public static void UseIntent (Android.Content.Intent intent) {}
}
}
…and it fails to build with [error CS0234][1]:
error CS0234: The type or namespace name 'Content' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?)
This only happens if the usage of `Android.Content.Intent` happens
*within* a `Microsoft.`-prefixed namespace. Which is most of MAUI.
The cause of the error is that while attempting to resolve
`Android.Content.Intent`, search starts from the *current* namespace
`Microsoft.Maui.Example`, and the compiler will attempt to resolve
the types:
* `Microsoft.Maui.Example.Android.Content.Intent`
(because the usage site is in the `Microsoft.Maui.Example` namespace.)
* `Microsoft.Maui.Android.Content.Intent`
(because `Microsoft.Maui` is a parent namespace)
* `Microsoft.Android.Content.Intent`
(because `Microsoft` is a parent parent namespace)
These are the *only* types that are resolved, and none of those exist.
This results in the CS0234 error.
There are two workarounds that the "offending" code can employ:
1. Use `global`:
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (global::Android.Content.Intent intent) {}
}
}
2. Add a `using Android.Content` and use `Intent`
using Android.Content;
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (Intent intent) {}
}
}
Both of these require changing things *outside* of xamarin-android.
Rephrasing & simplifying: commit dc3ccf28 constitutes an *API break*,
as code which *previously* compiled *no longer compiles*.
Fix this by updating the `<GenerateResourceDesignerAssembly/>` task
emits types into the `_Microsoft.Android.Resource.Designer` namespace;
note `_` prefix. No One™ should have their code in a `_Microsoft.*`
namespace, so this shouldn't break anybody. Additionally, update
`_Microsoft.Android.Resource.Designer.dll` so that
[`EditorBrowsableAttribute`][2] is placed on all the types, a'la:
namespace _Microsoft.Android.Resource.Designer;
[EditorBrowsable (EditorBrowsableState.Never)]
public partial class Resource {
}
// For App project builds
[EditorBrowsable (EditorBrowsableState.Never)]
internal partial class ResourceConstant {
}
Finally, update the Source Compatibility types so that the `Resource`
type disables the IDE0002 warning:
namespace %CompatibilityNamespace% {
#pragma warning disable IDE0002
public partial class Resource : _Microsoft.Android.Resource.Designer.Resource {
}
#pragma warning restore IDE0002
}
[0]: https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/basic-concepts.md#78-namespace-and-type-names
[1]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0234
[2]: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.editorbrowsableattribute?view=net-7.0
[3]: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0002 |
grendello
added a commit
to grendello/xamarin-android
that referenced
this pull request
Jan 18, 2023
* main: [Mono.Android] Android.Telecom.InCallService.SetAudioRoute() + enum (dotnet#7711) [Mono.Android] Fix some incorrect enums. (dotnet#7670) [Xamarin.Android.Build.Tasks] _Microsoft.Android.Resource.Designer namespace (dotnet#7681) LEGO: Merge pull request 7713 [Xamarin.Android.Build.Tasks] lazily populate Resource lookup (dotnet#7686)
grendello
added a commit
to grendello/xamarin-android
that referenced
this pull request
Jan 19, 2023
* main: [ci] Pass token when building Designer tests (dotnet#7715) [Mono.Android] Android.Telecom.InCallService.SetAudioRoute() + enum (dotnet#7711) [Mono.Android] Fix some incorrect enums. (dotnet#7670) [Xamarin.Android.Build.Tasks] _Microsoft.Android.Resource.Designer namespace (dotnet#7681) LEGO: Merge pull request 7713 [Xamarin.Android.Build.Tasks] lazily populate Resource lookup (dotnet#7686)
grendello
added a commit
to grendello/xamarin-android
that referenced
this pull request
Jan 26, 2023
* main: (32 commits) [monodroid] Replace `exit()` with `abort()` in native code (dotnet#7734) Bump to xamarin/Java.Interop/main@8a1ae57 (dotnet#7738) [build] bump `$(AndroidNet7Version)` (dotnet#7737) Bump to xamarin/Java.Interop/main@1366d99 (dotnet#7718) [Xamarin.Android.Build.Tasks] fix AndroidGenerateResourceDesigner (dotnet#7721) Bump to xamarin/monodroid@50faac94 (dotnet#7725) Revert "[Xamarin.Android.Build.Tasks] fix cases of missing `@(Reference)` (dotnet#7642)" (dotnet#7726) [marshal methods] Properly support arrays of arrays (dotnet#7707) Bump to dotnet/installer@9962c6a 8.0.100-alpha.1.23063.11 (dotnet#7677) [Actions] Add action to bump the hash used for the unified pipeline (dotnet#7712) Bump to xamarin/xamarin-android-tools/main@099fd95 (dotnet#7709) [ci] Move build stages into yaml templates (dotnet#7553) [Xamarin.Android.Build.Tasks] fix NRE in `<GenerateResourceCaseMap/>` (dotnet#7716) [ci] Pass token when building Designer tests (dotnet#7715) [Mono.Android] Android.Telecom.InCallService.SetAudioRoute() + enum (dotnet#7711) [Mono.Android] Fix some incorrect enums. (dotnet#7670) [Xamarin.Android.Build.Tasks] _Microsoft.Android.Resource.Designer namespace (dotnet#7681) LEGO: Merge pull request 7713 [Xamarin.Android.Build.Tasks] lazily populate Resource lookup (dotnet#7686) [Xamarin.Android.Build.Tasks] skip XA1034 logic in some cases (dotnet#7680) ...
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We if we used the current
Microsoft.Android.Resource.Designer
namespace we can get into issues with any other library which usesMicrosoft
in the namespace. When building maui we see the following build errorThis is because rather than looking for
Content
in the globalAndroid
namespace, it looks for it in theMicrosfot.Android
namespace. This can be fixed by the end user by addingglobal::
to the type usage, but this is not desireable in all senarios. Given this is an internal class which really should not be used lets change the namespace instead.In addition we should add a
#pragma
forIDE0002
[1] so that users are NOT asked to simplify the Resource designer class declaration. We also need to add theEditorBrowsable
attribute so that the types do not show up in code completion in the IDE.[1] https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0002