From 6703872481310afa8ca1bb8524c2501730682c27 Mon Sep 17 00:00:00 2001 From: Richard Sim Date: Fri, 27 Sep 2024 00:00:04 -0700 Subject: [PATCH 1/3] Added context to errors from include() - Display the error message returned from loadfile(), which includes the file and line information (necessary for nested includes!), as well as the specific error that was encountered. --- src/base/_foundation.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/base/_foundation.lua b/src/base/_foundation.lua index 8bb3d9b9f2..64a6449e85 100644 --- a/src/base/_foundation.lua +++ b/src/base/_foundation.lua @@ -244,9 +244,13 @@ local res = os.locate(fname, with_ext, p5, p4) res = res or fname - local compiled_chunk = loadfile(res) + local compiled_chunk, err = loadfile(res) if compiled_chunk == nil then - premake.error("Cannot find either " .. table.implode({fname, with_ext, p5, p4}, "", "", " or ")) + if err then + premake.error("Cannot load " .. fname .. ": " .. err) + else + premake.error("Cannot find either " .. table.implode({fname, with_ext, p5, p4}, "", "", " or ")) + end end return res, compiled_chunk end From 523848940f6f75c2cc5d192e5eca13ffb958eaed Mon Sep 17 00:00:00 2001 From: Richard Sim Date: Fri, 27 Sep 2024 20:38:12 -0700 Subject: [PATCH 2/3] Further improvements to error message display for includes Integrated PR feedback: - It can be assumed that err is not nil if compiled_chunk is nil (confirmed with the lua docs) - Check if the file was located before trying to load it, so we can display a descriptive error message with the alternate names that were searched - Improved the error messages by addubg the file/line information where the include originated, similar to the output of a compiler. The `res` is not output to the error message as in every case it was already part of the error message that was returned and that resulted in very long, repetitive errors --- src/base/_foundation.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/base/_foundation.lua b/src/base/_foundation.lua index 64a6449e85..130be76bae 100644 --- a/src/base/_foundation.lua +++ b/src/base/_foundation.lua @@ -242,14 +242,16 @@ local p5 = path.join(fname, "premake5.lua") local p4 = path.join(fname, "premake4.lua") + local compiled_chunk local res = os.locate(fname, with_ext, p5, p4) - res = res or fname - local compiled_chunk, err = loadfile(res) - if compiled_chunk == nil then - if err then - premake.error("Cannot load " .. fname .. ": " .. err) - else - premake.error("Cannot find either " .. table.implode({fname, with_ext, p5, p4}, "", "", " or ")) + if res == nil then + local caller = filelineinfo(3) + premake.error(caller .. ": Cannot find neither " .. table.implode({fname, with_ext, p5, p4}, "", "", " nor ")) + else + compiled_chunk, err = loadfile(res) + if err ~= nil then + local caller = filelineinfo(3) + premake.error(caller .. ": Error loading '" .. fname .. ": " .. err) end end return res, compiled_chunk From a514ebe5b31f36b2b5eab3d81f7799f873a69610 Mon Sep 17 00:00:00 2001 From: Richard Sim Date: Fri, 27 Sep 2024 20:40:20 -0700 Subject: [PATCH 3/3] 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. --- src/base/globals.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/base/globals.lua b/src/base/globals.lua index ee9385182e..d46ca00b68 100644 --- a/src/base/globals.lua +++ b/src/base/globals.lua @@ -48,10 +48,18 @@ io._includedFiles = {} function include(fname) - fname, compiled_chunk = premake.findProjectScript(fname) - if not io._includedFiles[fname] then - io._includedFiles[fname] = true - return compiled_chunk() + local actualFname, compiled_chunk = premake.findProjectScript(fname) + if not io._includedFiles[actualFname] then + io._includedFiles[actualFname] = true + local success, res = pcall(compiled_chunk) + if success then + -- res is the return value of the script + return res + else + -- res is the error message + local caller = filelineinfo(2) + premake.error(caller .. ": Error executing '" .. fname .. ": " .. res) + end end end