elixir-lsp / elixir-ls

A frontend-independent IDE "smartness" server for Elixir. Implements the "Language Server Protocol" standard and provides debugger support via the "Debug Adapter Protocol"
https://elixir-lsp.github.io/elixir-ls/
Apache License 2.0
1.47k stars 194 forks source link

ELX_ELIXIR_OPTS not loaded when starting dap server #1120

Open nicklayb opened 14 hours ago

nicklayb commented 14 hours ago

Precheck

Environment

Current behavior

Unable to start the dap adapter with ELS_ELIXIR_OPTS. I have this configuration:

        local dap = require('dap')
        local elixir_ls_debugger = vim.fn.exepath "elixir-ls-debugger"
        dap.adapters.mix_task = {
          type = "executable",
          command = elixir_ls_debugger,
        }

        dap.configurations.elixir = {
          {
            type = "mix_task",
            name = "phoenix server",
            task = "phx.server",
            request = "launch",
            projectDir = "${workspaceFolder}",
            exitAfterTaskReturns = false,
            debugAutoInterpretAllModules = false,
            env = {
              ELS_ELIXIR_OPTS = "--name elixirls@localhost --cookie elixirls"
            }
          }
        }

And added the following to application.ex

    IO.puts("""
    #{Node.self()}
    #{Node.get_cookie()}
    #{System.get_env("ELS_ELIXIR_OPTS")}
    """)

When starting the debugger it outputs

nonode@nohost
nocookie
--name elixirls@localhost --cookie elixirls

Expected behavior

I would expect my dap environment to have the node name elixirls@localhost with cookie elixirls and that application.ex would log

elixirls@localhost
elixirls
--name elixirls@localhost --cookie elixirls
lukaszsamson commented 3 hours ago

I tried reproducing it with an empty mix project in VSCode

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "mix_task",
      "name": "mix (Default task)",
      "request": "launch",
      "task": "run",
      "taskArgs": ["lib/dap_env.ex"],
      "projectDir": "${workspaceRoot}",
      "env": {
        "ELS_ELIXIR_OPTS": "--name elixirls@localhost --cookie elixirls"
      },
      "exitAfterTaskReturns": false
    }
  ]
}

lib/dap_env.ex

defmodule DapEnv do
  def hello do
    IO.puts("hello")
    IO.puts("""
    #{Node.self()}
    #{Node.get_cookie()}
    #{System.get_env("ELS_ELIXIR_OPTS")}
    """)
    :world
  end
end

DapEnv.hello()

and I'm seeing this output

hello
elixirls@localhost
elixirls
--name elixirls@localhost --cookie elixirls
lukaszsamson commented 2 hours ago

I also tried a new phoenix project and a launch config identical to yours @nicklayb

mix phx.new dap_env_phx

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "mix_task",
      "name": "phoenix server",
      "request": "launch",
      "task": "phx.server",
      "projectDir": "${workspaceRoot}",
      "env": {
        "ELS_ELIXIR_OPTS": "--name elixirls@localhost --cookie elixirls"
      },
      "exitAfterTaskReturns": false,
      "debugAutoInterpretAllModules": false
    }
  ]
}

lib/dap_env_ph/application.ex

defmodule DapEnvPhx.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    IO.puts("""
    #{Node.self()}
    #{Node.get_cookie()}
    #{System.get_env("ELS_ELIXIR_OPTS")}
    """)

    children = [
      DapEnvPhxWeb.Telemetry,
      ...

and once again it worked as expected

Generated dap_env_phx app
Running mix phx.server
elixirls@localhost
elixirls
--name elixirls@localhost --cookie elixirls

[info] Running DapEnvPhxWeb.Endpoint with Bandit 1.5.7 at 127.0.0.1:4000 (http)

Do you have a repo that reproduces it?