trisolaran

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 ... end clause
  • Split tasks into directories: (i.e. task/test, task/dev etc.) and play around with the elixirc_paths option 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! :slight_smile:

Showing Posts 1 to 10

dimitarvp

dimitarvp

You can always wrap the entire module code? :man_shrugging:t3:

if Mix.env() == :dev do

defmodule XYZ do
  # ...
end

end
LostKobrakai

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

trisolaran OP

@LostKobrakai thanks! The problem is getting rid of the compile-time warnings caused by the non-existent dependencies.

trisolaran

trisolaran OP

Yep, that’s probably the best solution so far. I find it ugly, but I guess I’ll go for it :slight_smile: 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

trisolaran OP

That would be an elegant solution: having different task folders.

  • “lib/mix/tasks”
  • “test/mix/tasks”

That way the test tasks would only be compiled in the test environment

dimitarvp

dimitarvp

A cleaner solution could be to use the elixirc_paths option from mix.exs:

elixirc_paths: elixirc_paths(Mix.env())

And then further below:

defp elixirc_paths(:staging), do: ["lib", "test/support", "test/integration"]
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

That way you can encapsulate functionality in directories and wall it.

trisolaran

trisolaran OP

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

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() == ... :slight_smile:

trisolaran

trisolaran OP

yes good old if is always the solution to most of our problems! :laughing:

josevalim

josevalim

Creator of Elixir

This should work. Elixir generally does not care which files define a module.

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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews