如何解决Windows系统下的treesitter报错问题?
解决Windows下nvim-treesitter编译报错问题

配置treesitter后一直出现上图报错,查资料说要参考Windows支持文档,但看不懂内容,也不知道怎么解决。自己猜测是C编译器的问题,但找不到分步讲解的教程。
以下是我的Neovim配置文件(目前较为杂乱,待熟悉lua与nvim后会拆分插件、配色等配置):
vim.cmd("set number") vim.cmd("set expandtab") vim.cmd("set tabstop=4") vim.cmd("set softtabstop=2") vim.cmd("set shiftwidth=2") -- Bootstrap lazy.nvim local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not (vim.uv or vim.loop).fs_stat(lazypath) then local lazyrepo = "https://github.com/folke/lazy.nvim.git" local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath }) if vim.v.shell_error ~= 0 then vim.api.nvim_echo({ { "Failed to clone lazy.nvim:\n", "ErrorMsg" }, { out, "WarningMsg" }, { "\nPress any key to exit..." }, }, true, {}) vim.fn.getchar() os.exit(1) end end vim.opt.rtp:prepend(lazypath) -- Set mapleader vim.g.mapleader = " " vim.g.maplocalleader = "\\" -- Setup lazy.nvim require("lazy").setup({ spec = { { "catppuccin/nvim", name = "catppuccin", priority = 1000 }, { 'nvim-telescope/telescope.nvim', tag = '0.1.8', dependencies = { 'nvim-lua/plenary.nvim' } }, {"nvim-treesitter/nvim-treesitter", build = ":TSUpdate"} }, install = { colorscheme = { "habamax" } }, checker = { enabled = true }, }) -- Telescope keybindings local builtin = require("telescope.builtin") vim.keymap.set('n', '<C-p>', builtin.find_files, {}) vim.keymap.set('n', '<leader>fg', builtin.live_grep, {}) require 'nvim-treesitter.install'.prefer_git = false require 'nvim-treesitter.install'.compilers = { "clang", "gcc" } local config = require("nvim-treesitter.configs") config.setup({ ensure_installed = {"c", "java", "python"}, highlight = { enable = true }, indent = { enable = true }, }) -- Catppuccin colorscheme setup require("catppuccin").setup() vim.cmd.colorscheme "catppuccin"
解决步骤
1. 安装适配的C编译工具链
Windows下nvim-treesitter必须依赖C编译工具才能正常编译解析器,推荐优先用MSVC(适配Windows生态更好):
- 打开微软商店搜索「Visual Studio Installer」并安装
- 运行Installer,选择「使用C的桌面开发」工作负载,务必勾选「MSVC v143 - VS 2022 C x64/x86生成工具」和「Windows 11 SDK」,点击安装即可
如果偏好MinGW:
- 下载MinGW-w64(选x86_64架构、posix线程模型、seh异常处理版本)
- 将MinGW安装目录下的
bin文件夹路径添加到系统环境变量PATH中
2. 调整treesitter编译器配置
如果用MSVC,直接删掉配置里这两行(treesitter会自动检测MSVC):
require 'nvim-treesitter.install'.prefer_git = false require 'nvim-treesitter.install'.compilers = { "clang", "gcc" }
如果用MinGW,保留上述配置,确保gcc能在命令行直接调用(验证方式:打开cmd输入gcc -v,能输出版本信息即正常)
3. 重新安装treesitter解析器
打开Neovim,依次执行以下命令:
:TSUninstall all :TSUpdate
等待所有解析器重新编译安装完成
4. 验证效果
打开一个C/Python/Java文件,检查语法高亮、自动缩进是否正常,之前的报错应该会消失
内容的提问来源于stack exchange,提问作者schubert
相关产品推荐
相关产品推荐

