Need help figuring out compilation error involving Router Helpers

My tests started randomly failing in GitLab one day. I say randomly because re-running the tests for previously passing code, now has them failing. I was able to reproduce on my laptop by deleting all my deps and _build folders, deleting all my docker volumes/images/etc, and rebuilding everything from blank.

I’m getting this compilation error:

== Compilation error in file lib/horse_racing_nation_web/entries/views/picks_view.ex ==
** (CompileError) lib/horse_racing_nation_web/entries/views/picks_view.ex:2: module HorseRacingNationWeb.Entries.Router.Helpers is not loaded and could not be found
    expanding macro: HorseRacingNationWeb.Entries.__using__/1
    lib/horse_racing_nation_web/entries/views/picks_view.ex:2: HorseRacingNationWeb.Entries.PicksView (module)
    (elixir 1.11.4) expanding macro: Kernel.use/2
    lib/horse_racing_nation_web/entries/views/picks_view.ex:2: HorseRacingNationWeb.Entries.PicksView (module)

The view file in the error:

lib/horse_racing_nation_web/entries/views/picks_view.ex

defmodule HorseRacingNationWeb.Entries.PicksView do
  use HorseRacingNationWeb.Entries, :view

  <snip>
end

I think this file is pretty much what Phoenix generates.

lib/horse_racing_nation_web/entries.ex

defmodule HorseRacingNationWeb.Entries do
  def controller do
    quote do
      use Phoenix.Controller, namespace: HorseRacingNationWeb.Entries
      import Plug.Conn
      import HorseRacingNationWeb.Entries.Router.Helpers
      import HorseRacingNationWeb.Gettext
    end
  end

  def view do
    quote do
      use Phoenix.View,
        root: "lib/horse_racing_nation_web/entries/templates",
        namespace: HorseRacingNationWeb.Entries

      # Import convenience functions from controllers
      import Phoenix.Controller, only: [get_flash: 2, view_module: 1]

      # Use all HTML functionality (forms, tags, etc)
      use Phoenix.HTML

      import HorseRacingNationWeb.Entries.Router.Helpers
      import HorseRacingNationWeb.Entries.ErrorHelpers
      import HorseRacingNationWeb.Gettext
    end
  end

  def router do
    quote do
      use Phoenix.Router
      import Plug.Conn
      import Phoenix.Controller
    end
  end

  def channel do
    quote do
      use Phoenix.Channel
      import HorseRacingNationWeb.Gettext
    end
  end

  @doc """
  When used, dispatch to the appropriate controller/view/etc.
  """
  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__, which, [])
  end
end

My router.
lib/horse_racing_nation_web/entries/router.ex

defmodule HorseRacingNationWeb.Entries.Router do
  use HorseRacingNationWeb.Entries, :router
  use Honeybadger.Plug

  pipeline :browser do
    plug(:accepts, ["html"])

    <snip>
  end

  pipeline :api do
    plug(:accepts, ["json"])

    <snip>
  end

  scope "/api", HorseRacingNationWeb.Entries, as: "api" do
    pipe_through(:api)

    <snip>
  end

  scope "/", HorseRacingNationWeb.Entries do
    pipe_through(:browser)

    <snip>
  end
end

I thought maybe there was some sort of circular dependency because both HorseRacingNationWeb.Entries.PicksView and HorseRacingNationWeb.Entries.Router are using HorseRacingNationWeb.Entries, so I tried to inline the use in the router. That didn’t help.

The code all seems to be correct to me, and I’ve been banging my head against a wall for too long. I’m guessing the router helpers aren’t being generated, but I don’t know why. Can someone help?

Have you recently upgraded Phoenix? Which version are you at now?

1 Like

Looks like I updated from Phoenix 1.5.8 → 1.5.10 on 8/12/2021. I’ll try reverting that.

Reverting to phoenix 1.5.8 fixed it! Thanks for getting me unstuck!

I don’t see anything in the Phoenix changelog that makes it look like that was supposed to break, so I’ll file an issue.

I’d try only using the Routes module/alias first.

I spoke too soon. After re-deleting my deps/_build folders, deleting all my docker volumes/images/etc, I still have the same compile error on Phoenix 1.5.8.

What do you mean “only using the Routes module/alias first”? I don’t understand.

Could it be that you are using routes helper functions like Routes. in your view/template?

If so try changing import HorseRacingNationWeb.Entries.Router.Helpers to alias HorseRacingNationWeb.Entries.Router.Helpers, as: Routes in lib/horse_racing_nation_web/entries.ex

Edit: Also check out Module BpmServer.Router.Helpers is not loaded and could not be found - #3 by benwilson512

But I think that would also be resolved by aliasing as mentioned above

1 Like

No, I grepped and we don’t have the word Routes anywhere in our app. This is a fairly old app that’s been incrementally upgraded over time.

I checked all my plugs, and none of them refer to the Router or Router.Helpers. I also tried commenting out all my custom plugs in my router, and that didn’t help anything.

What happens if you comment out your entire router.ex and try to recompile?

I’ll try that next. Currently I changed all the “import Router.Helpers” to “alias Router.Helpers, as: Routes” and I’m working on fixing 1,000 compile errors.

There was a migration required a while ago when upgrading (from Phoenix 1.3 to 1.4 I believe but I might be wrong) that did away with having import-ed path/url functions. You had to use Routes.xxxx_path from then on.

1 Like

Yeah you probably have to change all page_path()/foo_path() to Routes.page_path() etc?

1 Like

Correct. I finished making all those changes, and now it’s exploding with this:

== Compilation error in file lib/horse_racing_nation_web/amp/router.ex ==
** (CompileError) lib/horse_racing_nation_web/amp/router.ex:3: module Honeybadger.Plug is not loaded and could not be found
    (elixir 1.11.4) expanding macro: Kernel.use/1
    lib/horse_racing_nation_web/amp/router.ex:3: HorseRacingNationWeb.Amp.Router (module)

But the honeybadger hex package is definitely installed and there:

mix.lock:

  "honeybadger": {:hex, :honeybadger, "0.17.0", "4c6682bfe17907788d1d61338f123e2d698e15354c3868372e392c3dc2f729aa", [:mix], [{:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:hackney, "~> 1.1", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.0.0 and < 2.0.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, ">= 1.0.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a97182bbf7b09de3112393ee080f133637d9fdb1535765548e89b69977c43621"},

I can comment out the use Honeybadger.Plug, and everything finally works, but obviously I’d prefer that my error reporting is still enabled.