asdf-elixir icon indicating copy to clipboard operation
asdf-elixir copied to clipboard

Running `iex -S mix phx.server` fails

Open jarmo opened this issue 2 years ago • 35 comments

Running iex -S mix phx.server fails:

$ iex -S mix phx.server
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [hipe]

** (SyntaxError) /home/jarmo/.asdf/shims/mix:3:18: syntax error before: '.'
    (elixir 1.11.2) lib/code.ex:931: Code.require_file/2

Running mix phx.server seems to work without any issues.

Erlang and Elixir are system installations on Ubuntu 16.04.

jarmo avatar Dec 19 '21 19:12 jarmo

After installing Erlang 23.0 and Elixir 1.11.2 with asdf and using them instead of system installations then everything seems to work.

jarmo avatar Dec 19 '21 20:12 jarmo

@jarmo so this seems to be limited to when you tried to run iex -S mix phx.server with the system Erlang and Elixir versions?

Can you share the output of asdf info? Thanks!

Stratus3D avatar Dec 20 '21 14:12 Stratus3D

Yes, as soon as I don't specify local erlang/elixir system, but specify same versions of Erlang/Elixir installed by asdf then everything seems to work as expected.

$ asdf info
OS:
Linux caesar 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux

SHELL:
zsh 5.1.1 (x86_64-ubuntu-linux-gnu)

ASDF VERSION:
v0.8.1-a1ef92a

ASDF ENVIRONMENT VARIABLES:
ASDF_DIR=/home/jarmo/.asdf

ASDF INSTALLED PLUGINS:
elixir                       https://github.com/asdf-vm/asdf-elixir.git
erlang                       https://github.com/asdf-vm/asdf-erlang.git

Now, before Microsoft causes any confusion then the real "OS" is Ubuntu 16.04 inside WSL-1:

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.7 LTS"

jarmo avatar Dec 20 '21 16:12 jarmo

Did you have Erlang and Elixir installed on your system prior to asdf? And did they work when you ran simple things like just erl or iex?

Stratus3D avatar Dec 20 '21 20:12 Stratus3D

Yes, Erlang 23.0 and Elixir 1.11.2 were installed before and I used iex -S mix phx.server pretty often for development. Until yesterday asdf was not installed on my machine, but I needed newest versions of Erlang and Elixir to try out the newest version of Phonenix Framework, which made me to look into asdf, since I didn't want to cause any issues to my older project by upgrading Erlang and Elixir system-wide.

When I comment out asdf in my .zshrc then I can use iex -S mix phx.server for my older project without any issues as was the case before installing asdf.

Also, as mentioned when I install the same versions of Erlang and Elixir with asdf and set them as local versions for the same older project then everything works too so it's not that big of a problem that system installations will not work - I can even uninstall them since everything works with asdf too.

jarmo avatar Dec 20 '21 21:12 jarmo

Does this issue appear similar to yours? https://github.com/asdf-vm/asdf/issues/1130

Also, what happens if you run asdf reshim and then try the system version with asdf?

Stratus3D avatar Dec 20 '21 21:12 Stratus3D

asdf reshim does not seem to make any difference.

Some additional information:

$ cat .tool-versions
erlang system
elixir system

$ which erl
/home/jarmo/.asdf/shims/erl

$ which elixir
/home/jarmo/.asdf/shims/elixir

$ iex -S mix phx.server
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [hipe]

** (SyntaxError) /home/jarmo/.asdf/shims/mix:4:18: syntax error before: '.'
    (elixir 1.11.2) lib/code.ex:931: Code.require_file/2

$ asdf reshim

$ iex -S mix phx.server
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [hipe]

** (SyntaxError) /home/jarmo/.asdf/shims/mix:4:18: syntax error before: '.'
    (elixir 1.11.2) lib/code.ex:931: Code.require_file/2

$ cp -f .tool-versions.bck .tool-versions

$ cat .tool-versions
erlang 23.0
elixir 1.11.2

$ iex -S mix phx.server
Erlang/OTP 23 [erts-11.0] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [hipe]

[info] Running MyWeb.Endpoint with cowboy 2.9.0 at 0.0.0.0:4000 (https)
...

Also this:

$ asdf list
elixir
  1.11.2
  1.13.1-otp-24
erlang
  23.0
  24.2

jarmo avatar Dec 21 '21 18:12 jarmo

This looks like the same issue - https://github.com/asdf-vm/asdf-elixir/issues/62

Have you tried updating the plugin? asdf plugin-update elixir?

Stratus3D avatar Dec 23 '21 16:12 Stratus3D

No, I have not tried to update anything since I did install asdf and all of its plugins last week for the first time ever.

jarmo avatar Dec 25 '21 14:12 jarmo

Hmm... I'm out of ideas. What is the output of type -a iex and type -a mix? Do you happen to have any other version managers or anything else on your system that may interfere with asdf?

Stratus3D avatar Dec 29 '21 14:12 Stratus3D

$ type -a iex
iex is /home/jarmo/.asdf/shims/iex
iex is /usr/bin/iex
$ type -a mix
mix is /home/jarmo/.asdf/shims/mix
mix is /usr/bin/mix

About other version managers - I have only chruby installed for managing Rubies, but not anything else for Elixir/Erlang.

jarmo avatar Dec 29 '21 18:12 jarmo

That all looks correct. chruby shouldn't be a problem. The only thing I can think of is maybe something is wrong with your shims and asdf reshim isn't fixing it. You could try deleting all your shims (rm /home/jarmo/.asdf/shims/*) and then running asdf reshim to re-generate them. I'm not sure this would fix anything though. I'm out of ideas.

Stratus3D avatar Dec 30 '21 14:12 Stratus3D

Nope, didn't help. Here's my shim for mix:

$ cat ~/.asdf/shims/mix
#!/usr/bin/env bash
# asdf-plugin: elixir 1.11.2
# asdf-plugin: elixir 1.13.1-otp-24
exec /home/jarmo/.asdf/bin/asdf exec "mix" "$@"

I did read that WSL1 is not officially supported by asdf - can it be that this is the reason behind this problem although everything else seems to work as expected so far?

jarmo avatar Dec 30 '21 18:12 jarmo

No need to even execute phx.server:

$ iex -S mix
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [hipe]

** (SyntaxError) /home/jarmo/.asdf/shims/mix:4:18: syntax error before: '.'
    (elixir 1.11.2) lib/code.ex:931: Code.require_file/2

jarmo avatar Dec 30 '21 18:12 jarmo

Yeah I'm at a loss. I have no idea what the issue is. You might check this issue tracker for others who have encountered issues on WSL. I am not familiar with WSL and it isn't officially supported by asdf as far as I know.

Stratus3D avatar Jan 04 '22 23:01 Stratus3D

I'm having a similar problem on Mac OS, where system versions of erlang, elixir and hex were installed before I added asdf.

When I'm inside a Phoenix project and run iex -S mix phx.new I get the same error as @jarmo. However, using the slightly more convoluted asdf exec iex -S mix phx.server works fine. Smells like some kind of path issue, but right now I'm not smart enough to see how it can be solved to work as usual.

Edit:

It seems to be related to the line . $HOME/.asdf/asdf.sh in my shell-config (~/.zshrc) that I added during asdf installation, or rather, the referenced file asdf.sh . Outcommenting that line gives me access to the system version of elixir (1.13.1) and I can still use asdf exec iex -S mix phx.server to get the asdf version.

MathiasWedeken avatar Jan 23 '22 19:01 MathiasWedeken

For me using asdf exec as a command prefix still results in the same error as without.

jarmo avatar Jan 23 '22 20:01 jarmo

Somehow, between outcommenting the the line . $HOME/.asdf/asdf.sh, checking different shell scripts and undoing the uncomment, something changed for the better and now everything works without problems. So for me it was some path problem.

MathiasWedeken avatar Jan 23 '22 22:01 MathiasWedeken

@Stratus3D As far as I can tell what is happening is that asdf prepends to $PATH for asdf-elixir managed installations, but not for system installations (a.k.a., whatever it finds in $PATH) (What it prepends is $ASDF_PREFIX/plugins/elixir/shims:$ASDF_PREFIX/installs/elixir/$VERSION/bin:$ASDF_PREFIX/installs/elixir/$VERSION/.mix/escripts:)

iex -S $script assumes/requires $script to be the name of an elixir script in $PATH (or the path to one https://github.com/asdf-vm/asdf/issues/926#issuecomment-916917080).

This is why with asdf {global,local} elixir system, iex -S mix looks for mix in $PATH, sees the bash script $ASDF_PREFIX/shims/mix, tries to load it, fails.

Aside: $ASDF_PREFIX/plugins/elixir/shims doesn't contain anything executable so it doesn't seem to affect anything

AviKav avatar Mar 20 '22 03:03 AviKav

@MathiasWedeken good to know. I use asdf and elixir every day and don't encounter this issue. I suspect there could be an environmental element to this issue. Perhaps something else on the path, or some iex configuration affects this behavior. But in any case if you follow the instructions in the asdf docs you should have a working setup.

@AviKav asdf put its' shims on your $PATH, and those shims may point to asdf-managed version, or, if you've specified a system version, the version already installed on your system. Assuming iex is executing the script given with the -S this should all work. However I still don't understand why it works for me and not for others.

Stratus3D avatar Mar 24 '22 14:03 Stratus3D

@Stratus3D

asdf put its' shims on your $PATH

Not all the time. While asdf's rc scripts amends $PATH so it points to the shims first, the shims, when called, amend $PATH so that they aren't pointed to first. The problem is that they don't/can't do this for system installs

Assuming iex is executing the script given with the -S this should all work.

That would indeed be the case if iex was executing them via exec() family and not trying to read them

However I still don't understand why it works for me and not for others.

You mentioned half a year ago not having a system install to use with asdf. Is this still the case? https://github.com/asdf-vm/asdf/issues/926#issuecomment-917188213

AviKav avatar Mar 25 '22 02:03 AviKav

I think you may be able to make asdf think you have a system install available by adding an elixir install to '$PATH' before sourcing asdf

AviKav avatar Mar 25 '22 02:03 AviKav

@Stratus3D

But in any case if you follow the instructions in the asdf docs you should have a working setup.

I'm pretty sure that I followed asdf instructions in the doc and did the same when installing Elixir/Erlang long before installing asdf. I don't think that I have done anything to steer off the path of "official" way of installing/loading either of these.

I have created two Dockerfiles to reproduce this problem.

First is a Docker file, which uses only system installation to show that it works as expected:

# Dockerfile.system

FROM ubuntu

ARG DEBIAN_FRONTEND=noninteractive

RUN apt update -y
RUN apt upgrade -y
RUN apt install -y wget

# System installation of Erlang and Elixir according to https://elixir-lang.org/install.html
RUN wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && apt install -y --fix-broken ./erlang-solutions_2.0_all.deb
RUN apt update -y
RUN apt install -y esl-erlang
RUN apt install -y elixir

WORKDIR /home/elixir
CMD iex -S mix help

It works as expected:

$ docker build -t elixir-test -f Dockerfile.system . && docker run --rm -it elixir-test
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]
...
Interactive Elixir (1.13.0) - press Ctrl+C to exit (type h() ENTER for help)

Now, let's add Dockerfile for asdf:

# Dockerfile.asdf

FROM ubuntu

ARG DEBIAN_FRONTEND=noninteractive

RUN apt update -y
RUN apt upgrade -y
RUN apt install -y wget

# System installation of Erlang and Elixir according to https://elixir-lang.org/install.html
RUN wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && apt install -y --fix-broken ./erlang-solutions_2.0_all.deb
RUN apt update -y
RUN apt install -y esl-erlang
RUN apt install -y elixir

# asdf installation of Erlang and Elixir
RUN apt install -y curl git build-essential libssl-dev automake autoconf libncurses5-dev
RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.9.0
RUN echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
RUN bash -ic "asdf plugin add erlang && \
      asdf install erlang latest && \
      asdf plugin add elixir && \
      asdf install elixir latest"

WORKDIR /home/elixir
CMD bash -ic "asdf local erlang latest && asdf local elixir latest && iex -S mix help"
#CMD bash -ic "asdf local erlang system && asdf local elixir system && iex -S mix help"

This also works as expected:

$ docker build -t elixir-test -f Dockerfile.asdf . && docker run --rm -it elixir-test
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]
...
Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)

However, when you uncomment last CMD line in Dockerfile.asdf so that system versions of Elixir and Erlang would be used then problems happen:

$ docker build -t elixir-test -f Dockerfile.asdf . && docker run --rm -it elixir-test
** (SyntaxError) /root/.asdf/shims/mix:3:12: syntax error before: '.'
    |
  3 | exec /root/.asdf/bin/asdf exec "mix" "$@"
    |            ^
    (elixir 1.13.0) lib/code.ex:1183: Code.require_file/2

Hope this helps.

jarmo avatar Mar 25 '22 09:03 jarmo

Thanks for the Dockerfile @jarmo ! That helps some, however I've got a coworker who has this same issue manifesting with regular versions, not system versions. Have you or anyone else here encountered that?

Stratus3D avatar Apr 12 '22 14:04 Stratus3D

@Stratus3D you're welcome about Dockerfiles. I've only encountered this issue as is happening with reproducible Dockerfiles above. Not had issues like that in any other way yet.

jarmo avatar Apr 12 '22 14:04 jarmo

Have same issue. When install erlang localy, i added ERL_LIBS env in my .bashrc file. After remove it - all was fixed.

aywan avatar Jul 14 '22 21:07 aywan

Hitting this same issue, interestingly asdf worked correctly in one project, but not the other.

There was a slight difference in the .tool-versions file between the projects:

The non-working one:

 cat .tool-versions 
elixir 1.14.0
erlang 25.0.4

The working one:

 cat .tool-versions 
elixir 1.14
erlang 25.0.4

After changing elixir 1.14.0 to elixir 1.14 in the .tool-versions, I could launch iex -S mix again.

Arie avatar Oct 12 '22 15:10 Arie

I've experienced the same issue with system elixir. Described it here https://github.com/jfpedroza/neotest-elixir/issues/16

fuelen avatar Jan 26 '23 08:01 fuelen

I'm having this issue today and have never had a problem. I don't know what caused this.

adkron avatar May 09 '23 01:05 adkron

Fixed (YMMV):

Okay, I don't know what was happening as I had just installed the versions of Elixir and Erlang I was using. I tried to reshim, and that didn't fix it. I decided to do asdf uninstall elixir 1.14.4-otp-25. Then I did asdf install. The problem went away and it started working correctly. I don't like that I have no understanding of why it was broken, but if you need it to work maybe this is a good solution.

adkron avatar May 09 '23 01:05 adkron