Skip to content

fix(explorer): preserve trailing slash for Windows drive root#2829

Open
RaphCoder13 wants to merge 2 commits intofolke:mainfrom
RaphCoder13:fix/explorer_windows_drive_root
Open

fix(explorer): preserve trailing slash for Windows drive root#2829
RaphCoder13 wants to merge 2 commits intofolke:mainfrom
RaphCoder13:fix/explorer_windows_drive_root

Conversation

@RaphCoder13
Copy link
Copy Markdown

Description

Problem

On Windows, navigating to a drive root (e.g., C:\) in snacks.explorer displays incorrect content. Instead of listing the actual root directory, the explorer falls back to the current working directory.

Root Cause

The local norm(path) function in lua/snacks/explorer/tree.lua unconditionally strips trailing slashes:

return vim.fs.normalize(path):gsub("/$", ""):gsub("^$", "/")

On Windows, C: and C:\ (or C:/) are not semantically equivalent:

  • C: → Resolves to the drive's current working directory (session cwd or %USERPROFILE%)
  • C:\ → Resolves to the actual filesystem root

Stripping the slash forces Neovim/libuv to interpret C: as a relative drive path, which breaks root navigation

Steps to Reproduce

1. Start Neovim on Windows
2. :cd C:\some\folder
3. Open explorer: :lua Snacks.explorer()
4. Navigate up with <BS> until reaching the drive root (C:/)
5. Expected: Contents of C:\
6. Actual: Contents of the original cwd or incorrect directory

Proposed Fix

Preserve the trailing slash for Windows drive roots by adding a platform-aware check

local is_win = vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1

local function norm(path)
  local normalized = vim.fs.normalize(path)
  -- Preserve trailing slash for Windows drive roots (C:/, D:/, etc.)
  -- On Windows, "C:" means current dir of the drive, "C:/" means the actual root.
  if is_win and normalized:match("^[a-zA-Z]:/$") then
    return normalized
  end
  return normalized:gsub("/$", ""):gsub("^$", "/")
end

Cross-Platform Safety

  • Only activates on Windows (is_win check)
  • Only matches drive roots (^[a-zA-Z]:/$ pattern)
  • POSIX paths (/home, /tmp/, etc.) remain completely unaffected
  • vim.fs.normalize() already converts \ to /, so the regex works reliably across Windows input formats

Testing

Verified on Windows 11 and Windows 10 / Neovim 0.16.4. Drive root navigation now works correctly on first open, without requiring manual cd workarounds or cache pre-warming.

@github-actions github-actions Bot added explorer size/m Medium PR (<50 lines changed) labels Apr 16, 2026
@RaphCoder13 RaphCoder13 force-pushed the fix/explorer_windows_drive_root branch from 5683513 to 547a8df Compare April 21, 2026 12:02
Needed to allow folder opening from drive root in windows
@RaphCoder13 RaphCoder13 force-pushed the fix/explorer_windows_drive_root branch from 547a8df to b391196 Compare April 21, 2026 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

explorer size/m Medium PR (<50 lines changed)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant