From ec44db639c2a84e488abf7f7fbd31872afadcb2c Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 28 Feb 2024 20:04:39 -0300 Subject: feat: better error message when lacking permissions The lock error message has clobbered the check_command_permissions error message. This is an interim solution, but we should probably move the permissions check further up for a more informative error. --- spec/quick/build.q | 35 ++++++++++++++++++++++++++++------- src/luarocks/cmd.lua | 13 ++++++++----- src/luarocks/fs/unix/tools.lua | 5 ++++- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/spec/quick/build.q b/spec/quick/build.q index ee519adf..55bb7519 100644 --- a/spec/quick/build.q +++ b/spec/quick/build.q @@ -83,8 +83,15 @@ RUN: luarocks build --tree=/usr ./a_rock-1.0.1-rockspec EXIT: 4 STDERR: -------------------------------------------------------------------------------- -requires exclusive access -use --force-lock +requires exclusive write access +-------------------------------------------------------------------------------- + +We show the OS permission denied error, so we don't show the --force-lock +message. + +NOT_STDERR: +-------------------------------------------------------------------------------- +try --force-lock -------------------------------------------------------------------------------- NOT_EXISTS: %{testing_sys_rocks}/a_rock/1.0-1/a_rock-1.0-1.rockspec @@ -93,8 +100,15 @@ RUN: luarocks build --tree=/usr ./a_rock-1.0.1-rockspec --force-lock EXIT: 4 STDERR: -------------------------------------------------------------------------------- -requires exclusive access -failed to force the lock +requires exclusive write access +-------------------------------------------------------------------------------- + +We show the OS permission denied error, so we don't show the --force-lock +message. + +NOT_STDERR: +-------------------------------------------------------------------------------- +try --force-lock -------------------------------------------------------------------------------- NOT_EXISTS: %{testing_sys_rocks}/a_rock/1.0-1/a_rock-1.0-1.rockspec @@ -108,8 +122,15 @@ RUN: luarocks build --tree=/usr/invalid ./a_rock-1.0.1-rockspec EXIT: 4 STDERR: -------------------------------------------------------------------------------- -requires exclusive access -use --force-lock +requires exclusive write access +-------------------------------------------------------------------------------- + +We show the OS permission denied error, so we don't show the --force-lock +message. + +NOT_STDERR: +-------------------------------------------------------------------------------- +try --force-lock -------------------------------------------------------------------------------- NOT_EXISTS: %{testing_sys_rocks}/a_rock/1.0-1/a_rock-1.0-1.rockspec @@ -118,7 +139,7 @@ RUN: luarocks build --tree=/usr/invalid ./a_rock-1.0.1-rockspec --force-lock EXIT: 4 STDERR: -------------------------------------------------------------------------------- -requires exclusive access +requires exclusive write access failed to force the lock -------------------------------------------------------------------------------- diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index ca22c0db..b6d0d47e 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -732,12 +732,15 @@ function cmd.run_command(description, commands, external_namespace, ...) if cmd_mod.needs_lock and cmd_mod.needs_lock(args) then lock, err = fs.lock_access(path.root_dir(cfg.root_dir), args.force_lock) if not lock then - local try_force = args.force_lock - and (" - failed to force the lock" .. (err and ": " .. err or "")) - or " - use --force-lock to overwrite the lock" + err = args.force_lock + and ("failed to force the lock" .. (err and ": " .. err or "")) + or (err and err ~= "File exists") + and err + or "try --force-lock to overwrite the lock" + die("command '" .. args.command .. "' " .. - "requires exclusive access to " .. path.root_dir(cfg.root_dir) .. - try_force, cmd.errorcodes.LOCK) + "requires exclusive write access to " .. path.root_dir(cfg.root_dir) .. " - " .. + err, cmd.errorcodes.LOCK) end end diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index efd4d624..a0fba784 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -319,7 +319,10 @@ function tools.is_superuser() end function tools.lock_access(dirname, force) - fs.make_dir(dirname) + local ok, err = fs.make_dir(dirname) + if not ok then + return nil, err + end local tempfile = dir.path(dirname, ".lock.tmp." .. tostring(math.random(100000000))) -- cgit v1.2.3-55-g6feb