Skip to content

Commit

Permalink
Merge pull request #145 from mono/fix-load-system-text-json
Browse files Browse the repository at this point in the history
Fix loading System.Text.Json in ALC
  • Loading branch information
mhutch authored Oct 28, 2022
2 parents 2aedb6e + de2b3b6 commit 3dede18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Mono.TextTemplating.Tests/AssemblyLoadContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ protected override void VerifyFinalState ((SnapshotSet<string> assembliesInDefau

state.allContexts.AssertUnchanged ();
}

/// Issue #143: System.Text.Json is a framework assembly on .NET Core 3.0 and does not need to be specified by absolute path
[Fact]
public async Task LoadSystemTextJson ()
{
string template = "<#@ assembly name=\"System.Text.Json\" #><#=System.Text.Json.JsonValueKind.Array.ToString()#>";

var gen = new TemplateGenerator ();
(_, string content, _) = await gen.ProcessTemplateAsync (null, template, null);

CompilerError firstError = gen.Errors.OfType<CompilerError> ().FirstOrDefault ();
Assert.Null (firstError);

Assert.Equal ("Array", content);
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if FEATURE_ASSEMBLY_LOAD_CONTEXT

using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -52,9 +53,20 @@ protected override Assembly Load (AssemblyName assemblyName)
return hostAssembly;
}

foreach (var asmFile in templateAssemblyFiles) {
if (assemblyName.Name == Path.GetFileNameWithoutExtension (asmFile)) {
return LoadFromAssemblyPath (asmFile);
for (int i = 0; i < templateAssemblyFiles.Length; i++) {
var asmFile = templateAssemblyFiles[i];
if (asmFile is null) {
continue;
}
if (MemoryExtensions.Equals (assemblyName.Name, Path.GetFileNameWithoutExtension (asmFile.AsSpan()), StringComparison.OrdinalIgnoreCase)) {
// if the file doesn't exist, fall through to host.ResolveAssemblyReference
if (File.Exists (asmFile)) {
return LoadFromAssemblyPath (asmFile);
} else {
// null out the missing file so we don't check it exists again
templateAssemblyFiles[i] = null;
break;
}
}
}

Expand Down

0 comments on commit 3dede18

Please sign in to comment.