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 3044853
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 261 deletions.
10 changes: 6 additions & 4 deletions backend/schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,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 +89,6 @@ 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
}
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: 6 additions & 1 deletion examples/go/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ import (

var defaultName = ftl.Config[string]("default")

type Thing struct {
ID uint64 `json:"id"`
}

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

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

// Echo returns a greeting with the current time.
//
//ftl:export
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
func Echo(ctx context.Context, req Thing) (EchoResponse, error) {
tresp, err := ftl.Call(ctx, time.Time, time.TimeRequest{})
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 3044853

Please sign in to comment.