gluck / il-repack

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

Issue with Linux: "Failed to merge configuration files" #283

Closed bradwilson closed 11 months ago

bradwilson commented 3 years ago

When trying to run ILRepack against a .NET Framework application on Linux, I get an error which says that it failed to merge configuration files.

Repro:

  1. Clone https://github.com/xunit/xunit
  2. Run git checkout 9195c0b (this is on an in-progress branch, so I'm sending you to a moment in time)
  3. Run dotnet build src/xunit.v3.runner.console

Working on Windows:

image

Broken on Linux:

image

Our CI process uses the Linux build for making the official builds.

It does appear to be successfully running the repack, because it generates an executable which appears to run okay:

image

I'm not sure what it's trying to do with the configuration file(s), but on the successful Windows side, the resulting "merged" .exe.config file is identical to original .exe.config file, except that the XML declaration has had encoding="utf-8" removed (why?) and standalone="yes" added (understandable). I would be happy to run ILRepack in such a way as to bypass .config file handling, since it would be just as easy for me to copy the original .exe.config file into the merged folder, but I didn't see any command line switch that would turn off config file processing.

I am invoking ILRepack via the executable:

image

dotnet --info on Windows:

.NET SDK (reflecting any global.json):
 Version:   5.0.103
 Commit:    72dec52dbd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.103\

Host (useful for support):
  Version: 5.0.3
  Commit:  c636bbdc8a

.NET SDKs installed:
  3.1.406 [C:\Program Files\dotnet\sdk]
  5.0.103 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

dotnet --info on Linux:

.NET SDK (reflecting any global.json):
 Version:   5.0.103
 Commit:    9effbc8ad5

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  18.04
 OS Platform: Linux
 RID:         ubuntu.18.04-x64
 Base Path:   /usr/share/dotnet/sdk/5.0.103/

Host (useful for support):
  Version: 5.0.3
  Commit:  eae88cc11b

.NET SDKs installed:
  2.1.813 [/usr/share/dotnet/sdk]
  3.1.406 [/usr/share/dotnet/sdk]
  5.0.103 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.25 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.25 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.25 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
bradwilson commented 3 years ago

For the moment I've worked around the issue by copying all the to-be-merged files into a new premerge folder so that there's no .config files hanging around to trip the issue: https://github.com/xunit/xunit/blob/ea3007119bff2358873ac6fe0d922a60c5090f82/src/Directory.Build.targets#L11-L56

Will still appreciate a fix, though, so I can stop the unnecessary file copying.

wmjordan commented 3 years ago

This issue is really quite frustrating. I encountered that before.

Maybe it is possible to add an option to merge the config file or not. This is not so difficult to implement.

KirillOsenkov commented 11 months ago

Thanks @wmjordan for submitting the PR #291, the latest version has this option now: /skipconfig

Leaving the issue open to debug the underlying problem.

KirillOsenkov commented 11 months ago

Also the logic to merge config files was rewritten using XLinq, so it's likely it has been fixed accidentally, worth a check. Might not even need the /skipconfig now.

KirillOsenkov commented 11 months ago

Try https://www.nuget.org/packages/ILRepack/2.0.21, with and without /skipconfig

wmjordan commented 11 months ago

I just installed 2.0.21 and tried it on one of my production project, which was well merged by 2.0.15 before. I got the following exception:

System.Runtime.Serialization.SerializationException: Deserializing object failed. ---> System.Xml.XmlException: Invalid data on root element, line 1, position 1. at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3) at System.Xml.XmlUTF8TextReader.Read() at System.Xml.XmlBaseReader.IsStartElement() at System.Xml.XmlBaseReader.IsStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri) at System.Runtime.Serialization.XmlObjectSerializer.IsRootElement(XmlReaderDelegator reader, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns) at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader) at ILRepacking.Steps.ResourcesRepackStep.GetRepackListFromStream(Stream stream) at ILRepacking.Steps.ResourcesRepackStep.Perform() at ILRepacking.ILRepack.Repack() at ILRepacking.Application.Main(String[] args)

bradwilson commented 11 months ago

For my side, I can verify that the merge failure is gone now, using 2.0.21. I have been able to remove my premerge steps, and I verified that the only difference between the .config file in the unmerged folder and the .config file in the merged folder is that the merged version (benignly) added a UTF-8 BOM.

Change set (on top of the existing 2.0.21 upgrade in the commit prior): https://github.com/xunit/xunit/commit/920e3e521904741ffd2c1c100ef2073bbfa45cf5

KirillOsenkov commented 11 months ago

@wmjordan thanks for trying it out!

I found the regression and fixed in https://github.com/gluck/il-repack/commit/01b54ff113123da84d8283df0b70175549e926b0

Will publish a new release soon, need to do some testing and fix a couple unrelated bugs.

KirillOsenkov commented 11 months ago

I've now published https://www.nuget.org/packages/ILRepack/2.0.22

@wmjordan if you could try it out whenever you get a chance

@bradwilson you should now be able to call dotnet ILRepack.exe instead of mono ILRepack.exe on Unix, hopefully it'll work with both runtimes.

KirillOsenkov commented 11 months ago

I'll be closing this issue but please feel free to file new bugs if something else is wrong.

wmjordan commented 11 months ago

Hi, I downloaded the new version, used it on the previously reported project and it was working very well now.

bradwilson commented 11 months ago

Verified it works here as well. Thanks!