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

[bug] Java rule contains invalid kind matcher despite working on playground #1648

Closed
1 task done
jbarr21 opened this issue Dec 4, 2024 · 5 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@jbarr21
Copy link

jbarr21 commented Dec 4, 2024

Please read the FAQ for the bug you encountered.

  • I have read the existing FAQ

⏯ Playground Link

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmEiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoic2lnbmF0dXJlIiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogc3VwZXJ0eXBlcy1qYXZhXG5sYW5ndWFnZTogamF2YVxucnVsZTpcbiAgYW55OlxuICAgIC0ga2luZDogdHlwZV9pZGVudGlmaWVyXG4gICAgLSBraW5kOiBzY29wZWRfdHlwZV9pZGVudGlmaWVyXG4gICAgLSBraW5kOiBnZW5lcmljX3R5cGVcbiAgaW5zaWRlOlxuICAgIGFueTpcbiAgICAgIC0ga2luZDogaW50ZXJmYWNlX3R5cGVfbGlzdFxuICBwYXR0ZXJuOiAkTkFNRSIsInNvdXJjZSI6ImNsYXNzIE15Q2xhc3MxXG4gICAgXG5jbGFzcyBNeUNsYXNzMiB7IH1cblxuY2xhc3MgTXlDbGFzczMgZXh0ZW5kcyBQYXJlbnRDbGFzcyB7IH1cblxuY2xhc3MgTXlDbGFzczM1IGV4dGVuZHMgUGFyZW50Q2xhc3MuSW1wbCB7IH1cblxuY2xhc3MgTXlDbGFzczM1MiBleHRlbmRzIFBhcmVudENsYXNzLkltcGw8VHlwZT4geyB9XG5cbmNsYXNzIE15Q2xhc3M0IGltcGxlbWVudHMgTXlJbnRlcmZhY2UgeyB9XG5cbmNsYXNzIE15Q2xhc3M0NSBpbXBsZW1lbnRzIE15SW50ZXJmYWNlLkltcGwgeyB9XG5cbmNsYXNzIE15Q2xhc3M0NTIgaW1wbGVtZW50cyBNeUludGVyZmFjZS5JbXBsPFR5cGU+IHsgfVxuXG5jbGFzcyBNeUNsYXNzNSBleHRlbmRzIFBhcmVudENsYXNzIGltcGxlbWVudHMgTXlJbnRlcmZhY2UgeyB9XG5cbmNsYXNzIE15Q2xhc3M2IGV4dGVuZHMgUGFyZW50Q2xhc3MgaW1wbGVtZW50cyBNeUludGVyZmFjZTEsIE15SW50ZXJmYWNlMiB7IH1cblxuY2xhc3MgTXlDbGFzczdcbiAgICBleHRlbmRzIFBhcmVudENsYXNzXG4gICAgaW1wbGVtZW50cyBNeUludGVyZmFjZTEsXG4gICAgTXlJbnRlcmZhY2UyIHsgfVxuIn0=

💻 Code

Rule: ~/supertypes-java.yaml to match every java interface a class implements

id: supertypes-java
language: java
rule:
  any:
    - kind: type_identifier
    - kind: scoped_type_identifier
    - kind: generic_type
  inside:
    any:
      - kind: interface_type_list
  pattern: $NAME

🙁 Actual behavior

 % ast-grep scan -r ~/supertypes-java.yaml --json=pretty
Error: Cannot parse rule /home/user/supertypes-java.yaml
Help: The file is not a valid ast-grep rule. Please refer to doc and fix the error.
See also: https://ast-grep.github.io/guide/rule-config.html

✖ Caused by
╰▻ Fail to parse yaml as Rule.
╰▻ `rule` is not configured correctly.
╰▻ Rule contains invalid kind matcher.
╰▻ Kind `interface_type_list` is invalid.

🙂 Expected behavior

Expected to match java interfaces on class declarations as in the Playground link which shows proper matches

Screenshot 2024-12-04 at 10 33 27 AM

Additional information about the issue

Tried using ast-grep v0.30.0 and v0.31.0

@jbarr21 jbarr21 added the bug Something isn't working label Dec 4, 2024
@HerringtonDarkholme
Copy link
Member

BTW the tree-sitter parser version has been updated in the playground

image

@jbarr21
Copy link
Author

jbarr21 commented Dec 4, 2024

faq link

@HerringtonDarkholme I saw the FAQ, but based on the ast-grep help, it seems --debug-query is only applicable to patterns & I am using a rule. is there any further way to debug the rule since using it on rules provides no additional info?

utf-8 vs utf-16

would it be possible to have a flag for the CLI to run in either mode or is that too involved of a change?

parser versions

thank you for the parser update. would it be possible to list the parser version on the playground (even if need to hover over an info icon) so that the same environment could be recreated locally in a fork, if needed?

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Dec 4, 2024

is only applicable to patterns & I am using a rule

The issue is different parser version so you can use your source code as pattern to debug parser related issues.
For example you can use sg -p 'class P implements I {}' --debug-query=ast

would it be possible to have a flag for the CLI to run in either mode

No, this is an inherent issue in tree-sitter's library. tree-sitter wasm is a standalone library that uses utf-16 and has different heuristics. Using utf-16 does not help on CLI and it is probably not you want. The encoding error only happens when you have wrong pattern code, which you should not use anyway.

list the parser version on the playground

It isn't possible for now since all wasm files are updated manually (more precisely, some wasm files are downloaded from tree-sitter playground which don't have parser version)

tree-sitter also has the same issue tree-sitter/tree-sitter#3449

I have created an issue in ast-grep/ast-grep.github.io#605. While I don't have the capacity for it, open source contribution is warmly welcome!

@HerringtonDarkholme
Copy link
Member

I found an amazing thing that unpkg serves tree-sitter's wasm file. So it is possible to have the language version!

image

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

2 participants