You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

nvim配置tofu_ls报错:config未找到,请求协助排查

排查Neovim LSP中tofu_ls配置报错问题

报错信息:config "tofu_ls" not found. Ensure it is listed in config.md or added as a custom server

已完成的操作:

  • 安装tofu-ls服务且其已在系统路径中
  • 在nvim-lspconfig、mason-lspconfig中完成对应配置
  • 确认nvim-lspconfig官方支持tofu_ls

我的LSP配置

return {
  "neovim/nvim-lspconfig",
  event = { "BufReadPost", "BufWritePost", "BufNewFile" },
  branch = "master",
  dependencies = {
    "j-hui/fidget.nvim",
    "saghen/blink.cmp",
  },
  opts = {
    servers = {
      lua_ls = {},
      protols = {},
      postgres_lsp = {},
      ts_ls = {},
      tofu_ls = {
        cmd = { "tofu-ls", "serve" },
        filetypes = { "opentofu", "opentofu-vars" },
        root_markers = { ".terraform", ".git" },
      },
      svelte = {},
      emmet_language_server = {},
    },
  },
  keys = {
    { "<leader>rn", vim.lsp.buf.rename, mode = "n", desc = "Lsp Rename" },
    { "<leader>ca", vim.lsp.buf.code_action, mode = { "n", "v" }, desc = "Lsp Code Action" },
    { "<leader>gD", vim.lsp.buf.declaration, mode = "n", desc = "Go To Declaration" },
    { "<leader>gd", vim.lsp.buf.definition, mode = "n", desc = "Go To Definition" },
    { "<leader>gi", vim.lsp.buf.implementation, mode = "n", desc = "Go To Implementation" },
    { "<leader>gr", vim.lsp.buf.references, mode = "n", desc = "Go To References" },
    { "<leader>D", vim.lsp.buf.type_definition, mode = "n", desc = "Type Definition" },
    { "<C-k>", vim.lsp.buf.signature_help, mode = "n", desc = "Signature Help" },
    {
      "<leader>ih",
      function()
        vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
      end,
      mode = "n",
      desc = "Inlay Hints",
    },
  },
  config = function(_, opts)
    vim.diagnostic.config({
      virtual_text = true,
      float = {
        focusable = false,
        style = "minimal",
        border = "rounded",
        source = "always",
        header = "",
        prefix = "",
      },
    })

    local lspconfig = require("lspconfig")
    local cmp = require("blink.cmp")

    for server, config in pairs(opts.servers) do
      config.capabilities = cmp.get_lsp_capabilities(config.capabilities)
      lspconfig[server].setup(config)
    end

    lspconfig.rust_analyzer.setup({
      capabilities = cmp.get_lsp_capabilities(),
      settings = {
        ["rust-analyzer"] = {
          check = {
            command = "clippy",
          },
          checkOnSave = true,
          diagnostics = {
            enable = true,
          },
          inlayHints = {
            enable = true,
            parameterHints = true,
            typeHints = true,
            chainingHints = true,
          },
          hover = {
            actions = {
              enable = true,
            },
          },
          cargo = {
            allFeatures = true, -- Enable all features for cargo
          },
          procMacro = {
            enable = true, -- Enable proc-macro support
          },
        },
      },
    })
  end,
}

Mason配置

return {
  "mason-org/mason.nvim",
  keys = {
      {"<leader>cm", "<cmd>Mason<cr>", desc = "Mason" },
  },
  opts = {
      ui = {
          border = "rounded",
          package_installed = "✓",
          package_pending = "➜",
          package_uninstalled = "x",
      },
  },
}

Mason-lspconfig配置

return {
  "mason-org/mason-lspconfig.nvim",
  opts = {
    ensure_installed = {
      "lua_ls",
      "yamlls",
      "protols",
      "postgres_lsp",
      "ts_ls",
      "tofu_ls",
      "svelte",
      "dockerls",
      "emmet_language_server",
    },
    automatic_enable = true,
  },
}

排查建议

  1. 验证nvim-lspconfig中的tofu_ls配置是否加载
    在Neovim命令行执行print(require("lspconfig.configs").tofu_ls),如果输出nil,说明该配置未被正确注册。可以尝试在lspconfig的config函数开头手动加载:

    require("lspconfig.configs").tofu_ls = require("lspconfig.server_configurations.tofu_ls")
    

    重启Neovim后再测试。

  2. 检查nvim-lspconfig版本
    当前指定了branch = "master",可能存在分支代码未同步的情况。可以切换到最新稳定tag版本,或者执行:LspLog查看是否有版本相关的加载错误。

  3. 确认Mason与lspconfig的包名映射
    打开:Mason界面搜索tofu相关包,确认安装的包名是否与tofu_ls对应,部分LSP在Mason中的包名可能和lspconfig配置名存在差异。

  4. 验证tofu-ls可执行性
    在Neovim中执行:!tofu-ls --version,确认能正常输出版本信息,排除环境变量问题导致Neovim找不到可执行文件的情况。

  5. 检查文件类型关联
    打开OpenTofu文件后执行:set filetype?,确认文件类型为opentofu或opentofu-vars。如果不是,可手动添加关联:

    vim.filetype.add({
      extension = {
        tf = "opentofu",
        tfvars = "opentofu-vars",
      },
    })
    

内容的提问来源于stack exchange,提问作者Revanth Shalon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 09:33:19