mazz-seven

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 :slight_smile:

Thanks
Mazz

Most Liked

NobbZ

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

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

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

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

NobbZ

Don’t read randomly from your users dir. Ask them to explicitely specify where to serach.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement