最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - Delve debugger splits command line argument by space - Stack Overflow

programmeradmin1浏览0评论

I have a program that is supposed to take a json string as input and run some token extraction logic. I'm using flag.Parse to parse the string from the command line and this works when i run the compiled file. However, when i try to debug the code in neovim with delve through nvim-dap-go my string is split by space into separate arguments.

I'm at a loss for how to fix this because everything works fine from the command line. It only breaks when i run it with the debugger.

Here's the source code:

import (
    "flag"
    "io"
    "os"
)
var fileName string
var buf *bytes.Buffer = bytes.NewBuffer(make([]byte, 0))
    flag.StringVar(&fileName, "file", "", "Path to JSON file")
    flag.Parse()
    if fileName != "" {

        openFile, err := os.Open(fileName)

        _, copyErr := io.Copy(buf, openFile)
        handleError("Unable to read file ", err)
        handleError("Error opening file "+fileName, copyErr)
        defer openFile.Close()
    } else if jsonString := flag.Arg(0); jsonString == "" && fileName == "" {
        _, err := io.Copy(buf, os.Stdin)
        handleError("Unable to read from Stdin", err)
    } else {
        buf = bytes.NewBufferString(jsonString)
    }
    Lex(buf)

So if I pass the string '{"key": "value"}' to the program, it's split into '{"key": and "value"}' and so i get only the first half saved into jsonString

Here's my nvim-dap-go config:

return {

  "mfussenegger/nvim-dap",
  dependencies = {
    "rcarriga/nvim-dap-ui",
    "mfussenegger/nvim-dap-python",
    "nvim-neotest/nvim-nio",
    "theHamsta/nvim-dap-virtual-text",
    "leoluz/nvim-dap-go",
  },
  config = function()
    local dap, dapui = require("dap"), require("dapui")
    local mason_path = vim.fn.glob(vim.fn.stdpath("data") .. "/mason/")

    require("mason-nvim-dap").setup()
    require("dap-go").setup()
    require("nvim-dap-virtual-text").setup({})
    dapui.setup()
    dap.listeners.before.attach.dapui_config = function()
      dapui.open()
    end
    dap.listeners.before.launch.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

    vim.keymap.set("n", "<leader>dc", function()
      dap.continue()
    end, { desc = "continue debugger" })

    vim.keymap.set("n", "<leader>dk", function()
      dap.close()
    end, { desc = "kill debugger" })
    vim.keymap.set("n", "<leader>du", function()
      require("dapui").toggle({ reset = true })
    end, { desc = "Toggle UI" })

    vim.keymap.set("n", "<Leader>dso", function()
      dap.step_over()
    end, { desc = "Step over" })
    vim.keymap.set("n", "<Leader>dsi", function()
      dap.step_into()
    end, { desc = "Step in" })
    vim.keymap.set("n", "<Leader>dso", function()
      dap.step_out()
    end, { desc = "Step out" })

    vim.keymap.set("n", "<Leader>db", function()
      dap.toggle_breakpoint()
    end, { desc = "Toggle breakpoint" })
    vim.keymap.set("n", "<Leader>B", function()
      dap.set_breakpoint()
    end, { desc = "Set breakpoint" })
    vim.keymap.set("n", "<Leader>lp", function()
      dap.set_breakpoint(nil, nil, vim.fn.input("Log point message: "))
    end, { desc = "Log point message" })
    -- vim.keymap.set("n", "<Leader>dr", function()
    --   dap.repl.open()
    -- end, { desc = "Open REPL" })
    vim.keymap.set("n", "<Leader>dl", function()
      dap.run_last()
    end, { desc = "Run last" })
    vim.keymap.set({ "n", "v" }, "<Leader>dh", function()
      require("dap.ui.widgets").hover()
    end, { desc = "Hover" })
    vim.keymap.set({ "n", "v" }, "<Leader>dp", function()
      require("dap.ui.widgets").preview()
    end, { desc = "Preview" })
    vim.keymap.set("n", "<Leader>df", function()
      local widgets = require("dap.ui.widgets")
      widgets.centered_float(widgets.frames)
    end, { desc = "Open Frames" })
    vim.keymap.set("n", "<Leader>ds", function()
      local widgets = require("dap.ui.widgets")
      widgets.centered_float(widgets.scopes)
    end, { desc = "Open Scopes" })
  end,
}

Here are screenshots from the debugger vs the command line:

command line arguments and print out

delve arguments

delve print out

I would really appreciate if someone could tell me what I'm doing wrong and how to fix it. Thanks!!

发布评论

评论列表(0)

  1. 暂无评论