trisolaran
Hello everyone,
I wrote a Mix.Task that’s supposed to be run in a specific environment (:test) and nowhere else.
The task uses dependencies that are present only in the test environment.
I can set preferred_cli_env to make sure that the task automatically runs in the test environment when I invoke it, so that’s good.
However, when I compile my app in another environment (e.g. :dev) mix will try to compile that task, resulting in errors or at the very least compiler’s warnings due to the missing dependencies.
Is there an elegant way to solve this problem? I can think of a few options:
- Make the test-only dependencies used by the task available in all environments
- Enclose the code in my task in a huge
if Mix.env() == :test do ... endclause - Split tasks into directories: (i.e.
task/test,task/devetc.) and play around with theelixirc_pathsoption to disable compilation in specific environments
The last option is the only one appealing to me, but I think elixirc_paths only accepts a list of directories, and since lib is already in there, the tasks would have be moved out of lib. I wish there was a way to selectively exclude a directory, or even better a single file, but I believe that’s not possible.
Any ideas? Thank you very much in advance! ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
You can always wrap the entire module code?
LostKobrakai
Option 4: Make the mix task not fail in other envs by just printing an error message instead of doing what it normally does.
trisolaran
@LostKobrakai thanks! The problem is getting rid of the compile-time warnings caused by the non-existent dependencies.
trisolaran
Yep, that’s probably the best solution so far. I find it ugly, but I guess I’ll go for it
thanks
I was wondering if there is a way to configure the directory where tasks should reside. It looks like it has to be “lib/mix/tasks”.
trisolaran
That would be an elegant solution: having different task folders.
That way the test tasks would only be compiled in the test environment
dimitarvp
A cleaner solution could be to use the
elixirc_pathsoption frommix.exs:And then further below:
That way you can encapsulate functionality in directories and wall it.
trisolaran
I mentioned this in my first message.
The problem is that you can’t easily exclude/include subfolders of lib (where the tasks are located). You either compile everything under lib, or nothing.
And moving tasks to different directories doesn’t appear to be supported
dimitarvp
Yeah, if you can encapsulate then you can use this but if you can’t, it’s best if you go for good old
if Mix.env() == ...trisolaran
yes good old if is always the solution to most of our problems!
josevalim
This should work. Elixir generally does not care which files define a module.