Skip to content

Commit

Permalink
feat: find local_spec in parent directories as well. Closes #1519
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 15, 2024
1 parent 6944b10 commit e2e10d9
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ function Spec:import(spec)
---@type string[]
local modnames = {}

if spec.import == M.LOCAL_SPEC then
if spec.import:find(M.LOCAL_SPEC, 1, true) then
modnames = { spec.import }
else
Util.lsmod(spec.import, function(modname)
Expand All @@ -414,15 +414,19 @@ function Spec:import(spec)

for _, modname in ipairs(modnames) do
imported = imported + 1
Util.track({ import = modname })
local name = modname
if modname:find(M.LOCAL_SPEC, 1, true) then
name = vim.fn.fnamemodify(modname, ":~:.")
end
Util.track({ import = name })
self.importing = modname
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
local mod = nil
if modname == M.LOCAL_SPEC then
mod = M.local_spec()
if modname:find(M.LOCAL_SPEC, 1, true) then
mod = M.local_spec(modname)
else
mod = require(modname)
end
Expand Down Expand Up @@ -546,26 +550,48 @@ function M.update_state()
end
end

function M.local_spec()
local filepath = vim.fn.fnamemodify(".lazy.lua", ":p")
local file = vim.secure.read(filepath)
---@param path string
function M.local_spec(path)
local file = vim.secure.read(path)
if file then
return loadstring(file)()
end
return {}
end

---@return string?
function M.find_local_spec()
if not Config.options.local_spec then
return
end
local path = vim.uv.cwd()
while path ~= "" do
local file = path .. "/" .. M.LOCAL_SPEC

This comment has been minimized.

Copy link
@aarondill

aarondill Jun 15, 2024

@folke Note that in v0.10, this could all be replaced with return vim.fs.find(M.local_spec, { upward = true, type = “file” })[1]
see :h vim.fs.find

This comment has been minimized.

Copy link
@folke

folke Jun 15, 2024

Author Owner

I know, but lazy.nvim should work on older version too of course

if vim.fn.filereadable(file) == 1 then
return file
end
local p = vim.fn.fnamemodify(path, ":h")
if p == path then
break
end
path = p
end
end

function M.load()
M.loading = true
-- load specs
Util.track("spec")
Config.spec = Spec.new()

local local_spec = M.find_local_spec()

Config.spec:parse({
vim.deepcopy(Config.options.spec),
{
import = ".lazy.lua",
import = local_spec or M.LOCAL_SPEC,
cond = function()
return Config.options.local_spec and vim.fn.filereadable(M.LOCAL_SPEC) == 1
return local_spec ~= nil
end,
},
{ "folke/lazy.nvim" },
Expand Down

0 comments on commit e2e10d9

Please sign in to comment.