Skip to content

Commit 054c857

Browse files
authored
[PLUTO-1396] Handle tool errors gracefully (#75)
* [PLUTO-1396] Handle tool errors gracefully
1 parent 301c418 commit 054c857

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

cmd/analyze.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -188,42 +188,33 @@ func getToolName(toolName string, version string) string {
188188
return toolName
189189
}
190190

191-
func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) {
191+
func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {
192192
eslint := config.Config.Tools()["eslint"]
193193
eslintInstallationDirectory := eslint.InstallDir
194194
nodeRuntime := config.Config.Runtimes()["node"]
195195
nodeBinary := nodeRuntime.Binaries["node"]
196196

197-
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat)
197+
return tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat)
198198
}
199199

200-
func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
200+
func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
201201
trivy := config.Config.Tools()["trivy"]
202202
trivyBinary := trivy.Binaries["trivy"]
203203

204-
err := tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat)
205-
if err != nil {
206-
log.Fatalf("Error running Trivy: %v", err)
207-
}
204+
return tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat)
208205
}
209206

210-
func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
207+
func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
211208
pmd := config.Config.Tools()["pmd"]
212209
pmdBinary := pmd.Binaries["pmd"]
213210

214-
err := tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config)
215-
if err != nil {
216-
log.Fatalf("Error running PMD: %v", err)
217-
}
211+
return tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config)
218212
}
219213

220-
func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
214+
func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
221215
pylint := config.Config.Tools()["pylint"]
222216

223-
err := tools.RunPylint(workDirectory, pylint, pathsToCheck, outputFile, outputFormat)
224-
if err != nil {
225-
log.Fatalf("Error running Pylint: %v", err)
226-
}
217+
return tools.RunPylint(workDirectory, pylint, pathsToCheck, outputFile, outputFormat)
227218
}
228219

229220
var analyzeCmd = &cobra.Command{
@@ -265,7 +256,9 @@ var analyzeCmd = &cobra.Command{
265256
for toolName := range toolsToRun {
266257
log.Printf("Running %s...\n", toolName)
267258
tmpFile := filepath.Join(tmpDir, fmt.Sprintf("%s.sarif", toolName))
268-
runTool(workDirectory, toolName, args, tmpFile)
259+
if err := runTool(workDirectory, toolName, args, tmpFile); err != nil {
260+
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
261+
}
269262
sarifOutputs = append(sarifOutputs, tmpFile)
270263
}
271264

@@ -296,23 +289,25 @@ var analyzeCmd = &cobra.Command{
296289
// Run tools without merging outputs
297290
for toolName := range toolsToRun {
298291
log.Printf("Running %s...\n", toolName)
299-
runTool(workDirectory, toolName, args, outputFile)
292+
if err := runTool(workDirectory, toolName, args, outputFile); err != nil {
293+
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
294+
}
300295
}
301296
}
302297
},
303298
}
304299

305-
func runTool(workDirectory string, toolName string, args []string, outputFile string) {
300+
func runTool(workDirectory string, toolName string, args []string, outputFile string) error {
306301
switch toolName {
307302
case "eslint":
308-
runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat)
303+
return runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat)
309304
case "trivy":
310-
runTrivyAnalysis(workDirectory, args, outputFile, outputFormat)
305+
return runTrivyAnalysis(workDirectory, args, outputFile, outputFormat)
311306
case "pmd":
312-
runPmdAnalysis(workDirectory, args, outputFile, outputFormat)
307+
return runPmdAnalysis(workDirectory, args, outputFile, outputFormat)
313308
case "pylint":
314-
runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
309+
return runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
315310
default:
316-
log.Printf("Warning: Unsupported tool: %s\n", toolName)
311+
return fmt.Errorf("unsupported tool: %s", toolName)
317312
}
318313
}

tools/eslintRunner.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tools
22

33
import (
44
"codacy/cli-v2/config"
5+
"fmt"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -10,7 +11,7 @@ import (
1011
// * Run from the root of the repo we want to analyse
1112
// * NODE_PATH="<the installed eslint path>/node_modules"
1213
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
13-
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) {
14+
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {
1415
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
1516
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")
1617

@@ -54,6 +55,14 @@ func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
5455
// fmt.Println(cmd.Env)
5556
// fmt.Println(cmd)
5657

57-
// TODO eslint returns 1 when it finds errors, so we're not propagating it
58-
cmd.Run()
58+
// Run the command and handle errors
59+
err := cmd.Run()
60+
if err != nil {
61+
// ESLint returns 1 when it finds errors, which is not a failure
62+
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
63+
return nil
64+
}
65+
return fmt.Errorf("failed to run ESLint: %w", err)
66+
}
67+
return nil
5968
}

tools/pmdRunner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tools
22

33
import (
44
"codacy/cli-v2/config"
5+
"fmt"
56
"os"
67
"os/exec"
78
"strings"
@@ -56,7 +57,7 @@ func RunPmd(repositoryToAnalyseDirectory string, pmdBinary string, pathsToCheck
5657
// Exit code 4 means violations found – treat as success
5758
return nil
5859
}
59-
return err
60+
return fmt.Errorf("failed to run PMD: %w", err)
6061
}
6162
return nil
6263
}

tools/pylintRunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func RunPylint(workDirectory string, toolInfo *plugins.ToolInfo, files []string,
5959
// Pylint returns non-zero exit code when it finds issues
6060
// We should not treat this as an error
6161
if _, ok := err.(*exec.ExitError); !ok {
62-
return fmt.Errorf("failed to run pylint: %w", err)
62+
return fmt.Errorf("failed to run Pylint: %w", err)
6363
}
6464
}
6565

tools/trivyRunner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tools
22

33
import (
44
"codacy/cli-v2/config"
5+
"fmt"
56
"os"
67
"os/exec"
78
)
@@ -35,5 +36,9 @@ func RunTrivy(repositoryToAnalyseDirectory string, trivyBinary string, pathsToCh
3536
cmd.Stderr = os.Stderr
3637
cmd.Stdout = os.Stdout
3738

38-
return cmd.Run()
39+
err := cmd.Run()
40+
if err != nil {
41+
return fmt.Errorf("failed to run Trivy: %w", err)
42+
}
43+
return nil
3944
}

0 commit comments

Comments
 (0)