mix compile.elixir (Mix v1.11.2) View Source

Compiles Elixir source files.

Elixir is smart enough to recompile only files that have changed and their dependencies. This means if lib/a.ex is invoking a function defined over lib/b.ex, whenever lib/b.ex changes, lib/a.ex is also recompiled.

Note it is important to recompile a file's dependencies as there are often compile time dependencies between them.

__mix_recompile__?/0

A module may export a __mix_recompile__?/0 function that can cause the module to be recompiled using custom rules. For example, @external_resource already adds a compile-time dependency on an external file, however to depend on a dynamic list of files we can do:

defmodule MyModule do
  paths = Path.wildcard("*.txt")
  paths_hash = :erlang.md5(paths)

  for path <- paths do
    @external_resource path
  end

  def __mix_recompile__?() do
    Path.wildcard("*.txt") |> :erlang.md5() != unquote(paths_hash)
  end
end

Compiler calls __mix_recompile__?/0 for every module being compiled (or previously compiled) and thus it is very important to do there as little work as possible to not slow down the compilation.

If module has @compile {:autoload, false}, __mix_recompile__?/0 will not be used.

Command line options

  • --verbose - prints each file being compiled
  • --force - forces compilation regardless of modification times
  • --docs (--no-docs) - attaches (or not) documentation to compiled modules
  • --debug-info (--no-debug-info) - attaches (or not) debug info to compiled modules
  • --ignore-module-conflict - does not emit warnings if a module was previously defined
  • --warnings-as-errors - treats warnings in the current project as errors and return a non-zero exit code
  • --long-compilation-threshold N - sets the "long compilation" threshold (in seconds) to N (see the docs for Kernel.ParallelCompiler.compile/2)
  • --profile - if set to time, outputs timing information of compilation steps
  • --all-warnings - prints warnings even from files that do not need to be recompiled
  • --tracer - adds a compiler tracer in addition to any specified in the mix.exs file

Configuration

  • :elixirc_paths - directories to find source files. Defaults to ["lib"].

  • :elixirc_options - compilation options that apply to Elixir's compiler. See Code.put_compiler_option/2 for a complete list of options. These options are often overridable from the command line using the switches above.

  • [xref: [exclude: ...]] - a list of module or {module, function, arity} that should not be warned on in case on undefined modules or undefined application warnings.