Improve duplicate assembly resolving (#89958)
authorMike Voorhees <mrvoorhe@users.noreply.github.com>
Mon, 7 Aug 2023 16:06:20 +0000 (12:06 -0400)
committerGitHub <noreply@github.com>
Mon, 7 Aug 2023 16:06:20 +0000 (09:06 -0700)
commite1a6db99f1214a017e1a8b1ffb5d254e17bffba1
tree68c3a77ca25ac45761ba2bc48c4324656008cc40
parent38166fba8921a572e82c6ac3342ef0e2afa7b73e
Improve duplicate assembly resolving (#89958)

`GetAssembly` opens a new assembly and stream every time it is called and keeps them open for the duration of the run.

`RootAssemblyFile.LoadAssemblyFile` sort of handled an already loaded assembly, but it left a new `AssemblyDefinition` instance and `MemoryStream` open every time it happened.

`CacheAssembly` was easy to make a mistake with.   Forget to do the delicate dance that `RootAssemblyFile.LoadAssemblyFile` goes to already loaded assemblies and you can easily overwrite an already cached assembly.  Guard against mistakes rather than quitely overwriting the assembly that was already cached.

Why force callers of `CacheAssembly` to check if something is cached already?  Handle that case internally.

With these changes the logic in `RootAssemblyFile.LoadAssemblyFile` is simplified and an extra copy of the assembly is no longer left open in the scenario where an assembly is specified multiple times.
src/tools/illink/src/linker/Linker.Steps/RootAssemblyInputStep.cs
src/tools/illink/src/linker/Linker/AssemblyResolver.cs
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/ILLink.RoslynAnalyzer.Tests.Generator/ILLink.RoslynAnalyzer.Tests.TestCaseGenerator/CommandLineTests.g.cs
src/tools/illink/test/Mono.Linker.Tests.Cases/CommandLine/DuplicateRootAssembly.cs [new file with mode: 0644]