Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

ILMerge. Build error. #93

Open
amid0 opened this issue Jul 28, 2022 · 4 comments
Open

ILMerge. Build error. #93

amid0 opened this issue Jul 28, 2022 · 4 comments

Comments

@amid0
Copy link

amid0 commented Jul 28, 2022

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

@molsonkiko
Copy link
Contributor

molsonkiko commented Aug 24, 2022

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

3F/DllExport#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 added a commit to molsonkiko/JsonToolsNppPlugin that referenced this issue Aug 24, 2022
There was a bunch of .NET 6.0 syntax in the
JsonTools files that needed to be removed for
the code to compile in .NET framework 4.0,
and it's been removed.

Unfortunately there is now an error with
double.NegativeInfinity, double.PositiveInfinity,
and double.Nan causing compiler errors.
The code works fine otherwise.

See kbilsted/NotepadPlusPlusPluginPack.Net#93
@molsonkiko
Copy link
Contributor

molsonkiko commented Aug 25, 2022

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!

@kbilsted
Copy link
Owner

kbilsted commented Aug 26, 2022

is there a good substitute for ilmerge @molsonkiko

@molsonkiko
Copy link
Contributor

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 added a commit to molsonkiko/NotepadPlusPlusPluginPack.Net that referenced this issue Apr 26, 2023
Fix issue kbilsted#93
Add simple test (that also functions as a warning to devs) in Demo.cs
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants