jbyuki / one-small-step-for-vimkind

Debug adapter for Neovim plugins
MIT License
396 stars 10 forks source link

Cannot launch `osv` at the beginning of a neovim configuration #47

Closed ramboman closed 2 months ago

ramboman commented 4 months ago

I would like to debug some code, in my neovim configuration, that executes at the start of nvim.

Setup

I made and reduced this setup solely for the purpose of reproducing the problem

OS: Debian 12 NVIM version: v0.10.0 NVIM package manager: lazy.nvim

~/.config/nvim/init.lua:

local lazydir = vim.fn.stdpath("data") .. "/lazy"

-- Debug
local osvpath = lazydir .. "/one-small-step-for-vimkind"
if (vim.uv or vim.loop).fs_stat(osvpath) then
  local nvim_debug = vim.fn.getenv("NVIM_DEBUG")
  if nvim_debug ~= vim.NIL and nvim_debug == "y" then
    vim.opt.rtp:prepend(osvpath)
    require("osv").launch({ port = 8086 })
    vim.print("Press any key to continue")
    vim.fn.getchar()
  end
end

-- Code to debug
local some_number = require("do_something").add_some(1)

-- lazy.nvim bootstrap
local lazypath = lazydir .. "/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

vim.g.mapleader = " " -- Make sure to set `mapleader` before lazy so your mappings are correct
vim.g.maplocalleader = "\\" -- Same for `maplocalleader`

-- Keybindings
vim.api.nvim_set_keymap('n', '<F4>', [[:lua require"dapui".toggle({})<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<F8>', [[:lua require"dap".toggle_breakpoint()<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<F9>', [[:lua require"dap".continue()<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<F10>', [[:lua require"dap".step_over()<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<S-F10>', [[:lua require"dap".step_into()<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<F12>', [[:lua require"dap.ui.widgets".hover()<CR>]], { noremap = true })
vim.api.nvim_set_keymap('n', '<F5>', [[:lua require"osv".launch({port = 8086})<CR>]], { noremap = true })

-- Install packages
require("lazy").setup({
  { "mfussenegger/nvim-dap", },
  {
    "rcarriga/nvim-dap-ui",
    dependencies = {
      "mfussenegger/nvim-dap",
      "nvim-neotest/nvim-nio",
    },
    keys = {
    },
    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
  },
  {
    "jbyuki/one-small-step-for-vimkind",
    dependencies = { "mfussenegger/nvim-dap", },
    config = function()
      local dap = require("dap")
      dap.configurations.lua = {
        {
          type = "nlua",
          request = "attach",
          name = "Attach to running Neovim instance",
        },
      }
      dap.adapters.nlua = function(callback, conf)
        callback({
          type = "server",
          host = conf.host or "127.0.0.1",
          port = conf.port or 8086,
        })
      end
    end,
  },
})

~/.config/nvim/lua/do_something.lua:

local M = {
  add_some = function(a)
    local b = a + 1
    local c = b + 1
    local d = c + 1
    return d
  end,
}

return M

The general idea

I added a debug code (-- Debug) at the beginning of ~/.config/nvim/init.lua. I would like this code to launch osv if nvim was started with the environment variable NVIM_DEBUG equal "y". Right after osv is launched, nvim has to wait for a key press, so I have the time to setup the debugger on the other nvim. After I press any key on the debugged nvim, it has to continue its execution until it reaches a breakpoint.

How to reproduce the problem

Expected course of action

Actual course of action:

Questions

jbyuki commented 4 months ago

Thank you for opening an issue.

This is expected as launch() will spawn a headless neovim instance, and this headless instance will by means of theinit.lua launch itself again, resulting in an recursive launch which fails. To use launch directly in the init.lua, it requires a check so that that doesn't happen. This is currently in discussion in the open issue #43.

For an immediate solution, I would:

  1. Launch a neovim instance without loading init.lua. With -u NORC, or -u NONE.
  2. Add the path to the plugins folder (packpath, runtimetimepath, rtp)
  3. Launch osv with launch()
  4. Connect with another instance
  5. Manually source init.lua

I'm aware this is less than ideal so a better solution is welcome, or any proposition.

ramboman commented 3 months ago

I made some modifications to my debug code:

Here is the code:

local lazydir = vim.fn.stdpath("data") .. "/lazy"

-- Debug
local osvpath = lazydir .. "/one-small-step-for-vimkind"
if (vim.uv or vim.loop).fs_stat(osvpath) then
  local nvim_config_debug = vim.env.NVIM_CONFIG_DEBUG
  if nvim_config_debug ~= vim.NIL and nvim_config_debug == "y" then
    vim.opt.rtp:prepend(osvpath)
    vim.env.NVIM_CONFIG_DEBUG = ""
    require("osv").launch({ port = 8086 })
    vim.env.NVIM_CONFIG_DEBUG = nvim_config_debug
    vim.print("Press any key to continue")
    vim.fn.getchar()
  end
end

Now I can start the debugger at the beginning of the config.

I will leave this issue open since, osv, itself, alone, cannot be started at the beginning of the configuration. This is a hack to avoid this problem.

Thank you @jbyuki!

jbyuki commented 3 months ago

Nice one!

Yes, this is an on-going issue that needs better thought. Leaving it open will remind me/others that it's a problem that needs solving. Glad that it worked in your case for now.

blakedietz commented 3 months ago

I just ran into this very same issue the other day. Thanks for the recommendation @ramboman.

jbyuki commented 2 months ago

A more elegant way to debug configuration files has been added. The help file goes into more details on how it is done.

doc/osv.txt

I will close this for now as a more permanent solution has been now added to the plugin.

jbyuki commented 2 months ago

Here it is in action.

https://github.com/user-attachments/assets/ea3e5a55-f6f9-4149-a94f-c444b7e8f4e5