Skip to content

Commit

Permalink
feat: aggregate go build errors for LSP
Browse files Browse the repository at this point in the history
fixes #1233
fixes #1237
  • Loading branch information
worstell committed Apr 12, 2024
1 parent 6dda791 commit 453e95f
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 267 deletions.
23 changes: 19 additions & 4 deletions backend/schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schema
import (
"errors"
"fmt"
"sort"

schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
Expand Down Expand Up @@ -66,11 +67,12 @@ func ErrorListFromProto(e *schemapb.ErrorList) *ErrorList {
}
}

func Errorf(pos Position, endColumn int, format string, args ...any) Error {
return Error{Msg: fmt.Sprintf(format, args...), Pos: pos, EndColumn: endColumn}
func Errorf(pos Position, endColumn int, format string, args ...any) *Error {
err := Error{Msg: fmt.Sprintf(format, args...), Pos: pos, EndColumn: endColumn}
return &err
}

func Wrapf(pos Position, endColumn int, err error, format string, args ...any) Error {
func Wrapf(pos Position, endColumn int, err error, format string, args ...any) *Error {
if format == "" {
format = "%s"
} else {
Expand All @@ -88,5 +90,18 @@ func Wrapf(pos Position, endColumn int, err error, format string, args ...any) E
newEndColumn = endColumn
args = append(args, err)
}
return Error{Msg: fmt.Sprintf(format, args...), Pos: newPos, EndColumn: newEndColumn}
e := Error{Msg: fmt.Sprintf(format, args...), Pos: newPos, EndColumn: newEndColumn}
return &e
}

func SortErrorsByPosition(merr []error) {
sort.Slice(merr, func(i, j int) bool {
var ipe, jpe Error
if errors.As(merr[i], &ipe) && errors.As(merr[j], &jpe) {
ipp := ipe.Pos
jpp := jpe.Pos
return ipp.Line < jpp.Line || (ipp.Line == jpp.Line && ipp.Column < jpp.Column)
}
return merr[i].Error() < merr[j].Error()
})
}
1 change: 1 addition & 0 deletions buildengine/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func buildModule(ctx context.Context, sch *schema.Schema, module Module) error {
for _, e := range errorList.Errors {
errs = append(errs, *e)
}
schema.SortErrorsByPosition(errs)
return errors.Join(errs...)
}

Expand Down
1 change: 0 additions & 1 deletion buildengine/build_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package buildengine
import (
"context"
"fmt"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/go-runtime/compile"
)
Expand Down
7 changes: 5 additions & 2 deletions examples/go/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ var defaultName = ftl.Config[string]("default")

// An echo request.
type EchoRequest struct {
Name ftl.Option[string] `json:"name"`
Name ftl.Option[string] `json:"name"`
Again error
}

//ftl:export
type EchoResponse struct {
Message string `json:"message"`
Error error
}

// Echo returns a greeting with the current time.
//
//ftl:export
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
tresp, err := ftl.Call(ctx, time.Time, time.TimeRequest{})
tresp, err := ftl.Call(ctx, "", time.TimeRequest{}, "another")
if err != nil {
return EchoResponse{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion examples/go/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"time"
)

type TimeRequest struct{}
type TimeRequest struct {
}
type TimeResponse struct {
Time time.Time
}
Expand Down
4 changes: 2 additions & 2 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
if originalErr := err; err != nil {
var schemaErrs []*schema.Error
for _, e := range errors.DeduplicateErrors(errors.UnwrapAll(err)) {
var ce schema.Error
var ce *schema.Error
if errors.As(e, &ce) {
schemaErrs = append(schemaErrs, &ce)
schemaErrs = append(schemaErrs, ce)
}
}
el := schema.ErrorList{
Expand Down
9 changes: 5 additions & 4 deletions go-runtime/compile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var directiveParser = participle.MustBuild[directiveWrapper](
participle.Union[schema.IngressPathComponent](&schema.IngressPathLiteral{}, &schema.IngressPathParameter{}),
)

func parseDirectives(fset *token.FileSet, docs *ast.CommentGroup) ([]directive, error) {
func parseDirectives(node ast.Node, fset *token.FileSet, docs *ast.CommentGroup) ([]directive, *schema.Error) {
if docs == nil {
return nil, nil
}
Expand All @@ -86,17 +86,18 @@ func parseDirectives(fset *token.FileSet, docs *ast.CommentGroup) ([]directive,
directive, err := directiveParser.ParseString(pos.Filename, line.Text[2:])
if err != nil {
// Adjust the Participle-reported position relative to the AST node.
var scerr *schema.Error
var perr participle.Error
if errors.As(err, &perr) {
ppos := schema.Position{}
ppos.Filename = pos.Filename
ppos.Column += pos.Column + 2
ppos.Line = pos.Line
err = schema.Errorf(ppos, ppos.Column, "%s", perr.Message())
scerr = schema.Errorf(ppos, ppos.Column, "%s", perr.Message())
} else {
err = fmt.Errorf("%s: %w", pos, err)
scerr = wrapf(node, err, "")
}
return nil, fmt.Errorf("%s: %w", "invalid directive", err)
return nil, scerr
}
directives = append(directives, directive.Directive)
}
Expand Down
Loading

0 comments on commit 453e95f

Please sign in to comment.