nvim-neotest / neotest-jest

MIT License
124 stars 80 forks source link

DAP support? #31

Open xbc5 opened 2 years ago

xbc5 commented 2 years ago

Is there any DAP support? Does this plug-in intentionally target the pwa-node adapter?

When running the dap strategy it's saying that it's looking for the pwa-node adapter -- is this plug-in configured for such an adapter? I can't find any DAP documentation that doesn't make my brain slowly leak out of my nose, and there's zero documentation for pwa-node (progressive web app?).

michaeldelgado1 commented 2 years ago

I'm wondering the same thing. I keep seeing READMEs/Wikis that say that it's supported out of the box. In the neotest repo I read that It's supposed to set a strategy like this: require('neotest').run.run({strategy = 'dap'}) The config that seems to get sent to dap looks like it's getting set here: https://github.com/haydenmeade/neotest-jest/blob/main/lua/neotest-jest/init.lua#L149

The two things I noticed when trying to run this: First, I get a straight failure from the dap-vscode-js adapter saying there is a missing path. I found that it's talking about the cwd in the config I linked above. I worked around that by changing the code in the plugin itself to this:

dap = function()
  return {
    name = "Debug Jest Tests",
    type = "pwa-node",
    cwd = "${workspaceFolder}",
    request = "launch",
    args = { unpack(command, 2) },
    runtimeExecutable = command[1],
    console = "integratedTerminal",
    internalConsoleOptions = "neverOpen",
  }
end,

Second, I no longer fail, but my dapui config opens, and then imminently closes. I can open the ui manually again, but once I step/continue, it closes again. I'm not totally sure why that's happening, but it's a step closer to getting the configs working.

So back to the original question, Is dap officially supported? Is the documentation for it correct in the dap README? I'm also curious if this is the right plugin to install for debugging support: mxsdev/nvim-dap-vscode-js

I can make a different issue if need be.

haydenmeade commented 2 years ago

Hi, I'm not familiar with Dap. I would probably take a look at the vscode implementation and see what the neotest-jest config is missing. Otherwise maybe some of the other neotest adaptors have a dap implementation.

aaronmcadam commented 2 years ago

It doesn't look like pwa-node/vscode is DAP compliant. If we look in the DAPInstall.nvim plugin, we can see that the supported debuggers are vscode-chrome-debug and node-debug2: https://github.com/ravenxrz/DAPInstall.nvim#list-of-debuggers

michaeldelgado1 commented 2 years ago

I started with using the node-debug2 implementation, but that project has be archived. Microsoft won't support or document any of this in the pwa-node implementation, so we're a bit in a weird spot at the moment.

I was looking at a few plugins, and found reference that mentioned this plugin being dap compatible. The code I linked to was from this repo. It seems to use the pwa-node version. Was that intended to support dap at sometime, but not fully implemented?

aaronmcadam commented 2 years ago

@michaeldelgado1 I've got something working with the following change to init.lua:

local function get_strategy_config(strategy, command)
  local config = {
    dap = function()
      return {
        name = "Debug Jest Tests",
        type = 'node2',
        request = 'launch',
        sourceMaps = "inline",
        program = command[1],
        args = {
          unpack(command, 2)
        },
        cwd = vim.fn.getcwd(),
        protocol = 'inspector',
        console = 'integratedTerminal',
      }
    end,
  }
  if config[strategy] then
    return config[strategy]()
  end
end

Assuming you're using Packer to manage your plugins, you can try this hack yourself by changing ~/.local/share/nvim/site/pack/packer/start/neotest-jest/init.lua.

You'll also need the following DAP config:

local dap_status_ok, dap = pcall(require, "dap")
if not dap_status_ok then
  return
end

local dap_ui_status_ok, dapui = pcall(require, "dapui")
if not dap_ui_status_ok then
  return
end

local dap_install_status_ok, dap_install = pcall(require, "dap-install")
if not dap_install_status_ok then
  return
end

dap.adapters.node2 = {
    type = 'executable',
    command = 'node',
    args = {
        vim.fn.stdpath("data") .. "/dapinstall/jsnode_dbg/" ..
            '/vscode-node-debug2/out/src/nodeDebug.js'
    }
}

dap_install.setup()
dap_install.config("jsnode", {})

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
aaronmcadam commented 2 years ago

I don't think this is perfect yet, it looks like passing tests don't get reported, but it's a start! CleanShot 2022-08-16 at 00 18 14@2x

michaeldelgado1 commented 2 years ago

Oh that's cool! I've never used DAPInstall before, but it looks pretty convenient. I'll check this out tonight and see if I can get it working as well

aaronmcadam commented 2 years ago

I found these helpful when trying to piece together a solution:

michaeldelgado1 commented 2 years ago

Strange, I set up with several variations of what you posted, but I wasn't able to get dap to stop at my breakpoints. I'll do more debugging, and see if I can figure anything out.

aaronmcadam commented 2 years ago

@haydenmeade Would you be open to a PR with the above changes? I'd like to see if my fixes will work for others.

haydenmeade commented 2 years ago

@aaronmcadam that would be great!

entropitor commented 2 years ago

@aaronmcadam js-debug not yet DAP compliant because vscode-js-debug allows debug workers etc (and they are still working on standardizing on how child sessions work).

If you check https://github.com/microsoft/vscode-node-debug2 you see that it's deprecated in favor of the js-debug one...

This repo should already have out of the box support for js-debug but you might need https://github.com/mxsdev/nvim-dap-vscode-js to make it work with nvim-dap

aaronmcadam commented 2 years ago

@aaronmcadam js-debug not yet DAP compliant because vscode-js-debug allows debug workers etc (and they are still working on standardizing on how child sessions work).

If you check https://github.com/microsoft/vscode-node-debug2 you see that it's deprecated in favor of the js-debug one...

This repo should already have out of the box support for js-debug but you might need https://github.com/mxsdev/nvim-dap-vscode-js to make it work with nvim-dap

Thanks, I can't get the plugin to pick up my source maps for my TypeScript project.

aaronmcadam commented 2 years ago

I see a message saying it can't find a source map, and then I end up seeing the compiled js output.

aaronmcadam commented 2 years ago

@entropitor I've tried a lot of different hacks inside neotest-jest to try to make vscode-js-debug find my sourcemaps, but I can't get things working :(

DasOhmoff commented 1 year ago

I have the very same issue as @aaronmcadam , the source maps cannot be found. Is there some solution for this already. @aaronmcadam did you find something?

aaronmcadam commented 1 year ago

hey @DasOhmoff, I recently got something working by using Mason to install js-debug-adapter. Have a look around my dotfiles if you want to figure it out 😄

cw0 commented 1 year ago

I was able to get everything working fine except for it missing sourcemaps in the repl window. Has anyone figured out how to fix sourcemaps?

cw0 commented 1 year ago

I have the very same issue as @aaronmcadam , the source maps cannot be found. Is there some solution for this already. @aaronmcadam did you find something?

I used this workaround for the source maps, still having an issue with it looking for the typescript lib sourcemap but it got rid of all the mason sourcemap errors

https://github.com/mxsdev/nvim-dap-vscode-js/issues/35#issuecomment-1458311404

I think my issue at this point has to do with my project being a monorepo, does anyone know if its possible to pass a custom DAP config or are we stuck with the hardcoded one mentioned above

dsuttonpreece commented 1 year ago

Is there any movement on this?

Would it be hard to pass in custom dap configuration into run ?

require("neotest").run.run { 
  strategy = "dap",
  configuration = {
    -- custom dap config here
  }
}
navignaw commented 4 months ago

It's not super well-documented, but I did some spelunking in the code and found that you can override strategy_config opt to pass a custom function. This function takes two arguments, the default config and the full table of args.

For example, here is my config to add an extra "resolveSourceMapLocations" to the DAP config:

require("neotest-jest")({
  jestCommand = "yarn jest",
  strategy_config = function(default_strategy, _)
    default_strategy["resolveSourceMapLocations"] = {
      "${workspaceFolder}/**",
      "!**/node_modules/**",
    }
    return default_strategy
  end,
}),

You could do the same or return a custom dap config entirely.