getgrit / gritql

GritQL is a query language for searching, linting, and modifying code.
https://docs.grit.io/
MIT License
3.13k stars 77 forks source link

Allow for non-standard suffixes/filenames for languages #485

Closed dshahbaz closed 1 week ago

dshahbaz commented 1 month ago

I have files that should be treated as python, but do not end in .py. I can't convince grit to respect the files and treat them as python with any option or config value that I've found. Instead, grit fails with:

ERROR (code: 410) - Skipped <mypath here> since it is not a python file

My workaround is hardlinking to .py, but it would be ideal to force grit to ignore the suffix, or to register alternative patterns as a file type using grit.yaml.

morgante commented 1 month ago

I think there's two immediate things we could do:

Happy to review a PR.

morgante commented 1 week ago

I think option #1 is best.

morgante commented 1 week ago

/bounty $10

algora-pbc[bot] commented 1 week ago

💎 $10 bounty • Grit

Steps to solve:

  1. Start working: Comment /attempt #485 with your implementation plan
  2. Submit work: Create a pull request including /claim #485 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to getgrit/gritql!

Add a bounty • Share on socials

Attempt Started (GMT+0) Solution
🟢 @mobley-trent Oct 24, 2024, 11:14:16 AM WIP
🟢 @Alex-ley-scrub Oct 24, 2024, 10:46:04 PM #562
mobley-trent commented 1 week ago

/attempt #485

Algora profile Completed bounties Tech Active attempts Options
@mobley-trent 10 bounties from 5 projects
Python, Rust,
Jupyter Notebook
Cancel attempt
Alex-ley-scrub commented 1 week ago

/attempt #485

@dshahbaz what is the suffix of the file out of curiosity if you don't mind sharing?

It's not "pyi" by any chance is it? Or another (semi) valid/standard python extension? .pyi - Stub file (PEP 484)

I only ask because if it is .pyi I believe they are a true subset of .py files (they're just interface files with function signatues and type hints but no implementations, instead just have ... in the function bodies etc.) and should be able to be parsed just like a normal .py file.

@morgante should we consider to add "pyi" here (I've done that in this commit 63f113c in case the answer is yes, otherwise can revert):

https://github.com/getgrit/gritql/blob/a1b312bdefed4982f7a96304b88577762f7edcf1/crates/language/src/target_language.rs#L290

https://github.com/getgrit/gritql/blob/a1b312bdefed4982f7a96304b88577762f7edcf1/crates/language/src/target_language.rs#L346

This is my minimal reproducible example (added as a test case in this commit 9abf798):

These both fail for me using the released version of grit (with or without --force):

echo 'class MyClass(object): ...' > dummy.nopy
grit apply '`object` => ``' dummy.nopy --lang=python --force
# out: dummy.nopy: ERROR (code: 410) - Skipped dummy.nopy since it is not a python file
# out: Processed 2 files and found 0 matches

and

echo 'class MyClass(object): ...' > dummy.pyi
grit apply '`object` => ``' dummy.pyi --lang=python --force
# out: dummy.pyi: ERROR (code: 410) - Skipped dummy.pyi since it is not a python file
# out: Processed 2 files and found 0 matches

(as an aside the count of processed files seems to be doubled for the failed ones, which I also fixed in this commit ff3fafc)

see my PR attempt coming shortly

Algora profile Completed bounties Tech Active attempts Options
@Alex-ley-scrub 2 grit bounties
Rust
Cancel attempt
morgante commented 1 week ago

Adding .pyi seems reasonable, as well as fixing the overall problem.

morgante commented 1 week ago

/assign @Alex-ley-scrub

algora-pbc[bot] commented 1 week ago

💡 @Alex-ley-scrub submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] commented 1 week ago

🎉🎈 @Alex-ley-scrub has been awarded $10! 🎈🎊

dshahbaz commented 6 days ago

Thank you! I haven't tried this yet.

In my use case I was attempting to use bazel BUILD files, which approximately python, at least in terms of syntax.