4
votes

I have created a mix project and for learning purposes, am putting all my three files in the lib folder: Client1.ex, Client2.ex and MyLibrary.ex. I have identical code in Client1.ex and Client2.ex

defmodule Project.Client1
#nothing
end
IO.puts(inspect(Project.MyLibrary.myFunc()))

When I run this in emacs using alchemist-compile-this-buffer,the above code works for Client1 but not for Client2. I get the error

elixirc /Users/../lib/Client2.ex
lib/Client2.ex:1: warning: redefining module Project.Client2


== Compilation error on file lib/Client2.ex ==
** (UndefinedFunctionError) undefined function: Project.MyLibrary.myFunc/0 (module Project.MyLibrary is not available)
    Project.MyLibrary.myFunc()
    lib/Client2.ex:10: (file)
    (elixir) lib/kernel/parallel_compiler.ex:95: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8

Things I have observed:

  • Running mix compile works for Client2

  • Client2.beam is not created in the lib folder like the others BUT is in the project root folder which contains the lib folder.

2

2 Answers

3
votes

Thanks for using Alchemist, let me try to help you with the issues you encountered.

The alchemist-compile-this-buffer function is part of alchemist-compile module. The alchemist-compile module runs all compilations with elixirc and that always in the default-directory where your current-buffer-file is. alchemist-compile brings functionality to compile on-the-fly single files outside of mix based projects. So, BEAM files will always lie there where the default-directory and the current-buffer-file is.

So, in your case and when you create new Elixir project with mix you always need to use the alchemist-mix module. alchemist-mix-compile which runs mix compile will run the compilation in context of the Elixir project and will put BEAM files where they belongs under the _builds directory in your Elixir project root directory.

I hope that explanation helps you.

If you have more questions, please just call me on the #elixir-lang IRC channel on Freenode. You can just drop message, I'm always connected. ;-)

My username is: tonini

Cheers

Samuel

-1
votes

For some reason, Client2.beam was being created by alchemist-compile-this-buffer in the project root folder. Since I was hacking around, I can't remember the exact steps but

  • I deleted Client2.beam,
  • renamed defmodule Client2 to something like defmodule Client2_tmp
  • ran alchemist-compile-this-buffer again. It worked. Beam file created in lib/
  • renamed module back to Client2 and ran alchemist-compile-this-buffer again.

Yes, I feel like a pigeon in a B.F Skinner experiment. I expect to come back with another pigeon dance.