mxsdev / nvim-dap-vscode-js

nvim-dap adapter for vscode-js-debug
276 stars 30 forks source link

Couldn't connect to localhost:${port}: ECONNREFUSED #46

Open mawkler opened 1 year ago

mawkler commented 1 year ago

I can't get this plugin to work. After calling require('dap').continue() I get the error Couldn't connect to localhost:${port}: ECONNREFUSED. Below is the minimal config that I'm using:

Click to expand ```lua local root = vim.fn.fnamemodify(vim.fn.expand('$HOME') .. '/code-other/nvim-test-config', ':p') -- set stdpaths to use .repro for _, name in ipairs({ 'config', 'data', 'state', 'cache' }) do vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name end -- bootstrap lazy local lazypath = root .. '/plugins/lazy.nvim' if not vim.loop.fs_stat(lazypath) then vim.fn.system({ 'git', 'clone', '--filter=blob:none', 'https://github.com/folke/lazy.nvim.git', lazypath, }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { { 'williamboman/mason.nvim', config = true }, { 'mfussenegger/nvim-dap', dependencies = { 'mxsdev/nvim-dap-vscode-js' }, config = function() local dap = require('dap') require('dap-vscode-js').setup({ debugger_path = require('mason-registry').get_package('js-debug-adapter'):get_install_path(), debugger_cmd = { 'js-debug-adapter' }, adapters = { 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost', }, }) dap.adapters['pwa-node'] = { type = 'server', host = 'localhost', port = '${port}', executable = { command = 'js-debug-adapter', args = { '${port}' }, } } for _, language in ipairs { 'typescript', 'javascript' } do dap.configurations[language] = { { type = 'pwa-node', request = 'launch', name = 'Launch file', program = '${file}', cwd = '${workspaceFolder}', }, } end vim.keymap.set('n', 'db', dap.toggle_breakpoint) vim.keymap.set('n', 'dc', dap.continue) end, } } vim.g.mapleader = ' ' vim.keymap.set('n', '', 'qa') require('lazy').setup(plugins, { root = root .. '/plugins', }) ```

Here's the TL;DR of the config, which is based on the installation instructions and this thread:

local dap = require('dap')

require('dap-vscode-js').setup({
  debugger_path = require('mason-registry').get_package('js-debug-adapter'):get_install_path(),
  debugger_cmd = { 'js-debug-adapter' },
  adapters = {
    'pwa-node',
    'pwa-chrome',
    'pwa-msedge',
    'node-terminal',
    'pwa-extensionHost',
  },
})

dap.adapters['pwa-node'] = {
  type = 'server',
  host = 'localhost',
  port = '${port}',
  executable = {
    command = 'js-debug-adapter',
    args = { '${port}' },
  }
}

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

You should be able to download and launch the config above with nvim -u /path/to/file/above.

Steps to reproduce:

  1. nvim -u /path/to/file/above test.ts
  2. Press <space>dc (to start debugging)
  3. After a few seconds, the message Couldn't connect to localhost:${port}: ECONNREFUSED appears
anajuliabit commented 1 year ago

same issue here. Did you find the solution?

mawkler commented 1 year ago

@anajuliabit Not yet 😕

benelan commented 1 year ago

I was getting the same error when using Mason to install the dap. I got it to work using my plugin manager (lazy.nvim) instead. Also make sure not to set up the adapter manually (dap.adapters['pwa-node'] = ...) since that's what this plugin does. Here is the relevant snippet (full example in my dotfiles):

    {
      "microsoft/vscode-js-debug",
      build = "npm install --legacy-peer-deps && npx gulp vsDebugServerBundle && mv dist out",
    },
    {
      "mxsdev/nvim-dap-vscode-js",
      opts = {
        debugger_path = vim.fn.stdpath "data" .. "/lazy/vscode-js-debug",
        adapters = { "pwa-node", "pwa-chrome" },
      },
    }
ChrisBrowne commented 1 year ago

I was having the same issue, and found this js-debug issue. When i removed the ::1 entry in my /etc/hosts file, it all started working. I've now reverted my hosts file change, and have changed the host part of the adaptor config to the ip4 loopback address, so that it now looks like this:

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

and it's still working.

abhivashistha5 commented 1 year ago

I was facing the same issue. For me the issue was the node version. I was using node 14 which does not support the Nullish coalescing assignment operator ??= . Updating to v15.5.0 solved the issue.

samcurteis commented 1 year ago

I just had the same problem using a lazyvim setup. I did the same as @benelan but got this error:

[dap-js] Error trying to launch JS debugger: ...nvim/lazy/nvim-dap-vscode-js/lua/dap-vscode-js/utils.lua:64: Debugger entrypoint file '/Users/sam/.local/share/nvim/lazy/vscode-js-debug/out/src/vsDebugServer.js' does not exist. Did it build properly?

It seemed like lazyvim wasn't installing vscode-js-debug properly, so I had to do it manually:

git clone https://github.com/microsoft/vscode-js-debug
cd vscode-js-debug
npm install --legacy-peer-deps
npx gulp vsDebugServerBundle
mv dist out

Then move the out directory in vscode-js-debug to ~/.local/share/nvim/lazy/vscode-js-debug/

After that it started working again.

benelan commented 1 year ago

@samcurteis Did you try LazyVim's typescript extras? If that doesn't work maybe you could create a PR or log an issue to swap out LazyVim's implementation for this one.

CreeperMain commented 8 months ago

hi i ran into this issue was it ever solved

serranomorante commented 8 months ago

hi i ran into this issue was it ever solved

Just use the dap debug server directly without this plugin.

https://github.com/serranomorante/.dotfiles/blob/main/.config%2Fnvim%2Flua%2Fserranomorante%2Fplugins%2Fdap%2Fnvim-dap.lua#L94-L119

More context: vscode-js-debug is a package that provides 2 debug servers: vsDebugServer and dapDebugServer. If you go with vsDebugServer, then you need to use this plugin. If you go with dapDebugServer then you can use it directly without this plugin as showed in my config. I prefer the latter.

Make sure to always install latest version of vscode-js-debug. Right now that is 1.86.1 and that version is working fine for me even on Nextjs14 (server side and client side) projects with typescript and app router.

Don't use oudated guides. vscode-js-debug changes a lot. Don't put configurations like "resolveSourceMapLocations" if you dont understand them. Also vscode-js-debug already provides good defaults.

This stuff is hard, sadly.

CreeperMain commented 8 months ago

hi this came in useful ty you for ur quick reply

chevcast commented 5 months ago

I was having the same issue, and found this js-debug issue. When i removed the ::1 entry in my /etc/hosts file, it all started working. I've now reverted my hosts file change, and have changed the host part of the adaptor config to the ip4 loopback address, so that it now looks like this:

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

and it's still working.

This saved my bacon. Thank you 🙂

For those of you using LazyVim with the dap.core and lang.typescript extras installed, you need only change the hostname from localhost to 127.0.0.1 and you can do this easily. I just added it as a separate plugin file (~/.config/nvim/lua/plusings/dap.lua):

return {
    "mfussenegger/nvim-dap",
    opts = function()
        local dap = require("dap")
        dap.adapters["pwa-node"].host = "127.0.0.1"
    end,
}

This will leave all the config from the Lazy Extras intact and just override the specified hostname.

PS - Not sure if this is helpful to anyone or not but this issue existed on an M3 Macbook Pro but did not exist on my home PC in WSL. Also, commenting out ::1 in /etc/hosts did nothing despite the fact that changing localhost to 127.0.0.1 does indeed fix this issue on the M3 MBP.