ArchUnitNET
ArchUnitNET copied to clipboard
"Sequence contains no matching element" when building the Architecture
Problem Description
In AddMethodDependencies.HandleIterator(), the first found instruction with new object op code is an ArgumentNullException. The TypeDefinition of the declaring type does not have a method named "MoveNext", which causes an InvalidOperationException since First() does not find a matching method definition.
Possible Cause
Usage of JetBrains.Annotations.NotNullAttribute in the method associated with the method body.
Possible Fix (?) Find the first instruction with new object op code which at the same time is not an exception.
Stack Trace
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.ThrowHelper.ThrowNoMatchException()
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at ArchUnitNET.Loader.LoadTasks.AddMethodDependencies.HandleIterator(MethodDefinition& methodDefinition, MethodBody& methodBody, List`1 bodyTypes, ICollection`1 visitedMethodReferences)
at ArchUnitNET.Loader.LoadTasks.AddMethodDependencies.CreateMethodBodyDependencies(MethodDefinition methodDefinition, MethodMember methodMember)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
at ArchUnitNET.Loader.LoadTasks.AddMethodDependencies.<>c.<Execute>b__4_3(ValueTuple`2 tuple)
at ArchUnitNET.Domain.Extensions.EnumerableExtensions.ForEach[T](IEnumerable`1 source, Action`1[] actions)
at ArchUnitNET.Loader.LoadTasks.AddMethodDependencies.Execute()
at ArchUnitNET.Loader.LoadTaskRegistry.<ExecuteTasks>b__1_1(Type taskKey)
at ArchUnitNET.Domain.Extensions.EnumerableExtensions.ForEach[T](IEnumerable`1 source, Action`1[] actions)
at ArchUnitNET.Loader.LoadTaskRegistry.ExecuteTasks(List`1 taskOrder)
at ArchUnitNET.Loader.ArchBuilder.UpdateTypeDefinitions()
at ArchUnitNET.Loader.ArchBuilder.Build()
at ArchUnitNET.Loader.ArchLoader.Build()
Hi @Optano-Dev, could you provide a small example project that replicates the issue?
Hi @alexanderlinne, thank you very much for getting back to me.
Unfortunately, I was not able to produce a small example. However, I could narrow down the cause:
The problem occurs when using https://github.com/Fody/NullGuard (Mode="Explicit" IncludeDebugAssert="false"). This library generates code that throws ArgumentNullExceptions when objects annotated with [NotNull] are null.
As far as I could see, the problem occurred on methods which return IEnumerables, e.g.
public IEnumerable<SomeType> GetObjects([NotNull] SomeParameter someParameter)
In AddMethodDependencies.HandleIterator(), the instructions of the method body looked like this:
methodBody.Instructions = InstructionCollection
[0] = {Instruction} IL_0000: ldarg.1
[1] = {Instruction} IL_0001: brtrue.s IL_0013
[2] = {Instruction} IL_0003: ldstr "someParameter"
[3] = {Instruction} IL_0008: ldstr "[NullGuard] someParameter is null."
[4] = {Instruction} IL_000d: newobj System.Void System.ArgumentNullException::.ctor(System.String,System.String)
[5] = {Instruction} IL_0012: throw
[6] = {Instruction} IL_0013: ldc.i4.s -2
[7] = {Instruction} IL_0015: newobj System.Void SomeClass/<GetObjects>d__6::.ctor(System.Int32)
...
The critical part seems to be that the first newobj is the ArgumentNullException, which does not have a MoveNext() method (see https://github.com/TNG/ArchUnitNET/blob/main/ArchUnitNET/Loader/LoadTasks/AddMethodDependencies.cs#L330-L342).
Hope this helps to understand the problem.