elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

`plug` does not prevent compile-time dependencies for options #1109

Closed marcandre closed 2 years ago

marcandre commented 2 years ago

@josevalim It looks intentional that the plug macro does not expand_alias for plugs that are modules... but this means that code like the following will introduce compile-time dependencies (on HexpmWeb.PlugParser):

defmodule HexpmWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :hexpm
  # ...
  plug Plug.Parsers,
    parsers: [:urlencoded, :json, HexpmWeb.PlugParser],
    pass: ["*/*"],
    json_decoder: Jason

I'm missing the context here, so I don't understand why there is the and not module_plug?(plug). Is there a way to avoid introducing that compile-time dependency?

josevalim commented 2 years ago

A module plug may be initialized at compile-time, so the options are all compile time. Phoenix can address this though by reimplementing the plug macro and checking to Phoenix.plug_init_mode() to know the init mode. Then use Plug.Builder.compile to build the function. We do it in other places in Phoenix, so a PR is welcome.

We can't do it in Plug because we don't have the value of init_mode at compile time. In Phoenix it is a global, so it works.

marcandre commented 2 years ago

Thank you, this clarifies things. Yes, I saw the code used for controllers, I might have a go at that one day.