Start Phoenix within Visual Studio Code

Hello,

How do I start a phoenix app within visual studio code? I am using VSC with the elixir-ls plugin. When I try to start debugging with “mix phx.server”, the debugging-console writes:

Started ElixirLS debugger
Elixir version: "1.7.1 (compiled with Erlang/OTP 19)"
Erlang version: "19"

but nothing more happens.

I’ve added this to the list of “configurations” in my launch.json file of my project.

{
            "type": "mix_task",
            "name": "mix phx.server",
            "request": "launch",
            "projectDir": "${workspaceRoot}"
        }

Hi
Do you have node installed? if not you should install it is a requirement for phoenix pipe_through:browser apps(html rendered apps).

Also look over the installation guide here https://hexdocs.pm/phoenix/installation.html

Also if what i wrote doesn’t solve your problem then please specify:

operating system

erlang elixir version

phoenix version

also your familiarity with phoenix

That will help others and myself to offer better assistance with your problem

Are You working on windows? All unix systems have decent terminal. As a side note, if You are debugging your server, You could start it with…

$ iex -S mix phx.server

This will let You debug from inside the running server :slight_smile:

1 Like

To be more precise, starting the server with “mix phx.server” right from the command line works. I just want to get it working within the debugging tool of VSC.

Ok then this medium post at the bottom of the page explains how to debug in vscode and what settings you need step by step.

3 Likes

Thanks, I’m coming from that. But it’s only about elixir and not about phoenix. So I tried to enter ‘mix phx.server’ instead of ‘mix test’.

Sorry, it’s not that my problem is resolved. My previous message said that I have tried what the page said and changed “mix test” to “mix phx.server” because the page doesn’t deal with phoenix.

What I then get is

Started ElixirLS debugger
Elixir version: "1.7.1 (compiled with Erlang/OTP 19)"
Erlang version: "19"

but nothing else is happening.

So do you have node installed? verify using node -v and also npm -v in terminal,
If you do try the following:

I got the same symptoms on a Ubuntu and then:

sudo apt-get remove -purge node-js

 installed nvm 

then nvm install node

and everything worked fine after that.

Also nvm docs and install here https://github.com/nvm-sh/nvm#installation-and-update

Also what OS are you using? if it is windows 7 or 10 then try to open vscode as admin

Also can you share the code that you try to test or do you need a sample project with code that can be tested?

Do people in the Elixir world use debuggers? I haven’t seen any mention of it in other IDEs. What other IDE’s support an Elixir debugging experience?

KroniDeth’ intellij-elixir does support debugging as far as I know.

1 Like

I created a new phoenix app with ‘mix phx.new testit --no-ecto’ (I am using Windows 10).

This time I get an error:

Started ElixirLS debugger
Elixir version: "1.7.1 (compiled with Erlang/OTP 19)"
Erlang version: "19"
[warn] Phoenix is unable to create symlinks. Phoenix' code reloader will run considerably faster if symlinks are allowed. On Windows, the lack of symlinks may even cause empty assets to be served. Luckily, you can address this issue by starting your Windows terminal at least once with "Run as Administrator" and then running your Phoenix application.
[info] Application testit exited: Testit.Application.start(:normal, []) returned an error: shutdown: failed to start child: TestitWeb.Endpoint
    ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, TestitWeb.Endpoint.HTTP}
        ** (EXIT) an exception was raised:
            ** (ArgumentError) argument error
                (phoenix) Elixir.Phoenix.Endpoint.Cowboy2Adapter.erl:69: Phoenix.Endpoint.Cowboy2Adapter.info/3
                (phoenix) Elixir.Phoenix.Endpoint.Cowboy2Adapter.erl:54: Phoenix.Endpoint.Cowboy2Adapter.start_link/3
(Debugger) Task failed because an exception was raised:
    ** (Mix.Error) Could not start application testit: Testit.Application.start(:normal, []) returned an error: shutdown: failed to start child: TestitWeb.Endpoint
    ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, TestitWeb.Endpoint.HTTP}
        ** (EXIT) an exception was raised:
            ** (ArgumentError) argument error
                (phoenix) Elixir.Phoenix.Endpoint.Cowboy2Adapter.erl:69: Phoenix.Endpoint.Cowboy2Adapter.info/3
                (phoenix) Elixir.Phoenix.Endpoint.Cowboy2Adapter.erl:54: Phoenix.Endpoint.Cowboy2Adapter.start_link/3
        (mix) lib/mix.ex:323: Mix.raise/1
        (elixir) lib/enum.ex:765: Enum."-each/2-lists^foreach/1-0-"/2
        (elixir) lib/enum.ex:765: Enum.each/2
        (mix) lib/mix/tasks/app.start.ex:114: Mix.Tasks.App.Start.start/2
        (mix) lib/mix/tasks/app.start.ex:86: Mix.Tasks.App.Start.run/1
        (mix) lib/mix/task.ex:316: Mix.Task.run_task/3
        (mix) lib/mix/tasks/run.ex:129: Mix.Tasks.Run.run/5

Node is installed. As I wrote, starting the server from command line works.

I can’t help you, I use Linux, but there are the following posts

Is anyone using Visual Studio Code debugging with phoenix? Sucessfully?

Less than in other languages. Stopping a process and single-stepping it, while the process that calls it times out, is still useful for understanding what the code in one process does. But that kind of thing is less useful for the highly-concurrent OTP style of program, so I think stepping in a debugger is more of a beginner’s technique, not relied on once you’ve got a better understanding of how things work.

That said, without the debugger, I would NEVER have figured out what was going on when a bug in PostgreSQL’s parser broke postgrex. Yeah, I know, the last place in the world I would look for a bug.

1 Like

So instead of stepping through what do you use? TDD?

TDD, logging, tracing.

In case it’s not clear from my previous message, I fully agree that a visual debugger is needed for the environment, especially to help newcomers wrap their heads around Elixir. Just trying to point out why it’s less emphasized…

2 Likes

Yes! I heavily rely on VS Code and Elixir LS, and while the debugger is pretty slow as the project gets bigger it’s a super helpful tool.

I think you’re just missing one line from your launch.json file:

"task": "phx.server"

at the bottom.

Your whole launch.json could look like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "mix_task",
            "name": "mix phx.server",
            "request": "launch",
            "projectDir": "${workspaceRoot}",
            "task": "phx.server"
        }
    ]
}

I’m on a macbook so there may be some windows things to work out, but I think that should get you most of the way there. I did a mix phx.new on the latest version, and this configuration allowed me to successfully launch the debugger.

Here’s what I always reference when I’m setting up the debugger for Phoenix: https://github.com/JakeBecker/vscode-elixir-ls/issues/33#issuecomment-400342248

4 Likes

Thanks, I’ve tried it with the added line “task”: “phx.server”, but it is still the same.

Regarding debugging without IDE : I have created a package that might be useful : https://hex.pm/packages/qq

I’m an elixir newbie, so feedback about the tool, docs and code (github link on hex) is welcome.

1 Like