Skip to content

Commit a514ebe

Browse files
committed
Catch errors executing code loaded from includes
Depending on the specific error, sometimes code will load but be unable to execute, throwing an error. This now catches (pcall, "protected call") it and displays an appropriate error message, including the file that was included and where the include originated from.
1 parent 5238489 commit a514ebe

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/base/globals.lua

+12-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@
4848
io._includedFiles = {}
4949

5050
function include(fname)
51-
fname, compiled_chunk = premake.findProjectScript(fname)
52-
if not io._includedFiles[fname] then
53-
io._includedFiles[fname] = true
54-
return compiled_chunk()
51+
local actualFname, compiled_chunk = premake.findProjectScript(fname)
52+
if not io._includedFiles[actualFname] then
53+
io._includedFiles[actualFname] = true
54+
local success, res = pcall(compiled_chunk)
55+
if success then
56+
-- res is the return value of the script
57+
return res
58+
else
59+
-- res is the error message
60+
local caller = filelineinfo(2)
61+
premake.error(caller .. ": Error executing '" .. fname .. ": " .. res)
62+
end
5563
end
5664
end
5765

0 commit comments

Comments
 (0)