asdf-elixir: mix format fails when running from Emacs

Background

macOS 10.14.4 erlang 21.3.4 elixir 1.8.1

I’m using spacemacs with the elixir package installed.

Expected behaviour

Running elixir-format emacs command from emacs-elixir will format the current file and on save hook will also format the file.

Actual behaviour

Running elixir-format hook in emacs-elixir results in the following error message being displayed:

** (SyntaxError) /Users/josiah/.asdf/shims/mix:3: syntax error before: '.'
    (elixir) lib/code.ex:767: Code.require_file/2

The on save hook fails silently.

Other Information

This problem only seems to crop up when elixir is installed with asdf, when I had elixir installed with brew I had no problems.

Any help pointing me in the right direction is much appreciated 😃

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (13 by maintainers)

Commits related to this issue

Most upvoted comments

The issue is that elixir-format runs elixir /path/to/mix format and not /path/to/mix format. https://github.com/elixir-editors/emacs-elixir/blob/master/elixir-format.el I’m not exactly sure why, but it might be worth asking in their repository. When using asdf, mix executable is a shell script, and therefore, it fails.

Fortunately, it seems that they get the mix path using a function called elixir-format--mix-executable, so while waiting for a more permanent fix, you can override this function. Try adding the following to your ~/.emacs (or ~/.spacemacs if you use Spacemacs I suppose)

(eval-after-load "elixir-mode"
  '(defun elixir-format--mix-executable ()
     (string-trim-right (shell-command-to-string "asdf which mix"))))

This runs after elixir-mode, which provides elixir-format, has been loaded, and overrides elixir-format--mix-executable to return the result of asdf which mix instead of trying to look for mix in the path.

I’m not really sure there is much we can do about it on our side, as this is how shims work, but I will leave this open in case somebody wants to propose a solution.

Now is working:

❯ asdf plugin-remove elixir

❯ asdf plugin-add elixir
updating plugin repository...
HEAD is now at 1040bc8 Merge pull request #388 from asdf-vm/asdf-poetry

❯ iex -S mix phx.server
Erlang/OTP 23 [erts-11.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe] [dtrace]

Compiling 2 files (.ex)
.....