Skip to content

Commit

Permalink
- Changed MemoryFileAssemblyContextLoader to properly ignore template…
Browse files Browse the repository at this point in the history
…d and disposed ACLs.
  • Loading branch information
TBN-MapleWheels committed Oct 25, 2023
1 parent f6a7644 commit 4223b21
Showing 1 changed file with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public class MemoryFileAssemblyContextLoader : AssemblyLoadContext
protected bool IsResolving; //this is to avoid circular dependency lookup.
private AssemblyManager _assemblyManager;
public bool IsTemplateMode { get; set; }
public bool IsDisposed { get; private set; }

public MemoryFileAssemblyContextLoader(AssemblyManager assemblyManager) : base(isCollectible: true)
{
this._assemblyManager = assemblyManager;
this.IsDisposed = false;
base.Unloading += OnUnload;
}

Expand Down Expand Up @@ -157,11 +159,11 @@ public AssemblyLoadingSuccessState CompileAndLoadScriptAssembly(
if (externMetadataReferences is not null)
metadataReferences.AddRange(externMetadataReferences);

// build metadata refs from global where not an in-memory compiled assembly and not the same assembly as supplied.
metadataReferences.AddRange(AppDomain.CurrentDomain.GetAssemblies()
// build metadata refs from default where not an in-memory compiled assembly and not the same assembly as supplied.
metadataReferences.AddRange(AssemblyLoadContext.Default.Assemblies
.Where(a =>
{
if (a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit"))
if (a.IsDynamic || string.IsNullOrWhiteSpace(a.Location) || a.Location.Contains("xunit"))
return false;
if (a.FullName is null)
return true;
Expand All @@ -172,7 +174,28 @@ public AssemblyLoadingSuccessState CompileAndLoadScriptAssembly(
.Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit")))
.Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference)
).ToList());


// build metadata refs from ACL assemblies from files/disk.
foreach (AssemblyManager.LoadedACL loadedAcl in _assemblyManager.GetAllLoadedACLs())
{
if(loadedAcl.Acl.IsTemplateMode || loadedAcl.Acl.IsDisposed)
continue;
metadataReferences.AddRange(loadedAcl.Acl.Assemblies
.Where(a =>
{
if (a.IsDynamic || string.IsNullOrWhiteSpace(a.Location) || a.Location.Contains("xunit"))
return false;
if (a.FullName is null)
return true;
return !externAssemblyNames.Contains(a.FullName); // exclude duplicates
})
.Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference)
.Union(externAssemblyRefs // add custom supplied assemblies
.Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit")))
.Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference)
).ToList());
}

// build metadata refs from in-memory images
foreach (var loadedAcl in _assemblyManager.GetAllLoadedACLs())
{
Expand Down Expand Up @@ -252,7 +275,8 @@ protected override Assembly Load(AssemblyName assemblyName)
//try resolve against other loaded alcs
foreach (var loadedAcL in _assemblyManager.GetAllLoadedACLs())
{
if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode) continue;
if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode || loadedAcL.Acl.IsDisposed)
continue;

try
{
Expand Down Expand Up @@ -285,5 +309,6 @@ private void OnUnload(AssemblyLoadContext alc)
CompiledAssemblyImage = null;
_dependencyResolvers.Clear();
base.Unloading -= OnUnload;
this.IsDisposed = true;
}
}

0 comments on commit 4223b21

Please sign in to comment.