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

Automatic setup doesn't work with codelldb on Windows #51

Closed
aosterhage opened this issue Mar 23, 2023 · 12 comments · Fixed by #54
Closed

Automatic setup doesn't work with codelldb on Windows #51

aosterhage opened this issue Mar 23, 2023 · 12 comments · Fixed by #54

Comments

@aosterhage
Copy link
Contributor

aosterhage commented Mar 23, 2023

I want to preface by saying that I assume this will be the case for all adapters installed via mason.nvim on Windows.

I've installed codelldb via mason.nvim and done the typical setup:

require('mason').setup({})
require('mason-nvim-dap').setup({
  automatic_setup = true
})
require('mason-nvim-dap').setup_handlers({})

This does configure dap.adapters.codelldb but with the following values:

{
  executable = {
    args = { "--port", "${port}" },
    command = "codelldb",
    detached = false
  },
  port = "${port}",
  type = "server"
}

Running :DapContinue will prompt nvim-dap to ask for the executable location and once provided will error out on:

.../.local/share/nvim-data/lazy/nvim-dap/lua/dap/session.lua:1153: ENOENT: no such file or directory

Enabling DEBUG level logs in nvim-dap shows:

[ DEBUG ] 2023-03-23T15:37:56Z-0400 ] ....local/share/nvim-data/lazy/nvim-dap/lua/dap/session.lua:1120 ]	"Starting debug adapter server executable"	{
  args = { "--port", "${port}" },
  command = "codelldb",
  detached = false
}

which is the same configuration that mason-nvim-dap.nvim has setup via automatic_setup. The issue is that, for whatever reason, codelldb is not recognized as a file but codelldb.cmd is since this is the file actually installed by mason:

> ls ~/.local/share/nvim-data/mason/bin/
codelldb.cmd

If I modify the configuration like so:

:lua require('dap').adapters.codelldb.executable.command = 'codelldb.cmd'

then everything works great. But this defeats the purpose of automatic_setup for me.

I've been saying that I'm on Windows because I'm assuming its important but I don't actually know yet as I haven't tested on Linux (which I can if needed). I'm assuming Linux works or someone would have noticed by now.

@aosterhage
Copy link
Contributor Author

aosterhage commented Mar 24, 2023

I did some digging and I have an inclination of what to do but I'm new to all of the mason code and ecosystem so I'm not 100% sure this is the best approach.

In mappings/adapters.lua, change the command assignment for each adapter's executable table to utilize vim.fn.exepath. So for the codellb adapter:

command = vim.fn.exepath('codelldb'),

I did test this change and the automatic setup works perfectly now. Does anyone know if vim.fn.exepath is the proper way to get the executable location from a mason installed package?

@aosterhage
Copy link
Contributor Author

aosterhage commented Mar 24, 2023

Looks like mason-lspconfig.nvim uses vim.fn.exepath but only for Windows platforms:

            if platform.is.win and (config.cmd and config.cmd[1] ~= "cmd.exe") then
                local exepath = vim.fn.exepath(config.cmd[1])
                if exepath ~= "" then
                    config.cmd[1] = exepath
                else
                    log.error("Failed to expand cmd path", config.name, config.cmd)
                end
            end

Here's a similar issue on mason.nvim with some related details.

@aosterhage
Copy link
Contributor Author

I tested out vim.fn.exepath() on both Windows and Linux and they both returned the ~/.local/share/nvim/mason/bin/codelldb file, the only difference was that Windows includes the .CMD extension. So it seems like this could be fixed by just modifying the command for every adapter to be vim.fn.exepath(<adapter>).

aosterhage pushed a commit to aosterhage/dotfiles that referenced this issue Mar 24, 2023
Adds several plugins to Neovim configuration to support the Debug
Adapter Protocol (DAP). This enables VSCode-like debugging within
Neovim. Debug adapters can be installed via :Mason.

Note: there is currently a bug with mason-nvim-dap's automatic_setup.
Debug adapters installed via :Mason are not setup properly on Windows
(see jay-babu/mason-nvim-dap.nvim#51). Until
this is fixed, a workaround is to add 'vim.fn.exepath()' to the
'command' section of adapters.lua within the mason-nvim-dap source.
@jay-babu
Copy link
Owner

@aosterhage, do you mind creating a PR for codellb cmd changes? If everything works fine for a week or 2, this can be expanded to all other adapters. I am out of town otherwise I would do it right now

aosterhage pushed a commit to aosterhage/mason-nvim-dap.nvim that referenced this issue Mar 28, 2023
Mason.nvim utilizes symlinks to organize package files. On Windows
symlink support is complicated and so mason chooses to use .cmd script
files to simplify things. Setting "command" to "codelldb" will only
look for files with the name "codelldb", i.e. it will not find
"codelldb.cmd".

This commit changes "command" to use vim.fn.exepath() which seems to
find any executable file with an equivalent stem. This means it will
find both files "codelldb" and "codelldb.cmd".

Change-Id: Ibd8d63a8d4e4ef9f3744db7eda1c532226484921
aosterhage pushed a commit to aosterhage/mason-nvim-dap.nvim that referenced this issue Mar 28, 2023
Mason.nvim utilizes symlinks to organize package files. On Windows
symlink support is complicated and so mason chooses to use .cmd script
files to simplify things. Setting "command" to "codelldb" will only
look for files with the name "codelldb", i.e. it will not find
"codelldb.cmd".

This commit changes "command" to use vim.fn.exepath() which seems to
find any executable file with an equivalent stem. This means it will
find both files "codelldb" and "codelldb.cmd".

Change-Id: I09b185aaa5d5141e304e8a80fe8ea0ab08c03f17
aosterhage pushed a commit to aosterhage/mason-nvim-dap.nvim that referenced this issue Mar 28, 2023
Mason.nvim utilizes symlinks to organize package files. On Windows
symlink support is complicated and so mason chooses to use .cmd script
files to simplify things. Setting "command" to "codelldb" will only
look for files with the name "codelldb", i.e. it will not find
"codelldb.cmd".

This commit changes "command" to use vim.fn.exepath() which seems to
find any executable file with an equivalent stem. This means it will
find both files "codelldb" and "codelldb.cmd".
@aosterhage
Copy link
Contributor Author

Opened #52 with the fix.

I tested on Windows using a fresh configuration (deleted both nvim and nvim-data directories) and then used this as init.lua:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not 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)

require("lazy").setup({
  "williamboman/mason.nvim",
  "mfussenegger/nvim-dap",
  "aosterhage/mason-nvim-dap.nvim"
})

require("mason").setup({})
require("mason-nvim-dap").setup({
  automatic_setup = true
})
require("mason-nvim-dap").setup_handlers({})

After a :Mason install of codelldb, :DapContinue was able to start successfully using codelldb.

aosterhage pushed a commit to aosterhage/mason-nvim-dap.nvim that referenced this issue Mar 28, 2023
Mason.nvim utilizes symlinks to organize package files. On Windows
symlink support is complicated and so mason chooses to use .cmd script
files to simplify things. Setting "command" to "codelldb" will only
look for files with the name "codelldb", i.e. it will not find
"codelldb.cmd".

This commit changes "command" to use vim.fn.exepath() which seems to
find any executable file with an equivalent stem. This means it will
find both files "codelldb" and "codelldb.cmd".
@aosterhage
Copy link
Contributor Author

I replicated the same test as my previous comment in Linux and everything continued working as expected.

@jay-babu
Copy link
Owner

Thanks! Do you think it's a good idea to let this change boil for some time before applying everywhere?

@aosterhage
Copy link
Contributor Author

aosterhage commented Mar 28, 2023

I think either way is fine, waiting or not. I don't see much that can go wrong:

Something we should talk about is if there are any other implications of using exepath. It seems to me that exepath will find any executable in your path with the provided name.

If I look at my PATH in Neovim it looks like mason prepends its bin directory first so theoretically any call to exepath should resolve to the mason version first. But if you don't have any mason version installed and you do have one in your system PATH then exepath will happily use the system version. This doesn't feel like a huge issue since mason-nvim-dap only sets up configurations if an equivalent adapter is installed via mason.

If for some reason we want to specifically target only the mason binaries then we can do something like this:

require('mason-registry').get_package('codelldb'):get_install_path() .. '/extension/adapter/codelldb'

But I don't really like diving into the mason package paths when it provides a nice stdpath('data')/mason/bin for us to access.

@aosterhage
Copy link
Contributor Author

One more thing to consider before applying this to other adapters: do we want to only do this for Windows like mason-lspconfig with a if platform.is.win?

@jay-babu
Copy link
Owner

jay-babu commented Mar 30, 2023

This can be expanded to use exepath for all places. I am not worried about if enforcing the mason dependency being installed. It has to be otherwise it won't work anyways. I can create a PR next week unless you want to. Thanks for finding this!

No need to lock down to windows.

@jay-babu
Copy link
Owner

Thanks for this!

@aosterhage
Copy link
Contributor Author

Thank you for the plugin!

This was referenced Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants