Skip to content

[Bug]: NullReferenceException in ResidesInNamespace when using Types(true) #492

Closed

Description

@AvremelM

Description

Namespace predicates (ResideInNamespace, ResideInNamespaceMatching) throw a NullReferenceException when the architecture includes function-pointer types, because IType.Namespace is null for them and the extension dereferences it without a guard.

Minimal Working Example

using System.Linq;
using ArchUnitNET.Loader;

var architecture = new ArchLoader()
    .LoadAssemblies(typeof(System.DateTime).Assembly)   // System.Private.CoreLib
    .Build();

// Works fine
Types().That().ResideInNamespace("System").GetObjects(architecture).Count(); 

// Throws NRE
Types(true).That().ResideInNamespace("System").GetObjects(architecture).Count();

Expected Behavior

Types with null namespaces would not match any namespace predicate.

Actual Behavior

Types with null namespaces cause a NullReferenceException

Stack

Object reference not set to an instance of an object.
   at ArchUnitNET.Domain.Extensions.NamingExtensions.FullNameEquals(IHasName cls, String fullName)
   at ArchUnitNET.Domain.Extensions.TypeExtensions.ResidesInNamespace(IType e, String fullName)
   at ArchUnitNET.Fluent.Syntax.Elements.Types.TypePredicatesDefinition`1.<>c__DisplayClass9_0.<ResideInNamespace>b__0(T type)
   at System.Linq.Enumerable.ArrayWhereIterator`1.GetCount(Boolean onlyIfCheap, ReadOnlySpan`1 source, Func`2 predicate)
   at System.Linq.Enumerable.ListWhereIterator`1.GetCount(Boolean onlyIfCheap)

ArchUnitNET Version

0.13.4

.NET Version

.NET 9 (or any CoreCLR version)

Additional Context

32 types with a null Namespace, all Cecil FunctionPointerType, e.g.
method System.Object *(System.IntPtr,System.Object)
method System.Boolean *(System.Object)
They appear only in ReferencedTypes (0 in Types).

Suggested fix:

  • Option 1: Null-guard the namespace extensions, e.g. return e.Namespace?.FullNameMatches(pattern) ?? false; (same for ResidesInNamespace).
  • Option 2: Skip FunctionPointerType when building the type set — ArchBuilder already filters Microsoft.CodeAnalysis.EmbeddedAttribute, System.Runtime.CompilerServices.Nullable(Context)Attribute and Coverlet*.

I'm more than happy to open a PR for either approach (or any other solution) if that helps.

Activity

  1. added
    kind/bugCategorizes issue or PR as related to a bug.
    needs-triageIndicates that an issue needs to be categorized.
    on Sep 8, 2026
  2. alexanderlinne commented on Sep 11, 2026

    @alexanderlinne
    Collaborator

    Hi @AvremelM, thanks for opening the issue. Option 1 makes more sense here. Function pointer types are mostly generated as dependencies of other types, so ignoring them could leave the dependencies of those other types incomplete.

  3. added
    triage/acceptedIndicates an issue or PR is ready to be actively worked on.
    and removed
    needs-triageIndicates that an issue needs to be categorized.
    on Sep 11, 2026
  4. iamAdarshh commented on Sep 28, 2026

    @iamAdarshh
    Contributor

    Hi @alexanderlinne, I'd like to work on this, going with option 1 as you suggested (null-guarding the namespace extensions). I'll also make the failure descriptions of the ResideInNamespace / NotResideInNamespace conditions handle a missing namespace, since they dereference it too, and add regression tests using the System.Private.CoreLib repro. I should have a PR up in the next few days.

  5. added a commit that references this issue on Sep 28, 2026
    fcefb43
  6. added 2 commits that reference this issue on Oct 9, 2026
    89530b0
    4f4d3bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.triage/acceptedIndicates an issue or PR is ready to be actively worked on.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions