@@ -146,52 +146,61 @@ function Spec:import(spec)
146
146
147
147
local imported = 0
148
148
149
- --- @type ( string | ( fun (): LazyPluginSpec )) []
149
+ --- @type { modname : string , load : fun ():( LazyPluginSpec ?, string ?)} []
150
150
local modspecs = {}
151
151
152
152
if type (import ) == " string" then
153
153
Util .lsmod (import , function (modname , modpath )
154
- modspecs [# modspecs + 1 ] = modname
155
- package.preload [modname ] = function ()
156
- return loadfile (modpath )()
157
- end
154
+ modspecs [# modspecs + 1 ] = {
155
+ modname = modname ,
156
+ load = function ()
157
+ local mod , err = loadfile (modpath )
158
+ if mod then
159
+ return mod ()
160
+ else
161
+ return nil , err
162
+ end
163
+ end ,
164
+ }
165
+ end )
166
+ table.sort (modspecs , function (a , b )
167
+ return a .modname < b .modname
158
168
end )
159
- table.sort (modspecs )
160
169
else
161
- modspecs = { spec .import }
170
+ modspecs = { modname = import_name , load = spec .import }
162
171
end
163
172
164
173
for _ , modspec in ipairs (modspecs ) do
165
174
imported = imported + 1
166
- local modname = type ( modspec ) == " string " and modspec or import_name
175
+ local modname = modspec . modname
167
176
Util .track ({ import = modname })
168
177
self .importing = modname
169
178
-- unload the module so we get a clean slate
170
179
--- @diagnostic disable-next-line : no-unknown
171
180
package.loaded [modname ] = nil
172
181
Util .try (function ()
173
- local mod = type (modspec ) == " function" and modspec () or require (modspec )
174
- if type (mod ) ~= " table" then
175
- self .importing = nil
182
+ local mod , err = modspec .load ()
183
+ if err then
184
+ self :error (" Failed to load `" .. modname .. " `:\n " .. err )
185
+ elseif type (mod ) ~= " table" then
176
186
return self :error (
177
187
" Invalid spec module: `"
178
188
.. modname
179
189
.. " `\n Expected a `table` of specs, but a `"
180
190
.. type (mod )
181
191
.. " ` was returned instead"
182
192
)
193
+ else
194
+ self :normalize (mod )
183
195
end
184
- self :normalize (mod )
185
- self .importing = nil
186
- Util .track ()
187
196
end , {
188
197
msg = " Failed to load `" .. modname .. " `" ,
189
198
on_error = function (msg )
190
199
self :error (msg )
191
- self .importing = nil
192
- Util .track ()
193
200
end ,
194
201
})
202
+ self .importing = nil
203
+ Util .track ()
195
204
end
196
205
if imported == 0 then
197
206
self :error (" No specs found for module " .. spec .import )
0 commit comments