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

First 10 of 13 Posts Switch mode

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

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.

mazz-seven

mazz-seven OP

Forgot to write my use case :grimacing:.
I’m trying to read some Markdown files from the user project for example, read this file /lib/pages/index.md and compile it. But I don’t want the user to write some module to use a library module. (Don’t know if it’s make sense)

@NobbZ root dir for me is where the mix file is.

mat-hek

mat-hek

Membrane Core Team

Compiling non-elixir stuff can be done with custom compilers. To my knowledge, you can safely use File.cwd from there.

NobbZ

NobbZ

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

mazz-seven

mazz-seven OP

Thanks @mat-hek I will take a look at it.

@NobbZ My solution right now is to read a config key with the value Path.expand(“./”) to get the absolute path.

Is that ok?

NobbZ

NobbZ

That seams to be okay, though I wouldn’t expand, but just rely on the fact that its correctly specified as a relative or absolute path.

Also I do hope, that you do not persist the users files into your modules, this will cause a lot of headache and not changing behaviour on change of the source files…

mazz-seven

mazz-seven OP

If it’s note the absolute path it will point to the relative path of the library.

What do you mean by persist the file into my module?

NobbZ

NobbZ

You said you compile the found file. To what? Do you generate new modules from it? Files? Something else?

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement