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
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
Could someone tell me how to force elixir recompile a file, without changing the file content.
Most Liked
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









