tree-sitter: tree-sitter highlight has does not find the language.

I wanted to try out tree-sitter highlight but it seems like it has a near impossible time figuring out what language to use. For example, attempting to run tree-sitter highlight .\examples\jquery.js for tree-sitter-javascript yields No language found for path ".\\examples\\jquery.js".

Repro on Linux:

git clone --depth=1 https://github.com/tree-sitter/tree-sitter-javascript
cd tree-sitter-javascript
npm i
./node_modules/tree-sitter-cli/tree-sitter highlight ./examples/jquery.js

# yields
# No language found for path "./examples/jquery.js"

Despite that, tree-sitter test is able to run the highlighter tests just fine.

I’ve tried to get syntax highlighting working in my project, but have had no luck getting either the highlighting tests or the highlight command to work. Hopefully this issue can help shed some light on this problem.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 35 (14 by maintainers)

Most upvoted comments

Things that worked for me in the past.

First. See the path of the config file with tree-sitter init-config

$ tree-sitter init-config
Remove your existing config file first: /home/muller/.tree-sitter/config.json

Now, see if the "parser-directories" in the config.json`` is set correctly:

If your grammar is at /home/foo/git/tree-sitter-go/src/parser.c the parser-directories should include /home/foo/git.

Does it show when you run tree-sitter dump-languages?

See if there is any file path on tree-sitter-go/package.json and check if they are correctly set (might try to include some file that doesn’t exist).

Lastly, you can try running tree-sitter generate at tree-sitter-go. I had issues fix themselves after running this (might have been a coincidence).

After adding the correct path (for future readers - in my case, because the directory I was working in was ~/tree-sitter-javascript, I had to add ~ to the parser-directories subsection, with the config file located at ~/.tree-sitter/config.json after running ./tree-sitter init-config), it does!

It seems odd that tree-sitter doesn’t use the same behavior it does for tree-sitter test when looking for a highlighter to use. Perhaps it should? (I’d be glad to take up a PR to do so)

I’m seeing the module I’m trying to author, but it’s output in dump-languages looks wrong. Ringing my head after following the docs and suggestions here. I’ve tried both the local and globally installed tree-sitter, both giving the same results.

$ tree-sitter dump-languages
scope: 
parser: "/home/tony/nvim/tree-sitter-cue"
highlights: None
file_types: []
content_regex: None
injection_regex: None

$ tree-sitter highlight test/cue/first.cue
No language found for path "test/cue/first.cue"

  // from package.json
  "tree-sitter": {
    "scope": "source.cue",
    "file-types": ["cue"]
  }
  // from ~/.config/tree-sitter/config.json
  "parser-directories": [
    "/home/tony/nvim"
  ],

~Any thoughts or suggestions that could help here?~

The issue was that I did not make the entry in package.json a list…

@ahlinc Nope, it works fine on the upstream CLI. Now I’m just getting ERROR AST nodes without any explanation or what was expected.

@ahlinc I fixed it, my JSON file had a syntax error. (Oops?)

Issue summary

tree-sitter highlight and tree-sitter test don’t behave in the same way.

  • Test command works with the language defined in the current folder.
  • Highlight requires adding the parent-folder path to "parser-directories" in the config file (that’s the solution)

Preferred solution

  • Make Highlight behave just like test.

I hope this helps

TODO:

  • Add a tips flag to tree-sitter generator that would show suggestions and in this case it will suggest to set the scope field as source.LANG_NAME by meaning that it’s best practice to to name languages with the source. prefix.
  • In case of missing language add to tree-sitter parser error output suggestions what to check to resolve the issue.
  • Improve tree-sitter-loader select_language(...) behaviour. Actually done, but needs public PR.

Thanks @alemuller I didn’t think to try tree-sitter dump-languages. It does find the language but I noticed all languages have highlights: None, per following terminal dump:

(base) [gordon@hypersonic tree-sitter-go]$ tree-sitter dump-languages
scope: input.turbowave
parser: "/home/gordon/codes/tree-sitter-turbowave/"
highlights: None
file_types: ["tw"]
content_regex: None
injection_regex: None

scope: source.bas
parser: "/home/gordon/codes/tree-sitter-applesoft/"
highlights: None
file_types: ["bas"]
content_regex: None
injection_regex: None

scope: source.go
parser: "/home/gordon/codes/tree-sitter-go/"
highlights: None
file_types: ["go"]
content_regex: None
injection_regex: None

Is that a clue? The rest seems to check out. I did try tree-sitter generate as well (again) but still the same thread panic.