Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(misconf): Improve cause performance #6586

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/iac/scan/code.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package scan

import (
"bufio"
"bytes"
"fmt"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -149,7 +151,14 @@ func (r *Result) GetCode(opts ...CodeOption) (*Code, error) {
Lines: nil,
}

rawLines := strings.Split(string(content), "\n")
var rawLines []string
bs := bufio.NewScanner(bytes.NewReader(content))
for bs.Scan() {
rawLines = append(rawLines, bs.Text())
}
if bs.Err() != nil {
return nil, fmt.Errorf("failed to scan file : %w", err)
}

var highlightedLines []string
if settings.includeHighlighted {
Expand Down
34 changes: 20 additions & 14 deletions pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,26 @@ func NewCauseWithCode(underlying scan.Result) types.CauseMetadata {
},
})
}
if code, err := underlying.GetCode(); err == nil {
cause.Code = types.Code{
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
return types.Line{
Number: l.Number,
Content: l.Content,
IsCause: l.IsCause,
Annotation: l.Annotation,
Truncated: l.Truncated,
Highlighted: l.Highlighted,
FirstCause: l.FirstCause,
LastCause: l.LastCause,
}
}),

// only failures have a code cause
// failures can happen either due to lack of
// OR misconfiguration of something
if underlying.Status() == scan.StatusFailed {
if code, err := underlying.GetCode(); err == nil {
cause.Code = types.Code{
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
return types.Line{
Number: l.Number,
Content: l.Content,
IsCause: l.IsCause,
Annotation: l.Annotation,
Truncated: l.Truncated,
Highlighted: l.Highlighted,
FirstCause: l.FirstCause,
LastCause: l.LastCause,
}
}),
}
}
}
return cause
Expand Down