github / stack-graphs

Rust implementation of stack graphs
https://docs.rs/stack-graphs/*/stack_graphs/
Apache License 2.0
759 stars 136 forks source link

How to make it work for generating stack graphs for Rust code? #462

Open Boscop opened 1 month ago

Boscop commented 1 month ago

Thanks for making this, it seems very useful. I was trying to figure out how to make it work for generating stack graphs for Rust. Apparently there is no existing tree-sitter-stack-graphs-rust crate, only for other languages. I'm curious: Why not for Rust yet (considering that tree-sitter-stack-graphs is written in Rust, so there must be some interest in Rust)? Would making it work for Rust be so discouragingly complex that nobody has added it yet? :)

I tried to generate it for Rust:

D:\dev\proj\tree-sitter-stack-graphs-rust>tree-sitter-stack-graphs init

Give the name of the programming language the stack graphs definitions in this
project will target. This name will appear in the project description and comments.
Language name: Rust

Give an identifier for Rust. This identifier will be used for the suggested project
name and suggested dependencies. May only contain letters, numbers, dashes, and
underscores.
Language identifier: rust

Give the file extension for Rust. This file extension will be used for stub files in
the project. May only contain letters, numbers, dashes, and underscores.
Language file extension: rs-stub

Give the crate name for this project. May only contain letters, numbers, dashes,
and underscores.
Crate name: tree-sitter-stack-graphs-rust

Give the crate version for this project. Must be in MAJOR.MINOR.PATCH format.
Crate version: 0.1.0

Give the project author in the format NAME <EMAIL>. Leave empty to omit.
Author:

Give the project license as an SPDX expression. Choose "Other" to input
manually. Press ESC to deselect. See https://spdx.org/licenses/ for possible
license identifiers.
License: None

Give the crate name for the Tree-sitter grammar that is to be used for
parsing. May only contain letters, numbers, dashes, and underscores.
Grammar crate name: tree-sitter-rust

Give the crate version the tree-sitter-rust dependency. This must be a valid Cargo
dependency version. For example, 1.2, ^0.4.1, or ~3.2.4.
See https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html.
Grammar crate version: 0.23.0

=== Review project settings ===
Project directory          : .
Language name              : Rust
Language identifier        : rust
Language file extension    : rs-stub
Project package name       : tree-sitter-stack-graphs-rust
Project package version    : 0.1.0
Project author             : 
Project license            :
Grammar dependency name    : tree-sitter-rust
Grammar dependency version : 0.23.0

Project created. See .\README.md to get started!

E.g. it asked me which version of tree-sitter-rust crate I want to use, I said 0.23.0, which is the latest), and it generated a rust workspace, but it doesn't compile:

error[E0425]: cannot find function `language` in crate `tree_sitter_rust`
  --> rust/lib.rs:25:27
   |
25 |         tree_sitter_rust::language(),
   |                           ^^^^^^^^ help: a constant with a similar name exists: `LANGUAGE`
   |
  ::: C:\Users\me\scoop\persist\rustup\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tree-sitter-rust-0.23.0\bindings/rust/lib.rs:35:1
   |
35 | pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_rust) };
   | ------------------------------ similarly named constant `LANGUAGE` defined here

But, if I use that constant, it also doesn't work, because it's of the wrong type:

error[E0308]: mismatched types
  --> rust/lib.rs:25:9
   |
24 |     LanguageConfiguration::from_sources(
   |     ----------------------------------- arguments to this function are incorrect
25 |         tree_sitter_rust::LANGUAGE,
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Language`, found `LanguageFn`

and .into() doesn't work either:

error[E0277]: the trait bound `tree_sitter::Language: From<tree_sitter_language::LanguageFn>` is not satisfied
  --> rust/lib.rs:25:36
   |
25 |         tree_sitter_rust::LANGUAGE.into(),
   |                                    ^^^^ the trait `From<tree_sitter_language::LanguageFn>` is not implemented for `tree_sitter::Language`, which is required by `tree_sitter_language::LanguageFn: Into<_>`
   |
   = note: required for `tree_sitter_language::LanguageFn` to implement `Into<tree_sitter::Language>`

Please tell me how I can make this work for generating stack graphs for Rust 🙂 Thanks 🙏

ohanf commented 2 weeks ago

set the version to 0.20.4, it looks like there was an API change in the grammar crates