Registry Mirror
Skip to content

Getting Stub instead of real type in "FollowCustomCondition" #351

Closed

Description

@micha-frey

Hi all,

we are currently setting up the first tests with ArchUnitNET (btw. great framework 😊 ), but we are facing an issue with inside the "FollowCustomCondition" for PropertyMembers. The type that we get for the evaluation is marked with "IsStub == true" and that also probably means, that the base class is not set etc. See the following screenshot for the full rule.

Unfortunately, if we try to reproduce it in a small test project, we are not receiving the stubs. Any idea, what we can do to fix this, or is this somehow a bug in ArchUnitNET?

Image

Thanks in advance,
Michael

Activity

  1. micha-frey commented on Mar 3, 2025

    @micha-frey
    Author

    I have also find out, that even when the full classes are in the "loaded architecture" the base classes seems to be stubs when calling

    _architecture.GetClassOfType()

    results in the following issues:

    Image

    That behavior makes it really hard to use rules like "AreAssignableTo" as the result highly depends on whether stubs where used or not.

  2. added
    kind/support
    on Mar 3, 2025
  3. alexanderlinne commented on Mar 3, 2025

    @alexanderlinne
    Collaborator

    Hi @FreyAtGit, glad to hear that you like the library.

    With the information given I can really only make assumptions about what might go wrong. Stub types are generated for assemblies that are not explicitly loaded or requested to be loaded as a transitive dependency. That means that if you load your assembly through new ArchLoader().LoadAssemblies(…).Build(), then only the types from assemblies explicitly given as a parameter are loaded as non-stub types. Alternatively, you can also use LoadAssembliesIncludingDependencies(…) which includes the direct dependencies of explicitly loaded assemblies or LoadAssembliesIncludingDependencies(…, true) which also includes transitive dependencies.

    Regarding your picture, I could unfortunately not reproduce that. If I query a non-stub type through GetClassOfType then it is also non-stub when lookup up as the base class of another type.

    As a next step, I would recommend including all dependencies in your architecture to rule out any other bugs. Also, could you check which version of the library you are using?

  4. micha-frey commented on Mar 5, 2025

    @micha-frey
    Author

    Hi @alexanderlinne,

    the assemblies are loaded directly. In one case, alle types are in the same assembly. So, I don't think that it is related to missing loaded assemblies. And I would guess that I would not get any class if I would query one, that it is not loaded or just a simple "outside" dependency.

    I tried to point out, that if I query for each class separately I get the non-stub instance. So, the information is somehow there, but the base classes are not using these instances, they are creating stubs.

    If I try the reproduction in a simple test project, then I also had no success yet. There I get for the base classes the same instances as if I would query them directly.

    Just a guess without looking in your code: Are there performance optimization rules that create stubs even if the real type is loaded in the architecture?

  5. alexanderlinne commented on Mar 5, 2025

    @alexanderlinne
    Collaborator

    No, there are no such optimizations. In previous versions there were problems with multiple instances being created for the same type, but those should be resolved with 0.11.3. Did you check which version you are using?

  6. micha-frey commented on Mar 5, 2025

    @micha-frey
    Author

    Oh sorry, forgot to mention. We are currently with version 0.11.2. Would you recommend trying it with 0.11.3?

  7. alexanderlinne commented on Mar 5, 2025

    @alexanderlinne
    Collaborator

    Yes, then we can rule out whether this has been fixed with the changes included in 0.11.3

  8. micha-frey commented on Mar 7, 2025

    @micha-frey
    Author

    With the version 0.11.3, I am getting exceptions during loading of the architecture (same key already added to dictionary). I am investigating. Stay tuned ;)

  9. micha-frey commented on Mar 7, 2025

    @micha-frey
    Author

    The exception is in the TypeFactory.GetOrCreateGenericInstanceTypeInstanceFromTypeReference(). By just looking at the code, I don't understand how this can happen besides parallel execution / parallel modifications of the _allTypes dictionary.

    Image

  10. alexanderlinne commented on Mar 10, 2025

    @alexanderlinne
    Collaborator

    Since the methods creating the type instances are calling each other recursively this can happen when the same type that is currently being instantiated is also created as part of the recursive call. So we would need to add the type to the map before the recursive call in line 180 and add the generic arguments to the type instance afterwards. However, I can't come up with a test that leads to this error. At the point where the exception is thrown, could you have a look at the type that it is trying to add? Then we can maybe figure out the general structure of a type that leads to this behaviour and write a test case.

  11. added
    kind/bug
    and removed
    kind/support
    on Mar 10, 2025
  12. micha-frey commented on Mar 13, 2025

    @micha-frey
    Author

    The issue seems to be a problem in the recursion. See the callstack

    Image

    So, it passed (false branch) the TryGetValue check and then the method is called recursively adding the value and then it is not checked again whether the value is already in.

    The type that is failing is "System.Linq.IGrouping`2<User,T>, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

  13. micha-frey commented on Mar 14, 2025

    @micha-frey
    Author

    When I fix this dictionary issue locally, then it feels like the version 0.11.3 would fix the "stub"-issue :) So, maybe I will wait for the next version and try it again.

  14. alexanderlinne commented on Mar 28, 2025

    @alexanderlinne
    Collaborator

    Just from the stack trace I can't quite build a test case that reproduces the error. Ideally we would include one with the fix. Would you be able to either give me some more information about the code that leads to the error or write a small test case that reproduces it?

  15. micha-frey commented on Mar 28, 2025

    @micha-frey
    Author

    I will try, but it might take some time ...

  16. micha-frey commented on Apr 8, 2025

    @micha-frey
    Author

    Hi @alexanderlinne ,

    I can provide you an example now, that leads to this exception. I tried to reduce it even more, but could not. It might be related to the local function combined with the Linq expressions and the cast to (IReadOnlyCollection) and especially that it is a generic method. Hopefully, that helps you fixing the issue.

    using System.Collections.Generic;
    using System.Linq;
    
    namespace Test
    {
        class MyTest
        {
    	void Foo()
            {
                LocalFunc<MyObject>();
    			
                void LocalFunc<T>()
                {
                    var list = new List<MyObject>();
                    list.GroupBy(x => x.Parent)
                        .ToDictionary(g => g.Key, g => (IReadOnlyCollection<T>)g.ToList());
                }
            }
        }
    
        class MyObject
        {
            public MyObject Parent { get; set; }
        }
    }
    
  17. alexanderlinne commented on Apr 11, 2025

    @alexanderlinne
    Collaborator

    Thank you for the example. I've created a pull request that should fix the issue

  18. added
    triage/accepted
    and removed on Apr 25, 2025
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/bugtriage/accepted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions