From 10375ec4bb162f2b992fbe477d609aa59096d7de Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 14 Feb 2012 16:05:18 -0200 Subject: Don't crash on invalid config files. Report which config file is being loaded at luarocks --help. Thanks to Tomas Guisasola Gorham for the suggestions. --- src/luarocks/cfg.lua | 17 +++++++++++++++-- src/luarocks/help.lua | 4 ++++ src/luarocks/persist.lua | 10 ++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 8a783237..9fce84f8 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -91,6 +91,7 @@ end -- Path configuration: local sys_config_file, home_config_file +local sys_config_ok, home_config_ok = false, false if detected.windows or detected.mingw32 then home = os.getenv("APPDATA") or "c:" sys_config_file = "c:/luarocks/config.lua" @@ -106,12 +107,18 @@ end variables = {} rocks_trees = {} -persist.load_into_table(site_config.LUAROCKS_SYSCONFIG or sys_config_file, _M) +local ok, err = persist.load_into_table(site_config.LUAROCKS_SYSCONFIG or sys_config_file, _M) +if ok then + sys_config_ok = true +elseif err and ok == nil then + io.stderr:write(err.."\n") +end if not site_config.LUAROCKS_FORCE_CONFIG then home_config_file = os.getenv("LUAROCKS_CONFIG") or home_config_file - local home_overrides = persist.load_into_table(home_config_file, { home = home }) + local home_overrides, err = persist.load_into_table(home_config_file, { home = home }) if home_overrides then + home_config_ok = true local util = require("luarocks.util") if home_overrides.rocks_trees then _M.rocks_trees = nil @@ -120,6 +127,8 @@ if not site_config.LUAROCKS_FORCE_CONFIG then _M.rocks_servers = nil end util.deep_merge(_M, home_overrides) + elseif err and home_overrides == nil then + io.stderr:write(err.."\n") end end @@ -404,6 +413,10 @@ for _,tree in ipairs(rocks_trees) do end end +function which_config() + return sys_config_file, sys_config_ok, home_config_file, home_config_ok +end + --- Check if platform was detected -- @param query string: The platform name to check. -- @return boolean: true if LuaRocks is currently running on queried platform. diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index 718580ce..6272d213 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua @@ -25,9 +25,13 @@ function run(...) local flags, command = util.parse_flags(...) if not command then + local sys_file, sys_ok, home_file, home_ok = cfg.which_config() util.printout([[ LuaRocks ]]..cfg.program_version..[[, a module deployment system for Lua +Using system configuration file: ]]..sys_file .. " (" .. (sys_ok and "ok" or "failed") ..[[) +and user configuration file: ]]..home_file .. " (" .. (home_ok and "ok" or "failed") ..[[) + ]]..program_name..[[ - ]]..program_description..[[ usage: ]]..program_name..[[ [--from= | --only-from=] [--to=] [VAR=VALUE]... [] diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 91c28a32..6d411e0f 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua @@ -20,11 +20,17 @@ function load_into_table(filename, tbl) local chunk, err = loadfile(filename) if not chunk then - return nil, err + if err:sub(1,5) ~= filename:sub(1,5) then + return false, err + end + return nil, "Error loading file: "..err end local result = tbl or {} setfenv(chunk, result) - chunk() + local ok, err = pcall(chunk) + if not ok then + return nil, "Error running file: "..err + end return result end -- cgit v1.2.3-55-g6feb