-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Return empty string in InternalAssemblyBuilder.Location #57396
Conversation
Could you please also add a simple test? |
Hi @jkotas , I tried to add a test inside AssemblyBuilderTests, but I have not succeeded yet in getting Location = string.Empty. I found that the test uses assembly System.Private.CoreLib.dll inside .dotnet folder which is downloaded with dotnet-install.ps, instead of the one inside artifacts directory (containing my new code). Do you know how to force using the new version of System.Private.CoreLib.dll ? |
That is not expected. How are you running the tests ? https://github.com/dotnet/runtime/blob/main/docs/workflow/testing/libraries/testing.md has the instructions for running the tests. |
I use this link dotnet/sdk#7419 (comment) to debug tests inside VS |
object internalAssemblyBuilder = assembly.GetType() | ||
.GetProperty("InternalAssembly", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(assembly); | ||
object locationObj = internalAssemblyBuilder.GetType().GetProperty("Location", BindingFlags.Public | BindingFlags.Instance) | ||
.GetValue(internalAssemblyBuilder); | ||
string location = Assert.IsType<string>(locationObj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to rewrite the test to not use private reflection?
The test is valid regardless of how the builder is implemented, but now it would fail if we changed the implementation (to rename/remove the internal property).
I think it should be possible to call something like AssemblyLoadContext.Default.Assemblies
and find the one just defined and validate it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the clue
[Fact] | ||
public void DefineDynamicAssembly_InternalAssemblyLocationIsEmpty() | ||
{ | ||
AssemblyBuilder assembly = Helpers.DynamicAssembly(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AssemblyBuilder assembly = Helpers.DynamicAssembly(); | |
AssemblyBuilder assembly = Helpers.DynamicAssembly(nameof(DefineDynamicAssembly_InternalAssemblyLocationIsEmpty)); |
Otherwise the assembly will get a default name TestAssembly
and the test might succeed by accident if some other test uses the same assembly name. (Unlikely as the test would still likely do the right validation, but still)
{ | ||
AssemblyBuilder assembly = Helpers.DynamicAssembly(nameof(DefineDynamicAssembly_InternalAssemblyLocationIsEmpty)); | ||
Assembly internalAssemblyBuilder = AssemblyLoadContext.Default.Assemblies | ||
.FirstOrDefault(_ => _.FullName == assembly.FullName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.FirstOrDefault(_ => _.FullName == assembly.FullName); | |
.FirstOrDefault(a => a.FullName == assembly.FullName); |
Don't use _
for a variable name if you are actually going to use the variable. _
is used when you want to ignore the variable/return value.
@lambdageek Could you please advice about the behavior expected in Mono? It seems that defining a dynamic assembly doesn't add that assembly to the On top of that I expect the Mono's version to throw from the |
Seems like a bug in Mono's AssemblyLoadContext.GetLoadContext - a mono runtime/src/mono/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.Mono.cs Lines 78 to 88 in 6acb72b
I think it probably throws from here:
So we'd need a new override in runtime/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.Mono.cs Line 174 in 6acb72b
|
I use now AppDomain.CurrentDomain.GetAssemblies instead of AssemblyLoadContext.Assemblies in my test , it seemed to return the test assembly on my machine for coreclr and mono. Location should be updated to return string.Empty in src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs because it's the partial class of I'm not so sure of what I did, if there's something to adjust or drop, I would be happy to update my PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the fix! LGTM
Return empty string in AssemblyBuilder.Location
Issue #51924