Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error (freezes nvim) when opening markdown with code block name in different case (e.g. "Python", "JSON", "R"): no such language: R #6642

Closed
milanglacier opened this issue May 17, 2024 · 13 comments
Labels
bug Something isn't working

Comments

@milanglacier
Copy link
Contributor

milanglacier commented May 17, 2024

Describe the bug

This is a macOS only bug, I cannot reproduce it in linux.

When opening a markdown file that contains code block with same name with the treesitter parser but have different case (e.g. python vs Python, r vs R, Json vs json), you will get the following error:

stack traceback:
        [C]: in function '_ts_parse_query'
        ...m/0.10.0/share/nvim/runtime/lua/vim/treesitter/query.lua:252: in function 'fn'
        ...ovim/0.10.0/share/nvim/runtime/lua/vim/func/_memoize.lua:58: in function 'fn'
        ...ovim/0.10.0/share/nvim/runtime/lua/vim/func/_memoize.lua:58: in function 'get'
        ...0/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:128: in function 'new'
        ...0/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:511: in function 'add_child'
        ...0/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:395: in function '_add_injections'
        ...0/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:447: in function 'parse'
        ....0/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:397: in function <....0/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:392>
Error in decoration provider treesitter/highlighter.win:
Error executing lua: ...m/0.10.0/share/nvim/runtime/lua/vim/treesitter/query.lua:252: no such language: R

To Reproduce

  1. nvim --clean -u minimal-init.lua
  2. Opening the following example markdown file, save it to a name, say: hello.md: https://pastebin.com/XBZKgFiy
  3. Open it in neovim, get billions of errors so that you can't do anything with neovim.

Below is my example config:

local plugins = {
	ts = "https://github.com/nvim-treesitter/nvim-treesitter",
}

for name, url in pairs(plugins) do
	local install_path = "/tmp/nvim/site/" .. name
	if vim.fn.isdirectory(install_path) == 0 then
		vim.fn.system({ "git", "clone", "--depth=1", url, install_path })
	end
	vim.o.runtimepath = install_path .. "," .. vim.o.runtimepath
end

require("nvim-treesitter.configs").setup({
	-- One of "all", "maintained" (parsers with maintainers), or a list of languages
	ensure_installed = {
		"r",
	},
	sync_install = true,
	highlight = {
		enable = true,
	},
})


Expected behavior

  1. nvim should open this file normally.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.22.6 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v21.7.2 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 14.0.3 (clang-1403.0.22.14.1)
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "22.5.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 22.5.0: Mon Apr 24 20:53:44 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T8103"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - cpp                 ✓ ✓ ✓ ✓ ✓
  - css                 ✓ . ✓ ✓ ✓
  - go                  ✓ ✓ ✓ ✓ ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - javascript          ✓ ✓ ✓ ✓ ✓
  - json                ✓ ✓ ✓ ✓ .
  - latex               ✓ . ✓ . ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ ✓ ✓
  - markdown_inline     ✓ . . . ✓
  - org                 ✓ . ✓ . ✓
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - r                   ✓ ✓ . ✓ ✓
  - regex               ✓ . . . .
  - sql                 ✓ . . ✓ ✓
  - toml                ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713773202
Run "nvim -V1 -v" for more info

Additional context

R and r are both considered as "Canonical" name for R language, so you would expect to see different authors use R and r interchangably from different sources. While treesitter uses r as the parser name, the nvim-treesitter should handles language name R robustly.

@milanglacier milanglacier added the bug Something isn't working label May 17, 2024
@milanglacier milanglacier changed the title markdown with code block name in arbitrary case (e.g. "Python", "JSON", "R"): Error in decoration provider treesitter/highlighter.win: Error executing lua: ...m/0.10.0/share/nvim/runtime/lua/vim/treesitter/query.lua:252: no such language: R Error when opening markdown with code block name in different case (e.g. "Python", "JSON", "R"): no such language: R May 17, 2024
@milanglacier milanglacier changed the title Error when opening markdown with code block name in different case (e.g. "Python", "JSON", "R"): no such language: R Error (freezes nvim) when opening markdown with code block name in different case (e.g. "Python", "JSON", "R"): no such language: R May 17, 2024
@lucario387
Copy link
Member

This is intended. See neovim/neovim#28546

@milanglacier
Copy link
Contributor Author

This is intended. See neovim/neovim#28546

But you can never ensure language name in markdown code block is lower case. Others who do not use neovim write markdown code block in other cases, and then you open that file and your neovim session just freezes...

@iphauman

This comment was marked as duplicate.

@clason
Copy link
Contributor

clason commented May 17, 2024

I cannot reproduce this on macOS. And it's important here to distinguish the codeblock annotation (which is case-insensitive) and the language/parser name in your config/on your hard-drive (which must be lower-case).

@milanglacier
Copy link
Contributor Author

Screen.Recording.2024-05-17.at.03.49.29.mov

Please let me know if this is clear enough. The minimal config file and the sample markdown file has been already provided in the issue.

It is worth to note that The r parser is automatically installed by nvim-treesitter, and it is r.so, but the code block name in the markdown is R, not r.

@milanglacier
Copy link
Contributor Author

I use the release from github, not from homebrew. I delete all the files from ~/.cache/nvim. And the issue is still there. Are there any other potential problems occurred?

@clason
Copy link
Contributor

clason commented May 17, 2024

I'll have to investigate. It's a race condition (it doesn't happen if you :e the file after starting) that only appears with nvim-treesitter -- and only on master. If this is not an easy fix (which it looks like it isn't), then I'm afraid this will be "wontfix" and you are recommended to use the main branch on Nvim 0.10+.

@milanglacier
Copy link
Contributor Author

milanglacier commented May 17, 2024

actually the following patch works for me:

But I don't know if this is too simple where a lot of cases are not taken into account. But for Python vs python, JSON vs json, R vs r, it just works.

diff --git a/lua/nvim-treesitter/query_predicates.lua b/lua/nvim-treesitter/query_predicates.lua
index d4726107..e48f3279 100644
--- a/lua/nvim-treesitter/query_predicates.lua
+++ b/lua/nvim-treesitter/query_predicates.lua
@@ -17,7 +17,7 @@ local non_filetype_match_injection_language_aliases = {

 local function get_parser_from_markdown_info_string(injection_alias)
   local match = vim.filetype.match { filename = "a." .. injection_alias }
-  return match or non_filetype_match_injection_language_aliases[injection_alias] or injection_alias
+  return match or non_filetype_match_injection_language_aliases[injection_alias] or injection_alias:lower()
 end

 local function error(str)

@clason
Copy link
Contributor

clason commented May 17, 2024

Thank you, I forgot that master still uses that kludge (it's obsolete with 0.10). Your fix is correct, but I'll see how to best deal with that in the deprecation PR.

@clason
Copy link
Contributor

clason commented May 17, 2024

Hmm, no, because we can't do optional queries. So let's go with your patch. Do you want to make a PR for it? (Best to just lower the input argument, though, so the lookup also works.)

@milanglacier
Copy link
Contributor Author

you are recommended to use the main branch on Nvim 0.10+.

Thanks. Good to know that there is a main branch. Will definitely play around with the new main branch.

@milanglacier
Copy link
Contributor Author

Hmm, no, because we can't do optional queries. So let's go with your patch. Do you want to make a PR for it?

Ok. I can submit a PR for it.

@clason
Copy link
Contributor

clason commented May 17, 2024

Fixed by #6648

@clason clason closed this as completed May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants