diff --git a/lib/phoenix/verified_routes.ex b/lib/phoenix/verified_routes.ex index 0585bde717..5924bd6293 100644 --- a/lib/phoenix/verified_routes.ex +++ b/lib/phoenix/verified_routes.ex @@ -925,7 +925,19 @@ defmodule Phoenix.VerifiedRoutes do defp to_param(data), do: Phoenix.Param.to_param(data) defp build_route(route_ast, sigil_p, env, endpoint_ctx, router) do - config = Module.get_attribute(env.module, :phoenix_verified_config, []) + config = Module.get_attribute(env.module, :phoenix_verified_config) + + if is_nil(config) do + raise ArgumentError, """ + attempted to use Phoenix.VerifiedRoutes without calling `use Phoenix.VerifiedRoutes` first. + + You must use `use Phoenix.VerifiedRoutes` with the appropriate options instead of importing it: + + use Phoenix.VerifiedRoutes, endpoint: MyAppWeb.Endpoint, router: MyAppWeb.Router + + See the documentation for more details on configuration options. + """ + end router = case Macro.expand(router, env) do diff --git a/test/phoenix/verified_routes_test.exs b/test/phoenix/verified_routes_test.exs index 3fcb9654d6..9ffc58b44e 100644 --- a/test/phoenix/verified_routes_test.exs +++ b/test/phoenix/verified_routes_test.exs @@ -302,6 +302,17 @@ defmodule Phoenix.VerifiedRoutesTest do :code.delete(__MODULE__.SigilPPrefix) end + test "~p raises when VerifiedRoutes is imported instead of used" do + assert_raise ArgumentError, + ~r|attempted to use Phoenix.VerifiedRoutes without calling|, + fn -> + defmodule BadImportedVerifiedRoutes do + import Phoenix.VerifiedRoutes + def test, do: ~p"/posts/1" + end + end + end + test "path arities" do assert path(Endpoint, ~p"/posts/1") == "/posts/1" assert path(conn_with_endpoint(), ~p"/posts/1") == "/posts/1"