Skip to content

Commit a07e0f3

Browse files
committed
fix: fix Lizard runner
1 parent fd2d067 commit a07e0f3

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

tools/lizard/lizardRunner.go

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"codacy/cli-v2/constants"
77
"codacy/cli-v2/domain"
88
"codacy/cli-v2/tools"
9+
"codacy/cli-v2/utils/logger"
10+
"github.com/sirupsen/logrus"
911
"encoding/json"
1012
"fmt"
1113
"os"
@@ -19,6 +21,7 @@ func RunLizard(workDirectory string, binary string, files []string, outputFile s
1921
var patterns []domain.PatternDefinition
2022
var errConfigs error
2123

24+
2225
if exists {
2326
// Configuration exists, read from file
2427
patterns, errConfigs = ReadConfig(configFile)
@@ -36,6 +39,7 @@ func RunLizard(workDirectory string, binary string, files []string, outputFile s
3639
return fmt.Errorf("no valid patterns found in configuration")
3740
}
3841

42+
3943
// Construct base command with lizard module
4044
args := []string{"-m", "lizard", "-V"}
4145

@@ -46,66 +50,77 @@ func RunLizard(workDirectory string, binary string, files []string, outputFile s
4650
args = append(args, ".")
4751
}
4852

53+
4954
// For non-SARIF output, let Lizard handle file output directly
5055
if outputFormat != "sarif" && outputFile != "" {
5156
args = append(args, "-o", outputFile)
5257
}
5358

59+
5460
// Run the command
5561
cmd := exec.Command(binary, args...)
5662
cmd.Dir = workDirectory
57-
cmd.Stderr = os.Stderr
63+
5864

5965
var err error
66+
var stderr bytes.Buffer
67+
68+
cmd.Stderr = &stderr
69+
6070
// For SARIF output, we need to capture and parse the output
6171
if outputFormat == "sarif" {
6272
var stdout bytes.Buffer
6373
cmd.Stdout = &stdout
6474

65-
err = cmd.Run()
75+
cmd.Run()
76+
77+
if stderr.Len() > 0 {
78+
logger.Debug("Failed to run Lizard: ", logrus.Fields{
79+
"error": err.Error(),
80+
"stderr": string(stderr.Bytes()),
81+
})
82+
83+
return fmt.Errorf("failed to run Lizard: %w", err)
84+
}
85+
86+
// Parse the output and generate issues
87+
results, parseErr := parseLizardResults(stdout.String())
88+
if parseErr != nil {
89+
return fmt.Errorf("failed to parse Lizard output: %w", parseErr)
90+
}
91+
issues := generateIssuesFromResults(results, patterns)
92+
93+
// Convert issues to SARIF Report
94+
sarifReport := convertIssuesToSarif(issues, patterns)
95+
// Marshal SARIF Report report to Sarif
96+
sarifData, err := json.MarshalIndent(sarifReport, "", " ")
6697
if err != nil {
67-
// Lizard returns 1 when it finds issues, which is not a failure
68-
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
69-
// Parse the output and generate issues
70-
results, parseErr := parseLizardResults(stdout.String())
71-
if parseErr != nil {
72-
return fmt.Errorf("failed to parse Lizard output: %w", parseErr)
73-
}
74-
75-
issues := generateIssuesFromResults(results, patterns)
76-
77-
// Convert issues to SARIF Report
78-
sarifReport := convertIssuesToSarif(issues, patterns)
79-
80-
// Marshal SARIF Report report to Sarif
81-
sarifData, err := json.MarshalIndent(sarifReport, "", " ")
82-
if err != nil {
83-
return fmt.Errorf("failed to marshal SARIF report: %w", err)
84-
}
85-
86-
// Write SARIF output to file if specified, else stdout
87-
if outputFile != "" {
88-
err = os.WriteFile(outputFile, sarifData, constants.DefaultFilePerms)
89-
if err != nil {
90-
return fmt.Errorf("failed to write SARIF output: %w", err)
91-
}
92-
} else {
93-
fmt.Println(string(sarifData))
94-
}
95-
96-
return nil
98+
return fmt.Errorf("failed to marshal SARIF report: %w", err)
99+
}
100+
101+
// Write SARIF output to file if specified, else stdout
102+
if outputFile != "" {
103+
err = os.WriteFile(outputFile, sarifData, constants.DefaultFilePerms)
104+
if err != nil {
105+
return fmt.Errorf("failed to write SARIF output: %w", err)
97106
}
98-
return fmt.Errorf("failed to run Lizard: %w", err)
107+
} else {
108+
fmt.Println(string(sarifData))
99109
}
110+
111+
return nil
112+
100113
} else {
101114
// For non-SARIF output, let Lizard handle stdout
102115
cmd.Stdout = os.Stdout
103116
err = cmd.Run()
104-
if err != nil {
105-
// Lizard returns 1 when it finds issues, which is not a failure
106-
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
107-
return nil
108-
}
117+
118+
if stderr.Len() > 0 {
119+
logger.Debug("Failed to run Lizard: ", logrus.Fields{
120+
"error": err.Error(),
121+
"stderr": string(stderr.Bytes()),
122+
})
123+
109124
return fmt.Errorf("failed to run Lizard: %w", err)
110125
}
111126
}

0 commit comments

Comments
 (0)