File.cwd! Alternative on Heroku

Hey all,

I’ve been using File.cwd! in an elixir project happily in order to load a file relative to the project’s root. I’ve added this project as a dependency to another app, and it was working fine on digital ocean, but broke on Heroku.

I’ve tried my best to debug, but I can’t find the proper alternative.

The code is simply File.cwd! joined with path components, and I run this code in the init of a GenServer worker.

On Heroku, I noticed that using @symbol returns a path like so:

But, by the time the init is invoked, and I run File.cwd! directly, I get the root project folder! Notice, dama_logic is the subproject name, and I need to load files relative to it.

Also please note, the directory returned from the symbol no longer exists after the project is built.

The only solution I can think of is using File.cwd as a symbol, and then replacing the top level path with the new path relative the the project instead of the temp directory.

At least this way, I can “make use” of the deps path.

In general the only reliable way to accessing resources at runtime is putting them in the priv directory and accessing with Application.app_path(app, "priv/path/to/file"). This is a convention understood by all tools, which makes it “just work”.


Small nitpick:
The correct function seems to be Application.app_dir.

Also, I never knew Elixir had such magic directories. I just created priv, added a dummy file there, and all of a sudden, it was copied along side the built app. Very useful, but also implicit.

1 Like

It is part of the old erlang eco-system, not really implicit, it is documented all over in Erlang and all calls to it do tend to have priv in the name somewhere. :slight_smile:

1 Like