Skip to content

Commit

Permalink
Raise an error when using a reserved app name (#6011)
Browse files Browse the repository at this point in the history
* Raise an error when using a reserved app name

* Apply suggestions from code review

Co-authored-by: José Valim <[email protected]>

---------

Co-authored-by: José Valim <[email protected]>
  • Loading branch information
almirsarajcic and josevalim authored Jan 11, 2025
1 parent 79ad9db commit bc77433
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion installer/lib/mix/tasks/phx.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ defmodule Mix.Tasks.Phx.New do
adapter: :string
]

@reserved_app_names ~w(server table)

@impl true
def run([version]) when version in ~w(-v --version) do
Mix.shell().info("Phoenix installer v#{@version}")
Expand Down Expand Up @@ -329,7 +331,22 @@ defmodule Mix.Tasks.Phx.New do
end

defp check_app_name!(name, from_app_flag) do
unless name =~ Regex.recompile!(~r/^[a-z][a-z0-9_]*$/) do
with :ok <- validate_not_reserved(name),
:ok <- validate_app_name_format(name, from_app_flag) do
:ok
end
end

defp validate_not_reserved(name) when name in @reserved_app_names do
Mix.raise("Application name cannot be #{inspect(name)} as it is reserved")
end

defp validate_not_reserved(_name), do: :ok

defp validate_app_name_format(name, from_app_flag) do
if name =~ ~r/^[a-z][a-z0-9_]*$/ do
:ok
else
extra =
if !from_app_flag do
". The application name is inferred from the path, if you'd like to " <>
Expand Down
10 changes: 10 additions & 0 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,14 @@ defmodule Mix.Tasks.Phx.NewTest do
"Creates a new Phoenix project."
end)
end

test "new with reserved name" do
assert_raise Mix.Error, ~r/Application name cannot be "server" as it is reserved/, fn ->
Mix.Tasks.Phx.New.run(["server"])
end

assert_raise Mix.Error, ~r/Application name cannot be "table" as it is reserved/, fn ->
Mix.Tasks.Phx.New.run(["table"])
end
end
end

0 comments on commit bc77433

Please sign in to comment.