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

Question: How to correctly use dap-lldb plugin? #2794

Open
i-ilak opened this issue Jan 5, 2025 · 2 comments
Open

Question: How to correctly use dap-lldb plugin? #2794

i-ilak opened this issue Jan 5, 2025 · 2 comments

Comments

@i-ilak
Copy link

i-ilak commented Jan 5, 2025

I just saw that there is now a plugin for nixvim dap called dap-lldb, which should make it easier to setup debug configurations for C/C++/Rust.

My setup so far looked something like this:

{ config
, lib
, pkgs
, ...
}:
let
  codelldb-config = {
    name = "Launch (CodeLLDB)";
    type = "codelldb";
    request = "launch";
    program.__raw = ''
      function()
          return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. '/', "file")
      end
    '';
    cwd = ''''${workspaceFolder}'';
    stopOnEntry = false;
  };

  lldb-config = {
    name = "Launch (LLDB)";
    type = "lldb";
    request = "launch";
    program.__raw = ''
      function()
          return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. '/', "file")
      end'';
    cwd = ''''${workspaceFolder}'';
    stopOnEntry = false;
  };
in
{
  extraPackages = with pkgs;
    [
      coreutils
      lldb_18
    ];

  plugins = {
    dap = {
      enable = true;

      adapters = {
        executables = {
          lldb = {
            command = lib.getExe' pkgs.lldb "lldb-vscode";
          };
        };

        servers = {
          codelldb = {
            port = 13000;
            executable = {
              command = "${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb";
              args = [
                "--port"
                "13000"
              ];
            };
          };
        };
      };

      configurations = {
        c = [ lldb-config ];

        cpp =
          [ lldb-config ]
          ++ lib.optionals pkgs.stdenv.isLinux [
            codelldb-config
          ];

        rust =
          [ lldb-config ]
          ++ lib.optionals pkgs.stdenv.isLinux [
            codelldb-config
          ];
      };

      extensions = {
        dap-ui = {
          enable = true;
        };

        dap-virtual-text = {
          enable = false;
        };
      };
  };
}

This worked okey-ish, but was a bit cumbersome. So the idea was to switch now to this plugging which hopefully should make this easier. The documentation can be found here and I guess the interesting part is also this subpage.

To be honest, Im not entirely sure how to use this plugin correctly… I tried the following:

{ config
, lib
, pkgs
, ...
}:
{
  extraPackages = with pkgs;
    [
      coreutils
      lldb_18
    ];

  plugins = {
    dap = {
      enable = true;

      adapters = {
        executables = {
          lldb = {
            command = lib.getExe' pkgs.lldb "lldb-vscode";
          };
        };

        servers = {
          codelldb = {
            port = 13000;
            executable = {
              command = "${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb";
              args = [
                "--port"
                "13000"
              ];
            };
          };
        };
      };

      configurations = {
      };

      extensions = {
        dap-ui = {
          enable = true;
        };

        dap-virtual-text = {
          enable = false;
        };
      };
    };
    dap-lldb = {
      enable = true;
      settings.configurations = {
        rust = [
          {
            cwd = "$\${workspaceFolder}";
            name = "Debug";
            program = {
              __raw = ''
                function(selection)
                   local targets = list_targets(selection)
              
                   if targets == nil then
                      return nil
                   end
              
                   if #targets == 0 then
                      return read_target()
                   end
              
                   if #targets == 1 then
                      return targets[1]
                   end
              
                   local options = { "Select a target:" }
              
                   for index, target in ipairs(targets) do
                      local parts = vim.split(target, sep, { trimempty = true })
                      local option = string.format("%d. %s", index, parts[#parts])
                      table.insert(options, option)
                   end
              
                   local choice = vim.fn.inputlist(options)
              
                   return targets[choice]
                end
            '';
          };
          request = "launch";
          stopOnEntry = false;
          type = "lldb";
          }
        ]
      }
    };
  };
}

but:

  • This does not work. Trying to run a Rust application with the debugger results in a complaint that I need to register a dap.configurations.rust entry, and
  • This does not seem to be much more simple than writing in the original way…

So, in short, how would a correct example config look like for dap-lldb?

@GaetanLepage
Copy link
Member

Thanks for this detailed report.
Regarding the conciseness, it's quite normal that it remains large. Indeed, here we are thinly wrapping the plugin configuration options. This means that there is no real reason for the nixvim config to be shorter than its equivalent in lua.

I don't use dap myself, so I'm not sure that I can help there. However, you can start by checking the generated lua configuration using nixvim-print-init.

@i-ilak
Copy link
Author

i-ilak commented Jan 6, 2025

@GaetanLepage Sorry for the misunderstanding, I was not particularly clear in my second question: I think it makes total sense that the configuration in nix needs to mirror the code and options in lua. What is not clear to me is how dap-lldb is different to dap in general. I understood dap-lldb as a tool to make configuring lldb based debugging easier than with dap itself, but looking at the configuration examples in the docs, its not clear to me if it is...

Or, I'm of course just misunderstanding how it is intended to be used.

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

No branches or pull requests

2 participants