epwalsh / obsidian.nvim

Obsidian 🤝 Neovim
Apache License 2.0
3.3k stars 150 forks source link

[Bug] Wikilink autocomplete appears to have stopped working #538

Open crashmoney opened 2 months ago

crashmoney commented 2 months ago

🐛 Describe the bug

Wikilink autocomplete has inexplicably stopped working on my neovim, sometime within the last few days. :ObsidianFollowLink has stopped working as well, though sometimes (not sure what is the deciding factor) it and gf works to follow wikilinks.

I thought the bug might resolve itself at first, but alternately commenting my nvim-cmp, telescope.nvim and obsidian.nvim configurations out to try and isolate the problem hasn't worked out. I've also checked for updates on all plugins and neovim itself.

A note on the config pasted below – I realize that the README says not to specify a source on nvim-cmp, but mine wasn't picking it up properly until I listed obsidian, obsidian_tags, and obsidian_new. Removing the source specifications don't resolve the problem.

P.S. Thank you for this great plugin! It's one of my daily drivers in neovim.

Config

-- OBSIDIAN {{{
  require("obsidian").setup {
    workspaces = { -- path to vault directories
      {
        name = "vault",
        path = os.getenv("HOME") .. "/Library/Mobile Documents/iCloud~md~obsidian/Documents/The Vault",
        overrides = {
          templates = {
            subdir = "03 Resources/Templates",
            date_format = "%Y-%m-%d %A",
            time_format = "%H:%M",
            substitutions = { } -- A map for custom variables, the key should be the variable and the value a function
          },
          daily_notes = {
            folder = "02 Areas/Daily Notes",
            template = "Daily Note.md"
          }
        }
      },
      {
        name = "project",
        path = os.getenv("HOME") .. "/Library/Mobile Documents/iCloud~md~obsidian/Documents/project",
        overrides = {
          templates = {
            subdir = "x-config 🔧/templates",
            substitutions = { }
          }
        }
      },
      {
        name = "vault 2",
        path = os.getenv("HOME") .. "/Library/Mobile Documents/iCloud~md~obsidian/Documents/vault 2",
        overrides = {
          templates = {
            subdir = "extras 📁/templates 📝",
            substitutions = {  }
          }
        }
      },
      {
        name = "writing",
        path = os.getenv("HOME") .. "/Library/Mobile Documents/iCloud~md~obsidian/Documents/Writing",
        overrides = {
          templates = {
            subdir = "templates",
            substitutions = { }
          }
        }
      }
    },
    completion = {
      nvim_cmp = true,
      min_chars = 1, -- trigger completion
    },
    mappings = {
      -- overrides the 'gf' mapping to work on markdown/wiki links within the vault
      ["gf"] = {
        action = function()
          return require("obsidian").util.gf_passthrough()
        end,
        opts = { noremap = false, expr = true, buffer = true },
      },

      -- toggle check-boxes
      ["<leader>ch"] = {
        action = function()
          return require("obsidian").util.toggle_checkbox()
        end,
        opts = { buffer = true },
      },
    },

    picker = {
      name = "telescope.nvim",
      -- Create a new note from your query.
      new = "<C-x>",
      -- Insert a link to the selected note.
      insert_link = "<C-L>",
    },

    -- sort_by = "modified",
    -- sort_reversed = "true",
    disable_frontmatter = true -- disable frontmatter
  }
-- }}}

-- CMP {{{
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
        -- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
      end,
    },
    mapping = {
      ["<C-p>"] = cmp.mapping.select_prev_item(),
      ["<C-n>"] = cmp.mapping.select_next_item(),
      ["<C-d>"] = cmp.mapping.scroll_docs(-4),
      ["<C-f>"] = cmp.mapping.scroll_docs(4),
      ["<C-e>"] = cmp.mapping.close(),
      ["<CR>"] = cmp.mapping.confirm {
        behavior = cmp.ConfirmBehavior.Insert,
      },
      ['<C-Space>'] = cmp.mapping(function(fallback)
        if cmp.visible() then
          cmp.confirm({ select = true })
        else
          fallback()
        end
      end, { "i", "s" }),
    },
    sources = cmp.config.sources({
      { name = 'vsnip' },
      { name = 'obsidian' },
      { name = 'obsidian_new' },
      { name = 'obsidian_tags' }
    })
  })
-- }}}

-- TELESCOPE.NVIM {{{
  -- keymaps
  require('telescope').setup({
    defaults = {
      mappings = {
        n = { },
        i = {
          ["<C-h>"] = "which_key",
        }
      },
    },
    -- layout_config = {
    --   horizontal = {
    --     preview_cutoff = 0,
    --   },
    -- },
  })
  local builtin = require('telescope.builtin')
  vim.keymap.set('n', '<C-p>', builtin.find_files, {})
  vim.keymap.set('n', '<leader>r', builtin.live_grep, {})
  vim.keymap.set('n', '<leader>b', builtin.buffers, {})
  vim.keymap.set('n', '<leader>t', builtin.builtin, {})
-- }}}

Environment

$ nvim --version

NVIM v0.10.0-dev-2095+ga34451982-Homebrew
Build type: Release
LuaJIT 2.1.1710088188
Run "nvim -V1 -v" for more info

$ nvim --headless -c 'lua require("obsidian").info()' -c q

Obsidian.nvim v3.7.5 (df0c5cce68a02481c0d40d1b25433e368fd12a5a)
Status:
  • buffer directory: nil
  • working directory: /Users/crashmoney
Workspaces:
  ✓ active workspace: Workspace(name='vault', path='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/The Vault', root='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/The Vault')
  ✗ inactive workspace: Workspace(name='project', path='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/project', root='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/project')
  ✗ inactive workspace: Workspace(name='vault 2', path='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/vault 2', root='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/vault 2')
  ✗ inactive workspace: Workspace(name='writing', path='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/Writing', root='/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents/Writing')
Dependencies:
  ✓ plenary.nvim: 8aad4396840be7fc42896e3011751b7609ca4119
  ✓ nvim-cmp: ce16de5665c766f39c271705b17fff06f7bcb84f
  ✓ telescope.nvim: d90956833d7c27e73c621a61f20b29fdb7122709
Integrations:
  ✓ picker: TelescopePicker()
  ✓ completion: enabled (nvim-cmp) ✓ refs, ✓ tags, ✓ new
    all sources:
      • vsnip
      • obsidian
      • obsidian_new
      • obsidian_tags
Tools:
  ✓ rg: ripgrep 14.1.0
Environment:
  • operating system: Darwin
Config:
  • notes_subdir: nil
epwalsh commented 2 months ago

Hey @crashmoney sorry to hear your setup isn't working. Can you give me an example of a note filename and wikilink reference to that note that fails to work?

crashmoney commented 2 months ago

Hi @epwalsh, thanks for your quick reply. Unfortunately there isn't any specific filename that throws this error. It simply shows the 'create new note' option in cmp, as if the note doesn't exist. I can see this happening even when I type out the name of a note that exists within the vault; it doesn't seem to be searching for existing notes at all.

For example, typing [[Notes MOC]] simply pops up the (create) Reference completion. Since reporting this issue yesterday, the autocomplete search spontaneously worked on one of my secondary vaults and has since ceased to work.

If it helps, I'm using ripgrep/fzf and usually don't have any issues with the setup. My main vault is hovering at around a few thousand files including config ones (I'd say 3k-4k, which shouldn't be too much of an issue for ripgrep in any case).

epwalsh commented 2 months ago

I'm just looking for an example so I can try to repro. What's the path of the note corresponding to [[Notes MOC]]?

crashmoney commented 2 months ago

00 Atlas 🌎/Notes MOC.md.

At first I thought it might be the emoji, but other paths without emojis (e.g. 02 Areas/Notes/Example.md) present similarly.

epwalsh commented 2 months ago

Hmm yea that works fine for me even with the emoji and spaces in the filenames, and both with and without frontmatter 🤔

If you get a change, try running this Lua script and see if it collects any results:

-- save to 'tmp.lua', then from a markdown file in your vault, run with ':luafile tmp.lua'
local client = require("obsidian").get_client()
local results = client:find_notes "Notes MOC"
vim.print(string.format("Found %s notes:", #results))
for _, note in ipairs(results) do
  vim.print(string.format(" - '%s'", note:display_name()))
end
crashmoney commented 2 months ago

Hi @epwalsh, I tried out the script you gave and got this:

E5113: Error while calling lua chunk: tmp.lua:3: calling 'find_notes' on bad self (table expected, got nil)
stack traceback:
        [C]: in function 'find_notes'
        tmp.lua:3: in main chunk

It doesn't look like it managed to collect anything, unfortunately :(

crashmoney commented 2 months ago

Hi @epwalsh, I managed to catch an error message earlier today that might help – it seems that the problem may lie with ripgrep, though I don't think I configured it beyond defaults.

Captured stderr output while running command 'rg' with args '{ "--no-config", "--type=md", "--json", "--ignore-case",
"-g!03 Resources/Templates", "-e", "#[A-Za-z0-9_/-]*s[A-Za-
z0-9_/-]*", "-e", "\\s*- [A-Za-z0-9_/-]*s[A-Za-z0-9_/-]*$",
 "-e", "tags: .*[A-Za-z0-9_/-]*s[A-Za-z0-9_/-]*", "/Users/crashmoney/Library/Mobile Documents/iCloud~md~obsidian/Documents
/vault" }'
[stderr] rg: /Users/crashmoney/Library/Mobile Documents/iCloud~
md~obsidian/Documents/vault/02 Areas/Daily Notes/2021/2
021-05/2021-05-14.md: No such file or directory (os
error 2)

They both repeat, though the second command does so with varying file names.

epwalsh commented 2 months ago

@crashmoney that would definitely explain it! Now I wonder why it doesn't resolve that directory. Maybe the spaces in the path need to be escaped?

epwalsh commented 2 months ago

Although my personal vault is also on the same iCloud path but I've never had issues with that 🤔

crashmoney commented 2 months ago

Hey @epwalsh, just an update on this issue. gf has started working again with no explanation. I've since switched plugin managers to lazy.nvim and updated the plugin, so maybe those are factors?

The repeating messages are still showing up, even when I use files outside of my Obsidian vaults. However, autocomplete has started working again with a few strange quirks. With certain files, I have to type the exact beginning of a file to see it. Otherwise it presents me with half the file name. When this happens, the full name might show up at the bottom after a lot of header references – but I haven't observed this happening consistently.

I can also see the pop-up, likely from cmp, but can't select any of the file names I can see. This might be a quirk of my config, but I don't have this issue typing in any other files or autocompleting any other pop-ups.

Overall, it's a rather confounding issue 😅 Any further troubleshooting tips would be greatly appreciated!