NotepadPlusPlusPluginPack.Net icon indicating copy to clipboard operation
NotepadPlusPlusPluginPack.Net copied to clipboard

ILMerge. Build error.

Open amid0 opened this issue 3 years ago • 4 comments
trafficstars

Trying to use MSBuild.ILMerge.Task nuget package to merge Newtonsoft.Json package into main dll. On build getting error 1>D:\sources\NppPlugin\MyPlugin\MyPlugin\PluginInfrastructure\DllExport\NppPlugin.DllExport.targets(13,5): error : D:\sources\NppPlugin\MyPlugin\MyPlugin\PluginInfrastructure\ScintillaGateway.cs(16707566) : error : syntax error at token '-' in: IL_0027: ldc.r8 -inf Sometimes it fails on other classes from \PluginInfrastructure folder Target Framework: .NET Framework 4.5

amid0 avatar Jul 28 '22 09:07 amid0

Hi amid0, I'm getting the same error as you, but it's definitely not related to Newtonsoft.Json or any specific details of your code.

I get a similar error whenever there is any instance of double.PositiveInfinity, double.NegativeInfinity, or double.NaN anywhere in my code. This happens if I introduce those constants into the demo plugin as well, even though the demo plugin builds fine otherwise. OTOH, if I create a blank project in .NET 4.0 and include the lines

static void Main(string[] args)
{
    Console.WriteLine(double.NegativeInfinity);
    Console.WriteLine(double.PositiveInfinity);
    Console.WriteLine(double.NaN);
}

I get no errors at all, so it's not an issue with .NET 4.0.

My guess is that it's related to ILMerge or DLLExport. I know for a fact that ILMerge has been deprecated and unmaintained since July 2020, so it wouldn't surprise me in the least if that was the root of this problem.

I haven't done too much digging and I certainly haven't solved the problem, but the most relevant material I've found so far is as follows:

https://developercommunity.visualstudio.com/t/ildasmexe-regression-with-infinum-floating-point-v/545431

https://github.com/3F/DllExport/issues/128

and here's some documentation on an assembler that might help

https://docs.microsoft.com/en-us/dotnet/framework/tools/ilasm-exe-il-assembler

molsonkiko avatar Aug 24 '22 21:08 molsonkiko

So I have a hacky but functional solution if you need NaN or infinity in your code: generate it at runtime in such a way that the compiler doesn't get angry.

1d/0d or the like won't work - those kinds of math operations are pre-computed at compile time. I know because I tried.

Here's a class that does what you want.

using System;

namespace YourNameSpace
{
    public class NanInf
    {
        /// <summary>
        /// a/b<br></br>
        /// may be necessary to generate infinity or nan at runtime
        /// to avoid the compiler pre-computing things<br></br>
        /// since if the compiler sees literal 1d/0d in the code
        /// it just pre-computes it at compile time
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double Divide(double a, double b) { return a / b; }

        public static readonly double inf = Divide(1d, 0d);
        public static readonly double neginf = Divide(-1d, 0d);
        public static readonly double nan = Divide(0d, 0d);
    }
}

By the way, I've made a pull request to add this to the plugin pack!

molsonkiko avatar Aug 25 '22 01:08 molsonkiko

is there a good substitute for ilmerge @molsonkiko

kbilsted avatar Aug 26 '22 16:08 kbilsted

IlRepack bills itself as a good alternative, and it has been updated as recently as April 2022. No idea if it's compatible with .NET 4.0 though.

I haven't done too much investigation because switching from IlMerge to something else probably requires more work than this one problem merits.

molsonkiko avatar Aug 26 '22 18:08 molsonkiko