[Bug] Windows path conversion fails for forward-slash paths
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
- On Windows, open a file in Neovim where the path is represented with forward slashes.
- Run
:Oil. -
oil.nvimopens 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.
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:
- The fix proposed in this issue, which is a minimal solution for the forward-slash path problem.
- 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.
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.
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
- 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 (ievim.fn.bufadd('<filepath using backslashes>'). - Run
:Oil. - oil.nvim opens the directory but fails to select the current file, defaulting to the first entry.