{"id":18057653,"url":"https://github.com/blob42/nvim-dap-go","last_synced_at":"2025-04-05T10:26:09.228Z","repository":{"id":258886498,"uuid":"875581432","full_name":"blob42/nvim-dap-go","owner":"blob42","description":"leoluz/nvim-dap-go fork with support for mason-nvim-dap","archived":false,"fork":false,"pushed_at":"2024-11-18T21:26:07.000Z","size":233,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T21:19:02.722Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blob42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-20T11:21:31.000Z","updated_at":"2024-11-18T21:26:11.000Z","dependencies_parsed_at":"2024-10-27T06:52:01.499Z","dependency_job_id":null,"html_url":"https://github.com/blob42/nvim-dap-go","commit_stats":null,"previous_names":["blob42/nvim-dap-go"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blob42%2Fnvim-dap-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blob42%2Fnvim-dap-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blob42%2Fnvim-dap-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blob42%2Fnvim-dap-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blob42","download_url":"https://codeload.github.com/blob42/nvim-dap-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247321189,"owners_count":20919938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-31T02:08:52.592Z","updated_at":"2025-04-05T10:26:09.206Z","avatar_url":"https://github.com/blob42.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nvim-dap-go\n\nAn extension for [nvim-dap][1] providing configurations for launching go debugger (delve) and debugging individual tests.\n\n## Features\n\n- Auto launch Delve. No configuration needed. You just have to have `dlv` in your path.\n- Run just the closest test from the cursor in debug mode (uses treesitter). See [debugging individual tests](#debugging-individual-tests) section below for more details.\n- Configuration to attach nvim-dap and Delve into a running process and start a debug session.\n- Configuration to start a debug session in the main function.\n- Configuration to run tests in a debug session.\n- Final Delve configuration is resolved when a debug session starts. This allows to use different addresses and ports for each project or launch configs in a project.\n\n## Pre-reqs\n\n- Neovim \u003e= 0.9.0\n- [nvim-dap][1]\n- [delve][2] \u003e= 1.7.0\n\nThis plugin extension make usage of treesitter to find the nearest test to debug.\nMake sure you have the Go treesitter parser installed.\nIf using [nvim-treesitter][3] plugin you can install with `:TSInstall go`.\n\n## Installation\n\n- Install like any other neovim plugin:\n  - If using [vim-plug][4]: `Plug 'leoluz/nvim-dap-go'`\n  - If using [packer.nvim][5]: `use 'leoluz/nvim-dap-go'`\n\n## Usage\n\n### Register the plugin\n\nCall the setup function in your `init.vim` to register the go adapter and the configurations to debug go tests:\n\n```vimL\nlua require('dap-go').setup()\n```\n\n### Configuring\n\nIt is possible to customize nvim-dap-go by passing a config table in the setup function.\n\nThe example below shows all the possible configurations:\n\n```lua\nlua require('dap-go').setup {\n  -- Additional dap configurations can be added.\n  -- dap_configurations accepts a list of tables where each entry\n  -- represents a dap configuration. For more details do:\n  -- :help dap-configuration\n  dap_configurations = {\n    {\n      -- Must be \"go\" or it will be ignored by the plugin\n      type = \"go\",\n      name = \"Attach remote\",\n      mode = \"remote\",\n      request = \"attach\",\n    },\n  },\n  -- delve configurations\n  delve = {\n    -- the path to the executable dlv which will be used for debugging.\n    -- by default, this is the \"dlv\" executable on your PATH.\n    path = \"dlv\",\n    -- time to wait for delve to initialize the debug session.\n    -- default to 20 seconds\n    initialize_timeout_sec = 20,\n    -- a string that defines the port to start delve debugger.\n    -- default to string \"${port}\" which instructs nvim-dap\n    -- to start the process in a random available port.\n    -- if you set a port in your debug configuration, its value will be\n    -- assigned dynamically.\n    port = \"${port}\",\n    -- additional args to pass to dlv\n    args = {},\n    -- the build flags that are passed to delve.\n    -- defaults to empty string, but can be used to provide flags\n    -- such as \"-tags=unit\" to make sure the test suite is\n    -- compiled during debugging, for example.\n    -- passing build flags using args is ineffective, as those are\n    -- ignored by delve in dap mode.\n    -- avaliable ui interactive function to prompt for arguments get_arguments\n    build_flags = {},\n    -- whether the dlv process to be created detached or not. there is\n    -- an issue on Windows where this needs to be set to false\n    -- otherwise the dlv server creation will fail.\n    -- avaliable ui interactive function to prompt for build flags: get_build_flags\n    detached = vim.fn.has(\"win32\") == 0,\n    -- the current working directory to run dlv from, if other than\n    -- the current working directory.\n    cwd = nil,\n  },\n  -- options related to running closest test\n  tests = {\n    -- enables verbosity when running the test.\n    verbose = false,\n  },\n}\n```\n\n### Use nvim-dap as usual\n\n- Call `:lua require('dap').continue()` to start debugging.\n- All pre-configured debuggers will be displayed for you to choose from.\n- See `:help dap-mappings` and `:help dap-api`.\n\n### Debugging individual tests\n\nTo debug the closest method above the cursor use you can run:\n\n- `:lua require('dap-go').debug_test()`\n\nOnce a test was run, you can simply run it again from anywhere:\n\n- `:lua require('dap-go').debug_last_test()`\n\nIt is better to define a mapping to invoke this command. See the mapping section below.\n\n### Debugging with command-line arguments\n\n1. Select the option `Debug (Arguments)`\n1. Enter each argument separated by a space (i.e. `option1 option2 option3`)\n1. Press enter\n\n![Start Debug Session with Arguments](./images/image1.png \"Start Debug Session with Arguments\")\n![Enter Arguments](./images/image2.png \"Enter Arguments\")\n![Begin Debugging](./images/image3.png \"Being Debugging\")\n\n### Debugging with build flags\n\n1. Register a new option to debug with build flags:\n\n```lua\nrequire('dap-go').setup {\n  dap_configurations = {\n    {\n      type = \"go\",\n      name = \"Debug (Build Flags)\",\n      request = \"launch\",\n      program = \"${file}\",\n      buildFlags = require(\"dap-go\").get_build_flags,\n    },\n  },\n})\n```\n\n2. To prompt for both build flags and arguments register the following:\n\n```lua\nrequire(\"dap-go\").setup({\n    dap_configurations = {\n        {\n            type = \"go\",\n            name = \"Debug (Build Flags \u0026 Arguments)\",\n            request = \"launch\",\n            program = \"${file}\",\n            args = require(\"dap-go\").get_arguments,\n            buildFlags = require(\"dap-go\").get_build_flags,\n        },\n    }\n})\n```\n\n3. To create a custom debugging configuration that requires an interactive prompt the following functions can be\n   attached to the args and buildFlags fields of dap_configurations.\n   - `require('dap-go').get_arguments`\n   - `require('dap-go').get_buid_flags`\n\n### Debugging with dlv in headless mode\n\n1. Register a new option to attach to a remote debugger:\n\n```lua\nlua require('dap-go').setup {\n  dap_configurations = {\n    {\n      type = \"go\",\n      name = \"Attach remote\",\n      mode = \"remote\",\n      request = \"attach\",\n    },\n  },\n}\n```\n\n1. Start `dlv` in headless mode. You can specify subcommands and flags after `--`, e.g.,\n\n```sh\ndlv debug -l 127.0.0.1:38697 --headless ./main.go -- subcommand --myflag=xyz\n```\n\n1. Call `:lua require('dap').continue()` to start debugging.\n1. Select the new registered option `Attach remote`.\n\n## Mappings\n\n```vimL\nnmap \u003csilent\u003e \u003cleader\u003etd :lua require('dap-go').debug_test()\u003cCR\u003e\n```\n\n## VSCode launch config\n\nDefining the Go debug configurations for all your projects inside your Neovim configuration can be cumbersome and quite strict.\nFor more flexibility, `nvim-dap` supports the use of the VSCode launch configurations.\n\nThat allows for example to set the Delve port dynamically when you run a debug session. If you create this file in your project (`[root_project]/.vscode/launch.json`):\n\n```json\n{\n    \"version\": \"0.2.0\",\n    \"configurations\": [\n        {\n            \"name\": \"Remote debug API server\",\n            \"type\": \"go\",\n            \"request\": \"attach\",\n            \"mode\": \"remote\",\n            \"port\": 4444,\n            \"host\": \"127.0.0.1\",\n            \"substitutePath\": [\n                {\n                    \"from\": \"${workspaceFolder}\", \"to\": \"/usr/src/app\"\n                }\n            ]\n        }\n    ]\n}\n```\n\nA debug session `Remote debug API server` will appear in the choices, and the Delve port will be dynamically set to `4444`.\nThe current version of nvim-dap always loads the file if it exists.\n\nPlease see `:h dap-launch.json` for more information.\n\n## Acknowledgement\n\nThanks to the [nvim-dap-python][6] for the inspiration.\n\n[1]: https://github.com/mfussenegger/nvim-dap\n[2]: https://github.com/go-delve/delve\n[3]: https://github.com/nvim-treesitter/nvim-treesitter\n[4]: https://github.com/junegunn/vim-plug\n[5]: https://github.com/wbthomason/packer.nvim\n[6]: https://github.com/mfussenegger/nvim-dap-python\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblob42%2Fnvim-dap-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblob42%2Fnvim-dap-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblob42%2Fnvim-dap-go/lists"}