mfussenegger / nvim-dap

Debug Adapter Protocol client implementation for Neovim
GNU General Public License v3.0
5.1k stars 179 forks source link

Source Mapping not working in WSL for Node with Jest #1191

Open juaoantonio opened 2 months ago

juaoantonio commented 2 months ago

Debug adapter definition and debug configuration

I have installed the plugin via lazyvim with some aditional configs:

local js_based_languages = {
  "typescript",
  "javascript",
  "typescriptreact",
  "javascriptreact",
  "vue",
}

return {
  { "nvim-neotest/nvim-nio" },

  -- virtual text for the debugger
  {
    "theHamsta/nvim-dap-virtual-text",
    opts = {},
    enabled = false,
  },

  {
    "mfussenegger/nvim-dap",
    config = function()
      local dap = require("dap")

      local Config = require("lazyvim.config")
      vim.api.nvim_set_hl(0, "DapStoppedLine", { default = true, link = "Visual" })

      for name, sign in pairs(Config.icons.dap) do
        sign = type(sign) == "table" and sign or { sign }
        vim.fn.sign_define(
          "Dap" .. name,
          { text = sign[1], texthl = sign[2] or "DiagnosticInfo", linehl = sign[3], numhl = sign[3] }
        )
      end

      for _, language in ipairs(js_based_languages) do
        dap.configurations[language] = {
          -- Debug single nodejs files
          {
            type = "pwa-node",
            request = "launch",
            name = "Launch file",
            program = "${file}",
            cwd = vim.fn.getcwd(),
            sourceMaps = true,
          },
          -- Debug nodejs processes (make sure to add --inspect when you run the process)
          {
            type = "pwa-node",
            request = "attach",
            name = "Attach",
            processId = require("dap.utils").pick_process,
            cwd = vim.fn.getcwd(),
            sourceMaps = true,
          },
          -- Debug web applications (client side)
          {
            type = "pwa-chrome",
            request = "launch",
            name = "Launch & Debug Chrome",
            url = function()
              local co = coroutine.running()
              return coroutine.create(function()
                vim.ui.input({
                  prompt = "Enter URL: ",
                  default = "http://localhost:3000",
                }, function(url)
                  if url == nil or url == "" then
                    return
                  else
                    coroutine.resume(co, url)
                  end
                end)
              end)
            end,
            webRoot = vim.fn.getcwd(),
            protocol = "inspector",
            sourceMaps = true,
            userDataDir = false,
          },
          -- Divider for the launch.json derived configs
          {
            name = "----- ↓ launch.json configs ↓ -----",
            type = "",
            request = "launch",
          },
        }
      end
    end,
    keys = {
      {
        "<leader>dO",
        function()
          require("dap").step_out()
        end,
        desc = "Step Out",
      },
      {
        "<leader>do",
        function()
          require("dap").step_over()
        end,
        desc = "Step Over",
      },
      {
        "<leader>da",
        function()
          if vim.fn.filereadable(".vscode/launch.json") then
            local dap_vscode = require("dap.ext.vscode")
            dap_vscode.load_launchjs(nil, {
              ["pwa-node"] = js_based_languages,
              ["chrome"] = js_based_languages,
              ["pwa-chrome"] = js_based_languages,
            })
          end
          require("dap").continue()
        end,
        desc = "Run with Args",
      },
    },
    dependencies = {
      -- Install the vscode-js-debug adapter
      {
        "microsoft/vscode-js-debug",
        -- After install, build it and rename the dist directory to out
        build = "npm install --legacy-peer-deps --no-save && npx gulp vsDebugServerBundle && rm -rf out && mv dist out",
        version = "1.*",
      },
      {
        "mxsdev/nvim-dap-vscode-js",
        config = function()
          ---@diagnostic disable-next-line: missing-fields
          require("dap-vscode-js").setup({
            -- Path of node executable. Defaults to $NODE_PATH, and then "node"
            -- node_path = "node",

            -- Path to vscode-js-debug installation.
            debugger_path = vim.fn.resolve(vim.fn.stdpath("data") .. "/lazy/vscode-js-debug"),

            -- Command to use to launch the debug server. Takes precedence over "node_path" and "debugger_path"
            -- debugger_cmd = { "js-debug-adapter" },

            -- which adapters to register in nvim-dap
            adapters = {
              "chrome",
              "pwa-node",
              "pwa-chrome",
              "pwa-msedge",
              "pwa-extensionHost",
              "node-terminal",
            },

            -- Path for file logging
            -- log_file_path = "(stdpath cache)/dap_vscode_js.log",

            -- Logging level for output to file. Set to false to disable logging.
            -- log_file_level = false,

            -- Logging level for output to console. Set to false to disable console output.
            -- log_console_level = vim.log.levels.ERROR,
          })
        end,
      },
      {
        "Joakker/lua-json5",
        build = "./install.sh",
      },
    },
  },
}

Debug adapter version

0.7.0

Steps to Reproduce

Project tree:

.
├── .gitignore
├── .tool-versions
├── .vscode
│   ├── launch.json
│   └── settings.json
└── aula-01-setup
    ├── .tool-versions
    ├── jest.config.mjs
    ├── package-lock.json
    ├── package.json
    └── test
        └── template.test.js

In my node project i have this package.json file:

{
  "name": "aula-01-setup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
   "scripts": {
    "test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand test/",
    "test:dev": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand --watchAll test/",
    "test:debug": "node --experimental-vm-modules --inspect-brk ./node_modules/.bin/jest --runInBand --watchAll test/"
  },  
  "keywords": [],
  "author": "juaoantonio",
  "license": "ISC",
  "type": "module",
  "engines": {
    "node": "v18.17.0"
  },
  "devDependencies": {
    "jest": "29"
  }
}

And this launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Node.js Debug Port",
      "request": "attach",
      "port": 9229,
      "type": "pwa-node",
      "skipFiles": [
        "<node_internals>/**",
        "${workspaceFolder}/**/node_modules/**/*.js"
      ],
      "smartStep": true,
      "sourceMaps": true,
      "internalConsoleOptions": "openOnSessionStart",
      "restart": true
    }
  ]
}

In template.test.js:

import { it } from "@jest/globals";

function sum(a, b) {
  return a + b;
}

it("sums two values", () => {
  const soma = sum(2, 3)
  expect(soma).toBe(5);
});

Expected Result

I was expecting the debbuger works as expected, but i encountered this issue.

Actual Result

when i run the debbuger, inserting a breaking point anywhere, a source mapping url appears in file:

import { it } from "@jest/globals";
function sum(a, b) {
  return a + b;
}
it("sums two values", () => {
  const soma = sum(2, 3);
  expect(soma).toBe(5);
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJpdCIsInN1bSIsImEiLCJiIiwic29tYSIsImV4cGVjdCIsInRvQmUiXSwic291cmNlcyI6WyJ0ZW1wbGF0ZS50ZXN0LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGl0IH0gZnJvbSBcIkBqZXN0L2dsb2JhbHNcIjtcblxuZnVuY3Rpb24gc3VtKGEsIGIpIHtcbiAgcmV0dXJuIGEgKyBiO1xufVxuXG5pdChcInN1bXMgdHdvIHZhbHVlc1wiLCAoKSA9PiB7XG4gIGNvbnN0IHNvbWEgPSBzdW0oMiwgMylcbiAgZXhwZWN0KHNvbWEpLnRvQmUoNSk7XG59KTtcbiJdLCJtYXBwaW5ncyI6IkFBQUEsU0FBU0EsRUFBRSxRQUFRLGVBQWU7QUFFbEMsU0FBU0MsR0FBR0EsQ0FBQ0MsQ0FBQyxFQUFFQyxDQUFDLEVBQUU7RUFDakIsT0FBT0QsQ0FBQyxHQUFHQyxDQUFDO0FBQ2Q7QUFFQUgsRUFBRSxDQUFDLGlCQUFpQixFQUFFLE1BQU07RUFDMUIsTUFBTUksSUFBSSxHQUFHSCxHQUFHLENBQUMsQ0FBQyxFQUFFLENBQUMsQ0FBQztFQUN0QkksTUFBTSxDQUFDRCxJQUFJLENBQUMsQ0FBQ0UsSUFBSSxDQUFDLENBQUMsQ0FBQztBQUN0QixDQUFDLENBQUMiLCJpZ25vcmVMaXN0IjpbXX0=
mfussenegger commented 2 months ago

This sounds like an issue with the debug adapter, not with nvim-dap. Does this work in vscode ?