Error deploying to Fly.io re: file path "could not read file ...no such file or directory"

I’m deploying to flyio using the flyctl launch tool + my app runs locally.
I have a function that reads a file.

 def read_json(json_file) do
    json_file
    |> File.read!()
    |> Jason.decode!()
  end

It’s called like this (attempt to true relative path):
@json myApp.read_json("./alert_text.json")
or allow elixir to find it as usual:
@json myApp.read_json("alert_text.json")

The alert_text.json file is a root now, but I also tried moving it into the lib folder. No matter what, fly will not find it.

When I run the fly command I get ** (File.Error) could not read file "./alert_text.json": no such file or directory. I have no idea how to make fly find my file.

Is there some kind of function I can use to make it use absolute paths? I’d need to be environment agnostic to work on fly’s system and my local system.

Edit: just looked up Path module. Playing around with absname at the moment.

Is that file being copied to your container as part of your Docker build?

1 Like

The /priv dir is where you should store non-source files (if not in TheCloud™) and can be accessed like so:

file =
  Path.join([:code.priv_dir(:my_app), "path", "to", "file"])
  |> File.read!()

You must use :code.priv_dir/1 because the location changes once built.

1 Like

Both good paths for me to follow. Will investigate :grinning:

Storing non-ex files anywhere else isn’t going to work. lib gets compiled into a flat list of BEAM files and stuff like ./assets in your projects root just gets copied into the the build’s priv dir.

You can see what you get if you peek inside _build/dev/lib/my_app/.

Also consider using the @external_resource attribute to ensure that the module that’s reading that file into @json is recompiled when the JSON file changes.

1 Like

So this runs locally but I went to deploy to fly.io and it looks like the free plan is no longer there. I swear it was there when I posted this, but maybe all my errors covered up this fact. Bummer. I’m not paying $5 for this. I never fully recovered from the loss of the Heroku free tier.

Any tips for where to host Phoenix (hobby) app for free in perpetuity? Looking at render but I don’t want to bother with releases.

I do not, no. Gigalixir, maybe? But as something who recently spent a good 5 years avoiding any kind of ops, I can tell you mix releases are hardly a bother. I’ve never used Render, though. This is a good video on mix releases, though.

I’ve been grumpy and overwhelmed myself but you’re doing yourself a disservice by avoiding releases. There’s an initial investment but it’s not big.