diff --git a/utils/nvim/carbon.lua b/utils/nvim/carbon.lua index 7adf10b4a4f8..204d838dea30 100644 --- a/utils/nvim/carbon.lua +++ b/utils/nvim/carbon.lua @@ -19,8 +19,20 @@ function find_git_ancestor(startpath) end local root = find_git_ancestor(vim.env.PWD); -vim.lsp.config("carbon", { - cmd = { root .. "/bazel-bin/toolchain/carbon", "language-server" }, - filetypes = { "carbon" }, -}) -vim.lsp.enable("carbon") +toolchain_path = nil +if root and vim.uv.fs_stat(root .. "/bazel/carbon_rules") then + -- when we're in the carbon repo, use the toolchain from there + -- (note that this won't update if one `cd`s into the carbon repo) + toolchain_path = root .. "/bazel-bin/toolchain/carbon" +elseif vim.fn.executable("carbon") then + -- otherwise, check if it's on the path + toolchain_path = "carbon" +end + +if toolchain_path then + vim.lsp.config("carbon", { + cmd = { toolchain_path, "language-server" }, + filetypes = { "carbon" }, + }) + vim.lsp.enable("carbon") +end