vans163

vans163

Recompile project is compiletime ENVVAR changed?

Does anyone know how to force certain-files/project to recompile if compiletime envvars changed?

Example I have a config.exs like so

region = System.get_env("REGION", "us") |> String.to_atom()
config :app, :region, region

and code that uses it

defmodule MyModule do
    if Application.compile_env!(:app, :region) == :us do
      def lol() do "United" end
    else
      def lol() do "United in another language" end
    end
end

I would like MyModule to recompile (outside a release setting) if REGION envvar changed.

Example doing

REGION=us iex -S mix
compile
REGION=us iex -S mix
nothing
REGION=ca iex -S mix
compile
REGION=us iex -S mix
compile

I can change compile_env to get_env, then the app just runs without recompiling anything. I rather not crash, and not false negative.

First Post!

Ljzn

Ljzn

The simple solution is write a compiler task to edit the code file each time to force recompile.

defmodule Mix.Tasks.Compile.ForceRc do
  use Mix.Task

  def run(_args) do
    File.write("[path of your module]", " ", [:append])
    IO.puts "edited"
  end
end

run mix compile to load this module. Then add it before other compilers in mix.exs

compilers: [:force_rc, :phoenix, :gettext] ++ Mix.compilers(),

Then every time you run iex -S mix, the file you specified will be re-compile.

It is such a stupid solution :sweat_smile: Could someone tell me how to force elixir recompile a file, without changing the file content.

Most Liked

evadne

evadne

Note Elixir source, Mix.Tasks.Compile.App:

  defp app_changed?(properties, mods, compile_env) do
    Keyword.get(properties, :modules, []) != mods or
      Keyword.get(properties, :compile_env, []) != compile_env
  end

You can probably use this in conjunction with runtime configuration like in config/runtime.exs introduced as in Elixir 1.11, see Elixir v1.11 released - The Elixir programming language so stuff System.get_env(name) in config/runtime.exs and use Application.compile_env/3 to get it.

However IMO this is a smell and is best avoided

Last Post!

jsonmaur

jsonmaur

For anyone still having issues with this, you can use __mix_recompile__?/0.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement