Bad alias? Scrivener pagination can't find the Repo?

I noticed that when my app shared the same module name as one of my resources, things get weird. This might be a noob mistake, but since I’m developing a microservice dedicated for interacting with a single resource, it did make sense to name the app after the resource. I was working on getting pagination working on my JSON index endpoint – I followed the article on https://www.dailydrip.com/topics/elixirsips/drips/phoenix-api-pagination-with-scrivener to get the scrivener_ecto package (https://github.com/drewolson/scrivener_ecto) working.

When I start the Phoenix server, I got a warning like this:

warning: function Myresource.MyresourceCtx.Myresource.Repo.paginate/2 is undefined (module Myresource.MyresourceCtx.Myresource.Repo is not available)

My controller uses a simple alias:
alias Myresource.Repo

  def index(conn, params) do
    page =
      Myresource
      |> Repo.paginate(params)

    conn
    |> Scrivener.Headers.paginate(page)
    |> render("index.json", resource: page.entries)
  end

So it looks like Elixir gets confused by that name… Repo.paginate isn’t a method on Myresource.MyresourceCtx.Myresource.Repo – it’s a method on Myresource.Repo. I know I shouldn’t have named my application this way, but why does Elixir get confused?

(I was able to get this working on a separate Phoenix app where I didn’t mix up my module names).

Do you have any other aliases in that file? Could it be possible you are bitten by this?

Additionally to what @voger said, please also check in your XWeb module if the controller/0 function does add any uses and aliases and take them into account when reasoning about the order of alias. Or any other use you might have as well.

2 Likes

Ah, of course… yeah, one alias resolved into another creating a compound trainwreck of my module names. I hadn’t thought that through as I carelessly added the aliases. Thank you!

1 Like