oil.nvim icon indicating copy to clipboard operation
oil.nvim copied to clipboard

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

Open pidgeon777 opened this issue 4 months ago • 3 comments

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.

pidgeon777 avatar Oct 25 '25 20:10 pidgeon777

Hi @stevearc,

I've been discussing this issue and the related pull request #493 (https://github.com/stevearc/oil.nvim/pull/493) which also deals with Windows paths.

There are two potential solutions:

  1. The fix proposed in this issue, which is a minimal solution for the forward-slash path problem.
  2. The solution in PR #493, which is more comprehensive and adds support for UNC paths.

I would like to ask for guidance on which solution would be preferred. Would it be better to merge the minimal fix first to solve the immediate problem, or to work on getting the more comprehensive PR #493 merged?

The main goal is to get a fix in place for the Windows path handling. Any progress on either of these fronts would be greatly appreciated.

pidgeon777 avatar Oct 25 '25 23:10 pidgeon777

I would review a PR that's target just at fixing this issue. It looks like there were still two open questions on the other PR, both related to UNC paths specifically. If you want to champion that functionality and fork the PR, that works for me too.

stevearc avatar Oct 27 '25 04:10 stevearc

I'm looking into this issue today. Below is modified reproduction steps with more details in the first step since I was having issues consistently reproducing.

To Reproduce

  1. On Windows, open a file in Neovim where the path is represented with forward slashes. A consistent way to do so is run :edit <existing absolute filepath using forward slashes> For this issue the path must exist. The path also needs to be an absolute path or neovim will normalize the path name to use backslash when it constructs the absolute path. The file chosen also must not have already had a buffer added with the backslash filepath (ie vim.fn.bufadd('<filepath using backslashes>').
  2. Run :Oil.
  3. oil.nvim opens the directory but fails to select the current file, defaulting to the first entry.

crwebb85 avatar Nov 11 '25 14:11 crwebb85