jakesgordon

jakesgordon

Recompiling mix.exs when external_resource changes

I would like to load my app version from an external VERSION file (so that other CI/CD tools have easy access to the value). I’ve updated my mix.exs to load the value at compile time (see below), which works great, however mix.exs doesn’t get recompiled when I modify my external VERSION file. I’ve added it as an @external_resource but maybe that module attribute has no effect in .exs files (I’m not entirely sure if a .exs script is considered a module) ?

@external_resource version_file = "VERSION"
@version File.read!(version_file) |> String.trim

def project do
  [
    version: @version,
    # ...
  ]
end

I can workaround it by also doing a touch mix.exs whenever I bump the version number, but was looking to see if there’s anyway to get the @external_resource attribute to work inside mix.exs ?

Thanks!

Most Liked

axelson

axelson

Scenic Core Team

Hmmm, I’m pretty sure that mix.exs is “compiled” every single time you run a mix command. But what doesn’t happen is that the rest of the project is not recompiled when you change your VERSION file. Judging from the docs on @external_resource:

Specifies an external resource for the current module.

Sometimes a module embeds information from an external file. This attribute allows the module to annotate which external resources have been used.

Tools like Mix may use this information to ensure the module is recompiled in case any of the external resources change.

I think that what is happening is that since mix.exs is an .exs file it always needs to be recompiled so adding @external_resource does not change which files need to be recompiled. And the behavior that you see after a touch mix.exs is (I think) special behavior specifically for mix.exs and configuration files. I could be wrong on some of these details, and if so I would be glad for someone to correct me.

Are there other specific files that you want to be recompiled when your VERSION file changes? One potential solution is to add the @extrernal_resource to those files directly. Or if you have some other goal then it would be helpful to know exactly what you’re trying to do. If it’s an issue in CI/CD environment maybe it’s best to not cache parts of the build. But it’s a little hard to say without more details.

It is considered a module if there is a defmodule contained within. But there are a few different behaviors of .exs file vs .ex files, for example the .beam files for .exs files are not persisted to disk (although they are loaded in memory when the beam is running).

kokolegorille

kokolegorille

Hello and welcome,

exs files are for scripting, and are not compiled.

jakesgordon

jakesgordon

Update: I just noticed that if I look at the value of Mix.Project.config[:version] (in IEx) then that value does reflect the latest correct value from the VERSION file. So assuming that iex -S mix runs the mix.exs file each time that would make sense…

So I guess that just leaves me a bit confused about the difference between Mix.Project.config[:version] and Application.spec(:my_app, :vsn). The former is updated whenever I touch the VERSION file but the latter is only updated if (something) gets recompiled.

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement