Skip to content

DiagnosticTextWriter panics when writing a marked value #737

@wata727

Description

@wata727

See also terraform-linters/tflint#2243

The DiagnosticTextWriter will panic when it writes a diagnostic whose expression has a marked value. The reproducible code is below:

package main

import (
        "os"

        "github.com/hashicorp/hcl/v2"
        "github.com/hashicorp/hcl/v2/hclsyntax"
        "github.com/zclconf/go-cty/cty"
)

func main() {
        file, diags := hclsyntax.ParseConfig([]byte("foo = bar"), "main.tf", hcl.InitialPos)
        if diags.HasErrors() {
                panic(diags)
        }
        attrs, diags := file.Body.JustAttributes()
        if diags.HasErrors() {
                panic(diags)
        }
        expr := attrs["foo"].Expr
        ctx := &hcl.EvalContext{Variables: map[string]cty.Value{"bar": cty.StringVal("baz").Mark("x")}}

        diag := &hcl.Diagnostic{
                Severity:    hcl.DiagError,
                Summary:     "something went wrong",
                Subject:     expr.Range().Ptr(),
                Expression:  expr,
                EvalContext: ctx,
        }

        dwr := hcl.NewDiagnosticTextWriter(os.Stdout, map[string]*hcl.File{"main.tf": file}, 40, true)
        dwr.WriteDiagnostic(diag)
}
$ go run main.go
Error: something went wrong

  on main.tf line 1:
   1: foo = bar

panic: value is marked, so must be unmarked first

goroutine 1 [running]:
github.com/zclconf/go-cty/cty.Value.assertUnmarked(...)
        /go/pkg/mod/github.com/zclconf/[email protected]/cty/marks.go:141
github.com/zclconf/go-cty/cty.Value.AsString({{{0x67dfb0?, 0xc00009c00c?}}, {0x605d40?, 0xc0000b45a0?}})
        /go/pkg/mod/github.com/zclconf/[email protected]/cty/value_ops.go:1385 +0x47
github.com/hashicorp/hcl/v2.(*diagnosticTextWriter).valueStr(0x67dfb0?, {{{0x67dfb0?, 0xc00009c00c?}}, {0x605d40?, 0xc0000b45a0?}})
        /go/pkg/mod/github.com/hashicorp/hcl/[email protected]/diagnostic_text.go:274 +0x16c
github.com/hashicorp/hcl/v2.(*diagnosticTextWriter).WriteDiagnostic(0xc000093e70, 0xc000093e98)
        /go/pkg/mod/github.com/hashicorp/hcl/[email protected]/diagnostic_text.go:174 +0xc26
main.main()
        /workspaces/hcl/work/main.go:32 +0x44f
exit status 2
go.mod
module example.com/m

go 1.23.4

require github.com/hashicorp/hcl/v2 v2.23.0

require (
        github.com/agext/levenshtein v1.2.1 // indirect
        github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
        github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
        github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
        github.com/zclconf/go-cty v1.13.0 // indirect
        golang.org/x/mod v0.8.0 // indirect
        golang.org/x/sys v0.5.0 // indirect
        golang.org/x/text v0.11.0 // indirect
        golang.org/x/tools v0.6.0 // indirect
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions