Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.TestPlatform.CoreUtilities doesn't have a System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute #4638

Closed
RyuChigon opened this issue Aug 3, 2023 · 2 comments
Labels

Comments

@RyuChigon
Copy link

RyuChigon commented Aug 3, 2023

Description

Hello, I have used Microsoft.NET.Test.Sdk for testing my service (.NET 6.0).

In my service, reflection is used for finding specific custom attributes. It is okay until Microsoft.NET.Test.Sdk 17.3.3 version.

However, when using Microsoft.NET.Test.Sdk v17.4.0, I faced a problem about getting custom attributes by using MethodInfo.GetCustomAttributes(Type, bool).

The reason is

  • Microsoft.VisualStudio.TestPlatform.Common.Filtering.Condition::ThrownFormatExceptionForInvalidCondition(string) has DoesNotReturnAttribute
  • But, System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute could not be loaded from Microsoft.TestPlatform.CoreUtilities.

assembly_ThrowFormatExceptionForInvalidCondition_17_4_0

This is the IL of Void ThrowFormatExceptionForInvalidCondition(string).
It refers to System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute of Microsoft.TestPlatform.CoreUtilities.

I think Microsoft.VisualStudio.TestPlatform.Common.dll has wrong reference about System.Diagnostics.CodeAnalysis.

Could you solve this problem? Thank you :)


IL of Microsoft.TestPlatform.CoreUtilities v17.3.3

assembly_CoreUtilities_17_3_3

It includes DoesNotReturnAttribute


IL of Microsoft.TestPlatform.CoreUtilities v17.4.0

assembly_CoreUtilities_17_4_0

not include DoesNotReturnAttribute


Sample Code

using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var targetAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            var targetTypes = targetAssemblies.SelectMany(assembly => assembly.GetTypes());

            var targetMethodBindingFlag
                = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

            var targetMethods = targetTypes.SelectMany(type => type.GetMethods(targetMethodBindingFlag));

            foreach (var method in targetMethods)
            {
                var attributes = method.GetCustomAttributes(); // TypeLoadException !!
            }
        }
    }
}

Dependencies

  • .NET 6.0
  • Microsoft.NET.Test.Sdk (17.4.0)
  • MSTest.TestAdapter (2.2.10)
  • MSTest.TestFramework (2.2.10)

Diagnostic logs

  Message: 
Test method TestProject1.UnitTest1.TestMethod1 threw exception: 
System.TypeLoadException: Could not load type 'System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute' from assembly 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

  Stack Trace: 
ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
RuntimeMethodInfo.GetCustomAttributes(Type attributeType, Boolean inherit)
Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
CustomAttributeExtensions.GetCustomAttributes(MemberInfo element)
UnitTest1.TestMethod1() line 21

Environment

  • Windows 10
  • Visual Studio 2022
@nohwnd
Copy link
Member

nohwnd commented Aug 3, 2023

I think the reason for this is the same as #4624. The compiler attributes are emitted into the file as a polyfill and in packaging there are 2 versions of the dll put in the same folder. Those dlls are compatible on runtime, but they use different polyfills pointing at different types.

@nohwnd nohwnd added the bug label Aug 3, 2023
@nohwnd
Copy link
Member

nohwnd commented Jul 8, 2024

Duplicate of #4624

@nohwnd nohwnd marked this as a duplicate of #4624 Jul 8, 2024
@nohwnd nohwnd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants