Unable to compile default Elixir project

New to Elixir, I’m trying to compile the default project in the getting started guide and get this error:

mix compile
Compiling 1 file (.ex)

== Compilation error in file lib/kv.ex ==
** (ArgumentError) could not call Module.put_attribute/3 because the module KV.MixProject is already compiled
    (elixir 1.15.4) lib/module.ex:2310: Module.assert_not_readonly!/2
    (elixir 1.15.4) lib/module.ex:2007: Module.__put_attribute__/5
    lib/kv.ex:2: (module)

I encountered an OpenSSL problem I wasn’t able to workaround while installing erlang via asdf on my M1 mac. Eventually had to install erlang through homebrew which I gather is not recommended, so perhaps that may be part of the problem.

elixir 1.15.4-otp-26
erlang 26.0.2

Welcome to the Elixir community. To help to resolve this are you able to share:

  • What command did you use to initialise the project?
  • Can you show the contents of lib/kv.ex? The default on the page you linked to doesn’t have any occurrence of Module.put_attribute/3 so something is different.

What the message is telling you is that you are trying to update a module attribute in a module that is already compiled. Since module attributes are a compile-time only feature, they don’t exist after compilation and therefore can’t be updated.

Hi kip, thanks for the welcome. I’m running the command mix new kv --module KV to initialise the project same as in the guide. lib/kv.ex is the same as the guide as well and I haven’t changed anything else, I just tried again on a new default project and got the same error.

defmodule KV do
  @moduledoc """
  Documentation for `KV`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> KV.hello()
      :world

  """
  def hello do
    :world
  end
end

I believe the call to put_attribute is coming from the kernel. Afraid I’ve no idea why it would think I’m trying to update a module attribute after compiling. Removing the moduledoc and doc attributes gives me the same error.

Downgrading to 1.14.5-otp-26 gives me a different error that makes me think the problem is with the Erlang brew installation. I’ll have another go at getting it installed via asdf.

kv mix compile
Compiling 1 file (.ex)
** (MatchError) no match of right hand side value: {:error, {{:EXIT, {:function_clause, [{:disk_log_sup, :start_link, [], [file: 'disk_log_sup.erl', line: 26]}, {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 420]}, {:supervisor, :do_start_child, 2, [file: 'supervisor.erl', line: 406]}, {:supervisor, :handle_start_child, 2, [file: 'supervisor.erl', line: 712]}, {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 461]}, {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 1113]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 1142]}, {:proc_lib, :wake_up, 3, [file: 'proc_lib.erl', line: 251]}]}}, {:child, :undefined, :disk_log_sup, {:disk_log_sup, :start_link, []}, :permanent, false, 1000, :supervisor, [:disk_log_sup]}}}
    (kernel 9.0.2) disk_log_server.erl:142: :disk_log_server.ensure_started/0
    (kernel 9.0.2) disk_log_server.erl:52: :disk_log_server.open/1
    (stdlib 5.0.2) ets.erl:825: :ets.tab2file/3
    (mix 1.14.5) lib/mix/compilers/application_tracer.ex:250: Mix.Compilers.ApplicationTracer.write_manifest/1
    (mix 1.14.5) lib/mix/compilers/application_tracer.ex:41: Mix.Compilers.ApplicationTracer.prepare/2
    (mix 1.14.5) lib/mix/compilers/elixir.ex:150: Mix.Compilers.Elixir.compile/7
    (mix 1.14.5) lib/mix/state.ex:15: Mix.State.lock/2
    (mix 1.14.5) lib/mix/task.ex:421: anonymous fn/3 in Mix.Task.run_task/4

Definitely use asdf or rtx and just avoid system-installed Erlang / Elixir.

I don’t know what’s causing this, but that message has come up a couple times recently:

It’s for completely different modules, so it’s likely a symptom and not a cause - but maybe more examples will help somebody track it down.

1 Like

I’m sadly having no luck installing Erlang either via asdf or rtx. The error I get in both is:

checking for OpenSSL in /usr/local/Cellar/openssl@3/3.1.1_1... configure: error: neither static nor dynamic crypto library found in /usr/local/Cellar/openssl@3/3.1.1_1
...local/share/rtx/plugins/erlang/kerl-home/builds/asdf_26.0.2/otp_src_26.0.2/lib/crypto/configure failed!

neither static nor dynamic crypto library found is the error regardless of whether I attempt to use OpenSSL 3 or 1.1.

export KERL_CONFIGURE_OPTIONS="--without-javac --with-ssl=/usr/local/Cellar/openssl@3/3.1.1_1"
export LDFLAGS="-L/usr/local/Cellar/openssl@3/lib"
export CPPFLAGS="-I/usr/local/Cellar/openssl@3/include"

It has been a while since I last tinkered but here are the env vars that I have that are not directly related to my working environment but also helpful for having offline docs, shell history, homebrew-installed tools etc.:

# Offline Erlang docs.
export KERL_BUILD_DOCS=yes
export KERL_INSTALL_MANPAGES=yes
export KERL_INSTALL_HTMLDOCS=yes

# Enable Erlang / Elixir REPL history.
export ERL_AFLAGS="-kernel shell_history enabled"

# Stuff installed through `homebrew`
export PATH="/usr/local/sbin:$PATH"
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export PATH="/usr/local/opt/curl/bin:$PATH"

# I used this to give Emacs just-in-time compilation powers but who knows.
export LIBRARY_PATH=/usr/local/opt/gcc@12/lib/gcc/12:/usr/local/opt/libgccjit/lib/gcc/current:/usr/local/opt/gcc@12/lib/gcc/12/gcc/x86_64-apple-darwin21/12

I have both openssl@1.1 and openssl@3 installed but as you can see I have only given the former a PATH entry (seems to be related to your problem, you are using v3 and I am using v1.1).

After uncountable attempts I found that reinstalling the asdf erlang plugin allowed me to then install erlang via asdf without the crypto library error, and now mix compile is building the default project as intended. :tada:

I did install the erlang plugin slightly differently with asdf plugin add erlang instead of asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git, don’t see why that would make a difference but I thought I’d note it.

Along the way I also reinstalled homebrew but I don’t know whether that made a difference, it didn’t help until after I reinstalled the erlang asdf plugin at least. In the end I only had openssl@3 installed and no flags or options were set in .zshrc.

Thanks everybody for your help!

2 Likes

I also encountered this problem, and later I realized that the data on my Mac was migrated from the Intel version to the Apple version, so underlying libraries like OpenSSL were adapted for x86, so when I compiled Erlang, I saw some Intel-type processes in the monitor. Not only Erlang, but also Python code had some strange phenomena. After uninstalling and reinstalling Homebrew, I reinstalled all language environments such as Python and Erlang, and the problem was solved. I hope this experience can be helpful to you.

2 Likes

I had the same problem trying to run elixir for the first time (very frustrating). I’m using an m1 chip on macOS Sanoma. As suggested here before, it was most probably an architecture problem because after removing elixir again:

brew uninstall elixir
and reinstalling it forcing the architecture to be arm64:
arch -arm64 brew install elixir it worked for me!

I noticed that brew was using rosetta 2 since brew config gave back Rosetta 2: true. Simple fix and nice if it helps some other people aswell :slight_smile:.

1 Like