Skip to content

Commit

Permalink
fix: return all errors from module build (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell authored Apr 17, 2024
1 parent ec9eb1a commit 5dc7108
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
5 changes: 3 additions & 2 deletions buildengine/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ func buildModule(ctx context.Context, sch *schema.Schema, module Module) error {
}

if err != nil {
errs := errors.UnwrapAllExcludingIntermediaries(err)
// read runtime-specific build errors from the build directory
errorList, err := loadProtoErrors(module.AbsDeployDir())
if err != nil {
return fmt.Errorf("failed to read build errors for module: %w", err)
}
errs := make([]error, 0, len(errorList.Errors))
for _, e := range errorList.Errors {
errs = append(errs, *e)
errs = append(errs, e)
}
errs = errors.DeduplicateErrors(errs)
schema.SortErrorsByPosition(errs)
return errors.Join(errs...)
}
Expand Down
2 changes: 1 addition & 1 deletion buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
if err != nil {
switch project := event.Project.(type) {
case Module:
logger.Errorf(err, "build and deploy failed for module %q: %v", project.Config().Key, err)
logger.Errorf(err, "build and deploy failed for module %q", project.Config().Key)
case ExternalLibrary:
logger.Errorf(err, "build failed for library %q: %v", project.Config().Key, err)
}
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Build(ctx context.Context, moduleDir string, sch *schema.Schema) error {
return fmt.Errorf("failed to write errors: %w", err)
}

return fmt.Errorf("failed to extract module schema: %w", originalErr)
return originalErr
}
schemaBytes, err := proto.Marshal(main.ToProto())
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ func UnwrapAll(err error) []error {
return out
}

// UnwrapAllExcludingIntermediaries recursively unwraps all errors in err, excluding all intermediate errors.
//
//nolint:errorlint
func UnwrapAllExcludingIntermediaries(err error) []error {
out := []error{}
if inner, ok := err.(interface{ Unwrap() []error }); ok {
for _, e := range inner.Unwrap() {
out = append(out, UnwrapAllExcludingIntermediaries(e)...)
}
return out
}
if inner, ok := err.(interface{ Unwrap() error }); ok && inner.Unwrap() != nil {
out = append(out, UnwrapAllExcludingIntermediaries(inner.Unwrap())...)
}
// only add the error if it is not an intermediary error
if len(out) == 0 {
out = append(out, err)
}
return out
}

// Innermost returns true if err cannot be further unwrapped.
//
//nolint:errorlint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ class ExtractSchemaRule(config: DetektConfig) : Rule(config) {
}

val moduleName = root.extractModuleName()
if (moduleName == "builtin") {
throw IllegalArgumentException("Why does this fix it?")
}
modules[moduleName]?.let {
writeFile(it.toModule(moduleName).encode(), SCHEMA_OUT)
}
Expand Down
9 changes: 4 additions & 5 deletions lsp/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Server) Run() error {
return nil
}

type errSet map[string]schema.Error
type errSet []*schema.Error

// BuildStarted clears diagnostics for the given directory. New errors will arrive later if they still exist.
func (s *Server) BuildStarted(dir string) {
Expand All @@ -78,15 +78,14 @@ func (s *Server) post(err error) {

// Deduplicate and associate by filename.
for _, err := range ftlErrors.DeduplicateErrors(ftlErrors.UnwrapAll(err)) {
var ce schema.Error
var ce *schema.Error
if errors.As(err, &ce) {
filename := ce.Pos.Filename
if _, exists := errByFilename[filename]; !exists {
errByFilename[filename] = make(errSet)
errByFilename[filename] = errSet{}
}
errByFilename[filename][strings.TrimSpace(ce.Error())] = ce
errByFilename[filename] = append(errByFilename[filename], ce)
}

}

go publishErrors(errByFilename, s)
Expand Down

0 comments on commit 5dc7108

Please sign in to comment.