From 5045add053e5707ca46d76c37721246c863780ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=A7=E7=BE=8A=E7=8A=AC=E7=9C=9FQ?= Date: Tue, 6 Jan 2026 00:40:22 +0800 Subject: [PATCH 1/2] fix(chezmoi): handle .chezmoiroot in source_dir_path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for .chezmoiroot to configure source directory.## Problem When users have a .chezmoiroot file in their chezmoi source directory, the current chezmoi#source_dir_path setting doesn't include the root subdirectory. This causes chezmoi.vim's filetype detection to fail for files in .chezmoiscripts/ and other special directories. For example, with .chezmoiroot containing home: - Current: ~/.local/share/chezmoi - Expected: ~/.local/share/chezmoi/home ## Solution Read .chezmoiroot file if it exists and append its content to the source path, matching chezmoi.vim's built-in detection behavior. ## Testing Verified with: - .chezmoiscripts/run_once_before_*.sh.tmpl → sh.chezmoitmpl - dot_config/fish/config.fish.tmpl → fish.chezmoitmpl --- lua/lazyvim/plugins/extras/util/chezmoi.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lua/lazyvim/plugins/extras/util/chezmoi.lua b/lua/lazyvim/plugins/extras/util/chezmoi.lua index 201288a4..bc836c1d 100644 --- a/lua/lazyvim/plugins/extras/util/chezmoi.lua +++ b/lua/lazyvim/plugins/extras/util/chezmoi.lua @@ -55,7 +55,18 @@ return { "alker0/chezmoi.vim", init = function() vim.g["chezmoi#use_tmp_buffer"] = 1 - vim.g["chezmoi#source_dir_path"] = vim.env.HOME .. "/.local/share/chezmoi" + -- Handle .chezmoiroot for users who organize source files in subdirectories + local source = vim.env.HOME .. "/.local/share/chezmoi" + local root_file = source .. "/.chezmoiroot" + local f = io.open(root_file, "r") + if f then + local root = f:read("*l") + f:close() + if root and root ~= "" then + source = source .. "/" .. vim.trim(root) + end + end + vim.g["chezmoi#source_dir_path"] = source end, }, { From 9693ec638ec05629838e04ba18d6f533065afaa8 Mon Sep 17 00:00:00 2001 From: Collie Tsai Date: Tue, 6 Jan 2026 02:33:28 +0800 Subject: [PATCH 2/2] fix(chezmoi): handle .chezmoiroot in autocmd pattern Extends previous fix to also cover the BufRead/BufNewFile autocmd pattern for chezmoi.nvim file watching. Changes: - Extract source path resolution to shared `source_dir` variable - Use `chezmoi source-path` CLI when available (canonical solution) - Fallback with path normalization (vim.fs.normalize, vim.fs.joinpath) - Reject path traversal patterns (..) for security - Handle edge cases: leading ./, trailing slashes, etc. --- lua/lazyvim/plugins/extras/util/chezmoi.lua | 36 +++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lua/lazyvim/plugins/extras/util/chezmoi.lua b/lua/lazyvim/plugins/extras/util/chezmoi.lua index bc836c1d..a0e38d9f 100644 --- a/lua/lazyvim/plugins/extras/util/chezmoi.lua +++ b/lua/lazyvim/plugins/extras/util/chezmoi.lua @@ -1,3 +1,24 @@ +-- Resolve source path, respecting .chezmoiroot +local source_dir = (function() + local out = vim.fn.system("chezmoi source-path") + if vim.v.shell_error == 0 then + return vim.trim(out) + end + local base = vim.env.HOME .. "/.local/share/chezmoi" + local f = io.open(base .. "/.chezmoiroot", "r") + if f then + local root = f:read("*l") + f:close() + if root then + root = vim.fs.normalize(vim.trim(root), { expand_env = false }) + if root ~= "" and not root:find("..", 1, true) then + return vim.fs.joinpath(base, root) + end + end + end + return base +end)() + local pick_chezmoi = function() if LazyVim.pick.picker.name == "telescope" then require("telescope").extensions.chezmoi.find_files() @@ -55,18 +76,7 @@ return { "alker0/chezmoi.vim", init = function() vim.g["chezmoi#use_tmp_buffer"] = 1 - -- Handle .chezmoiroot for users who organize source files in subdirectories - local source = vim.env.HOME .. "/.local/share/chezmoi" - local root_file = source .. "/.chezmoiroot" - local f = io.open(root_file, "r") - if f then - local root = f:read("*l") - f:close() - if root and root ~= "" then - source = source .. "/" .. vim.trim(root) - end - end - vim.g["chezmoi#source_dir_path"] = source + vim.g["chezmoi#source_dir_path"] = source_dir end, }, { @@ -96,7 +106,7 @@ return { init = function() -- run chezmoi edit on file enter vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { - pattern = { vim.env.HOME .. "/.local/share/chezmoi/*" }, + pattern = { source_dir .. "/*" }, callback = function() vim.schedule(require("chezmoi.commands.__edit").watch) end,