dotnet / ILMerge

ILMerge is a static linker for .NET Assemblies.
MIT License
1.23k stars 170 forks source link

Could not find file or assembly "x" on runtime. #66

Open GeraldIr opened 5 years ago

GeraldIr commented 5 years ago

I merged multiple .dlls and an .exe of a CL-Tool I have been working on so it can be easily distributable.

The merging itself works and throws no errors, but when I go to call a command on the exe, so "app command -option value" and it immediately gives me an error that some File or Assembly (which the .exe has been merged with, I checked with ILSpy so it is in there and the log says so too) could not be found.

I tried using the closed option, but it didn't change anything.

Is there anything obvious I have missed?

mike-barnett commented 5 years ago

There are scenarios where assembly identities are encoded in strings. This is especially true for WPF applications. When this happens, ILMerge is unable to find and modify the string to reference the merged assembly instead of the original, input, assembly. If you run the merged assembly through ildasm to produce a text file you can search it for occurrences of "[A]" (where A is the name of the assembly that you're getting the error about) and see what context it occurs in. If you post that, then maybe we can figure out if there is something wrong with ILMerge or not. Sorry you're having problems

GeraldIr commented 5 years ago

I ran my executable through ILDASM and in the first screen it correctly lists all of my referenced assemblies (also the ones I get an error about). Then writing it out as a text file, the only library that I find in the whole file in this format "[A]" is mscorlib, the actual libraries I'm getting errors about cannot be found in that file.

By the way, It seems as though the exe is looking for the assemblies in its directory, when I add the missing ones it runs smoothly. Yet it seems that these libraries are included in the .exe.

The file is too big to upload here, but I can upload it somewhere else if it's needed.

Edit: The errors occur at runtime and some commands actually work (just help commands for some reason)

mike-barnett commented 5 years ago

Then I would guess that the program is using Reflection to load assembly A. Do you see any calls to Assembly. Load or Assembly.LoadFrom in the text file? If you can find a way to share the merged assembly and/or the text file dumped out by ildasm then I'd be happy to take a look at it. Another thing would be to try ILRepack and see if that works for you.

GeraldIr commented 5 years ago

I didn't find any of those either? I'll try ILRepack and report back. In the meantime here is the link to the text file: https://drive.google.com/file/d/1KC3H7GwhO3soqzZVh1s2Pr884xCyFPwM/view?usp=sharing

mike-barnett commented 5 years ago

Thanks! I downloaded the file. What is the name of the assembly that it complains about when you run the merged assembly? What is the name of the method that ends up getting called when run it and it complains?

GeraldIr commented 5 years ago

It first complains about "Microsoft.Extensions.Logging", then when you add that to the directory it goes on to "Microsoft.Extensions.Logging.Abstraction", I didn't test it further, but I would assume it would just go on until all the referenced assemblies have been added.

mike-barnett commented 5 years ago

If you look at the method that is defined starting on line 463994, you'll see that it is using reflection to load the assembly named "Microsoft.Extensions.Logging". So whatever you are doing that causes that method to be defined would have to be changed. I'd be very interested to know if ILRepack can handle this or not. Hope it does!

GeraldIr commented 5 years ago

Ahh, interesting. I sadly did not write that part myself so I'll have to concede here for now.

I will try ILRepack a bit more tomorrow, currently I'm running into some sort of compatibility issues with the ILRepacked executable, but I'm sure this can be remedied.

I will report back to you once I have a working executable (or not), anyways thank you so much for your help though I cannot help but feel as if I wasted your time!

Have a good day :)