Skip to content

Keep Opengrep results when it exits 2 with a full report - #45

Closed
ricardobernardino2024 wants to merge 1 commit into
mainfrom
fix/keep-results-on-exit-code-2
Closed

ricardobernardino2024 wants to merge 1 commit into
mainfrom
fix/keep-results-on-exit-code-2

Conversation

@ricardobernardino2024

Copy link
Copy Markdown

Problem

Since 0.0.29 (Opengrep 1.33.1) was deployed, Opengrep analyses on Codacy Cloud fail with:

KubernetesDockerRunner: Container for codacy/codacy-opengrep:0.0.29 exited with non-zero code 1
level=error msg="Failed to run the tool: Error running semgrep: ..."

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), reported Partially scanned: N files only partially analyzed due to parsing or internal Opengrep errors, and ended with exit status 2.

runCommand treats 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. appendErrorToResult already turns the report's errors into FileErrors, 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:

  • 0.0.28 (registry 9 Nov): one Opengrep run for all languages, and -j from the cgroup CPU quota. The Opengrep pods have no CPU limit, so this falls back to runtime.NumCPU(), which is 32–36 on the analysis nodes, under --max-memory 5000 in a 7 GiB pod.
  • 0.0.29 (registry 11 Nov): Opengrep 1.30.0 → 1.33.1. Locally, 1.33.1 at -j 36 peaked at 1.49 GB vs 0.91 GB for 1.30.0 on the same 906 files; at -j 1 both used the same amount (upstream parallelism: 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_JOBS for the Opengrep pods is the next step.

Tests

  • New runCommand tests: exit 2 with a JSON report → success; exit 2 without a report → error; another exit code with a report → error
  • go test ./... passes

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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity

Metric Results
Complexity 4

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

⚠️ Resolve issues before merging

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.go and internal/tool/command_test.go; coverage data was unavailable.

Test suggestions

  • A command exits 2 and emits a valid JSON report; runCommand returns the report without an error.
  • A command exits 2 and emits invalid or non-report output; runCommand returns an error.
  • A command exits with a status other than 2 while emitting valid JSON; runCommand returns 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

Comment thread internal/tool/command.go
// 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

@DMarinhoCodacy

Copy link
Copy Markdown
Contributor

I've used these changes in the PR #46, so I'll close this one.

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