{"record":{"id":"602ae24eb42a2ef6","repo":"pardeike/Harmony","slug":"cannot-create-replacement-for-original-fulldescription","errorCode":null,"errorMessage":"Cannot create replacement for {original.FullDescription()}","messagePattern":"Cannot create replacement for (.+?)","errorType":"exception","errorClass":"MissingMethodException","httpStatus":null,"severity":"critical","filePath":"Harmony/Internal/PatchFunctions.cs","lineNumber":37,"sourceCode":"\t\t\tvar sortedPostfixes = GetSortedPatchMethods(original, patchInfo.postfixes, debug);\r\n\t\t\tvar sortedTranspilers = GetSortedPatchMethods(original, patchInfo.transpilers, debug);\r\n\t\t\tvar sortedFinalizers = GetSortedPatchMethods(original, patchInfo.finalizers, debug);\r\n\t\t\tvar sortedInnerPrefixes = GetInfixes(patchInfo.innerprefixes);\r\n\t\t\tvar sortedInnerPostfixes = GetInfixes(patchInfo.innerpostfixes);\r\n\r\n\t\t\tvar patcher = new MethodCreator(new MethodCreatorConfig(\r\n\t\t\t\toriginal,\r\n\t\t\t\tnull,\r\n\t\t\t\tsortedPrefixes,\r\n\t\t\t\tsortedPostfixes,\r\n\t\t\t\tsortedTranspilers,\r\n\t\t\t\tsortedFinalizers,\r\n\t\t\t\tsortedInnerPrefixes,\r\n\t\t\t\tsortedInnerPostfixes,\r\n\t\t\t\tdebug\r\n\t\t\t));\r\n\t\t\tvar (replacement, finalInstructions) = patcher.CreateReplacement();\r\n\t\t\tif (replacement is null) throw new MissingMethodException($\"Cannot create replacement for {original.FullDescription()}\");\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tPatchTools.DetourMethod(original, replacement);\r\n\t\t\t}\r\n\t\t\tcatch (Exception ex)\r\n\t\t\t{\r\n\t\t\t\tthrow HarmonyException.Create(ex, finalInstructions);\r\n\t\t\t}\r\n\t\t\treturn replacement;\r\n\t\t}\r\n\r\n\t\tinternal static MethodInfo ReversePatch(HarmonyMethod standin, MethodBase original, MethodInfo postTranspiler)\r\n\t\t{\r\n\t\t\tif (standin is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(standin));\r\n\t\t\tif (standin.method is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(standin), $\"{nameof(standin)}.{nameof(standin.method)} is NULL\");\r","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/PatchFunctions.cs#L19-L55","documentation":"UpdateWrapper builds the detour replacement method from sorted patches/transpilers and calls CreateReplacement on the patcher. If the patcher returns null replacement, Harmony cannot compile a new method body for the original and throws this MissingMethodException, since the wrapper creation pipeline produced nothing to detour with.","triggerScenarios":"PatchClassProcessor.Patch()/Harmony.Patch runs UpdateWrapper and the internal MethodPatcher fails to produce a replacement body — typically from a failing transpiler, unresolvable patch parameter injection, or invalid patch method signatures that abort replacement generation.","commonSituations":"Transpilers throwing or yielding invalid IL, patches referencing missing types (type-load issues in trimmed/AOT environments), or combined prefix/postfix/finalizer sets that the generator cannot reconcile.","solutions":["Run with Harmony.DEBUG (Environment variable HARmony_DEBUG or Harmony.DEBUG) to get the full inner error/IL dump from CreateReplacement.","Fix or disable transpilers on this method first; they are the most common cause of null replacements.","Validate that all patch methods resolve their target and parameters (no nulls from AccessTools lookups).","Catch MissingMethodException around Patch() to report which original failed and skip it."],"exampleFix":"// before\nharmony.PatchAll(); // crashes on bad transpiler\n\n// after\ntry { harmony.Patch(original, prefix: prefix); }\ncatch (MissingMethodException ex) { Logger.Error($\"Patch failed: {ex.Message}\"); }","handlingStrategy":"try-catch","validationCode":"if (AccessTools.Method(targetType, targetName) is null)\n    throw new InvalidOperationException(\"Target method missing before patching\");\nforeach (var t in transpilers) DebugDumpInstructions(t, original); // dry-run in dev","typeGuard":null,"tryCatchPattern":"try { harmony.Patch(original, transpiler: transpiler); }\ncatch (MissingMethodException ex) when (ex.Message.Contains(\"Cannot create replacement\"))\n{ Logger.Error($\"Wrapper generation failed for {original.Name}: {ex.Message}\"); }","preventionTips":["Test every patch in CI with Harmony.DEBUG enabled to surface replacement failures early.","Keep transpilers minimal and defensive (try/catch inside the transpiler body).","Pin Harmony and target-library versions together; re-verify patches on upgrades.","Isolate patches per method so one failure doesn't abort PatchAll."],"tags":["harmony","patching","wrapper-generation","il-emission"],"backgroundTag":"internal-invariant-violation","analyzedSha":"e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c","analyzedAt":"2026-09-15T22:47:11.550Z","contentChangedAt":"2026-09-15T22:47:11.550Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}