Harmony icon indicating copy to clipboard operation
Harmony copied to clipboard

.NET 7 Runtime Skipping Patches

Open xcellsoft opened this issue 4 years ago • 10 comments

Using harmony for unit tests, has worked flawlessly with the past .NET versions including 6 and 5.

Recently upgraded to .NET 7 and harmony patching ONLY works with executing code interactively in debug using vscode (on unit tests click Debug above the test method, clicking Run will NOT patch but debug will).

Launching from the command line will NEVER access the patches.

I have reduced the code into it's simplest form to reproduce. Just compile and run from command line.

Execute this code with debugger and patch is fine, type dotnet run in the root folder and it skips the patches.

Very odd. Help would be appreciated.

Program.cs

using System;
using HarmonyLib;

var localHarmony = new Harmony("com.acme");
var original = typeof(GetPatchedVal).GetMethod("Val");
var prefix = typeof(GetPatchedVal_Val).GetMethod("Prefix");

localHarmony.Patch(original);
localHarmony.Patch(original, prefix: new HarmonyMethod(prefix));

Console.WriteLine(GetPatchedVal.Val());

[HarmonyPatch(typeof(GetPatchedVal), nameof(GetPatchedVal.Val))]
public static class GetPatchedVal_Val
{
    public static bool Prefix(
        ref string __result)
    {
        Console.WriteLine("Patched Method Called");

        __result = $"Patched Return Value";

        return false; // skip original implementation.
    }
}

public static class GetPatchedVal
{
    public static string Val()
    {
        return $"UNPATCHED VALUE UGH!!!";
    }
}

harmony7.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

<ItemGroup>
  <PackageReference Include="Lib.Harmony" Version="2.2.2" />
</ItemGroup>

</Project>

xcellsoft avatar Nov 17 '22 17:11 xcellsoft

.NET 7 is not yet supported (because MonoMod.Common does not support it)

pardeike avatar Nov 18 '22 22:11 pardeike

It seems like MonoMod.Common is no longer a thing and is replaced by MonoMod.Core or MonoMod.RuntimeDetour. I may have misinterpreted what someone had told me but if you are waiting on a new MonoMod.Common to support net7.0 you might be waiting a while.

jborean93 avatar Nov 23 '22 02:11 jborean93

It actually works in .NET 7 when you run it via VS Code, it's when you run from command line it just skips all the patches. .NET 6 is fine.

We have allot of tests that patch static methods.

Is there another mechanism besides harmony that we should look into? Any help would be appreciated.

xcellsoft avatar Nov 23 '22 16:11 xcellsoft

I'm making use of Harmony in a similar fashion (here https://github.com/Almamu/EVESharp/tree/main/Server/EVESharp.Node.Unit) to replace some extension methods in my tests and the same thing happens. If you run the tests the patches do not work, but if you run it in debug mode they do work just fine. @xcellsoft that might be why the tests work fine from VSCode. I guess our only option is to wait, I don't have that much low level knowledge of C# to help support .NET 7 on MonoMod.Common... In the meantime running the tests in debug mode might be a workaround

Almamu avatar Dec 13 '22 14:12 Almamu

Supposedly, it is fixed in the reorganize branch of MonoMod. I've not been able to find an ETA from MonoMod maintainers about when this branch could become official. So I guess in the meantime, one approach could be to fork/branch Harmony and update it to uses MonoModReorg.RuntimeDetour/ (and see if it doesn't introduce other issues).

I'm having the same problem as reported here, since I've updated to .NET 7. I've tried to disable tiered compilations and other stuff like that without success, so if nobody finds a workaround, I might try to use the previously mentioned package. If so, I'll update the thread here (but no guarantee -- feel free to do it too if you have better knowledge of Harmony / MonoMod).

jpboivin avatar Dec 13 '22 14:12 jpboivin

I'm having the same problem as reported here, since I've updated to .NET 7. I've tried to disable tiered compilations and other stuff like that without success, so if nobody finds a workaround, I might try to use the previously mentioned package. If so, I'll update the thread here (but no guarantee -- feel free to do it too if you have better knowledge of Harmony / MonoMod).

@jpboivin There is now a branch for MonoMod.Core which does exactly this: https://github.com/pardeike/Harmony/tree/feature/monomod-core Feedback welcome!

pardeike avatar Apr 04 '23 21:04 pardeike

@jpboivin There is now a branch for MonoMod.Core which does exactly this: https://github.com/pardeike/Harmony/tree/feature/monomod-core Feedback welcome!

Thanks for the update! I tested it, and for my use cases, working great on macOS Monterey (Intel) and on Linux :)

jpboivin avatar Apr 05 '23 02:04 jpboivin

@jpboivin There is now a branch for MonoMod.Core which does exactly this: https://github.com/pardeike/Harmony/tree/feature/monomod-core Feedback welcome!

Thanks. Works for me on windows.

SonicGD avatar Apr 06 '23 06:04 SonicGD

I'm having the same problem as reported here, since I've updated to .NET 7. I've tried to disable tiered compilations and other stuff like that without success, so if nobody finds a workaround, I might try to use the previously mentioned package. If so, I'll update the thread here (but no guarantee -- feel free to do it too if you have better knowledge of Harmony / MonoMod).

@jpboivin There is now a branch for MonoMod.Core which does exactly this: https://github.com/pardeike/Harmony/tree/feature/monomod-core Feedback welcome!

After updating to the version 2.3.0-alpha.5 always receive an error for windows for transpiler patching:

var harmony = new Harmony(id);
var processor = harmony.CreateProcessor(originalMethod);
processor.AddTranspiler(new HarmonyMethod(transpiler));
processor.Patch();

Exception message:

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.NotImplementedException : The method or operation is not implemented.
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at MonoMod.Utils.Cil.ILGeneratorShimExt.DynEmit(ILGenerator il, Object[] emitArgs)
   at MonoMod.Utils.Cil.ILGeneratorShimExt.DynEmit(ILGenerator il, OpCode opcode, Object operand)
   at HarmonyLib.MethodBodyReader.<>c__DisplayClass22_0.<FinalizeILCodes>b__2(CodeInstruction codeInstruction)
   at HarmonyLib.CollectionExtensions.Do[T](IEnumerable`1 sequence, Action`1 action)
   at HarmonyLib.MethodBodyReader.FinalizeILCodes(Emitter emitter, List`1 transpilers, List`1 endLabels, Boolean& hasReturnCode)
   at HarmonyLib.MethodCopier.Finalize(Emitter emitter, List`1 endLabels, Boolean& hasReturnCode)
   at HarmonyLib.MethodPatcher.CreateReplacement(Dictionary`2& finalInstructions)
   at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo)
   at HarmonyLib.PatchProcessor.Patch()

svtlv avatar Apr 09 '23 16:04 svtlv

@jpboivin There is now a branch for MonoMod.Core which does exactly this: https://github.com/pardeike/Harmony/tree/feature/monomod-core Feedback welcome!

I built 0Harmony.dll from feature/monomod-core and (after also manually adding the Mono.Cecil/MonoMod.* dlls to my project) my tests now work under .Net 7. Thanks! (And looking forward to the next release.)

TimothyByrd avatar May 20 '23 00:05 TimothyByrd