Bug in Phoenix Liveview router or my mistake? It's adding *Web twice to a route

Curious if this is a bug or just a mistake I made and I’m not noticing!

Using Phoenix 1.7.10

I have this liveview module:

defmodule MyAppWeb.SearchLive do
  use MyAppWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, socket}
  end
end

And in my router, the /search route is broken because for some reason Phoenix is looking for the wrong module name with MyAppWeb listed twice!

  scope "/", MyAppWeb do
    pipe_through [:browser]

    delete "/users/log_out", UserSessionController, :delete

    live_session :current_user,
      on_mount: [{MyAppWeb.UserAuth, :mount_current_user}] do
      live "/users/confirm/:token", UserConfirmationLive, :edit
      live "/users/confirm", UserConfirmationInstructionsLive, :new

      live "/search", SearchLive # BROKEN
      live "/searchx", SearchLivexxx # Adds MyAppWeb only once, good.
      live "/pineapple", SearchLive # Still adding it twice!
    end
  end

By visiting a gibberish URL I can see the routes Phoenix knows and check this out:

  GET     /search       MyAppWeb.MyAppWeb.SearchLive MyAppWeb.MyAppWeb.SearchLive
  GET     /searchx      MyAappWeb.SearchLivexxx MyAappWeb.SearchLivexxx
  GET     /pineapple    MyAppWeb.MyAppWeb.SearchLive MyAppWeb.MyAppWeb.SearchLive

Why is phoenix adding MyAppWeb twice to the module it’s trying to find?

1 Like

This happened to me when VSCode (or some plugin) added an extra alias at the top of my module during an autocomplete action.

3 Likes

I’ve closed my editor entirely and can confirm with cat that there is no double MyAppWeb in either router.ex or my module file.

Deleting the _build folder and the .elixir_ls/ folder does nothing as well.

1 Like

What do You have when You type?

mix phx.routes

And if You type? not for Window…

find lib -type f -name "*.ex" -exec grep -H MyAppWeb {} \;

Can You show the complete router file?

1 Like

Ya, are you sure you don’t have alias MyAppWeb.SearchLive at the top of your router?

This problem is starting to become almost as common as the charlist one :upside_down_face:

4 Likes

image

image

This will never happen to me again! VS Code probably sniffed out that SearchLive existed already and helpfully added the alias up top automatically for me!

Hope this helps other dudes!

1 Like

If you’re using ElixirLS, you can turn this off via elixirLS.autoInsertRequiredAlias. Of course, if you like that behaviour for other files then that’s not really a fix. There are issues logged in github. This is the one I logged which is a sorta-duplicate but there are links to other info there.

4 Likes