Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ramboman opened this issue Jun 8, 2024 · 6 comments
Closed

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

ramboman opened this issue Jun 8, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@ramboman
Copy link

ramboman commented Jun 8, 2024

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

  • Start nvim so it setups everything
  • Close nvim
  • Open the file containing the code to debug:
$ nvim "~/.config/nvim/lua/do_something.lua"
  • Place a break point in the add_some function
  • Start a nvim instance to debug:
$ NVIM_DEBUG=y nvim

Expected course of action

  • The debugged nvim instance launchs osv
  • It waits for a key press
  • I press <F4> in the client nvim to open the debug ui
  • I press <F9> so the dap connects to the debugged nvim
  • I press any key in the debugged nvim
  • The debugged nvim stops at the break points so I can start debugging

Actual course of action:

  • The debugged nvim prints this error message:
Server failed to launch on port 8086

Questions

  • How can I launch osv properly at the beginning of a configuration so I can debug it?
@jbyuki

This comment was marked as outdated.

@ramboman
Copy link
Author

I made some modifications to my debug code:

  • The nvim debug environment variable is now NVIM_CONFIG_DEBUG
  • I temporarily change NVIM_CONFIG_DEBUG to something that is not "y" before launching osv and reset it back to its original value right after.

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
Copy link
Owner

jbyuki commented Jun 12, 2024

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
Copy link

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

@jbyuki jbyuki added the enhancement New feature or request label Jul 10, 2024
jbyuki added a commit that referenced this issue Aug 1, 2024
@jbyuki
Copy link
Owner

jbyuki commented Aug 1, 2024

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 jbyuki closed this as completed Aug 1, 2024
@jbyuki
Copy link
Owner

jbyuki commented Aug 1, 2024

Here it is in action.

output.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants