How did mix new MyApp --sup get changed in Mix 1.4?

mix new MyApp --sup creates a new app including a supervision tree. (docs) I’ve been going through the official guide (link) and discovered that its behavior has changed in 1.4. I’m trying to understand what’s going on and update the guide accordingly.

My main question is this: how can I edit the MyApp.Application that seems to get automatically created by Mix when I can’t find the source file for it in the application?

I’d appreciate it if someone could explain to me what has changed or point me to discussions or documentation about this change.


Here’s details about the change.

Pre 1.4, mix new MyApp --sup automatically included use Application and default start/2 implementation in the main module file.

lib/my_app.ex

defmodule MyApp do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # worker(MyApp.Worker, [arg1, arg2, arg3])
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

And it was loaded in mix.exs like this:

def application do
  [applications: [:logger],
   mod: {MyApp, []}]
end

But in Elixir 1.4, it seems to create a separate MyApp.Application module for it, while keeping the lib/my_app.ex identical to the one created without the --sup option.

lib/my_app.ex

defmodule MyApp do
  @moduledoc """
  Documentation for MyApp.
  """
      
   @doc """
   Hello world.
     
  ## Examples

      iex> MyApp.hello
      :world
  
  """
  def hello do
    :world
  end
end

mix.exs

def application do
  [extra_applications: [:logger],
  mod: {MyApp.Application, []}]
end

The problem is, I can’t find the file for MyApp.Application. I could locate Elixir.MyApp.Application.beam in the _build folder, but not its source file. If I wanted to change the behaviour of MyApp.Application.start/2, how can I do it?

2 Likes

The application is (predictably) specified in “lib/my_application/application.ex”. It’s a nice change and it follows the convention completely.

2 Likes

Ah, of course! I can’t believe I didn’t think of going in there. Thank you @gon782!

I’m just learning elixir and I’m trying to understand why they are separated now?

It would be very helpful if someone could explain the concerns of each file now that they are separated.

For example I don’t really know what to do with MyApp if my client and server call backs are going into MyApp.Application.

Or am I even using MyApp.Application correctly now?

1 Like

This is also what I’ve been thinking about a lot lately.

For now I’m using that top module as the place to define functions that serve as the public interface of that Elixir application. Those public functions call smaller functions defined in modules in /lib folder.

In terms of Phoenix application, that public interface would be equivalent to router.ex.

This structure makes sense in my head, but I have no idea whether this approach is appropriate or maintainable. I’m also wondering how other people are using that top module.

2 Likes

Check out the conversation on Github Issues, particulary this comment by @fishcakez https://github.com/elixir-lang/elixir/pull/5275#issuecomment-251385810

This explains the reasoning behind the change.

In short: MyApp can be a place now where you can put some public API, also comment-docs, while MyApp.Application is private code, where your supervision trees/app gets initialized.

3 Likes