Jakobeha / type-sitter

generate typed wrappers for tree-sitter grammars from node-types.json and queries
18 stars 2 forks source link

Generated query references nonexistent `tree_sitter_foobar::language` symbol #7

Closed eikopf closed 4 days ago

eikopf commented 6 days ago

I found that while trying to generate code from a very simple query, the generated code made reference to a tree_sitter_foobar::language symbol. My understanding is that this is outdated, and was replaced with the LANGUAGE: LanguageFn symbol in an older Tree-sitter version.

I'm using tree-sitter v0.22.6 and type-sitter-gen v0.4.0. The query I was trying to generate is this:

(comment)+ @comments

My build.rs looks like this:

use std::path::{Path, PathBuf};
use std::{env, fs};
use type_sitter_gen::{generate_nodes, generate_queries, super_nodes, tree_sitter};

fn main() {
    // common setup
    let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
    println!("cargo::rerun-if-changed=build.rs");

    // rerun if the grammar changes
    println!("cargo::rerun-if-changed=../../tree-sitter-foobar");

    // generate CST nodes
    let path = Path::new("../../tree-sitter-foobar/src/node-types.json");
    fs::write(
        out_dir.join("nodes.rs"),
        generate_nodes(path, &tree_sitter())
            .unwrap()
            .collapse() // this line doesn't appear in the type-sitter README
            .to_string(),
    )
    .unwrap();

    // generate queries for CST analysis
    fs::write(
        out_dir.join("queries.rs"),
        generate_queries(
            "../../tree-sitter-foobar/queries",
            "../../tree-sitter-foobar",
            &super_nodes(),
            false,
            &tree_sitter(),
        )
        .unwrap()
        .to_string(),
    )
    .unwrap();
}

This might just a version mismatch? I'm not sure. It's probably also worth noting that this build script is a component of a lib crate in a larger workspace.

This is the error produced when I run cargo build inside the relevant crate:

image

eikopf commented 4 days ago

Found a hacky fix for now: by pointing to the type-sitter and type-sitter-gen crates in this repository (rather than on crates.io), the project was able to build. I assume the issue is that the current documentation in the README reflects the current state of the main branch, rather than the published version on crates.io (v0.4.0).

For now I'll plug in a particular revision to keep my project as stable as possible, but hopefully we get a v0.5.0 release soon?

Jakobeha commented 4 days ago

Hi, sorry for the delay. I was holding off publishing v0.5 until someone asked in case there were any last-minute API changes. I just published everything now.

Note for the version on crates.io, I also updated everything to tree-sitter v0.24 (the latest version) which has some more API changes. Specifically, QueryMatches and QueryCaptures are both streaming-iterators. So the version you're pointing to only works with tree-sitter v0.23, and the version on crates.io only works with v0.24.

Jakobeha commented 4 days ago

Also, v0.5 has a lot of its own changes, one of which is that you shouldn't need to put tree-sitter in Cargo.toml anymore unless you're also using yak-sitter, because otherwise everything in tree-sitter is exposed via type_sitter::raw.