fireproofsocks

fireproofsocks

How can you run a .exs file from another .exs file? My inspiration comes from the basic config.exs file which runs another .exs via import_config "#{Mix.env()}.exs". I’d like to do the same type of thing, but not with just config files.

Would something like Mix.Tasks.Run.run(["path/to/my.exs"]) work in an umbrella? How does Mix know which directory is the base directory?

Thanks for any pointers!

Showing Posts 1 to 9

josevalim

josevalim

Creator of Elixir
easco

easco

According to the source, import_config calls __import__ which eventually leads to a call to Code.eval_file. elixir/lib/mix/lib/mix/config.ex at a444533db118aebb7bbeadff7b9b9985059309f8 · elixir-lang/elixir · GitHub

fireproofsocks

fireproofsocks OP

This works in a single app:
Code.require_file("priv/repo/#{Mix.env()}.exs")

But when that is inside an umbrella, it doesn’t work, presumably because the path would need to be something like Code.require_file("apps/app_one/priv/repo/#{Mix.env()}.exs")

josevalim

josevalim

Creator of Elixir

Use Application.app_dir to build the path within a certain app.

joaoevangelista

joaoevangelista

You can use Application.app_dir/2 to fetch the file that lies inside the application folder like that:

 Application.app_dir(:myapp, ["priv/repo", "#{Mix.env()}.exs"])
 |> Code.require_file() 

The second argument will join the parts of the path and return a String

fireproofsocks

fireproofsocks OP

Application.app_dir seems to work fine from the root of the umbrella, but that seems to be where my code breaks when I try to run it during a Docker deployment.

The error I get is this:

** (exit) exited in: GenServer.call(Mix.State, {:get, {Map, :get, [:env, :dev]}}, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir) lib/gen_server.ex:979: GenServer.call/3
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (stdlib) erl_eval.erl:273: :erl_eval.expr/5
    (stdlib) eval_bits.erl:88: :eval_bits.eval_field/3
    (stdlib) eval_bits.erl:68: :eval_bits.expr_grp/4
    (stdlib) erl_eval.erl:484: :erl_eval.expr/5
    (stdlib) erl_eval.erl:232: :erl_eval.expr/5
    (stdlib) erl_eval.erl:233: :erl_eval.expr/5
ericmj

ericmj

Elixir Core Team

You can’t call mix functions at runtime because they are not available in your release. You need to evaluate Mix.env() at compile time, module attributes can help you do that:

@env Mix.env()

def foo do
  Application.app_dir(:myapp, "priv/repo/#{@env}.exs")
  |> Code.require_file() 
end
fireproofsocks

fireproofsocks OP

Brilliant, thank you! I had guessed that may have been the problem and I tried using System.get_env("MIX_ENV") – that worked too. Not every docker image involved here has Mix in it..

c4710n

c4710n

Code.eval_file/2 works, too.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews