nvim-telescope / telescope-frecency.nvim

A telescope.nvim extension that offers intelligent prioritization when selecting files from your editing history.
MIT License
688 stars 35 forks source link

Cannot open files in home directory in Cygwin #170

Open delphinus opened 5 months ago

delphinus commented 5 months ago

ref #164

This is because vim.uv.os_homedir() returns C:\Users\foo, but the exact home directory ($HOME) in Cygwin is C:\cygwin64\home\foo.


procedure to fail opening files

  1. :cd C:\Windows (outside of the home directory)
  2. :Telescope frecency and try to open C:\Users\foo\AppData\Local\Nvim\init.lua
  3. Internally, it makes the path be normalized: ~\AppData\Local\Nvim\init.lua.
  4. Then Neovim tries to open ~\AppData\Local\Nvim\init.lua, and then fails. This is because ~ is not C:\Users\foo but \home\foo in Cygwin.

procedure to open files successfully

  1. :cd C:\Users\foo (home directory)
  2. :Telescope frecency and try to open C:\Users\foo\AppData\Local\Nvim\init.lua
  3. Internally, it makes the path to relative one: AppData\Local\Nvim\init.lua.
  4. Neovim successfully opens the file.
delphinus commented 5 months ago

This is caused by plenary.nvim's path.lua itself. Path:normalize() makes vim.uv.os_homedir() string into ~, but ~ is not $HOME in Cygwin. With this diff below, it works validly.

diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua
index a253859..64df772 100644
--- a/lua/plenary/path.lua
+++ b/lua/plenary/path.lua
@@ -368,16 +368,18 @@ function Path:normalize(cwd)

   self:make_relative(cwd)

-  -- Substitute home directory w/ "~"
-  -- string.gsub is not useful here because usernames with dashes at the end
-  -- will be seen as a regexp pattern rather than a raw string
-  local home = path.home
-  if string.sub(path.home, -1) ~= path.sep then
-    home = home .. path.sep
-  end
-  local start, finish = string.find(self.filename, home, 1, true)
-  if start == 1 then
-    self.filename = "~" .. path.sep .. string.sub(self.filename, (finish + 1), -1)
+  if path.home == vim.env.HOME then
+    -- Substitute home directory w/ "~"
+    -- string.gsub is not useful here because usernames with dashes at the end
+    -- will be seen as a regexp pattern rather than a raw string
+    local home = path.home
+    if string.sub(path.home, -1) ~= path.sep then
+      home = home .. path.sep
+    end
+    local start, finish = string.find(self.filename, home, 1, true)
+    if start == 1 then
+      self.filename = "~" .. path.sep .. string.sub(self.filename, (finish + 1), -1)
+    end
   end

   return _normalize_path(clean(self.filename), self._cwd)

Should we change this? Still considering.

delphinus commented 5 months ago

telescope.nvim calls Path:normalize ↓here. This is the problem.

https://github.com/nvim-telescope/telescope.nvim/blob/da8b3d485975a8727bea127518b65c980521ae22/lua/telescope/actions/set.lua#L199-L199