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

Files opened via `:Telescope find_files` won't save, but opened via `:e` will.

Open theherk opened this issue 1 year ago • 3 comments

Description

I commented on another issue on the topic, but it was closed several years ago. It does seem there is still an issue though, so I thought maybe it was worth opening a new issues.

Neovim version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

Operating system and version

MacOS Sonoma 14.3

Telescope version / branch / rev

1bfbb1f

checkhealth telescope

telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 14.1.0
- OK fd: found fd 9.0.0

===== Installed extensions ===== ~

Steps to reproduce

This is a tough one. It isn't for all files opened with find_files only some. I'm not sure I can identify the subset, but I think it is a set of files that were removed, but still somewhere cached by Telescope, but then added again. So...

I have tried to recreate this using a minimal new git repository and simply cannot. But the problem does exist, and can be seen in the attached video.

Using the minimal config provided further below I can turn the issue on and off by commenting / uncommenting this section which was missing from my config but I stole from the LazyVim treesitter spec:

      -- init = function(plugin)
      --   -- PERF: add nvim-treesitter queries to the rtp and it's custom query predicates early
      --   -- This is needed because a bunch of plugins no longer `require("nvim-treesitter")`, which
      --   -- no longer trigger the **nvim-treeitter** module to be loaded in time.
      --   -- Luckily, the only thins that those plugins need are the custom queries, which we make available
      --   -- during startup.
      --   require("lazy.core.loader").add_to_rtp(plugin)
      --   require("nvim-treesitter.query_predicates")
      -- end,

If I uncomment that the issue goes away. So I have a solution to my problem, but I think it indicates there is a problem. Somewhere maybe telescope needs to require("nvim-treesitter")?

Expected behavior

The same behavior if the file is opened via Telescope as if opened via :e.

Actual behavior

E13: File exists (add ! to override)

Minimal config

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
  defaults = { version=false },
  spec = {
    {
      "nvim-telescope/telescope.nvim",
      dependencies = { 'nvim-lua/plenary.nvim' }
    },
    {
      "nvim-treesitter/nvim-treesitter",
      version = false,
      build = ":TSUpdate",
      event = { "BufReadPost", "BufNewFile" },
      -- init = function(plugin)
      --   -- PERF: add nvim-treesitter queries to the rtp and it's custom query predicates early
      --   -- This is needed because a bunch of plugins no longer `require("nvim-treesitter")`, which
      --   -- no longer trigger the **nvim-treeitter** module to be loaded in time.
      --   -- Luckily, the only thins that those plugins need are the custom queries, which we make available
      --   -- during startup.
      --   require("lazy.core.loader").add_to_rtp(plugin)
      --   require("nvim-treesitter.query_predicates")
      -- end,
      dependencies = {
        {
          "nvim-treesitter/nvim-treesitter-textobjects",
          init = function()
            require("lazy.core.loader").disable_rtp_plugin("nvim-treesitter-textobjects")
            load_textobjects = true
          end,
        },
      },
      cmd = { "TSUpdateSync" },
      opts = {
        highlight = { enable = true },
        incremental_selection = {
          enable = true,
          keymaps = {
            node_incremental = "v",
            node_decremental = "V",
          },
        },
        indent = { enable = true },
        ensure_installed = {
          "bash",
          "comment",
          "hcl",
          "html",
          "javascript",
          "json",
          "lua",
          "markdown",
          "markdown_inline",
          "norg",
          "org",
          "python",
          "query",
          "regex",
          "tsx",
          "typescript",
          "vim",
          "vimdoc", -- from lazyvim
          "yaml",
        },
      },
      config = function(_, opts)
        if type(opts.ensure_installed) == "table" then
          ---@type table<string, boolean>
          local added = {}
          opts.ensure_installed = vim.tbl_filter(function(lang)
            if added[lang] then return false end
            added[lang] = true
            return true
          end, opts.ensure_installed)
        end
        require("nvim-treesitter.configs").setup(opts)
      end,
    },
  }
})

video:

https://github.com/nvim-telescope/telescope.nvim/assets/3605078/dfdfc48e-3936-440f-bd16-27e477546fa0

theherk avatar Jan 25 '24 19:01 theherk

Sorry just to clarify, I should be able to replicate this with your minimal config if I uncomment out the treesitter init config?

I tried with and without the init function and I can't reproduce this.

https://github.com/nvim-telescope/telescope.nvim/assets/66286082/0580920d-e6cb-4ff3-8124-5e58644631e8

Have you tried your own minimal config? Your screen recording seems to suggest you're just using your standard config.

jamestrew avatar Jan 26 '24 15:01 jamestrew

Yes. I did it with the minimal config given, but that was after the screen recording was done. However, I can't promise you can recreate it because I can't sort the condition where this arises. But toggling this commented section on and off does resolve and unresolve the issue. The comment provided by lazyvim may clarify the source of the issue, but I'm not sure. If you want to close it out of hand, that's fine, but there is an issue however difficult to induce.

theherk avatar Jan 26 '24 15:01 theherk

My guess is that Telescope tries to use Treesitter highlighting in the preview(note the error in the picker preview), but doesn't trigger any of the two lazy loading trigger events, and this breaks Treesitter, which in turn somehow breaks :w (???)

One workaround is to not lazy load, or use an earlier event (VeryLazy or put Treesitter in the dependencies of Telescope)

Btw this thing randomly stops reproducing for me, and what's weirder, it only happens with lua files

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1693350652
-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "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", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
	"folke/tokyonight.nvim",
	{
		"nvim-treesitter/nvim-treesitter",
		build = ":TSUpdate",
		event = { "BufReadPre", "BufNewFile" },
		config = function()
			require("nvim-treesitter.configs").setup({
				highlight = { enable = true },
			})
		end,
	},
	{
		"nvim-telescope/telescope.nvim",
		dependencies = {
			"nvim-lua/plenary.nvim",
		},
		keys = { {
			"<space>fo",
			function()
				require("telescope.builtin").find_files()
			end,
		} },
	},
}
require("lazy").setup(plugins, {
	root = root .. "/plugins",
})

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

https://github.com/nvim-telescope/telescope.nvim/assets/45295405/15550582-3909-4d81-bcb3-f5249515e34c

anuramat avatar Feb 15 '24 20:02 anuramat

I revisited the minimal config with neovim 0.9.5 and I can reproduce the issue. But not with neovim nightly. The Neovim version seems to be a common ground between users experiencing this and other similar issues.

I'm going to say this is upstream issue that's been fixed.

jamestrew avatar Mar 01 '24 14:03 jamestrew