stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
4.03k stars 121 forks source link

bug: new buffers are opened with files, that are relative to current working directory, using their absolute paths #234

Open ArtAndreev opened 11 months ago

ArtAndreev commented 11 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

v0.9.4

Operating system/version

macOS 14.1.1 (23B81)

Describe the bug

Oil opens new buffers with files, that are relative to current nvim working directory, using their absolute paths. Better to understand by looking at example below.

I expect relative paths, because files are relative and some code expects these buffers to have relative paths in names too.

Steps To Reproduce

  1. nvim -u repro.lua file1
  2. :echo expand('%') | This prints file1.
  3. :Oil
  4. pick file2 (j), press <CR>
  5. :echo expand('%') | This prints /Users/<username>/path/to/cwd/file2.

Expected Behavior

Expected printed buffer names after calling expand:

Directory structure

file1 file2

Repro

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
        "stevearc/oil.nvim",
        config = function()
            require("oil").setup({
              -- add any needed settings here
            })
        end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Did you check the bug with a clean config?

ArtAndreev commented 11 months ago

Well, netrw has the same behavior, nvim-tree too (issue https://github.com/nvim-tree/nvim-tree.lua/issues/2127)

Maybe issue should be converted from bug to feature request.

sxwpb commented 10 months ago

a workaround for now can be adding a autocmd like this in your config:

-- oil fix relative path
vim.api.nvim_create_augroup('OilRelPathFix', {})
vim.api.nvim_create_autocmd("BufLeave", {
    group = 'OilRelPathFix',
    pattern  = "oil:///*",
    callback = function ()
        vim.cmd("cd .")
    end
})