Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle compilation exiting #514

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/common/lib/elixir/features.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ defmodule Elixir.Features do
def config_reader? do
Version.match?(System.version(), ">= 1.11.0")
end

def after_verify? do
Version.match?(System.version(), ">= 1.14.0")
end
end
14 changes: 13 additions & 1 deletion apps/remote_control/lib/lexical/remote_control/build/document.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
defmodule Lexical.RemoteControl.Build.Document do
alias Lexical.Document
alias Lexical.RemoteControl.Build
alias Lexical.RemoteControl.Build.Document.Compilers
alias Lexical.RemoteControl.Build.Isolation

@compilers [Compilers.Config, Compilers.Elixir, Compilers.EEx, Compilers.HEEx, Compilers.NoOp]

def compile(%Document{} = document) do
compiler = Enum.find(@compilers, & &1.recognizes?(document))
compiler.compile(document)
compile_fun = fn -> compiler.compile(document) end

case Isolation.invoke(compile_fun) do
{:ok, result} ->
result

{:error, {exception, stack}} ->
diagnostic = Build.Error.error_to_diagnostic(document, exception, stack, nil)
diagnostics = Build.Error.refine_diagnostics([diagnostic])
{:error, diagnostics}
end
end
end
25 changes: 25 additions & 0 deletions apps/remote_control/lib/lexical/remote_control/build/isolation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Lexical.RemoteControl.Build.Isolation do
@moduledoc """
Runs functions in an isolated, monitored process
"""

@spec invoke((() -> term())) :: {:ok, term()} | {:error, term()}
def invoke(function) when is_function(function, 0) do
me = self()

{pid, ref} =
spawn_monitor(fn ->
send(me, {:result, function.()})
end)

receive do
{:result, result} ->
# clean up the DOWN message from the above process in the mailbox.
Process.demonitor(ref, [:flush])
{:ok, result}

{:DOWN, ^ref, :process, ^pid, reason} ->
{:error, reason}
end
end
end
24 changes: 23 additions & 1 deletion apps/remote_control/lib/lexical/remote_control/build/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ defmodule Lexical.RemoteControl.Build.Project do
alias Lexical.Project
alias Lexical.RemoteControl
alias Lexical.RemoteControl.Build
alias Lexical.RemoteControl.Build.Isolation
alias Lexical.RemoteControl.Plugin
alias Mix.Task.Compiler.Diagnostic

use Build.Progress
require Logger
Expand All @@ -17,7 +19,7 @@ defmodule Lexical.RemoteControl.Build.Project do
Mix.Task.clear()

with_progress building_label(project), fn ->
result = Mix.Task.run(:compile, mix_compile_opts(force?))
result = compile_in_isolation(force?)
Mix.Task.run(:loadpaths)
result
end
Expand All @@ -44,6 +46,26 @@ defmodule Lexical.RemoteControl.Build.Project do
end)
end

defp compile_in_isolation(force?) do
compile_fun = fn -> Mix.Task.run(:compile, mix_compile_opts(force?)) end

case Isolation.invoke(compile_fun) do
{:ok, result} ->
result

{:error, {exception, [{_mod, _fun, _arity, meta} | _]}} ->
diagnostic = %Diagnostic{
file: Keyword.get(meta, :file),
severity: :error,
message: Exception.message(exception),
compiler_name: "Elixir",
position: Keyword.get(meta, :line, 1)
}

{:error, [diagnostic]}
end
end

defp prepare_for_project_build(false = _force?) do
:ok
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
compilation_errors-*.tar

# Temporary files, for example, from tests.
/tmp/
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule CompileCallbackError do
@after_verify __MODULE__
def __after_verify__(_) do
raise "boom"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule CompilationCallbackErrors.MixProject do
use Mix.Project

def project do
Code.put_compiler_option(:ignore_module_conflict, true)

[
app: :compilation_callback_errors,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

def application do
[
extra_applications: [:logger]
]
end

defp deps do
[]
end
end
Loading
Loading