Cannot use function in custom mix task defined in same app

I have an app with the following structure:


The file lib/my_app.ex defines a module MyApp with a function load. The mix/tasks/my_mix_task.ex includes

defmodule Mix.Tasks.MyApp.MyTask do 
  use Mix.Task
 def run(_) do"app.start")
    # IO stuff

When I run mix my_app.my_task the call to MyApp.load() gives

UndefinedFunctionError … (module MyApp is not available)

unless I run it from the root directory of my_app, in which case I get

(UndefinedFunctionError) function Hex.Mix.overridden_deps/1 is undefined (module Hex.Mix is not available)

I’ve run mix and mix archive.install to make it available globally.

Any ideas?

What are your compilation paths? (In mix.exs)

It’s not specified in mix.exs. I believe it defaults to _build. FWIW, in my_app/_build/dev/lib/my_app/ebin/ I see:

{application, my_app, [
  {optional_applications, []}, 
  {applications, [kernel,stdllib,elixir,logger,my_dependency]},
  {description, "my_app"},
  {modules, ['Elixir.MyApp', 'Elixir.Mix.Tasks.MyApp.MyTask']}
  {registered, []},
  {vsn, "0.1.0"}

(some formatting changed to make it more readable)
– BTW updated OP with some additional error info when run from same directory it is defined in.

mix archive lists the my_app archive in the same path as things like credo, hex, and phx_new. And mix local lists the my_app.my_task along with the tasks defined by those other archives.

Ah no, I meant the elixirc_paths in your mix.exsproject function.

elixirc_paths is not defined.

I would guess the MyApp in the error above is referring to the module Mix.Tasks.MyApp, you need to define that first. Elixir would print out the full module path, so this is wrong.

To also prevent confusion, I would recommend doing an abbreviation of some sort–this also adds the convenience of making it easier to type in the terminal, e.g. mix vs mix

edit: I tested this locally and it seems to work fine, so the above seems to be wrong. I just suspected some sort of module shadowing was taking place

Is Elixier a copy-paste mistake?

Oops, yeah

I’m not sure how to explain it but the error is still an undefined function error … module not available, but now it’s not the MyApp module but the dependency that MyApp.load uses. I just don’t understand how that dependency is not compiled and included when building the archive.

Have you tried putting this inside the project function inside mix.exs?

elixirc_paths: ["lib"],

no dice.

No other ideas. You should try to make a small project reproducing the issue and put it on GitHub so we can try our hand at it.

I’m an idiot who should RTFM:

For instance, archives do not include dependencies, as those would conflict with any dependency in a Mix project after the archive is installed.