Nvim_set_keymap doesn't work in function

Hi,
I have the following code to set up my keybinds.

local key = require("which-key")
key.setup({
})

local function map(label, mode, input, command, opts)
   vim.api.nvim_set_keymap(
      mode,
      input,
      command,
      opts
   )

   local which_key_map = {}
   which_key_map[input] = {
      command,
      label
   }

   local which_key_opts = opts
   opts.mode = mode

   key.register(which_key_map, which_key_opts)
end

map("Find File", "n", "<leader>f", "<cmd>Telescope find_files<cr>", {})
map("Save", "n", "<leader>w", "<cmd>w<CR>", {})

map("Run", "n", "<leader>r", "", {
   callback = function()
      print("A")
   end
})

vim.api.nvim_set_keymap(
   "n",
   "E",
   "$",
   {noremap = true}
)

When I press r, it should just print A. However, nothing happens. The weird thing is that the other two keybindings work, and the <leader>r keybinding works when I don’t use the map() function:

vim.api.nvim_set_keymap("n", "<leader>r", "", {
   callback = function()
      print("A")
   end
})

works perfectly fine.

I can’t see why this would be the case since map() should be a 1-to-1 wrapper for nvim_set_keymap. Then again, I know lua has some eccentricities so I was hoping someone more experienced could help me out with this.

I honestly don’t know, BUT, using vim.keymap.set is the recommend way to do it nowadays.

It works fine here. Are you sure it’s not printing A? Did you check :message?