/
build.ex
600 lines (501 loc) · 18.5 KB
/
build.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
defmodule ElixirLS.LanguageServer.Build do
alias ElixirLS.LanguageServer.{Server, JsonRpc, Diagnostics, Tracer, SourceFile}
alias ElixirLS.Utils.MixfileHelpers
require Logger
def build(parent, root_path, opts) when is_binary(root_path) do
spawn_monitor(fn ->
with_build_lock(fn ->
{us, result} =
:timer.tc(fn ->
Logger.info("Starting build with MIX_ENV: #{Mix.env()} MIX_TARGET: #{Mix.target()}")
# read cache before cleaning up mix state in reload_project
cached_deps = read_cached_deps()
mixfile = SourceFile.Path.absname(MixfileHelpers.mix_exs(), root_path)
case reload_project(mixfile, root_path) do
{:ok, mixfile_diagnostics} ->
{deps_result, deps_raw_diagnostics} =
with_diagnostics([log: true], fn ->
try do
# this call can raise
current_deps = Mix.Dep.load_on_environment([])
purge_changed_deps(current_deps, cached_deps)
if Keyword.get(opts, :fetch_deps?) and current_deps != cached_deps do
fetch_deps(current_deps)
end
state = %{
get: Mix.Project.get(),
# project_file: Mix.Project.project_file(),
config: Mix.Project.config(),
# config_files: Mix.Project.config_files(),
config_mtime: Mix.Project.config_mtime(),
umbrella?: Mix.Project.umbrella?(),
apps_paths: Mix.Project.apps_paths(),
# deps_path: Mix.Project.deps_path(),
# deps_apps: Mix.Project.deps_apps(),
# deps_scms: Mix.Project.deps_scms(),
deps_paths: Mix.Project.deps_paths(),
# build_path: Mix.Project.build_path(),
manifest_path: Mix.Project.manifest_path()
}
ElixirLS.LanguageServer.MixProject.store(state)
:ok
catch
kind, err ->
{payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__)
{:error, kind, payload, stacktrace}
end
end)
deps_diagnostics =
deps_raw_diagnostics
|> Enum.map(&Diagnostics.code_diagnostic/1)
case deps_result do
:ok ->
if Keyword.get(opts, :compile?) do
{status, compile_diagnostics} = run_mix_compile()
compile_diagnostics =
Diagnostics.normalize(compile_diagnostics, root_path, mixfile)
Server.build_finished(
parent,
{status, mixfile_diagnostics ++ deps_diagnostics ++ compile_diagnostics}
)
:"mix_compile_#{status}"
else
Server.build_finished(
parent,
{:ok, mixfile_diagnostics ++ deps_diagnostics}
)
:mix_compile_disabled
end
{:error, kind, err, stacktrace} ->
# TODO get path from exception message
Server.build_finished(
parent,
{:error,
mixfile_diagnostics ++
deps_diagnostics ++
[
Diagnostics.error_to_diagnostic(
kind,
err,
stacktrace,
mixfile,
root_path
)
]}
)
:deps_error
end
{:error, mixfile_diagnostics} ->
Server.build_finished(parent, {:error, mixfile_diagnostics})
:mixfile_error
:no_mixfile ->
Server.build_finished(parent, {:no_mixfile, []})
:no_mixfile
end
end)
if Keyword.get(opts, :compile?) do
Tracer.save()
Logger.info("Compile took #{div(us, 1000)} milliseconds")
else
Logger.info("Mix project load took #{div(us, 1000)} milliseconds")
end
JsonRpc.telemetry("build", %{"elixir_ls.build_result" => result}, %{
"elixir_ls.build_time" => div(us, 1000)
})
end)
end)
end
def clean(clean_deps? \\ false) do
with_build_lock(fn ->
Mix.Task.clear()
run_mix_clean(clean_deps?)
end)
end
def with_build_lock(func) do
:global.trans({__MODULE__, self()}, func)
end
def reload_project(mixfile, root_path) do
if File.exists?(mixfile) do
if module = Mix.Project.get() do
build_path = Mix.Project.config()[:build_path]
deps_paths =
try do
# this call can raise (RuntimeError) cannot retrieve dependencies information because dependencies
# were not loaded. Please invoke one of "deps.loadpaths", "loadpaths", or "compile" Mix task
Mix.Project.deps_paths()
catch
kind, payload ->
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
message = Exception.format(kind, payload, stacktrace)
Logger.warning("Unable to prune mix project: #{message}")
[]
end
for {app, path} <- deps_paths do
child_module =
try do
Mix.Project.in_project(app, path, [build_path: build_path], fn mix_project ->
mix_project
end)
catch
kind, payload ->
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
message = Exception.format(kind, payload, stacktrace)
Logger.warning("Unable to prune mix project module for #{app}: #{message}")
end
if child_module do
purge_module(child_module)
end
end
unload_mix_project_apps()
# FIXME: Private API
Mix.Project.pop()
purge_module(module)
end
# We need to clear persistent cache, otherwise `deps.loadpaths` task fails with
# (Mix.Error) Can't continue due to errors on dependencies
# see https://github.com/elixir-lsp/elixir-ls/issues/120
# originally reported in https://github.com/JakeBecker/elixir-ls/issues/71
# Note that `Mix.State.clear_cache()` is not enough (at least on elixir 1.14)
Mix.Project.clear_deps_cache()
Mix.State.clear_cache()
Mix.Task.clear()
if Version.match?(System.version(), ">= 1.15.0-dev") do
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
Logger.error("Build without intercepted logger #{inspect(:logger.get_handler_ids())}")
for handler_id <- :logger.get_handler_ids() do
:ok = :logger.remove_handler(handler_id)
end
:ok =
:logger.add_handler(
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc.handler_config()
)
end
end
# we need to reset compiler options
# project may leave tracers after previous compilation and we don't want them interfering
# see https://github.com/elixir-lsp/elixir-ls/issues/717
set_compiler_options()
# Override build directory to avoid interfering with other dev tools
# FIXME: Private API
Mix.ProjectStack.post_config(build_path: ".elixir_ls/build")
Mix.ProjectStack.post_config(prune_code_paths: false)
Mix.ProjectStack.post_config(
test_elixirc_options: [
docs: true,
debug_info: true
]
)
# Mix.ProjectStack.post_config(state_loader: {:cli, List.first(args)})
# added in https://github.com/elixir-lang/elixir/commit/9e07da862784ac7d18a1884141c49ab049e61691
# TODO refactor to use a custom state loader when we require elixir 1.15?
# since elixir 1.10 mix disables undefined warnings for mix.exs
# see discussion in https://github.com/elixir-lang/elixir/issues/9676
# https://github.com/elixir-lang/elixir/blob/6f96693b355a9b670f2630fd8e6217b69e325c5a/lib/mix/lib/mix/cli.ex#L41
old_undefined = Code.get_compiler_option(:no_warn_undefined)
Code.put_compiler_option(:no_warn_undefined, :all)
# We can get diagnostics if Mixfile fails to load
{mixfile_status, mixfile_diagnostics} =
case Kernel.ParallelCompiler.compile([mixfile]) do
{:ok, _, warnings} ->
{:ok, Enum.map(warnings, &Diagnostics.mixfile_diagnostic(&1, :warning))}
{:error, errors, warnings} ->
{
:error,
Enum.map(warnings, &Diagnostics.mixfile_diagnostic(&1, :warning)) ++
Enum.map(errors, &Diagnostics.mixfile_diagnostic(&1, :error))
}
end
# restore warnings
Code.put_compiler_option(:no_warn_undefined, old_undefined)
if mixfile_status == :ok do
# The project may override our logger config, so we reset it after loading their config
# store log config
logger_config = Application.get_all_env(:logger)
{config_result, config_raw_diagnostics} =
with_diagnostics([log: true], fn ->
try do
Mix.Task.run("loadconfig")
:ok
catch
kind, err ->
{payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__)
{:error, kind, payload, stacktrace}
after
# reset log config
Application.put_all_env(logger: logger_config)
if Version.match?(System.version(), ">= 1.15.0-dev") do
# remove all log handlers and restore our
for handler_id <- :logger.get_handler_ids(),
handler_id != Logger.Backends.JsonRpc do
:ok = :logger.remove_handler(handler_id)
end
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
:ok =
:logger.add_handler(
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc.handler_config()
)
end
end
# make sure ANSI is disabled
Application.put_env(:elixir, :ansi_enabled, false)
end
end)
config_diagnostics =
config_raw_diagnostics
|> Enum.map(&Diagnostics.code_diagnostic/1)
case config_result do
{:error, kind, err, stacktrace} ->
config_path = Mix.Project.config()[:config_path]
{:error,
mixfile_diagnostics ++
config_diagnostics ++
[Diagnostics.error_to_diagnostic(kind, err, stacktrace, config_path, root_path)]}
:ok ->
{:ok, mixfile_diagnostics ++ config_diagnostics}
end
else
{mixfile_status, mixfile_diagnostics}
end
else
msg =
"No mixfile found in project. " <>
"To use a subdirectory, set `elixirLS.projectDir` in your settings"
Logger.warning(msg <> ". Looked for mixfile at #{inspect(mixfile)}")
:no_mixfile
end
end
defp run_mix_compile do
opts = [
"--return-errors",
"--ignore-module-conflict",
"--no-protocol-consolidation"
]
opts =
if Version.match?(System.version(), ">= 1.15.0-dev") do
opts
else
opts ++ ["--all-warnings"]
end
case Mix.Task.run("compile", opts) do
{status, diagnostics} when status in [:ok, :error, :noop] and is_list(diagnostics) ->
{status, diagnostics}
status when status in [:ok, :noop] ->
{status, []}
other ->
Logger.debug("mix compile returned #{inspect(other)}")
{:ok, []}
end
end
defp run_mix_clean(clean_deps?) do
opts = []
opts =
if clean_deps? do
opts ++ ["--deps"]
else
opts
end
results = Mix.Task.run("clean", opts) |> List.wrap()
if Enum.all?(results, &match?(:ok, &1)) do
:ok
else
Logger.error("mix clean returned #{inspect(results)}")
JsonRpc.telemetry(
"mix_clean_error",
%{"elixir_ls.mix_clean_error" => inspect(results)},
%{}
)
{:error, :clean_failed}
end
end
defp purge_module(module) do
:code.purge(module)
:code.delete(module)
end
defp purge_app(app, purge_modules? \\ true) do
Logger.debug("Stopping #{app}")
case Application.stop(app) do
:ok -> :ok
{:error, {:not_started, _}} -> :ok
{:error, error} -> Logger.warning("Application.stop failed for #{app}: #{inspect(error)}")
end
if purge_modules? do
modules =
case :application.get_key(app, :modules) do
{:ok, modules} -> modules
_ -> []
end
if modules != [] do
Logger.debug("Purging #{length(modules)} modules from #{app}")
for module <- modules, do: purge_module(module)
end
end
Logger.debug("Unloading #{app}")
case Application.unload(app) do
:ok -> :ok
{:error, {:not_loaded, _}} -> :ok
{:error, error} -> Logger.warning("Application.unload failed for #{app}: #{inspect(error)}")
end
end
defp get_deps_by_app(deps), do: get_deps_by_app(deps, %{})
defp get_deps_by_app([], acc), do: acc
defp get_deps_by_app([curr = %Mix.Dep{app: app, deps: deps} | rest], acc) do
acc = get_deps_by_app(deps, acc)
list =
case acc[app] do
nil -> [curr]
list -> [curr | list]
end
get_deps_by_app(rest, acc |> Map.put(app, list))
end
defp maybe_purge_dep(%Mix.Dep{status: status, deps: deps} = dep) do
for dep <- deps, do: maybe_purge_dep(dep)
purge? =
case status do
{:nomatchvsn, _} -> true
:lockoutdated -> true
{:lockmismatch, _} -> true
_ -> false
end
if purge? do
purge_dep(dep)
end
end
defp purge_dep(%Mix.Dep{app: app} = dep) do
if app in [
:language_server,
:elixir_ls_utils,
:elixir_sense,
:jason_v,
:path_glob_vendored,
:dialyxir_vendored,
:erlex_vendored,
:erl2ex_vendored
] do
raise "Unloading required #{app}"
end
for path <- Mix.Dep.load_paths(dep) do
Code.delete_path(path)
end
purge_app(app)
end
defp purge_changed_deps(_current_deps, nil), do: :ok
defp purge_changed_deps(current_deps, cached_deps) do
current_deps_by_app = get_deps_by_app(current_deps)
cached_deps_by_app = get_deps_by_app(cached_deps)
removed_apps = Map.keys(cached_deps_by_app) -- Map.keys(current_deps_by_app)
removed_deps =
cached_deps_by_app
|> Map.take(removed_apps)
|> Enum.flat_map(&elem(&1, 1))
|> Enum.uniq()
# purge removed dependencies
for dep <- removed_deps do
purge_dep(dep)
end
# purge current dependencies in invalid state
for dep <- current_deps do
maybe_purge_dep(dep)
end
end
defp unload_mix_project_apps() do
# note that this will unload config so we need to call loadconfig afterwards
mix_project_apps =
if Mix.Project.umbrella?() do
Mix.Project.apps_paths() |> Enum.map(&elem(&1, 0))
else
# in umbrella Mix.Project.apps_paths() returns nil
# get app from config instead
[Mix.Project.config()[:app]]
end
# purge mix project apps
# elixir compiler loads apps only on initial compilation
# on subsequent ones it does not update application controller state
# if we don't unload the apps we end up with invalid state
# e.g. :application.get_key(app, :modules) returns outdated module list
# see https://github.com/elixir-lang/elixir/issues/13001
for app <- mix_project_apps do
purge_app(app, false)
end
end
defp fetch_deps(current_deps) do
# FIXME: private struct
missing_deps =
current_deps
|> Enum.filter(fn %Mix.Dep{status: status, scm: scm} ->
case status do
{:unavailable, _} -> scm.fetchable?()
{:nomatchvsn, _} -> true
:nolock -> true
:lockoutdated -> true
{:lockmismatch, _} -> true
_ -> false
end
end)
# FIXME: Private struct
|> Enum.map(fn %Mix.Dep{app: app, requirement: requirement} -> "#{app} #{requirement}" end)
if missing_deps != [] do
JsonRpc.show_message(
:info,
"Fetching #{Enum.count(missing_deps)} deps: #{Enum.join(missing_deps, ", ")}"
)
Mix.Task.run("deps.get")
JsonRpc.show_message(
:info,
"Done fetching deps"
)
else
Logger.debug("All deps are up to date")
end
:ok
end
def set_compiler_options(options \\ [], parser_options \\ []) do
parser_options =
Keyword.merge(parser_options,
columns: true,
token_metadata: true
)
options =
Keyword.merge(options,
tracers: [Tracer],
debug_info: true,
docs: true,
parser_options: parser_options
)
options =
if Version.match?(System.version(), ">= 1.14.0-dev") do
Keyword.merge(options,
# this disables warnings `X has already been consolidated`
# when running `compile` task
ignore_already_consolidated: true
)
else
options
end
Code.compiler_options(options)
end
defp read_cached_deps() do
# FIXME: Private api
# we cannot use Mix.Dep.cached() here as it tries to load deps
project = Mix.Project.get()
# in test do not try to load cache from elixir_ls
if project != nil and project != ElixirLS.LanguageServer.Mixfile do
env_target = {Mix.env(), Mix.target()}
case Mix.State.read_cache({:cached_deps, project}) do
{^env_target, deps} -> deps
_ -> nil
end
end
end
def with_diagnostics(opts \\ [], fun) do
# Code.with_diagnostics is broken on elixir < 1.15.3
if Version.match?(System.version(), ">= 1.15.3") do
Code.with_diagnostics(opts, fun)
else
{fun.(), []}
end
end
end