Skip to content

Commit c25c315

Browse files
fix: Add path param validation
- Added a new function to validate the existence of provided paths before analysis. - Updated the analyze command's short and long descriptions for clarity. - Improved error logging when failing to retrieve the current working directory. - Enhanced SARIF output merging to handle empty files and invalid JSON more gracefully.
1 parent f97be27 commit c25c315

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

cmd/analyze.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,33 @@ func runTool(workDirectory string, toolName string, pathsToCheck []string, outpu
369369
return runToolByName(toolName, workDirectory, pathsToCheck, autoFix, outputFile, outputFormat, tool, runtime)
370370
}
371371

372+
// validatePaths checks if all provided paths exist and returns an error if any don't
373+
func validatePaths(paths []string) error {
374+
for _, path := range paths {
375+
if _, err := os.Stat(path); os.IsNotExist(err) {
376+
return fmt.Errorf("❌ Error: cannot find file or directory '%s'", path)
377+
}
378+
}
379+
return nil
380+
}
381+
372382
var analyzeCmd = &cobra.Command{
373383
Use: "analyze",
374-
Short: "Runs all configured linters.",
375-
Long: "Runs all configured tools for code analysis. Use --tool flag to run a specific tool.",
384+
Short: "Analyze code using configured tools",
385+
Long: `Analyze code using configured tools and output results in the specified format.`,
376386
Run: func(cmd *cobra.Command, args []string) {
387+
// Validate paths before proceeding
388+
if err := validatePaths(args); err != nil {
389+
fmt.Println(err)
390+
os.Exit(1)
391+
}
392+
393+
// Get current working directory
377394
workDirectory, err := os.Getwd()
378395
if err != nil {
379-
log.Fatal(err)
396+
log.Fatalf("Failed to get current working directory: %v", err)
380397
}
398+
381399
var toolsToRun map[string]*plugins.ToolInfo
382400

383401
if toolsToAnalyzeParam != "" {

utils/sarif.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,26 @@ func MergeSarifOutputs(inputFiles []string, outputFile string) error {
204204
return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
205205
}
206206

207+
// Skip empty files
208+
if len(data) == 0 {
209+
continue
210+
}
211+
207212
var sarif SimpleSarifReport
208213
if err := json.Unmarshal(data, &sarif); err != nil {
209-
return fmt.Errorf("failed to parse SARIF file %s: %w", file, err)
214+
// If file is empty or invalid JSON, create an empty SARIF report - extra protection from invalid files
215+
emptySarif := SimpleSarifReport{
216+
Version: "2.1.0",
217+
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
218+
Runs: []json.RawMessage{},
219+
}
220+
emptyData, err := json.Marshal(emptySarif)
221+
if err != nil {
222+
return fmt.Errorf("failed to create empty SARIF report: %w", err)
223+
}
224+
if err := json.Unmarshal(emptyData, &sarif); err != nil {
225+
return fmt.Errorf("failed to parse empty SARIF report: %w", err)
226+
}
210227
}
211228

212229
mergedSarif.Runs = append(mergedSarif.Runs, sarif.Runs...)

0 commit comments

Comments
 (0)