Reading files from non-scripts (.ex) should disallow relative files

Hi,

I was stumbling across a pesky issue, that at first sight was hard to spot, because dev/test did not catch it, only prod would.

Is is about simple File.read/1 and File.read!/1 that allows reading files relative the the project (or executing, idk) directory in dev/test whereas in prod (release) it obviously won’t work as not everything within the project directory is included in the release.

Would there be some kind of way to detect this, without the implicit knowledge of having to put everything into priv/?

priv/ is mean to be the place to store files related to an application. There’s even :code.priv_dir(:app) to get the path to that folder. If you want things to work without hassle simply put files there.

I am getting that. But what weirds me out, is that beginners might run in this problem, since relative reading works in both dev and test environments, but not within a release.

So there is no automatic check, except a crash in the release or someone going through code pedantically, checking that people who are new to the codebase/Elixir made no mistake in handling files.

It’s not a matter of which mix env you’re testing with, but a matter of running mix vs. running releases. There’s also the fact that only the priv folder will be moved into a release by default. So if you have some static assets in any other folder of your mix project it’s not going to work without some extra config as well.

I’m certainly with you that distillery could add some documentation for that, but not that there would be a need for any automatic checks.

No they don’t, they read relative to the cwd, just like in every other language that I can think of?

2 Likes