From 8f3d11991fcee108d0503281fdb5c48e8988c4aa Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Fri, 29 Nov 2024 21:10:33 +0100 Subject: [PATCH 1/2] Support running debugpy via uv run --- README.md | 38 ++++++++++++++++++++++++++++++-------- lua/dap-python.lua | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c3a1340..c37566f 100644 --- a/README.md +++ b/README.md @@ -18,21 +18,36 @@ tree sitter parser for Python. ### Debugpy -You need to install `debugpy` using either your package manager or via `pip`. -For example: +Options to install [debugpy][3]: + +1. Via a system package manager. For example: ```bash -mkdir .virtualenvs -cd .virtualenvs +pacman -S python-debugpy +``` + +2. Via `pip` in a `venv`: + +```bash +mkdir ~/.virtualenvs +cd ~/.virtualenvs python -m venv debugpy debugpy/bin/python -m pip install debugpy ``` -If you're using virtual environments for your project it will automatically get -picked up if it is active before `nvim` starts, or if it is in a default -location. See [Python dependencies and +Note that the virutalenv used for the `debugpy` can be independent from any +virtualenv you're using in your projects. + +`nvim-dap-python` tries to detect your project `venv` and should recognize any +dependencies your project has. See [Python dependencies and virtualenv](#python-dependencies-and-virtualenv) + +3. Implicit via [uv][uv] + +See [Usage](#usage): You need to use `require("dap-python").setup("uv")` + + ### Tree-sitter If you're using a properly packaged `nvim` version 0.10+, a python tree-sitter @@ -61,7 +76,7 @@ can install it manually using either: -- must work in the shell ``` - Or if installed globally: + If installed globally: ```lua require("dap-python").setup("python3") @@ -69,6 +84,12 @@ can install it manually using either: -- must work in the shell ``` + If using [uv][uv]: + + ```lua + require("dap-python").setup("uv") + ``` + 2. Use `nvim-dap` as usual. @@ -201,3 +222,4 @@ vimcats -f -t lua/dap-python.lua > doc/dap-python.txt [7]: https://github.com/wbthomason/packer.nvim [debugpy_wiki]: https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings [vimcats]: https://github.com/mrcjkb/vimcats +[uv]: https://docs.astral.sh/uv/ diff --git a/lua/dap-python.lua b/lua/dap-python.lua index 5a7995e..06a2be7 100644 --- a/lua/dap-python.lua +++ b/lua/dap-python.lua @@ -207,7 +207,9 @@ end --- Register the python debug adapter ----@param python_path string|nil Path to the python interpreter. Path must be absolute or in $PATH and needs to have the debugpy package installed. Default is `python3` +--- +---@param python_path "python"|"python3"|"uv"|string|nil Path to python interpreter. Must be in $PATH or an absolute path and needs to have the debugpy package installed. Defaults to `python3`. +--- If `uv` then debugpy is launched via `uv run` ---@param opts? dap-python.setup.opts See |dap-python.setup.opts| function M.setup(python_path, opts) local dap = load_dap() @@ -219,7 +221,9 @@ function M.setup(python_path, opts) local port = (config.connect or config).port ---@diagnostic disable-next-line: undefined-field local host = (config.connect or config).host or '127.0.0.1' - cb({ + + ---@type dap.ServerAdapter + local adapter = { type = 'server', port = assert(port, '`connect.port` is required for a python `attach` configuration'), host = host, @@ -227,17 +231,33 @@ function M.setup(python_path, opts) options = { source_filetype = 'python', } - }) + } + cb(adapter) else - cb({ - type = 'executable'; - command = python_path; - args = { '-m', 'debugpy.adapter' }; - enrich_config = enrich_config; - options = { - source_filetype = 'python', + ---@type dap.ExecutableAdapter + local adapter + if python_path == "uv" then + adapter = { + type = "executable", + command = "uv", + args = {"run", "--with", "debugpy", "python", "-m", "debugpy.adapter"}, + enrich_config = enrich_config, + options = { + source_filetype = "python" + } + } + else + adapter = { + type = "executable", + command = python_path, + args = {"-m", "debugpy.adapter"}; + enrich_config = enrich_config, + options = { + source_filetype = "python" + } } - }) + end + cb(adapter) end end dap.adapters.debugpy = dap.adapters.python From 55b82d1aeb3e8cd36b1711c1786070f8eece5df2 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Fri, 29 Nov 2024 21:16:57 +0100 Subject: [PATCH 2/2] Update tree-sitter setup instructions; python parser is no longer bundled --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c37566f..9239e2d 100644 --- a/README.md +++ b/README.md @@ -50,14 +50,9 @@ See [Usage](#usage): You need to use `require("dap-python").setup("uv")` ### Tree-sitter -If you're using a properly packaged `nvim` version 0.10+, a python tree-sitter -parser should be included and you're all set. +To install the python tree-sitter parser you can either: -If you're using an older version, or your distribution excluded the parser you -can install it manually using either: - - -- Via `:TSInstall python` of [nvim-treesitter][4] +- Use `:TSInstall python` from [nvim-treesitter][4] - Compile the parser from [tree-sitter-python][5] and copy it into `.config/nvim/parser/`: - `git clone https://github.com/tree-sitter/tree-sitter-python.git` - `cd tree-sitter-python`