Run `Macro.expand` on `use` macro with different __ENV__

Hello!

I’m stuck on a meta programming problem and was hoping someone could enlighten me.

My library rewire is re-wiring aliases of any Elixir module by loading the module’s source file, using Code.string_to_quoted to get the AST, transforming it, and finally using Code.eval_quoted to generate the new module.

But some modules have a use macro in it (like use GenServer) and I’d like to run the AST through Macro.expand on it so get the final code before rewiring it. Problem is that the __ENV__ I have is not the one for the module I’m rewiring but simply my library’s module. So when I use Macro.expand, I see:

you must require GenServer before invoking the macro GenServer.__using__/

To play around with it, I’ve narrow it down to this code snippet:

quote do
  use GenServer
end |> Macro.prewalk(fn (ast) ->
  Macro.expand(ast, __ENV__) |> IO.inspect()
end)

This gives me the same error message from above.

The intermediate output is

{:__block__, [],
 [
   {:__block__, [],
    [
      {:require, [counter: -576460752303423071, context: Kernel], [GenServer]},
      {{:., [], [GenServer, :__using__]}, [], [[]]}
    ]}
 ]}

So it resolves the use to those two instructions but then it fails.

How can I expand this macro correctly?

is that even possible? I have a use case for exactly that!