Skip to content

Commit 89fd8f3

Browse files
author
José Valim
committed
Print a nice prelude if we fail to load a project, closes #2430
1 parent 353c043 commit 89fd8f3

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

lib/mix/lib/mix.ex

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,30 +228,17 @@ defmodule Mix do
228228
Raises a mix error that is nicely formatted.
229229
"""
230230
def raise(message) when is_binary(message) do
231-
Kernel.raise Mix.Error, mix: mix_info, message: message
231+
Kernel.raise Mix.Error, mix: true, message: message
232232
end
233233

234234
@doc """
235235
Raises a mix compatible exception.
236236
237-
A mix compatible exception has a `mix_error` field which mix
237+
A mix compatible exception has a `mix` field which mix
238238
uses to store the project or application name which is
239239
automatically by the formatting tools.
240240
"""
241241
def raise(exception, opts) when is_atom(exception) do
242-
Kernel.raise %{exception.exception(opts) | mix: mix_info}
243-
end
244-
245-
defp mix_info do
246-
case Mix.ProjectStack.peek do
247-
%{name: name, config: config, pos: pos} when pos > 0 ->
248-
if app = config[:app] do
249-
{:app, app}
250-
else
251-
{:project, name}
252-
end
253-
_ ->
254-
:none
255-
end
242+
Kernel.raise %{exception.exception(opts) | mix: true}
256243
end
257244
end

lib/mix/lib/mix/cli.ex

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,16 @@ defmodule Mix.CLI do
7575
exception ->
7676
stacktrace = System.stacktrace
7777

78-
if info = Map.get(exception, :mix) do
78+
if Map.get(exception, :mix) do
7979
mod = exception.__struct__ |> Module.split() |> Enum.at(0, "Mix")
80-
Mix.shell.error "** (#{mod})#{show_mix_info(info)} #{Exception.message(exception)}"
80+
Mix.shell.error "** (#{mod}) #{Exception.message(exception)}"
81+
exit({:shutdown, 1})
8182
else
8283
reraise exception, stacktrace
8384
end
84-
85-
exit({:shutdown, 1})
8685
end
8786
end
8887

89-
defp show_mix_info({:project, proj}), do: " [#{inspect proj}]"
90-
defp show_mix_info({:app, app}), do: " [#{app}]"
91-
defp show_mix_info(:none), do: ""
92-
9388
defp change_env(task) do
9489
if nil?(System.get_env("MIX_ENV")) &&
9590
(env = preferred_cli_env(task)) do

lib/mix/lib/mix/project.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,19 @@ defmodule Mix.Project do
172172
def in_project(app, path, post_config \\ [], fun)
173173

174174
def in_project(app, ".", post_config, fun) do
175-
cached = load_project(app, post_config)
176-
result = try do
175+
cached = try do
176+
load_project(app, post_config)
177+
rescue
178+
any ->
179+
Mix.shell.error "Error while loading project #{inspect app} at #{File.cwd!}"
180+
reraise any, System.stacktrace
181+
end
182+
183+
try do
177184
fun.(cached)
178185
after
179186
Mix.Project.pop
180187
end
181-
result
182188
end
183189

184190
def in_project(app, path, post_config, fun) do
@@ -264,7 +270,7 @@ defmodule Mix.Project do
264270
app = config[:app] ->
265271
Path.join([build_path(config), "lib", Atom.to_string(app)])
266272
config[:apps_path] ->
267-
raise "Trying to access app_path for an umbrella project but umbrellas have no app"
273+
Mix.raise "Trying to access app_path for an umbrella project but umbrellas have no app"
268274
true ->
269275
Mix.raise "Cannot access build without an application name, " <>
270276
"please ensure you are in a directory with a mix.exs file and it defines " <>

lib/mix/test/mix/project_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ defmodule Mix.ProjectTest do
8888
end
8989
end
9090

91+
test "in_project prints nice error message if fails to load file" do
92+
in_fixture "no_mixfile", fn ->
93+
File.write "mix.exs", """
94+
raise "oops"
95+
"""
96+
97+
assert_raise RuntimeError, "oops", fn ->
98+
Mix.Project.in_project :hello, ".", [], fn _ ->
99+
:ok
100+
end
101+
end
102+
103+
assert_receive {:mix_shell, :error, ["Error while loading project :hello at" <> _]}
104+
end
105+
end
106+
91107
test "config_files" do
92108
Mix.Project.push(SampleProject)
93109

@@ -104,7 +120,7 @@ defmodule Mix.ProjectTest do
104120
refute "config/.exs" in files
105121
end
106122
end
107-
123+
108124
defp assert_proj_dir_linked_or_copied(source, target, symlink_path) do
109125
case :file.read_link(source) do
110126
{:ok, path} -> assert path == symlink_path

0 commit comments

Comments
 (0)