Skip to content

fix(main): handle ast-grep exit code 1 as non-error in run_command fu…#24

Merged
HerringtonDarkholme merged 1 commit intoast-grep:mainfrom
khaitranhq:fix/empty-result-exit-1
Dec 17, 2025
Merged

fix(main): handle ast-grep exit code 1 as non-error in run_command fu…#24
HerringtonDarkholme merged 1 commit intoast-grep:mainfrom
khaitranhq:fix/empty-result-exit-1

Conversation

@khaitranhq
Copy link
Contributor

Summary

Modified the run_command function in main.py to properly handle ast-grep's exit code 1, which indicates "no matches found" rather than an error condition.

Changes

Error Handling Improvement

  • Changed check=True to check=False in subprocess.run() to allow manual exit code handling
  • Implemented custom logic to differentiate between ast-grep's "no matches" (exit code 1) and actual errors
  • Added validation for ast-grep output to determine if exit code 1 represents valid "no matches" scenarios

Exit Code 1 Handling for ast-grep

The function now recognizes exit code 1 as valid when:

  • stdout is empty string, empty JSON array ([]), or starts with [ (valid JSON array)
  • --json flag is not present and stdout is empty (text format with no matches)

Error Reporting

  • Refactored error handling from CalledProcessError exception to explicit return code checking
  • Preserved meaningful error messages with stderr output for actual failures
  • Only raises RuntimeError for genuine command failures (exit codes other than 0 and ast-grep's special case 1)

Impact

This change prevents false error reporting when ast-grep returns no matches, which is a normal operational outcome rather than a failure condition. The function now correctly distinguishes between:

  1. Successful execution with no matches (exit code 1 from ast-grep)
  2. Successful execution with matches (exit code 0)
  3. Actual command failures (other non-zero exit codes)

Copy link
Member

@HerringtonDarkholme HerringtonDarkholme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

the change looks good to me

@HerringtonDarkholme HerringtonDarkholme merged commit 1941ad0 into ast-grep:main Dec 17, 2025
0 of 3 checks passed
@khaitranhq khaitranhq deleted the fix/empty-result-exit-1 branch December 17, 2025 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants