From e8f06da70fda8d5eb096e206d39e0c645e62f08a Mon Sep 17 00:00:00 2001 From: Ricardo Bernardino Date: Thu, 12 Nov 2026 17:53:00 +0000 Subject: [PATCH] Keep Opengrep results when it exits 2 with a full report 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 --- internal/tool/command.go | 6 ++++++ internal/tool/command_test.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/internal/tool/command.go b/internal/tool/command.go index 590db82..419b5f9 100644 --- a/internal/tool/command.go +++ b/internal/tool/command.go @@ -132,6 +132,12 @@ func runCommand(cmd *exec.Cmd) (*string, *string, error) { var stderr bytes.Buffer cmd.Stderr = &stderr cmdOutput, err := cmd.Output() + // 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) { + err = nil + } if err != nil { stderrString := stderr.String() return nil, &stderrString, err diff --git a/internal/tool/command_test.go b/internal/tool/command_test.go index 914abd6..de4cadc 100644 --- a/internal/tool/command_test.go +++ b/internal/tool/command_test.go @@ -79,6 +79,44 @@ func TestRunCommand_Error(t *testing.T) { assert.Empty(t, stderr, "Expected empty stderr for a failed command") } +func TestRunCommand_ExitCode2WithReport(t *testing.T) { + // Arrange + report := `{"results": [], "errors": [{"level": "error", "message": "Out of memory"}]}` + mockCmd := exec.Command("sh", "-c", "echo '"+report+"'; echo 'Scan Status' >&2; exit 2") + + // Act + stdout, stderr, err := runCommand(mockCmd) + + // Assert + assert.NoError(t, err) + assert.Nil(t, stderr) + assert.Equal(t, report+"\n", *stdout) +} + +func TestRunCommand_ExitCode2WithoutReport(t *testing.T) { + // Arrange + mockCmd := exec.Command("sh", "-c", "echo 'not a report'; exit 2") + + // Act + stdout, _, err := runCommand(mockCmd) + + // Assert + assert.Error(t, err) + assert.Nil(t, stdout) +} + +func TestRunCommand_OtherExitCodeWithReport(t *testing.T) { + // Arrange + mockCmd := exec.Command("sh", "-c", `echo '{"results": [], "errors": []}'; exit 7`) + + // Act + stdout, _, err := runCommand(mockCmd) + + // Assert + assert.Error(t, err) + assert.Nil(t, stdout) +} + func TestParseCommandOutput(t *testing.T) { // Arrange mockPatternDescriptions := []codacy.PatternDescription{