# `Juggernaut.Schemata.Compile`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/schemata/compile.ex#L1)

Compiles the sandbox incrementally with Mix, in a subprocess, and returns Mix's
diagnostics as data (design notes §10.3, `docs/dependencies/mix-compile.md`).

The subprocess is `elixir -e <script>` with the sandbox as its working directory,
`MIX_ENV=test` and `MIX_DEPS_PATH` set to the sandbox's dependency mirror
(`Juggernaut.Sandbox.deps_dir/1`). The script loads the sandbox's `mix.exs` and the
project config (`loadconfig`, as the `mix` command line does before any task, so code
that reads the config while it compiles sees the project's values), turns
type inference off in the project's `elixirc_options` (`infer_signatures: false`,
when `Code.compiler_options/0` has the option; Mix's compiler sets the option from
there, so `Code.put_compiler_option/2` is not enough), runs `loadpaths`, loads
`Juggernaut.Peer.Switch` from Juggernaut's own beam when the project does not provide
it (code run at compile time can reach a metamutant), and runs the compile task itself,
`Mix.Tasks.Compile.run(["--no-verification", "--return-errors"])`, then writes
`{status, diagnostics}` as an external term to a file. A project alias of `compile` is
not run: `Mix.Task.run("compile")` would return the alias's result instead of the
diagnostics, and an alias's extra steps are not needed to run tests. A compile that
raises with `--no-verification` is run once more without it, and the result says so. A
subprocess that leaves no result is an infrastructure error carrying the end of its
output.

A build compiles the sandbox many times (poison isolation compiles again after each
failure), and starting the VM, loading `mix.exs` and `loadpaths` cost more than an
incremental compile of one changed file. A compile server (`open/2`) is one such
subprocess kept for the build: it sets the project up on its first compile and, for
each later one, re-enables the compile tasks as IEx's `recompile` does and runs the
compile task again. `run/2` with `:server` compiles there; a server that has stopped
(its VM exited) is never used again, and that compile and the later ones run in a
subprocess of their own, as without a server.

Diagnostics come back with paths relative to the sandbox and positions as
`{line, column}`, a line, or `nil` (no position).

# `cmd`

```elixir
@type cmd() :: (String.t(), [String.t()], keyword() -&gt;
            {Collectable.t(), non_neg_integer()})
```

# `diagnostic`

```elixir
@type diagnostic() :: %{
  file: String.t(),
  position: {pos_integer(), pos_integer()} | pos_integer() | nil,
  message: String.t(),
  severity: atom()
}
```

# `result`

```elixir
@type result() :: %{
  status: :ok | :noop | :error,
  diagnostics: [diagnostic()],
  verification?: boolean(),
  output: String.t(),
  ms: non_neg_integer()
}
```

# `server`

```elixir
@type server() :: %{port: port() | nil, marker: String.t()}
```

A compile server (`open/2`): one subprocess kept for a build's compiles.

# `close`

```elixir
@spec close(server()) :: :ok
```

Stops a compile server (its VM exits at the end of its input).

# `errors`

```elixir
@spec errors(result()) :: [diagnostic()]
```

The error diagnostics of a result.

# `open`

```elixir
@spec open(Path.t(), keyword()) :: server()
```

Starts a compile server for `sandbox` (created if missing): an `elixir` subprocess that
compiles it on each `run/2` given `server: server`, until `close/1`. Options as
`run/2`'s `:deps_path` and `:preload`. A server that cannot start is returned stopped,
so every compile runs in a subprocess of its own.

# `position`

```elixir
@spec position(term()) :: {pos_integer(), pos_integer()} | pos_integer() | nil
```

A diagnostic position as `{line, column}`, a line, or `nil`.

# `run`

```elixir
@spec run(Path.t(), keyword()) :: {:ok, result()} | {:error, Juggernaut.Error.t()}
```

Compiles `sandbox`. Options: `:deps_path` (default
`Juggernaut.Sandbox.deps_dir(sandbox)`), `:preload` (beam paths without `.beam` loaded
into the compiling VM unless their module already loads; default
`Juggernaut.Peer.Switch`'s), `:server` (a compile server from `open/2`, which then
compiles, with the options it was opened with) and `:cmd` (a `System.cmd/3`-shaped
function, for tests). Unknown options raise.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
