Skip to content

Encoding.ASCII is trimmed in Android/iOS in .NET8 #97576

Open

Description

@devWR

Description

When trimming is enabled I cannot retrieve ASCII Encoding object. It looks like it's removed from Environment variables. It's working when trimmiing is disabled. Sample attached:
Expected:

  • Click button
  • Button text will update with Encoding name (US-ASCII) and encoding object will be initiated
    Actual:
  • Click button
  • Button text updates with incorrect name: Globalization_cp_20127 and then crashes when trying to retrieve encoding object

I tried to include in Linker.xml:



but unfortunately it didn't help.

Steps to Reproduce

  1. Create project in Maui (can be Android or iOS)
  2. Enable trimming
  3. Try to invoke: System.Text.Encoding.GetEncoding(System.Text.Encoding.ASCII.EncodingName, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback.
  4. Crash happens

Link to public reproduction project repository

https://github.com/devWR/maui_encodingissue

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

Android 14/iOS15

Did you find any workaround?

No

Relevant log output

Encoding ASCII: Globalization_cp_20127
Argument_EncodingNotSupported, Globalization_cp_20127 Arg_ParamName_Name, name
System.ArgumentException: Argument_EncodingNotSupported, Globalization_cp_20127 Arg_ParamName_Name, name
   at System.Text.EncodingTable.InternalGetCodePageFromName(String )
   at System.Text.EncodingTable.GetCodePageFromName(String )
   at System.Text.Encoding.GetEncoding(String , EncoderFallback , DecoderFallback )

Activity

  1. transferred this issue fromdotnet/mauion Jan 26, 2024
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 26, 2024
  3. removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 27, 2024
  4. added this to the 8.0.x milestone on Jan 27, 2024
  5. jeffschwMSFT commented on Jan 29, 2024

    @jeffschwMSFT
    Member

    fyi, we are deprecating area-corelib-mono and replacing with specific areas paths

  6. vitek-karas commented on Mar 1, 2024

    @vitek-karas
    Member

    This is because of resource stripping.
    To reduce application size, we strip all string resources from CoreLib - this will remove the resources used by EncodingTable.GetDisplayName link.
    This means that in trimmed app the display name is returned as the "wrong" Globalization_cp_20127 since that's the resource key name. But the reverse lookup table which is in

    stays the same. So the reverse lookup fails since the wrong name is not there.

    The correct usage is to use WebName instead - see the docs here https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getencoding?view=net-8.0#system-text-encoding-getencoding(system-string). It states that any WebName is valid. EncodingName is a humanly readable string, potentially localized, so the reverse lookup won't work for it.

    That said - it's a good question if MAUI should be trimming encoding display names in the first place.

    @jeffhandley as the owner of the System.Text.Encoding - mostly for opinions on the behavior of trimmed app as described above.

  7. vitek-karas commented on Jul 9, 2024

    @vitek-karas
    Member

    This is basically a problem where the display name is broken when UseSystemResourceKeys=true which is the default for all mobile and Blazor projects. @jeffhandley I'll leave it up to you to decide what to do about this - I'll be glad to help with the technical specifics around trimming if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Text.Encodinglinkable-frameworkIssues associated with delivering a linker friendly frameworktrimming-for-aot`EnableAggressiveTrimming=true` used for running tests with AOT

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions