gluck / il-repack

Open-source alternative to ILMerge
Apache License 2.0
1.19k stars 217 forks source link

IL-repack fails when merging assemblies with netmodules #300

Closed peter-villadsen closed 7 months ago

peter-villadsen commented 3 years ago

I need to create an assembly that contains the content of an assembly that contains references to netmodules. This fails with a null references when trying to access the .Assembly information for the ModuleDefinition for the modules. I do not know if this is a Cecil issue, but I was able to write some code where Cecil correctly referenced all the types in the assembly correctly. To repro, build two netmodules with some content - The content of the cs files is not important here: Use an empty class:

csc /debug /t:module /out:netmodule1.netmodule code1.cs csc /debug /t:module /out:netmodule2.netmodule code2.cs al.exe /out:mydll.dll /t:lib netmodule1.netmodule netmodule2.netmodule

Now the assembly mydll.dll (and the corresponding PDB files) contain the references to the two netmodules. If I consume it in JustDecompile it will show the netmodules and the included types correctly.

Now attempt to use IL-Repack to create a new assembly ilrepack /out:result.dll /verbose includesNetmodules.dll

It will fail with a null dereference exception, producing the output listed below (I removed references to the exact paths for legibility). If I write some code that opens the assembly and traverses the modules with the newest version of Cecil:

using Mono.Cecil; using Mono.Cecil.Pdb; using System; using System.IO; using System.Linq; using System.Reflection;

namespace MergerApp { class Program { static void Main(string[] args) { using (AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(args[0], readerParameters)) { foreach (var module in assemblyDefinition.Modules) { foreach (var typeDefinition in module.Types.Where(t => !t.Name.StartsWith("<"))) { Console.WriteLine(typeDefinition.FullName); } } } } } }

the types are correctly listed.

Here is the log:

INFO: IL Repack - Version 2.1.0 VERBOSE: Runtime: ILRepack, Version=2.1.0.0, Culture=neutral, PublicKeyToken=null INFO: ------------- IL Repack Arguments ------------- /out:r.dll referencesDLls.dll

INFO: Adding assembly for merge: referencesDLls.dll INFO: Processing references INFO: Processing types VERBOSE: - Importing from referencesDLls.dll VERBOSE: - Importing INFO: Merging VERBOSE: - Importing from netmodule1.netmodule VERBOSE: - Importing INFO: Merging System.NullReferenceException: Object reference not set to an instance of an object. at ILRepacking.MappingHandler.GetScopeName(IMetadataScope scope) in \il-repack\ILRepack\MappingHandler.cs:line 82 at ILRepacking.MappingHandler.GetTypeKey(IMetadataScope scope, String fullName) in \il-repack\ILRepack\MappingHandler.cs:line 74 at ILRepacking.MappingHandler.GetTypeKey(TypeReference reference) in \il-repack\ILRepack\MappingHandler.cs:line 69 at ILRepacking.MappingHandler.StoreRemappedType(TypeDefinition orig, TypeDefinition renamed) in \il-repack\ILRepack\MappingHandler.cs:line 55 at ILRepacking.RepackImporter.Import(TypeDefinition type, Collection`1 col, Boolean internalize) in \il-repack\ILRepack\RepackImporter.cs:line 169 at ILRepacking.Steps.TypesRepackStep.RepackTypes() in \il-repack\ILRepack\Steps\TypesRepackStep.cs:line 67 at ILRepacking.Steps.TypesRepackStep.Perform() in \il-repack\ILRepack\Steps\TypesRepackStep.cs:line 54 at ILRepacking.ILRepack.Repack() in \il-repack\ILRepack\ILRepack.cs:line 337 at ILRepacking.Application.Main(String[] args) in \il-repack\ILRepack\Application.cs:line 29

KirillOsenkov commented 7 months ago

Hi @peter-villadsen it's been a while!

I tried your repro on the latest ILRepack (2.0.30) and it seems to be working fine:

IL Repack - Version 2.0.31
Runtime: ILRepack, Version=2.0.31.0, Culture=neutral, PublicKeyToken=null
------------- IL Repack Arguments -------------
/lib:C:\users\kiril\.nuget\packages\microsoft.netcore.app.ref\6.0.0\ref\net6.0 /out:Test.dll C:\Temp\ilrepack300\mydll.dll /log /verbose /pause
-----------------------------------------------
Adding assembly for merge: C:\Temp\ilrepack300\mydll.dll
Processing references
Processing types
- Importing <Module> from mydll.dll
- Importing <Module>
Merging <Module>
- Importing <Module> from netmodule1.netmodule
- Importing <Module>
Merging <Module>
- Importing <Module> from netmodule2.netmodule
- Importing <Module>
Merging <Module>
Processing exported types
Processing resources
Fixing references
- Fixing references for type <Module>
Processing public types tree
Processing XAML resource paths ...
Writing temporary assembly: C:\temp\ilrepack359\2\ILRepack-7188-706666\Test.dll
Wrote C:\temp\ilrepack359\2\Test.dll

If there are still any issues in the latest version feel free to file a new issue and I'll be happy to look into it.