mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
492 stars 46 forks source link

debugging a flask application crashes before starting #112

Closed brunobmello25 closed 7 months ago

brunobmello25 commented 1 year ago

Here is my current configuration:

return {
  {
    "mfussenegger/nvim-dap",
    config = function()
      vim.keymap.set('n', '<F10>', '<cmd>lua require"dap".step_over()<CR>')
      vim.keymap.set('n', '<F11>', '<cmd>lua require"dap".step_into()<CR>')
      vim.keymap.set('n', '<F12>', '<cmd>lua require"dap".step_out()<CR>')
      vim.keymap.set('n', '<F5>', '<cmd>lua require"dap".continue()<CR>')
      vim.keymap.set('n', '<leader>db', '<cmd>lua require"dap".toggle_breakpoint()<CR>')
      vim.keymap.set('n', '<leader>dc',
        '<cmd>lua require"dap".set_breakpoint(vim.fn.input("Breakpoint condition: "))<CR>')

      vim.keymap.set('n', '<leader>du', '<cmd>lua require"dapui".toggle()<CR>')
    end
  },

  {
    'rcarriga/nvim-dap-ui',
    dependencies = {
      'mfussenegger/nvim-dap',
    },
    config = function()
      local dap = require('dap')
      local dapui = require('dapui')
      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
  },

  {
    'jay-babu/mason-nvim-dap.nvim',
    dependencies = {
      'williamboman/mason.nvim',
      'mfussenegger/nvim-dap',
    },
    config = function()
      require("mason-nvim-dap").setup({
        ensure_installed = { "python", "rust" },
      })
    end
  },

  {
    'mfussenegger/nvim-dap-python',
    dependencies = {
      'mfussenegger/nvim-dap',
      'rcarriga/nvim-dap-ui',
    },
    ft = { 'python' },
    config = function()
      local dap = require('dap')
      local dap_python = require('dap-python')

      local path = vim.fn.getcwd() .. '/venv/bin/python'

      dap_python.setup(path)

      table.insert(dap.configurations.python, {
        name = "Launch Flask server",
        type = "python",
        request = "launch",
        console = "integratedTerminal",
        cwd = "${workspaceFolder}",
        program = path,
        args = {
          "run",
          "flask",
          "run",
        },
      })
    end
  }
}

I tried running the flask project with this launch flask server configuration but I fall in this error:

Traceback (most recent call last):
  File "/usr/lib64/python3.11/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/brubs/dev/work/kowalski/venv/lib64/python3.11/site-packages/debugpy/lau
ncher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/brubs/dev/work/kowalski/venv/lib64/python3.11/site-packages/debugpy/lau
ncher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/brubs/dev/work/kowalski/venv/lib64/python3.11/site-packages/debugpy/lau
ncher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/brubs/dev/work/kowalski/venv/lib64/python3.11/site-packages/debugpy/_ve
ndored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 320, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brubs/dev/work/kowalski/venv/lib64/python3.11/site-pac
ndored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 294, in _get_cod
    code = compile(f.read(), fname, 'exec')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: source code string cannot contain null bytes

[Process exited 1]

I also tried running with mason's venv (which also has debugpy installed), by changing my config to be like this:

return {
  ... -- rest of the config is the same as the above
  {
    'mfussenegger/nvim-dap-python',
    dependencies = {
      'mfussenegger/nvim-dap',
      'rcarriga/nvim-dap-ui',
    },
    ft = { 'python' },
    config = function()
      local dap = require('dap')
      local dap_python = require('dap-python')

      local path = require('mason-registry').get_package('debugpy'):get_install_path() .. '/venv/bin/python'

      dap_python.setup(path)

      table.insert(dap.configurations.python, {
        name = "Launch Flask server",
        type = "python",
        request = "launch",
        console = "integratedTerminal",
        cwd = "${workspaceFolder}",
        program = path,
        args = {
          "run",
          "flask",
          "run",
        },
      })
    end
  }
}

but it falls on the same error

I also tried running vscode debugger to have a way to be sure that it's not a problem with the project, but vscode debugger works fine using the venv interpreter. Here is the config:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "python",
      "request": "launch",
      "name": "Flask",
      "module": "flask",
      "args": [
        "--app",
        "app",
        "run"
      ],
      "console": "integratedTerminal"
    }
  ]
}

Sorry if this is not the proper place to ask this question. I can close the issue if it's not okay, no problem

mfussenegger commented 8 months ago

The config you are using for nvim-dap is current than the vscode config, is that intentional?

It should look like this to have the same:

table.insert(dap.configurations.python, {
        name = "Launch Flask server",
        type = "python",
        request = "launch",
        console = "integratedTerminal",
        module = "flask",
        args = {
          "--app",
          "app",
          "run",
        },
      })

Or you could load the launch.json directly