Skip to content

Commit 8e29672

Browse files
committed
fix(fs): use rmdir for directory removal on Windows
1 parent a637d3e commit 8e29672

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

src/mods/fs.lua

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ local isdir = is.dir
1717
local islink = is.link
1818

1919
local open = io.open
20-
local remove = os.remove
20+
local os_remove = os.remove
2121
local rename = os.rename
2222

2323
---@type mods.fs
2424
local M = {}
2525

2626
local CURDIR = "."
2727
local PARDIR = ".."
28+
local is_windows = mods.runtime.is_windows
2829
local entry_types = {
2930
["block device"] = "block",
3031
["char device"] = "char",
@@ -39,6 +40,24 @@ local function is_hidden(entry)
3940
return entry:sub(1, 1) == "."
4041
end
4142

43+
local function remove(item)
44+
if islink(item) then
45+
if is_windows then
46+
local ok = lfs.rmdir(item)
47+
if ok then
48+
return true
49+
end
50+
end
51+
return os_remove(item)
52+
end
53+
54+
if isdir(item) then
55+
return lfs.rmdir(item)
56+
end
57+
58+
return os_remove(item)
59+
end
60+
4261
-- `lfs.dir` throws on failure, so use `pcall` to preserve its error text as `false, err`.
4362
local function open_dir(p)
4463
local ok, iter, dir_obj = pcall(lfs.dir, p)
@@ -333,24 +352,20 @@ function M.rm(p, recursive)
333352
end
334353

335354
local items = {}
336-
local ok, err
337355

338-
ok, err = scan_dir(p, items, false)
356+
local ok, err = scan_dir(p, items, false)
339357
if not ok then
340358
return nil, err
341359
end
342360

343-
local rmdir = lfs.rmdir
344361
for i = #items, 1, -1 do
345-
local item = items[i]
346-
local fn = (isdir(item) and not islink(item)) and rmdir or remove
347-
ok, err = fn(item)
362+
ok, err = remove(items[i]) ---@diagnostic disable-line: cast-local-type
348363
if not ok then
349364
return nil, err
350365
end
351366
end
352367

353-
return rmdir(p)
368+
return remove(p)
354369
end
355370

356371
return remove(p)

0 commit comments

Comments
 (0)