fix(exrc): exrc knows its own location (#34638)

fix(exrc): lua exrc files know their location

Problem:
'exrc' files are inherently bound to their location / workspace and
therefore require to "know" their location on the filesystem. However,
currently using `debug.getinfo(1, 'S')` returns `"<nvim>"`.

Solution:
Include the filepath as chunkname in `loadstring()` and `nlua_exec()`.
This commit is contained in:
Yochem van Rosmalen
2025-06-29 17:19:10 +02:00
committed by GitHub
parent 2f95abddfa
commit f7c939fa7a
12 changed files with 24 additions and 11 deletions

View File

@@ -956,7 +956,7 @@ do
local trusted = vim.secure.read(file) --[[@as string|nil]]
if trusted then
if vim.endswith(file, '.lua') then
assert(loadstring(trusted))()
assert(loadstring(trusted, '@' .. file))()
else
vim.api.nvim_exec2(trusted, {})
end

View File

@@ -2194,6 +2194,8 @@ vim.bo.et = vim.bo.expandtab
--- Unset 'exrc' to stop further searching of 'exrc' files in parent
--- directories, similar to `editorconfig.root`.
---
--- To get its own location, Lua exrc files can use `debug.getinfo()`.
---
--- Compare 'exrc' to `editorconfig`:
--- - 'exrc' can execute any code; editorconfig only specifies settings.
--- - 'exrc' is Nvim-specific; editorconfig works in other editors.

View File

@@ -1871,7 +1871,8 @@ function vim.fn.exp(expr) end
--- <SID> "<SNR>123_" where "123" is the
--- current script ID |<SID>|
--- <script> sourced script file, or script file
--- where the current function was defined
--- where the current function was defined.
--- Use |debug.getinfo()| in Lua scripts.
--- <stack> call stack
--- <cword> word under the cursor
--- <cWORD> WORD under the cursor