Restarting A Neovim LSP When Changing Buffers

This currently only works some times. Not sure yet, but I think if the client crashes it doesn't restart properly. Leaving this hear as a work in progress

I'm working on a LSP for my custom file format. I set up the following autocmd and added it to my nvim.lua config file to restart the LSP everytime I enter the buffer for a .tmp file (which is what I'm testing with for now)

Code

vim.api.nvim_create_autocmd("BufEnter", {
  pattern = "*.tmp",
  group = vim.api.nvim_create_augroup("LSPTestRestartGroup", { clear = true }),
  callback = function() 
    print("restarting lsp")
    vim.lsp.stop_client(vim.lsp.get_active_clients())
    vim.lsp.start({
      name = 'neopolitan',
      cmd = {'/Users/alan/workshop/neopolitan-lsp/target/debug/nrs-language-server'},
      root_dir = vim.fs.dirname(vim.fs.find({'Cargo.toml'}, { upward = true })[1]),
    })
  end,
})

References