mazz-seven
How to get root project directory from a library
Hey,
I’m writing a library, in my library I want to get the root project(the actual project using my library) directory path.
I tried, File.cwd!() but I’m getting the root dir of the library.
Few things to know.
I’m calling File.cwd!() outside a function.
I’m using Mix dependency path to bring my library into the project (for testing purposes).
One more thing.
Using File.cwd!() inside a macro will get me the project path. but I don’t want to use a macro 
Thanks
Mazz
Most Liked
NobbZ
What is “root project dir”? The source folder? It might not exist in a later phase of the applications life cycle…
Also the cwd might be anything. Totally depends on from which folder mix was called when compiling or from where release was actually started…
Under some very rare circumstances you do not even have a CWD.
Please be aware of those culprits, and try to avoid the usage in a compile time environment… Especially as those paths might not exist anymore at runtime…
hauleth
From what you have written I assume that you want to distribute some file with your library/application and then read it in the user runtime. This is why there is priv/ directory and :code.priv_dir/1.
NobbZ
Then you can just ignore my words of warning.
Still I hope you do it in a way that ensures recompilation on change of the source files.
To be honest, implementing a “compiler” that can be added to the key with the same name in the project description is probably your best option.
lucaong
That implies that your call File.cwd!() is executed at compile time. That explains why you see the dependency directory.
In other words:
defmodule MyLibrary do
@cwd File.cwd! # this is executed at compile time
def foo do
IO.puts("cwd at compile time: #{@cwd}")
IO.puts("cwd at runtime: #{File.cwd!}")
end
end
NobbZ
Don’t read randomly from your users dir. Ask them to explicitely specify where to serach.







