tom-englert / ILMerge.Fody

Merges the referenced types of local dependencies as private types into the target assembly, and removes the references to the local dependencies.
MIT License
36 stars 5 forks source link

No support for duplicated types? #8

Closed ajaskiewiczpl closed 3 years ago

ajaskiewiczpl commented 3 years ago

ILMerge supports the /allowDup command line argument to allow duplicated types. However when I try to use ILMerge.Fody, in runtime I get the error System.BadImageFormatException Duplicate type with name 'System.Web.Http.Error' in assembly [...]. Is there a reason why it's not supported?

tom-englert commented 3 years ago

I did not see a use case for this. When would you have two assemblies having e.g. System.Web.Http already embedded?

tom-englert commented 3 years ago

No feedback, closed as stale

otuncelli commented 3 years ago

I'd like to suggest reopening this issue.

Introduced with C# 7.2, readonly structs are causing System.Runtime.CompilerServices.IsReadOnlyAttribute and Microsoft.CodeAnalysis.EmbeddedAttribute types to be embedded in the resulting assembly. And they cause this issue if multiple assemblies are containing these.

Note: In my tests, netstandard2.1 and beyond are not affected by this issue.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }

    public readonly struct Test
    {
    }
}

ILSpy of Assembly: image

Thanks.

tom-englert commented 3 years ago

@otuncelli that should be already working. Anything with the [Embedded] attribute is replaced with the existing target type. I'm using this heavily for assemblies with nullable reference types. Can you provide a sample where this is not working?

otuncelli commented 3 years ago

I'm currently working with PdfPig Library. It contains these readonly structs in its different assemblies.

Edit: It seems to happen only when I use NamespacePrefix

The error message I get when I try debugging: image

ILSpy of resulting assembly: image

My Configuration:

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
    <ILMerge NamespacePrefix="PdfFileTypePlugin">
        <IncludeAssemblies>PDFiumSharp|UglyToad.PdfPig.Core|UglyToad.PdfPig|UglyToad.PdfPig.DocumentLayoutAnalysis|UglyToad.PdfPig.Fonts|UglyToad.PdfPig.Package|UglyToad.PdfPig.Tokenization|UglyToad.PdfPig.Tokens</IncludeAssemblies>
        <FullImport>true</FullImport>
    </ILMerge>
</Weavers>
tom-englert commented 3 years ago

@otuncelli can you try V 1.18?

otuncelli commented 3 years ago

Solved! Thank you very much.

tom-englert commented 3 years ago

It seems to happen only when I use NamespacePrefix

That was the key information