Skip to content

Commit b2e33b4

Browse files
committed
Raise if app in mix new already exists, closes #11300
1 parent cd1ea80 commit b2e33b4

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

lib/mix/lib/mix/tasks/app.tree.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ defmodule Mix.Tasks.App.Tree do
8080
end
8181

8282
defp load(app, type) do
83-
case Application.load(app) do
83+
case Application.ensure_loaded(app) do
8484
:ok -> true
85-
{:error, {:already_loaded, ^app}} -> true
8685
_ when type == :optional -> false
8786
_ -> Mix.raise("could not find application #{app}")
8887
end

lib/mix/lib/mix/tasks/new.ex

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ defmodule Mix.Tasks.New do
2828
An `--umbrella` option can be given to generate an
2929
umbrella project.
3030
31-
3231
## Examples
3332
3433
mix new hello_world
@@ -167,10 +166,9 @@ defmodule Mix.Tasks.New do
167166
end
168167

169168
defp check_application_name!(name, inferred?) do
170-
unless name =~ ~r/^[a-z][a-z0-9_]*$/ do
169+
if message = invalid_app(name) || reserved_app(name) do
171170
Mix.raise(
172-
"Application name must start with a lowercase ASCII letter, followed by " <>
173-
"lowercase ASCII letters, numbers, or underscores, got: #{inspect(name)}" <>
171+
message <>
174172
if inferred? do
175173
". The application name is inferred from the path, if you'd like to " <>
176174
"explicitly name the application then use the \"--app APP\" option"
@@ -181,6 +179,19 @@ defmodule Mix.Tasks.New do
181179
end
182180
end
183181

182+
defp invalid_app(name) do
183+
unless name =~ ~r/^[a-z][a-z0-9_]*$/ do
184+
"Application name must start with a lowercase ASCII letter, followed by " <>
185+
"lowercase ASCII letters, numbers, or underscores, got: #{inspect(name)}"
186+
end
187+
end
188+
189+
defp reserved_app(name) do
190+
if name |> String.to_atom() |> Application.ensure_loaded() == :ok do
191+
"Cannot use application name #{inspect(name)} because it is already used by Erlang/OTP or Elixir"
192+
end
193+
end
194+
184195
defp check_mod_name_validity!(name) do
185196
unless name =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/ do
186197
Mix.raise(

lib/mix/test/mix/tasks/new_test.exs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ defmodule Mix.Tasks.NewTest do
1616
assert_file("hello_world/.gitignore")
1717

1818
assert_file("hello_world/lib/hello_world.ex", ~r/defmodule HelloWorld do/)
19-
2019
assert_file("hello_world/test/test_helper.exs", ~r/ExUnit.start()/)
2120

2221
assert_file("hello_world/test/hello_world_test.exs", fn file ->
@@ -160,6 +159,12 @@ defmodule Mix.Tasks.NewTest do
160159
Mix.Tasks.New.run(["007invalid"])
161160
end
162161

162+
assert_raise Mix.Error,
163+
~r"Cannot use application name \"tools\" because it is already used by Erlang/OTP or Elixir",
164+
fn ->
165+
Mix.Tasks.New.run(["tools"])
166+
end
167+
163168
assert_raise Mix.Error,
164169
~r"followed by lowercase ASCII letters, numbers, or underscores",
165170
fn ->
@@ -185,18 +190,6 @@ defmodule Mix.Tasks.NewTest do
185190
end
186191
end)
187192

188-
in_tmp("new with an already taken application name", fn ->
189-
assert_raise Mix.Error, ~r"Module name \w+ is already taken", fn ->
190-
Mix.Tasks.New.run(["mix"])
191-
end
192-
end)
193-
194-
in_tmp("new with an already taken application name from the app option", fn ->
195-
assert_raise Mix.Error, ~r"Module name \w+ is already taken", fn ->
196-
Mix.Tasks.New.run(["valid", "--app", "mix"])
197-
end
198-
end)
199-
200193
in_tmp("new with an already taken module name from the module options", fn ->
201194
assert_raise Mix.Error, ~r"Module name \w+ is already taken", fn ->
202195
Mix.Tasks.New.run(["valid", "--module", "Mix"])

0 commit comments

Comments
 (0)