Skip to content

Commit

Permalink
fix: inaccurate line numbers in suggest comments (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraagnalluri authored Sep 8, 2023
1 parent d0ee473 commit 4769399
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
60 changes: 45 additions & 15 deletions internal/suggestions/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,40 @@ func startSuggestSchemaFile(ctx context.Context, schemaPath string, suggestionsC
return nil, fmt.Errorf("failed to read schema file %s: %w", schemaPath, err)
}

errs, err := validate(schema, schemaPath, suggestionsConfig.Level, true)
if err != nil {
return nil, err
}

var errsWithLineNums []errorAndCommentLineNumber
if strings.Contains(detectFileType(schemaPath), "yaml") {
schema, err = convertYamlToJson(schema)
if err != nil {
return nil, fmt.Errorf("failed to convert schema file from YAML to JSON %s: %w", schemaPath, err)
}

jsonErrs, err := validate(schema, schemaPath, suggestionsConfig.Level, false)
if err != nil {
return nil, err
}
errsWithLineNums = updateErrsWithLineNums(jsonErrs, errs)
}

errorSummary, err := suggest(schema, schemaPath, *suggestionsConfig)
if len(errsWithLineNums) == 0 {
for _, err := range errs {
vErr := errors.GetValidationErr(err)
if vErr == nil {
continue
}

errsWithLineNums = append(errsWithLineNums, errorAndCommentLineNumber{
error: vErr,
lineNumber: vErr.LineNumber,
})
}
}

errorSummary, err := suggest(schema, schemaPath, errsWithLineNums, *suggestionsConfig)
if err != nil {
fmt.Println(promptui.Styler(promptui.FGRed, promptui.FGBold)(fmt.Sprintf("cannot fetch llm suggestions: %s", err.Error())))
return nil, err
Expand All @@ -123,7 +149,13 @@ func startSuggestSchemaFile(ctx context.Context, schemaPath string, suggestionsC
return errorSummary, nil
}

func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSummary, error) {
func suggest(schema []byte, schemaPath string, errsWithLineNums []errorAndCommentLineNumber, config Config) (*SchemaErrorSummary, error) {
if len(errsWithLineNums) == 0 {
return nil, nil
}

initialErrCount := len(errsWithLineNums)

l := log.NewLogger(schemaPath)

// local authentication should just be set in env variable
Expand Down Expand Up @@ -157,15 +189,6 @@ func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSumma
return nil, err
}

errsWithLineNums, err := suggest.revalidate(true)
if err != nil {
return nil, err
}

if len(errsWithLineNums) == 0 {
return nil, nil
}

/**
* Parallelized suggestions
*/
Expand All @@ -175,7 +198,6 @@ func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSumma

suggest.Verbose = false
continueSuggest := true
var err error

// Request suggestions in parallel, in batches of at most suggestionBatchSize
for continueSuggest {
Expand All @@ -201,7 +223,7 @@ func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSumma
*/
for _, validationErrWithLineNum := range errsWithLineNums {
validationErr := validationErrWithLineNum.error
if !checkSuggestionCount(len(errsWithLineNums), suggest.suggestionCount, config.MaxSuggestions) {
if !checkSuggestionCount(initialErrCount, suggest.suggestionCount, config.MaxSuggestions) {
break
}

Expand All @@ -224,7 +246,7 @@ func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSumma
}

if suggest.awaitShouldApply() {
err := suggest.commitSuggestion(newFile)
err = suggest.commitSuggestion(newFile)
if err != nil {
return nil, err
}
Expand All @@ -235,10 +257,18 @@ func suggest(schema []byte, schemaPath string, config Config) (*SchemaErrorSumma

suggest.suggestionCount++

errsWithLineNums, err = suggest.revalidate(false)
newErrs, err := validate(suggest.File, suggest.FilePath, suggest.Config.Level, false)
if err != nil {
return nil, err
}

var oldErrs []error
for _, oldErr := range errsWithLineNums {
oldErrs = append(oldErrs, oldErr.error)
}

// flip order of newErrs and oldErrs because we want the old line numbers
errsWithLineNums = updateErrsWithLineNums(newErrs, oldErrs)
}

errorSummary := getSchemaErrorSummary(errsWithLineNums)
Expand Down
49 changes: 25 additions & 24 deletions internal/suggestions/suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,17 @@ func (s *Suggestions) revalidate(printSummary bool) ([]errorAndCommentLineNumber

var errsWithLineNums []errorAndCommentLineNumber
if s.FileType == "yaml" {
errsWithLineNums, err = s.updateErrsWithYamlLineNums(errs)
yamlFile, err := convertJsonToYaml(s.File)
if err != nil {
return nil, err
}

yamlErrs, err := validate(yamlFile, s.FilePath, s.Config.Level, false)
if err != nil {
return nil, err
}

errsWithLineNums = updateErrsWithLineNums(errs, yamlErrs)
} else {
for _, err := range errs {
vErr := errors.GetValidationErr(err)
Expand Down Expand Up @@ -326,41 +333,35 @@ func validate(schema []byte, schemaPath string, level errors.Severity, printSumm
return errs, nil
}

func (s *Suggestions) updateErrsWithYamlLineNums(errs []error) ([]errorAndCommentLineNumber, error) {
yamlFile, err := convertJsonToYaml(s.File)
if err != nil {
return nil, err
}

yamlErrs, err := validate(yamlFile, s.FilePath, s.Config.Level, false)
if err != nil {
return nil, err
}

/*
updateErrsWithYamlLineNums returns the old errors with the line numbers of the new errors, provided the errors are
equivalent except for the line numbers.
*/
func updateErrsWithLineNums(oldErrs []error, newErrs []error) []errorAndCommentLineNumber {
// Return each error in the JSON document along with the line number of the corresponding error in the YAML document
var newErrs []errorAndCommentLineNumber
for _, jsonErr := range errs {
vjErr := errors.GetValidationErr(jsonErr)
if vjErr == nil {
var retErrs []errorAndCommentLineNumber
for _, oldErr := range oldErrs {
voErr := errors.GetValidationErr(oldErr)
if voErr == nil {
continue
}

for _, yamlErr := range yamlErrs {
vyErr := errors.GetValidationErr(yamlErr)
if vyErr == nil {
for _, newErr := range newErrs {
vnErr := errors.GetValidationErr(newErr)
if vnErr == nil {
continue
}

if validationErrsEqualExceptLineNumber(*vyErr, *vjErr) {
newErrs = append(newErrs, errorAndCommentLineNumber{
error: vjErr,
lineNumber: vyErr.LineNumber,
if validationErrsEqualExceptLineNumber(*voErr, *vnErr) {
retErrs = append(retErrs, errorAndCommentLineNumber{
error: voErr,
lineNumber: vnErr.LineNumber,
})
}
}
}

return newErrs, nil
return retErrs
}

func validationErrsEqualExceptLineNumber(err1, err2 errors.ValidationError) bool {
Expand Down

0 comments on commit 4769399

Please sign in to comment.