patch icon indicating copy to clipboard operation
patch copied to clipboard

Patching BIFs fails with `MatchError`

Open ollien opened this issue 1 year ago • 0 comments

Hiya!

I may be running into a "this cannot be supported" situation, but the failure mode was somewhat surprising.

For a small project I was trying to patch Kernel.node/0, but ran into trouble (the patch seemingly had no effect). I assumed this might be because Kernel.node/0 is inlined by the compiler and tried this on a whim, but got the following MatchError (truncated for readability; you can find the full error here)

  test "can patch node" do
    patch(:erlang, :node, :me@localhost)

    assert Kernel.node() == :me@localhost
  end
** (MatchError) no match of right hand side value: {:error, {:abstract_forms_invalid, ...}}
     code: patch(:erlang, :node, :me@localhost)
     stacktrace:
       (patch 0.14.0) lib/patch.ex:492: Patch.patch/3
       (patch 0.14.0) lib/patch.ex:507: Patch.patch/3
       test/user/patch/erlang_test.exs:28: (test)

I took a quick stab at fixing it, but found myself out of my depth somewhat quickly; this seems to boil down to this error from :compile.abstract_forms/2. At least one of these errors (the :undefined_function) seems legitimate and not suitable to ignore, so I am unsure of a decent path forward.

   [
     {~c"erlang.erl",
      [
        {{57, 2}, :erl_lint, {:bad_deprecated, {:now, 0}}},
        {{60, 2}, :erl_lint, {:bad_deprecated, {:phash, 2}}},
        {{1411, 2}, :erl_lint, {:undefined_function, {:halt, 2}}}
      ]}
   ],

I acknowledge, I am probably doing something that would not be best practice for writing a test :) The easy solution is to wrap my node check in a mockable function, but I wanted to report this behavior.

ollien avatar Dec 08 '24 22:12 ollien