`:code.priv_dir(:my_app)` points to `priv` directory in `_build`

We are just in sync, I had the same thoughts.

But it’s not the same priv directory… the one You mention is for release. While the other is being used when starting the project with mix.

If You run this as a release, You can use

:code.priv_dir(:my_app)

# or 

Application.app_dir(:my_app)

# They are not really equivalent! but You can choose between both.

When it is used with mix, You should use priv from application root folder, because it is served by plug static.

I configured a path for dev and test, and when it is not configured… for example in prod, I use this

# in dev and test
config :waffle, storage_dir: "priv/static/uploads"

# in the uploader
  def storage_dir(version, {_file, scope}) do
    Path.join uploads_root(), "/painting/display_images/#{version}/#{scope.id}"
  end

  defp uploads_root do
    Application.get_env(:waffle, :storage_dir) || Application.app_dir(:calabrese, "priv/static/uploads")
  end

This allows me to have a different config between dev (mix) and prod (release).

It’s not the only way, You can also test if Mix is available with.

function_exported? Mix, :__info__, 1

# true with mix
# false in release

I found it was more elegant to define a static path for dev and test only than checking if Mix is available.

2 Likes