File tree Expand file tree Collapse file tree 4 files changed +33
-29
lines changed Expand file tree Collapse file tree 4 files changed +33
-29
lines changed Original file line number Diff line number Diff line change @@ -228,30 +228,17 @@ defmodule Mix do
228
228
Raises a mix error that is nicely formatted.
229
229
"""
230
230
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
232
232
end
233
233
234
234
@ doc """
235
235
Raises a mix compatible exception.
236
236
237
- A mix compatible exception has a `mix_error ` field which mix
237
+ A mix compatible exception has a `mix ` field which mix
238
238
uses to store the project or application name which is
239
239
automatically by the formatting tools.
240
240
"""
241
241
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 }
256
243
end
257
244
end
Original file line number Diff line number Diff line change @@ -75,21 +75,16 @@ defmodule Mix.CLI do
75
75
exception ->
76
76
stacktrace = System . stacktrace
77
77
78
- if info = Map . get ( exception , :mix ) do
78
+ if Map . get ( exception , :mix ) do
79
79
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 } )
81
82
else
82
83
reraise exception , stacktrace
83
84
end
84
-
85
- exit ( { :shutdown , 1 } )
86
85
end
87
86
end
88
87
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
-
93
88
defp change_env ( task ) do
94
89
if nil? ( System . get_env ( "MIX_ENV" ) ) &&
95
90
( env = preferred_cli_env ( task ) ) do
Original file line number Diff line number Diff line change @@ -172,13 +172,19 @@ defmodule Mix.Project do
172
172
def in_project ( app , path , post_config \\ [ ] , fun )
173
173
174
174
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
177
184
fun . ( cached )
178
185
after
179
186
Mix.Project . pop
180
187
end
181
- result
182
188
end
183
189
184
190
def in_project ( app , path , post_config , fun ) do
@@ -264,7 +270,7 @@ defmodule Mix.Project do
264
270
app = config [ :app ] ->
265
271
Path . join ( [ build_path ( config ) , "lib" , Atom . to_string ( app ) ] )
266
272
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"
268
274
true ->
269
275
Mix . raise "Cannot access build without an application name, " <>
270
276
"please ensure you are in a directory with a mix.exs file and it defines " <>
Original file line number Diff line number Diff line change @@ -88,6 +88,22 @@ defmodule Mix.ProjectTest do
88
88
end
89
89
end
90
90
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
+
91
107
test "config_files" do
92
108
Mix.Project . push ( SampleProject )
93
109
@@ -104,7 +120,7 @@ defmodule Mix.ProjectTest do
104
120
refute "config/.exs" in files
105
121
end
106
122
end
107
-
123
+
108
124
defp assert_proj_dir_linked_or_copied ( source , target , symlink_path ) do
109
125
case :file . read_link ( source ) do
110
126
{ :ok , path } -> assert path == symlink_path
You can’t perform that action at this time.
0 commit comments