Browse Source

Fix #2930: follow references past the ones a document's assembly names

Whether an element is a markup extension is decided by walking its base
types, and that walk leaves the assemblies the document's own assembly
references: WPFLocalizeExtension's LocExtension, in the report, derives
from a type in XAMLMarkupExtensions, which the application never
references itself. Only the assemblies named by the module were loaded,
so the base type resolved to nothing, the extension was not recognised,
and it went out as an element tree carrying the decompiler's own
placeholder namespace - https://github.com/icsharpcode/ILSpy - into the
XAML.

The reference closure is now followed transitively. It is built once per
assembly, and the cost is bounded: for a .NET 8 WPF application the type
system grows from 87 to 158 modules and from 92 to 118 ms, for a .NET
Framework one from 9 to 24 modules and 39 to 44 ms. Over the 1158 BAML
documents of a DevExpress theme assembly the output does not change at
all - this only decides cases that used to resolve to nothing.

Assisted-by: Claude:claude-opus-5:Claude Code
pull/4110/head
Siegfried Pammer 2 weeks ago
parent
commit
03588ba4ae
  1. 9
      ICSharpCode.BamlDecompiler/BamlDecompilerTypeSystem.cs
  2. 1
      ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj
  3. 111
      ILSpy.BamlDecompiler.Tests/TransitiveReferenceTests.cs

9
ICSharpCode.BamlDecompiler/BamlDecompilerTypeSystem.cs

@ -126,6 +126,15 @@ namespace ICSharpCode.BamlDecompiler @@ -126,6 +126,15 @@ namespace ICSharpCode.BamlDecompiler
if (asm != null)
{
referencedAssemblies.Add(asm);
// Whether an element is a markup extension is decided by walking its base types,
// and a base type can sit in an assembly the document's own assembly never names.
// Stopping at the assemblies it does name leaves such a type unresolved, the
// extension unrecognised, and the decompiler's placeholder namespace in the XAML
// (issue #2930).
foreach (var indirectReference in asm.AssemblyReferences)
{
assemblyReferenceQueue.Enqueue((true, asm, indirectReference));
}
var metadata = asm.Metadata;
foreach (var h in metadata.ExportedTypes)
{

1
ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj

@ -46,6 +46,7 @@ @@ -46,6 +46,7 @@
<Compile Include="FacadeAssemblyTests.cs" />
<Compile Include="InvalidXmlCharacterTests.cs" />
<Compile Include="MissingReferencesTests.cs" />
<Compile Include="TransitiveReferenceTests.cs" />
<Compile Include="MarkupExtensionQuotingTests.cs" />
<Compile Include="RuntimeNamePropertyTests.cs" />
<Compile Include="StartupUriTests.cs" />

111
ILSpy.BamlDecompiler.Tests/TransitiveReferenceTests.cs

@ -0,0 +1,111 @@ @@ -0,0 +1,111 @@
// Copyright (c) 2026 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using ICSharpCode.BamlDecompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using NUnit.Framework;
namespace ILSpy.BamlDecompiler.Tests
{
/// <summary>
/// Deciding whether an element is a markup extension means walking its base types, and those
/// leave the assemblies a document's own assembly names. A base type landing in an assembly
/// nobody loaded resolves to nothing, the extension is not recognised, and it goes out as an
/// element tree carrying the decompiler's own placeholder namespace,
/// "https://github.com/icsharpcode/ILSpy", into the XAML (issue #2930).
/// </summary>
[TestFixture]
public class TransitiveReferenceTests
{
/// <summary>
/// ICSharpCode.BamlDecompiler stands in for a document's assembly here: it has references of
/// its own, and those references have references it does not name itself.
/// </summary>
static readonly string MainModulePath = Path.Combine(
Path.GetDirectoryName(typeof(TransitiveReferenceTests).Assembly.Location),
"ICSharpCode.BamlDecompiler.dll");
static PEFile Load(string path)
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return new PEFile(path, stream, streamOptions: PEStreamOptions.PrefetchEntireImage);
}
static ICompilation TypeSystemOf(PEFile file)
{
var resolver = new UniversalAssemblyResolver(file.FileName, throwOnError: false,
file.DetectTargetFrameworkId(), file.DetectRuntimePack());
return new BamlDecompilerTypeSystem(file, resolver);
}
/// <summary>
/// An assembly the main module reaches only through one of its own references. Derived from
/// the metadata rather than named here, so that changing what either assembly references
/// cannot quietly turn this test into one that proves nothing.
/// </summary>
static string IndirectlyReferencedAssembly(PEFile mainModule)
{
var direct = new HashSet<string>(mainModule.AssemblyReferences.Select(r => r.Name));
foreach (var reference in mainModule.AssemblyReferences)
{
if (reference.Name != "ICSharpCode.Decompiler")
continue;
using var referenced = Load(Path.Combine(
Path.GetDirectoryName(mainModule.FileName), reference.Name + ".dll"));
string indirect = referenced.AssemblyReferences
.Select(r => r.Name)
.FirstOrDefault(name => !direct.Contains(name));
Assert.That(indirect, Is.Not.Null,
"the premise of this test is gone: every assembly ICSharpCode.Decompiler "
+ "references is now referenced by ICSharpCode.BamlDecompiler as well");
return indirect;
}
Assert.Fail("ICSharpCode.BamlDecompiler no longer references ICSharpCode.Decompiler");
return null;
}
[Test]
public void AnAssemblyReachedOnlyThroughAReferenceIsLoaded()
{
using var mainModule = Load(MainModulePath);
string indirect = IndirectlyReferencedAssembly(mainModule);
var compilation = TypeSystemOf(mainModule);
Assert.That(compilation.Modules.Select(m => m.AssemblyName), Does.Contain(indirect));
}
[Test]
public void TheAssembliesTheModuleNamesItselfAreStillLoaded()
{
using var mainModule = Load(MainModulePath);
var compilation = TypeSystemOf(mainModule);
Assert.That(compilation.Modules.Select(m => m.AssemblyName),
Does.Contain("ICSharpCode.Decompiler"));
}
}
}
Loading…
Cancel
Save