Skip to content

Conversation

@zhamborova
Copy link
Contributor

@zhamborova zhamborova commented Apr 14, 2025

This pull request includes several changes to improve error handling in the analysis tools and ensure that errors are properly propagated and logged. The most important changes include modifying the analysis functions to return errors, updating the runTool function to handle these errors, and adding error handling in the tool runners.

Error handling improvements:

  • cmd/analyze.go: Modified runEslintAnalysis, runTrivyAnalysis, runPmdAnalysis, and runPylintAnalysis functions to return errors instead of logging fatal errors directly. Updated runTool function to handle these errors and log appropriate messages. [1] [2] [3]

Tool runner updates:

  • tools/eslintRunner.go: Modified RunEslint function to return an error if the command fails, except when ESLint returns 1 due to finding issues. [1] [2]
  • tools/pmdRunner.go: Updated RunPmd function to return a formatted error message if the command fails.
  • tools/pylintRunner.go: Adjusted error handling in RunPylint function to return a formatted error message.
  • tools/trivyRunner.go: Modified RunTrivy function to return a formatted error message if the command fails.

@codacy-production
Copy link

codacy-production bot commented Apr 14, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.07% 21.62%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (9a3ae32) 2646 860 32.50%
Head commit (484c72c) 2653 (+7) 864 (+4) 32.57% (+0.07%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#75) 37 8 21.62%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@zhamborova zhamborova requested a review from Copilot April 14, 2025 11:54
@zhamborova zhamborova marked this pull request as ready for review April 14, 2025 11:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

cmd/analyze.go Outdated
}

for toolName, err := range failedTools {
utils.AddErrorRun(&mergedSarif, toolName, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

In my head, each tool has the responsability of returning its error and output to its SARIF if that is the format. Meaning, that on this method, I would not expect to have to mess with their SARIF outputs, does it make sense? 🤔

So the error that the tool propagates, is the error that we show on the STDOUT, but the SARIF would not necessarily be an extra one with that error.

I guess the question/concern is, if ESLint still creates a SARIF with an error, it would be nice to use that SARIF. Do you know if ESLint or other tools create a SARIF with information in case of failures?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The eslint does indeed capture some errors and outputs it in sarif, and current implementation lets eslint handle its own errors and report them in SARIF format but if it was unable to run, we would handle the catastrophic failures where the tool couldnt't run at all.

Definitely open to refining this logic to maybe separate "tool-reported" vs "wrapper-reported" errors, or do the intention was not to even capture those? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah let's sync tomorrow, for me, especially for tools that already handle some things well on their own SARIF, would avoid creating generic error SARIF. Would aim to share the errors on the STDOUT for those 'fatal problems' where the tool didn't even ran.

cmd/analyze.go Outdated
runTool(workDirectory, toolName, args, tmpFile)
if err := runTool(workDirectory, toolName, args, tmpFile); err != nil {
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

I would see us not having these continues. I thing that the 'append' of sarifs, should be ready to receive a file that does not exist or something.

But would like not to miss any possible generated sarif from the tool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, will remove 👍

cmd/analyze.go Outdated
runTool(workDirectory, toolName, args, outputFile)
if err := runTool(workDirectory, toolName, args, outputFile); err != nil {
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this one does not do anything and ca be removed right? or?

if err := runTool(workDirectory, toolName, args, tmpFile); err != nil {
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
}
sarifOutputs = append(sarifOutputs, tmpFile)
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming that this line works even if tmpFile foes not exist, LGTM!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, in the mergesarif function non existent files should be handled 👍

		data, err := os.ReadFile(file)
		if err != nil {
			if os.IsNotExist(err) {
				// Skip if file doesn't exist (tool might have failed)
				continue
			}
			return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
		}```

@zhamborova zhamborova merged commit 054c857 into main Apr 15, 2025
8 checks passed
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.

3 participants