Skip to content

Commit

Permalink
fix: Autofix timeout bug (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
BBerabi authored Oct 7, 2024
1 parent e23b2e0 commit 45039e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
4 changes: 4 additions & 0 deletions domain/ide/command/code_fix_diffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (cmd *codeFixDiffs) Execute(ctx context.Context) (any, error) {
}

suggestions, err := cmd.codeScanner.GetAutofixDiffs(ctx, folderPath, relPath, issue)
if err == nil && len(suggestions) == 0 {
logger.Info().Msg("Autofix run successfully but there were no good fixes")
return suggestions, nil
}
if err != nil {
// as long as the backend service doesn't support good error handling, we'll just log the error
logger.Err(err).Msgf("received an error from API: %s", err.Error())
Expand Down
54 changes: 38 additions & 16 deletions infrastructure/code/autofix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"time"

"github.com/snyk/snyk-ls/application/config"
"github.com/snyk/snyk-ls/domain/snyk"
performance2 "github.com/snyk/snyk-ls/internal/observability/performance"
Expand All @@ -41,29 +40,47 @@ func (a AutofixUnifiedDiffSuggestion) GetUnifiedDiffForFile(filePath string) str
return a.UnifiedDiffsPerFile[filePath]
}

func (s *SnykCodeHTTPClient) GetAutofixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error) {
func (s *SnykCodeHTTPClient) GetAutofixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (
unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion,
status AutofixStatus,
err error,
) {
method := "GetAutofixDiffs"
logger := config.CurrentConfig().Logger().With().Str("method", method).Logger()
span := s.instrumentor.StartSpan(ctx, method)
defer s.instrumentor.Finish(span)

var response AutofixResponse
requestId, err := performance2.GetTraceId(ctx)
if err != nil {
logger.Err(err).Msg(failedToObtainRequestIdString + err.Error())
return unifiedDiffSuggestions, err
return nil, failed, err
}

logger.Info().Str("requestId", requestId).Msg("Started obtaining autofix diffs")
defer logger.Info().Str("requestId", requestId).Msg("Finished obtaining autofix diffs")

response, err = s.RunAutofix(span.Context(), options)
if err != nil || response.Status == failed.message {
logger.Err(err).Msg("error getting autofix suggestions")
return unifiedDiffSuggestions, err
response, err := s.RunAutofix(span.Context(), options)
if err != nil {
return nil, failed, err
}

logger.Debug().Msgf("Status: %s", response.Status)

if response.Status == failed.message {
logger.Error().Str("responseStatus", response.Status).Msg("autofix failed")
return nil, failed, errors.New("Autofix failed")
}

if response.Status == "" {
logger.Error().Str("responseStatus", response.Status).Msg("unknown response status (empty)")
return nil, failed, errors.New("Unknown response status (empty)")
}

return response.toUnifiedDiffSuggestions(baseDir, options.filePath), err
if response.Status != completeStatus {
return nil, status, nil
}

return response.toUnifiedDiffSuggestions(baseDir, options.filePath), AutofixStatus{message: response.Status}, err
}

func (sc *Scanner) GetAutofixDiffs(
Expand Down Expand Up @@ -109,14 +126,19 @@ func (sc *Scanner) GetAutofixDiffs(
logger.Error().Msg(msg)
return nil, errors.New(msg)
case <-ticker.C:
suggestions, err := codeClient.GetAutofixDiffs(span.Context(), baseDir, options)
if err != nil {
logger.Err(err).Msg("Error getting autofix suggestions")
return nil, err
}
if len(suggestions) > 0 {
return suggestions, nil
suggestions, fixStatus, autofixErr := codeClient.GetAutofixDiffs(span.Context(), baseDir, options)
if autofixErr != nil {
logger.Err(autofixErr).Msg("Error getting autofix suggestions")
return nil, autofixErr
} else if fixStatus.message == completeStatus {
if len(suggestions) > 0 {
return suggestions, nil
} else {
logger.Info().Msg("AI fix returned successfully but no good fix could be computed.")
return suggestions, nil
}
}
// If err == nil and fixStatus.message != completeStatus, we will keep polling.
}
}
}
6 changes: 4 additions & 2 deletions infrastructure/code/fake_snyk_code_api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ type FakeSnykCodeClient struct {
maxConcurrentScans int
NoFixSuggestions bool
UnifiedDiffSuggestions []AutofixUnifiedDiffSuggestion
AutofixStatus AutofixStatus
Options map[string]AnalysisOptions
C *config.Config
}

func (f *FakeSnykCodeClient) GetAutofixDiffs(_ context.Context, _ string, _ AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error) {
return f.UnifiedDiffSuggestions, nil
func (f *FakeSnykCodeClient) GetAutofixDiffs(_ context.Context, _ string, _ AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, status AutofixStatus, err error) {
f.AutofixStatus = AutofixStatus{message: completeStatus }
return f.UnifiedDiffSuggestions, f.AutofixStatus, nil
}

func (f *FakeSnykCodeClient) addCall(params []any, op string) {
Expand Down
6 changes: 5 additions & 1 deletion infrastructure/code/snyk_code_http_client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ type SnykCodeClient interface {

SubmitAutofixFeedback(ctx context.Context, fixId string, result string) error

GetAutofixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error)
GetAutofixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (
unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion,
status AutofixStatus,
err error,
)
}

0 comments on commit 45039e1

Please sign in to comment.