How to compile and run files outside `lib` of a mix project?

Hello.

I have a mix project created by mix new blah. I want to have an examples dir that is outside the lib dir, that way files/modules in there aren’t compiled and made available to people using the library/application.

If I do that, how can I run files in there?

For example, the structure of my examples dir is like this:

examples/module1.ex
examples/module2.ex
examples/run.exs

How can I run mix run examples/run.exs and have it compile examples/module1.ex and examples/module2.ex as well as make all modules defined in lib available?

Thanks for the help!

2 Likes

This is not possible to achieve via command-line options. You could add examples/ to the :elixirc_paths option under a particular Mix environment, say examples. Then you would be able to run MIX_ENV=examples mix run <whatever> and have Mix compile the source files in examples/.

2 Likes

Another approach is to use custom mix tasks.
This is a bit advanced and may not make sense in your case, but if you want, for example, more source code directories and have to add/change different environment variables before compiling it may be worth the effort.

1 Like