Skip to content

[assembly-preparer] Fix IL2036 unresolved type in DynamicDependency with link-all. Fixes #26017#26040

Open
rolfbjarne wants to merge 1 commit into
mainfrom
dev/rolf/issue-26017-assembly-preparer-nsexpression
Open

[assembly-preparer] Fix IL2036 unresolved type in DynamicDependency with link-all. Fixes #26017#26040
rolfbjarne wants to merge 1 commit into
mainfrom
dev/rolf/issue-26017-assembly-preparer-nsexpression

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

When building with PrepareAssemblies=true and link all (MtouchLink=Full), ILLink failed with:

error IL2036: <Module>..cctor(): Unresolved type 'Foundation.NSExpression' in 'DynamicDependencyAttribute'.
error NETSDK1144: Optimizing assemblies for size failed.

The assembly-preparer's ApplyPreserveAttributeStep translates [Preserve (typeof (X))] into a [DynamicDependency] attribute on the module's static constructor. CreateDynamicDependencyAttribute (DynamicallyAccessedMemberTypes, TypeDefinition) passed the raw cross-assembly TypeDefinition as the Type argument. Cecil serializes such an argument without an assembly-qualified name, because a TypeDefinition's Scope is its own module (so RequiresFullyQualifiedName returns false). The resulting attribute referenced Foundation.NSExpression with no assembly, which ILLink couldn't resolve once the app assembly was trimmed in full-link mode.

Fix: import the type into the current assembly's module before using it as the Type argument, so Cecil emits a proper assembly-qualified name (Foundation.NSExpression, Microsoft.iOS, ...).

This only affected the assembly-preparer path: the regular linker step emits an XML descriptor instead of [DynamicDependency] attributes. It also only surfaced with link all, since the module cctor's attributes are only processed by ILLink when the app assembly is trimmed.

No new test is needed: on .NET 11 (where PrepareAssemblies defaults to true) the existing xharness monotouch-test "Release (link all)" variation builds monotouch-test with link all + PrepareAssemblies, and monotouch-test contains [assembly: Preserve (typeof (NSExpression))] (tests/monotouch-test/Foundation/NSExpressionTest.cs), which reproduces this exact failure.

Fixes #26017

🤖 Pull request created by Copilot

…ith link-all. Fixes #26017

When building with `PrepareAssemblies=true` and link all (`MtouchLink=Full`),
ILLink failed with:

    error IL2036: <Module>..cctor(): Unresolved type 'Foundation.NSExpression' in 'DynamicDependencyAttribute'.
    error NETSDK1144: Optimizing assemblies for size failed.

The assembly-preparer's `ApplyPreserveAttributeStep` translates
`[Preserve (typeof (X))]` into a `[DynamicDependency]` attribute on the module's
static constructor. `CreateDynamicDependencyAttribute (DynamicallyAccessedMemberTypes, TypeDefinition)`
passed the raw cross-assembly `TypeDefinition` as the `Type` argument. Cecil
serializes such an argument *without* an assembly-qualified name, because a
`TypeDefinition`'s `Scope` is its own module (so `RequiresFullyQualifiedName`
returns false). The resulting attribute referenced `Foundation.NSExpression`
with no assembly, which ILLink couldn't resolve once the app assembly was
trimmed in full-link mode.

Fix: import the type into the current assembly's module before using it as the
`Type` argument, so Cecil emits a proper assembly-qualified name
(`Foundation.NSExpression, Microsoft.iOS, ...`).

This only affected the assembly-preparer path: the regular linker step emits an
XML descriptor instead of `[DynamicDependency]` attributes. It also only
surfaced with link all, since the module cctor's attributes are only processed
by ILLink when the app assembly is trimmed.

No new test is needed: on .NET 11 (where `PrepareAssemblies` defaults to true)
the existing xharness `monotouch-test` "Release (link all)" variation builds
monotouch-test with link all + PrepareAssemblies, and monotouch-test contains
`[assembly: Preserve (typeof (NSExpression))]`, which reproduces this exact
failure.

Fixes #26017.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 10, 2026 12:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a trimming/link-all failure in the assembly-preparer path where [Preserve (typeof (X))] is translated into a module-level [DynamicDependency] that ILLink couldn’t resolve due to a non-assembly-qualified Type argument.

Changes:

  • Import cross-assembly TypeDefinition values into the current module before emitting them as the Type constructor argument for [DynamicDependency], ensuring Cecil serializes an assembly-qualified type name.
  • Prevents IL2036 “Unresolved type … in DynamicDependencyAttribute” when PrepareAssemblies=true and MtouchLink=Full / full trim is enabled.

@rolfbjarne rolfbjarne enabled auto-merge (squash) July 10, 2026 13:23
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 66caa58cf249d229518413b811ee9d3c0c4276df [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #66caa58] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

2 tests crashed, 0 tests failed, 193 tests passed.

Failures

❌ framework tests

🔥 Failed catastrophically on VSTS: test results - framework (no summary found).

Html Report (VSDrops) Download

❌ interdependent-binding-projects tests

🔥 Failed catastrophically on VSTS: test results - interdependent-binding-projects (no summary found).

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 17 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 18 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 66caa58cf249d229518413b811ee9d3c0c4276df [PR build]

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[assembly-preparer] PrepareAssemblies + link all (Full trim) fails: IL2036 Unresolved type 'Foundation.NSExpression' in DynamicDependencyAttribute

3 participants