Skip to content

Commit

Permalink
[custom_attrs] Use the image's ALC instead of the default one (dotnet…
Browse files Browse the repository at this point in the history
…#68266) (dotnet#70346)

* [custom_attrs] Use the image's ALC instead of the default one (dotnet#68266)

Running ApiCompat task under Mono fails with:

    Could not load type System.Collections.Generic.IEqualityComparer`1[[Microsoft.Cci.ITypeReference, Microsoft.Cci ...

The reason is that cattr_type_from_name() uses a default ACL instead of
the image's one. This is important, because ExportCciSettings.Settings
field has the

      [Export(typeof(IEqualityComparer<ITypeReference>))]

custom attribute [1], where ITypeReference comes from the Microsoft.Cci
assembly. ApiCompat task runs under MSBuild, which provides its own
MSBuildLoadContext ALC. Microsoft.Cci is supposed to be found using
this ALC, not the default one.

[1] https://github.com/dotnet/arcade/blob/8f311fed1f2acf0ecfdfcecbc7a9fa871ed634cc/src/Microsoft.DotNet.ApiCompat/src/Microsoft.DotNet.ApiCompat.Core/ExportCciSettings.cs#L28

* [sre] Set the ALC for the dynamic image

   Don't leave it NULL

Co-authored-by: Aleksey Kliger <[email protected]>
  • Loading branch information
iii-i and lambdageek committed Jul 1, 2022
1 parent 5e088b0 commit 32670bd
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/custom-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static MonoType*
cattr_type_from_name (char *n, MonoImage *image, gboolean is_enum, MonoError *error)
{
ERROR_DECL (inner_error);
MonoAssemblyLoadContext *alc = mono_alc_get_ambient ();
MonoAssemblyLoadContext *alc = mono_image_get_alc (image);
MonoType *t = mono_reflection_type_from_name_checked (n, alc, image, inner_error);
if (!t) {
mono_error_set_type_load_name (error, g_strdup(n), NULL,
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/metadata/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ mono_reflection_dynimage_basic_init (MonoReflectionAssemblyBuilder *assemblyb, M
return_if_nok (error);
image = mono_dynamic_image_create (assembly, assembly_name, g_strdup ("RefEmit_YouForgotToDefineAModule"));
image->initial_image = TRUE;
image->image.alc = alc;
assembly->assembly.aname.name = image->image.name;
assembly->assembly.image = &image->image;

Expand Down

0 comments on commit 32670bd

Please sign in to comment.