1

I'm using ast-grep to detect patterns in Java files, but the files and ignores filter configuration isn't working as expected.

According to the documentation, we can apply pattern matching to specific files or ignore certain files using:

files:
  - src/**/*.js
  - src/**/*.ts

ignores:
  - test/**/*.js
  - test/**/*.ts

I have a project structure with both src and tst directories, and I'm trying to match patterns ONLY in the source files, but not in test files.

Here's my pattern detector configuration:

id: detect_initialise
language: java
rule:
  kind: expression_statement
  regex: Service.initialize
files:
   - "**/src/**/*.java"
ignores:
  - "**/tst/**/*.java"

I'm executing the ast-grep command on the project workspace using this Python code:

command = [
    f"{AST_GREP_CMD}",
    "scan",
    "-r",
    f"{str(path_to_ast-grep_recipe)}",
    f"{project_repo_path}",
]
ast_grep_dryrun_result = self.executor.run(command)

Issue

The pattern is being matched in Java classes from BOTH src and tst directories, even though I've set the files filter to only include src directories and explicitly added an ignore rule for tst directories.

I expected the results to only include matches from files in the src directory, but the output contains matches from both src and tst directories.

  1. Is my syntax for the files and ignore fields correct?
  2. Are there any known issues or limitations with the file filtering in ast-grep?
  3. Is there something I'm missing in how ast-grep processes these files/ignores configurations?

1 Answer 1

0

From the ignore documentation

Glob patterns that exclude rules from applying to files. It is superseded by files if both are specified.

One way to work around that is to remove the files glob.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.