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

Does Piranha support scala projects #199

Open
eankit opened this issue Jun 27, 2022 · 28 comments
Open

Does Piranha support scala projects #199

eankit opened this issue Jun 27, 2022 · 28 comments

Comments

@eankit
Copy link

eankit commented Jun 27, 2022

Does Piranha have support for scala with current code or can it be supported with minimalistic changes in java?

@mkr-plse
Copy link
Contributor

You can potentially add rules for Scala and make Piranha applicable for Scala. See here

@ketkarameya
Copy link
Collaborator

@eankit you can use https://github.com/tree-sitter/tree-sitter-scala for the grammar. I think the rules will be very similar to the ones implemented for Java/Kotlin.

@eankit
Copy link
Author

eankit commented Dec 6, 2022

can some help with how to and where to make changes in .rs files in the polygot piranha. I understand the .toml file syntaxes but it seems a lot goes into .rs files before it could be used in the toml file.
@ketkarameya @mkr-plse

@ketkarameya
Copy link
Collaborator

ketkarameya commented Dec 6, 2022

@eankit adding new language support requires very minimal changes .rs files . I am streamlining the implementation here - #283.

After this merges, it should be easy (though slightly tedious) to add scala support. I will update the README and keep you posted.

Basically you will have to do the following:

  1. Add configurations for Scala (like the parser, rules, )
  2. Write rules for cleaning up potentially stale code (You can get "inspiration" from our java or kotlin rules :) )
  3. Write tests to show that your rules work.

@eankit
Copy link
Author

eankit commented Dec 9, 2022

Hey @ketkarameya
I am getting this error while building the latest code on master, similar error is seen if I add scala as new language along with typescript, removing the ts and TSX language specific code from language.rs things seems to work fine.
What am I doing wrong

_Building wheels for collected packages: polyglot_piranha
Building wheel for polyglot_piranha (pyproject.toml) ... error
error: subprocess-exited-with-error

× Building wheel for polyglot_piranha (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [33 lines of output]
Running maturin pep517 build-wheel -i /Users/anbhanda/Documents/Pirahnah_Polygot/piranha/polyglot/piranha/.env/bin/python3 --compatibility off
📡 Using build options bindings from pyproject.toml
Compiling piranha v0.2.1 (/Users/anbhanda/Documents/Pirahnah_Polygot/piranha/polyglot/piranha)
error: could not compile piranha due to 3 previous errors
💥 maturin failed
Caused by: Failed to build a native library through cargo
Caused by: Cargo build finished with "exit status: 101": "cargo" "rustc" "--release" "--message-format" "json" "--lib" "--" "-C" "link-arg=-undefined" "-C" "link-arg=dynamic_lookup" "-C" "link-args=-Wl,-install_name,@rpath/polyglot_piranha.cpython-38-darwin.so"
🔗 Found pyo3 bindings
🐍 Found CPython 3.8 at /Users/anbhanda/Documents/Pirahnah_Polygot/piranha/polyglot/piranha/.env/bin/python3
error[E0308]: mismatched types
--> src/models/language.rs:154:19
|
154 | language: tree_sitter_typescript::language_typescript(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

  error[E0308]: mismatched types
     --> src/models/language.rs:163:19
      |
  163 |         language: tree_sitter_typescript::language_tsx(),
      |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `tree_sitter::Language`, found a different struct `tree_sitter::Language`
      |
      = note: perhaps two different versions of crate `tree_sitter` are being used?
  
  
  error: aborting due to 2 previous errors
  
  
  For more information about this error, try `rustc --explain E0308`.
  
  Error: command ['maturin', 'pep517', 'build-wheel', '-i', '/Users/anbhanda/Documents/Pirahnah_Polygot/piranha/polyglot/piranha/.env/bin/python3', '--compatibility', 'off'] returned non-zero exit status 1
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for polyglot_piranha
Failed to build polyglot_piranha
ERROR: Could not build wheels for polyglot_piranha, which is required to install pyproject.toml-based projects_

@ketkarameya
Copy link
Collaborator

ketkarameya commented Dec 9, 2022

I assume you get this error on adding the tree-sitter-scala right? I think there is a tree sitter version mismatch. How have u added it in Cargo.toml? Can you add the GitHub repo link there (like tree-sitter-java )

@eankit
Copy link
Author

eankit commented Dec 9, 2022

@ketkarameya I have added the it in Cargo.toml, I have added the one mentioned above -
My cargo.toml looks like this -
tree-sitter-kotlin = { git = "https://github.com/ketkarameya/tree-sitter-kotlin.git" }
tree-sitter-java = { git = "https://github.com/tree-sitter/tree-sitter-java.git" }
tree-sitter-swift = { git = "https://github.com/ketkarameya/tree-sitter-swift.git", branch = "add_parser" }
tree-sitter-strings = { git = "https://github.com/ketkarameya/tree-sitter-strings.git" }
tree-sitter-python = { git = "https://github.com/tree-sitter/tree-sitter-python.git" }
tree-sitter-go = { git = "https://github.com/tree-sitter/tree-sitter-go.git" }
tree-sitter-typescript = { git = "https://github.com/tree-sitter/tree-sitter-typescript.git" }
tree-sitter-scala = { git = "https://github.com/tree-sitter/tree-sitter-scala.git" }

@eankit
Copy link
Author

eankit commented Dec 9, 2022

@ketkarameya upon fetching the latest pull from master, I am getting this error without adding scala. This error is coming for typescript also up doing a pip install .

@ketkarameya
Copy link
Collaborator

ketkarameya commented Dec 9, 2022

But the CI is passing . What machine are you using ? ubuntu /mac / windows?

@eankit
Copy link
Author

eankit commented Dec 9, 2022

I am on my local mac

@ketkarameya
Copy link
Collaborator

So you are able to build the application locally (with no changes)?

@eankit
Copy link
Author

eankit commented Dec 9, 2022

no I have to make some change to run it locally, if I remove typescript from your code/master it is building code correctly

@eankit
Copy link
Author

eankit commented Dec 9, 2022

@ketkarameya after making the below change I am able to run the pip install . successfully

diff --git a/polyglot/piranha/src/models/language.rs b/polyglot/piranha/src/models/language.rs
index d5a90fc..639e371 100644
--- a/polyglot/piranha/src/models/language.rs
+++ b/polyglot/piranha/src/models/language.rs
@@ -148,24 +148,6 @@ impl From<&str> for PiranhaLanguage {
scopes: vec![],
comment_nodes: vec![],
},

  •  "ts" => PiranhaLanguage {
    
  •    name: language.to_string(),
    
  •    supported_language: SupportedLanguage::Ts,
    
  •    language: tree_sitter_typescript::language_typescript(),
    
  •    rules: None,
    
  •    edges: None,
    
  •    scopes: vec![],
    
  •    comment_nodes: vec![],
    
  •  },
    
  •  "tsx" => PiranhaLanguage {
    
  •    name: language.to_string(),
    
  •    supported_language: SupportedLanguage::Tsx,
    
  •    language: tree_sitter_typescript::language_tsx(),
    
  •    rules: None,
    
  •    edges: None,
    
  •    scopes: vec![],
    
  •    comment_nodes: vec![],
    
  •  },
     _ => panic!("Language not supported"),
    
    }
    }

@ketkarameya
Copy link
Collaborator

I am using a Mac too. I did the following

  1. git clone ...
  2. cd piranha/polyglot/piranha directory.
    Then ran the command cargo build --no-default-features (which ran successfully)

Are you doing the same?

@eankit
Copy link
Author

eankit commented Dec 9, 2022

I am following these steps

git clone https://github.com/uber/piranha.git
cd polyglot/piranha
Create a virtual environment:
python3 -m venv .env
source .env/bin/activate
Install Polyglot Piranha
pip install --upgrade pip
pip install . to run demo against current source code (please install Rust, it takes less than a minute)
Or, pip install polyglot-piranha to run demos against the latest release.

@eankit
Copy link
Author

eankit commented Dec 9, 2022

however running cargo build --no-default-features also gives me the following error, not sure what I am doing wrong ?

error[E0308]: mismatched types
--> src/models/language.rs:154:19
|
154 | language: tree_sitter_typescript::language_typescript(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

error[E0308]: mismatched types
--> src/models/language.rs:163:19
|
163 | language: tree_sitter_typescript::language_tsx(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

For more information about this error, try rustc --explain E0308.
error: could not compile piranha due to 2 previous errors

@ketkarameya
Copy link
Collaborator

Can you please try cargo build --no-default-features after cd piranha/polyglot/piranha step ?

@eankit
Copy link
Author

eankit commented Dec 9, 2022

@ketkarameya I wish I could share my screen

anbhanda@Ankits-MacBook-Pro-6 piranha % git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
anbhanda@Ankits-MacBook-Pro-6 piranha % cargo build --no-default-features
Compiling piranha v0.2.1 (/Users/anbhanda/Documents/Pirahnah_Polygot/piranha/polyglot/piranha)
error[E0308]: mismatched types
--> src/models/language.rs:154:19
|
154 | language: tree_sitter_typescript::language_typescript(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

error[E0308]: mismatched types
--> src/models/language.rs:163:19
|
163 | language: tree_sitter_typescript::language_tsx(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

For more information about this error, try rustc --explain E0308.
error: could not compile piranha due to 2 previous errors

@ketkarameya
Copy link
Collaborator

hmmm. Let me think. Can you see if installing tree-sitter cli help?
https://github.com/tree-sitter/tree-sitter/blob/master/cli/README.md

@ketkarameya
Copy link
Collaborator

ketkarameya commented Dec 9, 2022

Also can you start in a fresh directory?

git clone ...
cd piranha/polyglot/piranha directory.
cargo build --no-default-features

I think maturin has messed up your deps :|

@eankit
Copy link
Author

eankit commented Dec 9, 2022

@ketkarameya
Started in a fresh directory works fine for existing code!!!!
Thanks for your help, will start with making changes for Scala now.
Any suggestions that you want me to follow before starting up with the changes in language.rs and cargo.toml
Or do you feel it will be more changes required before I start to write the rules for scala

@ketkarameya
Copy link
Collaborator

Hey! I think once u add that struct for scala you will need to write rules (no more rust changes)

@eankit
Copy link
Author

eankit commented Dec 11, 2022

It compiles will with adding scala dependency in cargo.toml file but compilation fails with following error
It looks to me a rust issue
rust-lang/cargo#8536

anbhanda@-MacBook-Pro-6 piranha % cargo build --release --no-default-features
Compiling piranha v0.2.1 (/Users/anbhanda/Documents/Pirahnah_Polygot_2/piranha/polyglot/piranha)
error[E0308]: mismatched types
--> src/models/language.rs:172:19
|
172 | language: tree_sitter_scala::language(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct tree_sitter::Language, found a different struct tree_sitter::Language
|
= note: perhaps two different versions of crate tree_sitter are being used?

For more information about this error, try rustc --explain E0308.
error: could not compile piranha due to previous error
PS: I compiles just after doing git clone and making the required changes

@ketkarameya
Copy link
Collaborator

ketkarameya commented Dec 11, 2022

Yes, I is a tree-sitter version mismatch. So we (Piranha) is using 0.20.6, while tree-sitter scala is using 0.19.

I would recommend upstreaming the version update change for tree-sitter scala to something like this.
While the upstream accepts your changes, you can make a fork of tree-sitter-scala, update the tree-sitter version in a particular branch and point to this fork & branch of tree-sitter-scala (this will allow you to continue to experiment with piranha) in Piranha's Cargo.toml. Let me know if this works for you .

@ketkarameya
Copy link
Collaborator

This latest PR adds built-in cleanup rules for Go.
#265

You can use it as reference :)

@eankit
Copy link
Author

eankit commented Dec 12, 2022

@ketkarameya thanks a lot, forking scala tree-sitter worked.
Currently struggling with comprehending query language for scala, any help in this regard will be appreciated.

@ketkarameya
Copy link
Collaborator

Hmmm. yea.
Did you read the README throughly (specially this)? Where are you stuck at ? Can you make a PR for any work you have in progress

@ketkarameya
Copy link
Collaborator

@eankit. Can you create a PR for just adding Scala to Piranha ?
We can take it from there, one step at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants