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

Duplicate Namespaces #11

Closed black-empower closed 1 year ago

black-empower commented 1 year ago

Hi, is there a way to handle duplicate namespaces. Currently the weaving generates an error, if multiple dependencies use the same transitive dependency or the main assembly also uses the same dependency.

Best regards

     MSBUILD : error : Fody: An unhandled exception occurred:
   MSBUILD : error : Exception:
   MSBUILD : error : Failed to execute weaver C:\Users\Christopher\.nuget\packages\ilmerge.fody\1.23.0\build\..\weaver\ILMerge.Fody.dll
   MSBUILD : error : Type:
   MSBUILD : error : System.Exception
   MSBUILD : error : StackTrace:
   MSBUILD : error :    at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 222
   MSBUILD : error :    at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 112
   MSBUILD : error : Source:
   MSBUILD : error : FodyIsolated
   MSBUILD : error : TargetSite:
   MSBUILD : error : Void ExecuteWeavers()
   MSBUILD : error : IL Import failed: System.ExceptionArgument already exists in target module.
   MSBUILD : error : Type:
   MSBUILD : error : System.InvalidOperationException
   MSBUILD : error : StackTrace:
   MSBUILD : error :    at FodyTools.CodeImporter.ImportTypeDefinition(TypeDefinition sourceType)
   MSBUILD : error :    at FodyTools.CodeImporter.InternalImportType(TypeReference source, MethodReference targetMethod)
   MSBUILD : error :    at FodyTools.CodeImporter.CopyParameters(IMethodSignature sourceMethod, MethodReference targetMethod)
   MSBUILD : error :    at FodyTools.CodeImporter.ImportMethodDefinition(MethodDefinition sourceDefinition, TypeDefinition targetType)
   MSBUILD : error :    at FodyTools.CodeImporter.MergeIntoExistingType(TypeDefinition existing, TypeDefinition source)
   MSBUILD : error :    at FodyTools.CodeImporter.ImportTypeDefinition(TypeDefinition sourceType)
   MSBUILD : error :    at FodyTools.CodeImporter.CloneInstruction(Instruction source, MethodDefinition targetMethod, IReadOnlyDictionary`2 instructionMap)
   MSBUILD : error :    at FodyTools.CodeImporter.CopyInstructions(MethodDefinition source, MethodDefinition target)
   MSBUILD : error :    at FodyTools.CodeImporter.<>c__DisplayClass59_0.<ImportMethodBody>b__0()
   MSBUILD : error :    at FodyTools.CodeImporter.ProcessDeferredActions[T](T target)
   MSBUILD : error :    at FodyTools.CodeImporter.ImportType(TypeReference source, MethodReference targetMethod)
   MSBUILD : error :    at FodyTools.CodeImporterExtensions.ILMerge(CodeImporter codeImporter)
   MSBUILD : error :    at ILMerge.Fody.ModuleWeaver.Execute()
   MSBUILD : error :    at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186
   MSBUILD : error : Source:
   MSBUILD : error : ILMerge.Fody
   MSBUILD : error : TargetSite:
   MSBUILD : error : Mono.Cecil.TypeDefinition ImportTypeDefinition(Mono.Cecil.TypeDefinition)
tom-englert commented 1 year ago

Looks like these transitive dependencies do not end up in the same type/assembly. Can you post a sample to reproduce this?

tom-englert commented 1 year ago

Looks like System.ExceptionArgument does not meet the criteria of an embedded shared type: https://github.com/tom-englert/FodyTools/blob/9dbbf10a7353d7df732158c6fce572a2a80d1462/FodyTools/CodeImporter.cs#L1268

Where does this come from?

black-empower commented 1 year ago

So currently it seems like it is connected with referencing Autofac and creating a Lifetimescope and creating at least one element from the IoC. I attached a small project which reproduces the issue. TestLinking.zip

tom-englert commented 1 year ago

System.ExceptionArgument is an internal enum that's present in various system assemblies (e.g. System.Memory, System.Private.CoreLib e.a.). It has different values in each assembly, so it's not possible to merge them.

Anyhow it's not a good idea to merge system assemblies, you should exclude them.

If you want a single file exe, use the dot net single file assembly feature.

black-empower commented 1 year ago

Problem is, we don't want a single file executable, but create a nuget package with internalized dependencies so the dependency hell nuget generates is not there.

tom-englert commented 1 year ago

With parts of dotnet framework embedded?????

tom-englert commented 1 year ago

You don't need to embed netstandard, that should be available always

black-empower commented 1 year ago

ok, maybe there is some problem with the understanding how this works. Does it take all the assemblies under Assemblies or only the dlls copied to the output directory. I tried it befire with ILRepack and it worked with everything in the output directory, but sadly it was not dotnet6 compatible.

tom-englert commented 1 year ago

All direct and transient references from the output directory; for netcore/netstandard this includes a lot of system assemblies.