Skip to content

[Bug] Windows path conversion fails for forward-slash paths #676

@pidgeon777

Description

@pidgeon777

Describe the bug
On Windows, oil.nvim fails to select the current file when opening if the buffer's path uses forward slashes (e.g., C:/Users/...) instead of backslashes. This is because the os_to_posix_path function in lua/oil/fs.lua only handles paths with backslashes when converting a Windows path to a POSIX-like path.

To Reproduce

  1. On Windows, open a file in Neovim where the path is represented with forward slashes.
  2. Run :Oil.
  3. oil.nvim opens the directory but fails to select the current file, defaulting to the first entry.

Expected behavior
oil.nvim should correctly parse the path and select the active file regardless of whether the path uses forward or backslashes.

Fix
The os_to_posix_path function was updated to be more robust. The corrected version first normalizes all backslashes to forward slashes and then correctly parses the drive letter.

Here is the code for the fix:

---@param path string
---@return string
M.os_to_posix_path = function(path)
  if M.is_windows then
    local p = path:gsub("\\", "/")
    if p:match("^%a:/") then
      local drive, rem = p:match("^([^:]+):/(.*)$")
      return string.format("/%s/%s", drive:upper(), rem)
    else
      return p
    end
  else
    return path
  end
end

This change ensures that paths like C:\path and C:/path are both handled correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions