Repository navigation
Keep Opengrep results when it exits 2 with a full report - #45
ricardobernardino2024 wants to merge 1 commit into
Conversation
Since codacy-opengrep 0.0.29 was deployed, more than half of the Opengrep runs on Codacy Cloud fail with "Error running semgrep": Opengrep finishes the scan, reports files that were only partially analyzed, prints the complete JSON report and exits 2. Any non-zero exit discarded the report, so the whole analysis failed. Exit code 2 with a valid JSON report is now a successful run. The report's errors are already reported as file errors, so the files that failed show up in Codacy instead of failing every file. Any other exit code, or exit code 2 without a report, still fails the run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
The exit-2 handling can incorrectly treat null or {} as a successful empty scan, discarding stderr instead of preserving the failure. Validate that the output is a structurally valid report before suppressing the command error.
Codacy also found:
- No new static-analysis or duplication issues.
- Complexity increased in
internal/tool/command.goandinternal/tool/command_test.go; coverage data was unavailable.
Test suggestions
- A command exits 2 and emits a valid JSON report;
runCommandreturns the report without an error. - A command exits 2 and emits invalid or non-report output;
runCommandreturns an error. - A command exits with a status other than 2 while emitting valid JSON;
runCommandreturns an error. - The preserved JSON report is parsed and its errors become file errors.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. The preserved JSON report is parsed and its errors become file errors.
TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback
| // Opengrep exits 2 when some files had errors but still prints the full JSON report, | ||
| // whose errors are reported as file errors, so its results are kept | ||
| var exitErr *exec.ExitError | ||
| if errors.As(err, &exitErr) && exitErr.ExitCode() == 2 && json.Valid(cmdOutput) { |
There was a problem hiding this comment.
🟡 MEDIUM RISK
If Opengrep exits 2 with null or {} on stdout, the scan is treated as successful with no findings, and stderr is discarded. Consider requiring a report object with the expected results and errors fields before suppressing the exit error.
TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.
|
I've used these changes in the PR #46, so I'll close this one. |
Problem
Since 0.0.29 (Opengrep 1.33.1) was deployed, Opengrep analyses on Codacy Cloud fail with:
In a 150-second sample of production pods (read-only watch, 12 Nov ~17:45 UTC), 61 of 104 runs failed this way; another 6 hit the 5-minute deadline and 37 succeeded. In every one of the 61, the logged stderr shows Opengrep finished the scan (
Ran N rules on N files: N findings), reportedPartially scanned: N files only partially analyzed due to parsing or internal Opengrep errors, and ended withexit status 2.runCommandtreats any non-zero exit as fatal and discards stdout, so the whole analysis fails. That includes the results for every file that was scanned correctly.Fix
When Opengrep exits 2 and stdout is valid JSON, the run is treated as successful.
appendErrorToResultalready turns the report'serrorsintoFileErrors, so the files that failed show up in Codacy instead of failing the whole analysis. Any other exit code, or exit 2 without a valid report, still fails.This also means the actual error-level Opengrep errors (currently invisible, because the JSON is thrown away) will appear as file errors, which should tell us the root cause.
Root cause: not confirmed
Two changes reached production in the last few days:
-jfrom the cgroup CPU quota. The Opengrep pods have no CPU limit, so this falls back toruntime.NumCPU(), which is 32–36 on the analysis nodes, under--max-memory 5000in a 7 GiB pod.-j 36peaked at 1.49 GB vs 0.91 GB for 1.30.0 on the same 906 files; at-j 1both used the same amount (upstreamparallelism: a 4M-word minor heap on every domain).I couldn't reproduce exit 2 locally. Once this is deployed, the file errors should show which error-level errors are behind it. If they are out-of-memory errors, setting
OPENGREP_JOBSfor the Opengrep pods is the next step.Tests
runCommandtests: exit 2 with a JSON report → success; exit 2 without a report → error; another exit code with a report → errorgo test ./...passes