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

[.NET 10] change default value for PublishTrimmed #9526

Open
jonathanpeppers opened this issue Nov 19, 2024 · 0 comments
Open

[.NET 10] change default value for PublishTrimmed #9526

jonathanpeppers opened this issue Nov 19, 2024 · 0 comments
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Milestone

Comments

@jonathanpeppers
Copy link
Member

jonathanpeppers commented Nov 19, 2024

Android framework version

net9.0-android

Affected platform version

any

Description

Context: #9525
Context: dotnet/runtime#109724

As seen in the above issue, there is currently the expectation in .NET:

  • PublishTrimmed=true set for all configurations, it means "I intend to trim this project"
  • Not all builds need to actually run illink. For console apps, dotnet build doesn't trim or illink, dotnet publish does trim and run illink.
  • .NET SDK sets various feature switches based on PublishTrimmed

Unfortunately, Android doesn't follow this pattern:

  • Debug mode, PublishTrimmed=false for build performance, inner loop
  • Release mode, PublishTrimmed=true

To fix this in .NET 10, we should:

  • Set PublishTrimmed=true always
  • Disable illink in Debug mode via other means

Steps to Reproduce

  1. dotnet new android
  2. dotnet build (debug mode)
  3. Observe RuntimeInstanceNotAllowed Error Initializing XmlSerializer on Android Release Build in .NET 9 runtime#109724

Did you find any workaround?

For the case above you could set $(_DefaultValueAttributeSupport) as done in #9525.

@jonathanpeppers jonathanpeppers added the Area: App+Library Build Issues when building Library projects or Application projects. label Nov 19, 2024
@jonathanpeppers jonathanpeppers added this to the .NET 10 milestone Nov 19, 2024
@dotnet-policy-service dotnet-policy-service bot added the needs-triage Issues that need to be assigned. label Nov 19, 2024
jonathanpeppers added a commit that referenced this issue Nov 20, 2024
…=partial` (#9525)

Context: dotnet/runtime#109724

In .NET 9, certain apps could crash with:

    System.ArgumentException: RuntimeInstanceNotAllowed
      ?, in object DefaultValueAttribute.get_Value()
      ?, in new XmlAttributes(ICustomAttributeProvider)
      ?, in XmlAttributes XmlReflectionImporter.GetAttributes(MemberInfo)
      ?, in bool XmlReflectionImporter.InitializeStructMembers(StructMapping, StructModel, bool, string, RecursionLimiter)
      ?, in StructMapping XmlReflectionImporter.ImportStructLikeMapping(StructModel, string, bool, XmlAttributes, RecursionLimiter)

.NET's concept of `$(PublishTrimmed)` is used to decide which trimmer
feature switches are toggled. This is normally set for both Debug &
Release, but Android only sets it for Release. This means that the
`$(_DefaultValueAttributeSupport)` feature switch is not set properly
in some cases, which causes the crash.

For now, we can set `$(_DefaultValueAttributeSupport)` for
`TrimMode=partial`, the default in .NET MAUI apps.

See #9526 for how we might
better address this in the future.

In order to test this change:

* Add a `XmlSerializerTest` to `Mono.Android-Tests`

* Run a copy of `Mono.Android-Tests` with `-p:TestsFlavor=TrimModePartial`

* Also set `$(_DefaultValueAttributeSupport)` for `TrimMode=full` in
  our test project, so `XmlSerializerTest` will pass in that mode as
  well.

Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
jonathanpeppers added a commit that referenced this issue Nov 20, 2024
…=partial` (#9525)

Context: dotnet/runtime#109724

In .NET 9, certain apps could crash with:

    System.ArgumentException: RuntimeInstanceNotAllowed
      ?, in object DefaultValueAttribute.get_Value()
      ?, in new XmlAttributes(ICustomAttributeProvider)
      ?, in XmlAttributes XmlReflectionImporter.GetAttributes(MemberInfo)
      ?, in bool XmlReflectionImporter.InitializeStructMembers(StructMapping, StructModel, bool, string, RecursionLimiter)
      ?, in StructMapping XmlReflectionImporter.ImportStructLikeMapping(StructModel, string, bool, XmlAttributes, RecursionLimiter)

.NET's concept of `$(PublishTrimmed)` is used to decide which trimmer
feature switches are toggled. This is normally set for both Debug &
Release, but Android only sets it for Release. This means that the
`$(_DefaultValueAttributeSupport)` feature switch is not set properly
in some cases, which causes the crash.

For now, we can set `$(_DefaultValueAttributeSupport)` for
`TrimMode=partial`, the default in .NET MAUI apps.

See #9526 for how we might
better address this in the future.

In order to test this change:

* Add a `XmlSerializerTest` to `Mono.Android-Tests`

* Run a copy of `Mono.Android-Tests` with `-p:TestsFlavor=TrimModePartial`

* Also set `$(_DefaultValueAttributeSupport)` for `TrimMode=full` in
  our test project, so `XmlSerializerTest` will pass in that mode as
  well.

Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
@jpobst jpobst removed the needs-triage Issues that need to be assigned. label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

No branches or pull requests

3 participants