Skip to content

Raise when :router given to use Phoenix.VerifiedRoutes is not a literal - #6806

Open
novaugust wants to merge 4 commits into
phoenixframework:mainfrom
novaugust:warn-non-literal-verified-routes-router
Open

Raise when :router given to use Phoenix.VerifiedRoutes is not a literal#6806
novaugust wants to merge 4 commits into
phoenixframework:mainfrom
novaugust:warn-non-literal-verified-routes-router

Conversation

@novaugust

@novaugust novaugust commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

What is this

Warn Raises when non-literal modules are passed for the :router option with VerifiedRoutes

Background

In an effort to avoid compilation-cycles, we had code where the VerifiedRoutes router was being created dynamically.
While it technically worked, that could create scenarios where it looked like routes were being enforced/verified at compile time when they really weren't, as the compiler was not aware of the dependencies between the modules.

I've since updated our code to not have that problem, but thought it might be worth having a warning come out of Phoenix itself.

There's already existing code that does similar work within the private build_route function:

router =
case Macro.expand(router, env) do
mod when is_atom(mod) ->
mod
other ->
raise ArgumentError, """
expected router to be to module, got: #{inspect(other)}
If your router is not defined at compile-time, use unverified_path/3 instead.
"""
end

I'm wondering if the intention there was the same as what i have here, save that that missed cases like this where the value could be expanded into a module.

Example

Given a dynamically resolved router:

defmodule MyAppWeb.SubMod.NeatCode do
  use Phoenix.VerifiedRoutes,
    router: __MODULE__ |> Module.split() |> List.replace_at(-1, "Router") |> Module.concat()

  def my_path, do: ~p"/my_path"
end

in our setup, I could get this buggy behaviour due to the missing explicit compile-time dependency:

  1. have the example code above
  2. delete the relevant action from the router
  3. compile & get a warning about the invalid ~p"/my_path" route
  4. restore the route to the router
  5. compile, and still get the warning

Assisted by: claude-sonnet-5

Comment thread lib/phoenix/verified_routes.ex Outdated
@SteffenDE
SteffenDE requested a review from josevalim August 27, 2026 08:52
@novaugust novaugust changed the title Warn when :router given to use Phoenix.VerifiedRoutes is not a literal Raise when :router given to use Phoenix.VerifiedRoutes is not a literal Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants