rcarriga / nvim-dap-ui

A UI for nvim-dap
MIT License
2.69k stars 102 forks source link

TS/JS Debugging attempts show: Couldn't connect to 127.0.0.1:8727: ECONNREFUSED #300

Open maibarra opened 1 year ago

maibarra commented 1 year ago

I am getting the following error message when attempting to start the debugger through <Leader>db , <Leader>dr Result:

  1. <Leader>db ✅ correctly marks the debugging line
  2. <Leader>dr gets the following:

    Couldn't connect to 127.0.0.1:8727: ECONNREFUSED

Is there a clean implementation of what I've attempted?

The (almost) perfect Neovim setup for Node.js

I also tried adding $HOME/.local/share/nvim/mason/bin to my PATH, but that did nothing.

Here's what my~/.config/nvim/lua/custom/plugins.lua looks like:

local plugins = {
  {
    "rcarriga/nvim-dap-ui",
    event = "VeryLazy",
    dependencies = "mfussenegger/nvim-dap",
    config = function ()
      local dap = require("dap")
      local dapui = require("dapui")
      require("dapui").setup()
      dap.listeners.after.event_initialized["dapui_config"] = function()
        dapui.open()
      end
      dap.listeners.before.event_terminated["dapui_config"] = function()
        dapui.close()
      end
      dap.listeners.before.event_exited["dapui_config"] = function()
        dapui.close()
      end
    end
  },
  {
    "mfussenegger/nvim-dap",
    config = function()
      require "custom.configs.dap"
      require("core.utils").load_mappings("dap")
    end
  },
  {
    "mhartington/formatter.nvim",
    event = "VeryLazy",
    opts = function()
      return require "custom.configs.formatter"
    end
  },
  {
    "mfussenegger/nvim-lint",
    event = "VeryLazy",
    config = function ()
      require "custom.configs.lint"
    end
  },
  {
    "williamboman/mason.nvim",
    opts = {
      ensure_installed = {
        "eslint-lsp",
        "js-debug-adapter",
        "prettier",
        "typescript-language-server",
        "black",
        "mypy",
        "ruff",
        "pyright",
      },
    },
  },
  {
    "neovim/nvim-lspconfig",
    config = function ()
      require "plugins.configs.lspconfig"
      require "custom.configs.lspconfig"
    end,
  },
}
return plugins

Here's what my~/.config/nvim/lua/custom/config/dap.lua looks like:

local M = {
  filetype = {
    javascript = {
      require("formatter.filetypes.javascript").prettier
    },
    typescript = {
      require("formatter.filetypes.typescript").prettier
    },
    ["*"] = {
      require("formatter.filetypes.any").remove_trailing_whitespace
    }
  }
}

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  command = "FormatWriteLock"
})

return M

Here's what my~/.config/nvim/lua/custom/config/lspconfig.lua looks like:

local config = require("plugins.configs.lspconfig")
local on_attach = config.on_attach
local capabilities = config.capabilities

local lspconfig = require("lspconfig")

local function organize_imports()
  local params = {
    command = "_typescript.organizeImports",
    arguments = {vim.api.nvim_buf_get_name(0)},
  }
  vim.lsp.buf.execute_command(params)
end

lspconfig.tsserver.setup {
  on_attach = on_attach,
  capabilities = capabilities,
  init_options = {
    preferences = {
      disableSuggestions = true,
    }
  },
  commands = {
    OrganizeImports = {
      organize_imports,
      description = "Organize Imports",
    }
  }
}

lspconfig.pyright.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  filetypes = {"python"},
})
phortonssf commented 1 year ago

Looks like it is trying to attach to a debug server. What was your typescript/node server running on? You need to run a node process with --inspect-brk flag when doing the attach. When you do that you will see the servers ip and port. In your dap configuration you then can specify that port and url.

For example: nodemode node -r ts-node/register --inspect-brk src/index.ts which starts the server and reloads with nodemon on changes and will ouput the following Debugger listening on ws://127.0.0.1:9229 Leave this running.

Then in another shell/terminal start neovim try to connect using the attach method. This should bring up a list of processes, type in node and you should see a process that is running your server above.

The dap.configuration you use should have the proper keys and values

url = "ws://127.0.0.1:9229",
 port = 9229,

Your url and port will differ. Those valuse come from your debug server you started above.

maibarra commented 1 year ago

Thanks for the reply @phortonssf

When I attempt to run the node -r ts-node/register --inspect-brk src/index.ts command on the shell, and start nvim on another tab, I get the following:

Error executing luv callback:
...s/maibarra/.local/share/nvim/lazy/nvim-dap/lua/dap/rpc.lua:81: ...s/maibarra/.local/share/nvim/lazy/nvim-dap/lua/dap/rpc.lua:23: Content-Length not found in headers
. "Content-Type: text/html; charset=UTF-8"
stack traceback:
        [C]: in function 'parse_chunk'
        ...s/maibarra/.local/share/nvim/lazy/nvim-dap/lua/dap/rpc.lua:81: in function <...s/maibarra/.local/share/nvim/lazy/nvim-dap/lua/dap/rpc.lua:67>

The closest thing to a dap.configuration i have is: lua/custom/configs/dap.lua and it contains:

local dap = require("dap")

dap.adapters["pwa-node"] = {
  type = "server",
  host = "127.0.0.1",
  port = 9229,
  executable = {
    command = "js-debug-adapter",
  }
}

for _, language in ipairs{ "typescript", "javascript" } do
  dap.configurations[language] = {
    {
      type = "pwa-node",
      request = "launch",
      name = "Launch file",
      program = "${file}",
      cwd = "${workspaceFolder}",
      runtimeExecutable = "node",
    },
  }
end

Am I missing something?