Merge pull request #34090 from yochem/exrc-search-parent

feat(exrc): unset 'exrc' to stop searching parent directories
This commit is contained in:
Gregory Anders
2025-05-20 12:06:08 -05:00
committed by GitHub
6 changed files with 47 additions and 26 deletions

View File

@@ -928,23 +928,31 @@ do
vim.api.nvim_create_autocmd('VimEnter', {
group = vim.api.nvim_create_augroup('nvim.find_exrc', {}),
desc = 'Find project-local configuration',
desc = 'Find exrc files in parent directories',
callback = function()
if vim.o.exrc then
local files = vim.fs.find(
{ '.nvim.lua', '.nvimrc', '.exrc' },
{ type = 'file', upward = true, limit = math.huge }
)
for _, file in ipairs(files) do
local trusted = vim.secure.read(file) --[[@as string|nil]]
if trusted then
if vim.endswith(file, '.lua') then
loadstring(trusted)()
else
vim.api.nvim_exec2(trusted, {})
end
if not vim.o.exrc then
return
end
local files = vim.fs.find({ '.nvim.lua', '.nvimrc', '.exrc' }, {
type = 'file',
upward = true,
limit = math.huge,
-- exrc in cwd already handled from C, thus start in parent directory.
path = vim.fs.dirname(vim.uv.cwd()),
})
for _, file in ipairs(files) do
local trusted = vim.secure.read(file) --[[@as string|nil]]
if trusted then
if vim.endswith(file, '.lua') then
loadstring(trusted)()
else
vim.api.nvim_exec2(trusted, {})
end
end
-- If the user unset 'exrc' in the current exrc then stop searching
if not vim.o.exrc then
return
end
end
end,
})

View File

@@ -2080,6 +2080,9 @@ vim.bo.et = vim.bo.expandtab
--- directories (ordered upwards), if the files are in the `trust` list.
--- Use `:trust` to manage trusted files. See also `vim.secure.read()`.
---
--- Unset 'exrc' to stop further searching of 'exrc' files in parent
--- directories, similar to `editorconfig.root`.
---
--- Compare 'exrc' to `editorconfig`:
--- - 'exrc' can execute any code; editorconfig only specifies settings.
--- - 'exrc' is Nvim-specific; editorconfig works in other editors.