Loading runtime generated module into remote nodes

Hey people,

I have a bit strange problem/question. I am trying to load a module that is generated at runtime into other node connected via distribution.

In overall the module creation looks like this:

module = Module.concat(MyProject, MyModule)
contents = Code.string_to_quoted!("def base(), do: IO.puts(\"test\")")
Module.create(module, contents, Macro.Env.location(__ENV__))

Now I am trying to find out how to load it into a remote note. In erlang I would do something like this:

{_Module, Binary, Filename} = code:get_object_code(Module).
rpc:call(Node, code, load_binary, [Module, Filename, Binary]).

See also: https://medium.com/@stavro/intro-slave-nodes-and-remote-code-loading-d1168bff7b20

However, it does not seem to work for me at all. E.g.

{:module, name, binary, _other} = Module.create(module, contents, Macro.Env.location(__ENV__))
:rpc.call(:"worker@127.0.0.1", :code, :load_binary, [name, "filename?", binary])

{:badrpc,
 {:EXIT,
  {:function_clause,
   [
     {:code, :load_binary,
      [
        MyProject.MyModule,
        "filename?",
        <<70, 79, 82, 49, 0, 0, 4, 244, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0,
          0, 152, 0, 0, 0, 15, 25, 69, 108, 105, 120, 105, 114, 46, 77, 121, 80,
          114, 111, ...>>
      ], [file: 'code.erl', line: 184]}
   ]}}}

o_O. Actually, a filename should be a charlist. Now it works.

1 Like