Skip to content

Improve multiple error handling #1669

@wata727

Description

@wata727

Introduction

Currently, TFLint will stop immediately and return an error if an error occurs on the way:

$ tflint
Failed to check ruleset; Failed to check `azurerm_lb_probe_invalid_protocol` rule: main.tf:6,53-72: Invalid template interpolation value; The expression result is null. Cannot include a null value in a string template.

However, if multiple problems exist, you need to run TFLint multiple times until all errors are resolved. This is inefficient.

Fortunately, HCL parse errors (hcl.Diagnostics) already support multiple error handling:

if errors.As(err, &diags) {
fmt.Fprintf(f.Stderr, "%s:\n\n", err)
writer := hcl.NewDiagnosticTextWriter(f.Stderr, parseSources(sources), 0, !f.NoColor)
_ = writer.WriteDiagnostics(diags)

This proposal mainly focuses on multiple error handling otherwise (especially errors returned by plugins).

See also #1665

Proposal

Change the plugin interface to allow multiple errors to be returned. google.golang.org/grpc/status.WithDetails allows us to attach multiple pieces of information to the status code, so we might be able to take advantage of this. FYI, the SDK already uses this mechanism to propagate application errors:
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/toproto/toproto.go#L226-L254
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/fromproto/fromproto.go#L283-L321

When the Check returns an error, the SDK follows a convention to split it into multiple errors and turn them into status details. As the convention, errors.Join, introduced in Go 1.20, is a good option.
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/host2plugin/server.go#L119

Consideration should be given to how hcl.Diagnostics should be treated. hcl.Diagnostics can already handle multiple errors, but not all contexts over the wire protocol (e.g. EvalContext, Extra). Maybe we should invent our own error representation like tfdiags.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementneeds-designDetailed design is required for implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions