diff --git a/command/format/diagnostic.go b/command/format/diagnostic.go index 469351218..3dd9238dd 100644 --- a/command/format/diagnostic.go +++ b/command/format/diagnostic.go @@ -62,6 +62,14 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color // Make sure the snippet includes the highlight. This should be true // for any reasonable diagnostic, but we'll make sure. snippetRange = hcl.RangeOver(snippetRange, highlightRange) + if snippetRange.Empty() { + snippetRange.End.Byte++ + snippetRange.End.Column++ + } + if highlightRange.Empty() { + highlightRange.End.Byte++ + highlightRange.End.Column++ + } var src []byte if sources != nil { @@ -77,13 +85,6 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color file, offset := parseRange(src, highlightRange) headerRange := highlightRange - if snippetRange.Empty() { - // We assume that empty range signals diagnostic - // related to the whole body, so we lookup the definition - // instead of attempting to render empty range - snippetRange = hcled.ContextDefRange(file, offset-1) - headerRange = snippetRange - } contextStr := hcled.ContextString(file, offset-1) if contextStr != "" { @@ -100,18 +101,14 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color continue } beforeRange, highlightedRange, afterRange := lineRange.PartitionAround(highlightRange) - if highlightedRange.Empty() { - fmt.Fprintf(&buf, "%4d: %s\n", lineRange.Start.Line, sc.Bytes()) - } else { - before := beforeRange.SliceBytes(src) - highlighted := highlightedRange.SliceBytes(src) - after := afterRange.SliceBytes(src) - fmt.Fprintf( - &buf, color.Color("%4d: %s[underline]%s[reset]%s\n"), - lineRange.Start.Line, - before, highlighted, after, - ) - } + before := beforeRange.SliceBytes(src) + highlighted := highlightedRange.SliceBytes(src) + after := afterRange.SliceBytes(src) + fmt.Fprintf( + &buf, color.Color("%4d: %s[underline]%s[reset]%s\n"), + lineRange.Start.Line, + before, highlighted, after, + ) } } @@ -180,12 +177,6 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color return buf.String() } -// sourceCodeContextStr attempts to find a user-friendly description of -// the location of the given range in the given source code. -// -// An empty string is returned if no suitable description is available, e.g. -// because the source is invalid, or because the offset is not inside any sort -// of identifiable container. func parseRange(src []byte, rng hcl.Range) (*hcl.File, int) { filename := rng.Filename offset := rng.Start.Byte diff --git a/go.mod b/go.mod index 032d5f3a1..01e199be4 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,7 @@ require ( github.com/hashicorp/go-version v1.1.0 github.com/hashicorp/golang-lru v0.5.0 // indirect github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f - github.com/hashicorp/hcl2 v0.0.0-20190327223817-3fb4ed0d9260 + github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 github.com/hashicorp/logutils v1.0.0 github.com/hashicorp/memberlist v0.1.0 // indirect diff --git a/go.sum b/go.sum index d26784073..a3d613e85 100644 --- a/go.sum +++ b/go.sum @@ -202,6 +202,8 @@ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh github.com/hashicorp/hcl2 v0.0.0-20181208003705-670926858200/go.mod h1:ShfpTh661oAaxo7VcNxg0zcZW6jvMa7Moy2oFx7e5dE= github.com/hashicorp/hcl2 v0.0.0-20190327223817-3fb4ed0d9260 h1:C3vhYEXk8ihs+Xvq093axRyYhfLERrZ6Uv5tfRw9yvw= github.com/hashicorp/hcl2 v0.0.0-20190327223817-3fb4ed0d9260/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= +github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 h1:zCITwiA0cog6aYr/a/McDHKtgsEpYxXvTIgugv5iu8o= +github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= diff --git a/tfdiags/contextual_test.go b/tfdiags/contextual_test.go index 937dfaf22..5dfef867c 100644 --- a/tfdiags/contextual_test.go +++ b/tfdiags/contextual_test.go @@ -58,8 +58,8 @@ simple_attr = "val" } emptySrcRng := &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 33, Column: 1, Byte: 440}, - End: SourcePos{Line: 33, Column: 1, Byte: 440}, + Start: SourcePos{Line: 1, Column: 1, Byte: 0}, + End: SourcePos{Line: 1, Column: 1, Byte: 0}, } testCases := []struct { diff --git a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/structure.go b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/structure.go index 22e389c81..476025d1b 100644 --- a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/structure.go +++ b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/structure.go @@ -279,7 +279,11 @@ func (b *Body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { } func (b *Body) MissingItemRange() hcl.Range { - return b.EndRange + return hcl.Range{ + Filename: b.SrcRange.Filename, + Start: b.SrcRange.Start, + End: b.SrcRange.Start, + } } // Attributes is the collection of attribute definitions within a body. diff --git a/vendor/modules.txt b/vendor/modules.txt index ae1663f76..19c619954 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -307,7 +307,7 @@ github.com/hashicorp/hcl/hcl/scanner github.com/hashicorp/hcl/hcl/strconv github.com/hashicorp/hcl/json/scanner github.com/hashicorp/hcl/json/token -# github.com/hashicorp/hcl2 v0.0.0-20190327223817-3fb4ed0d9260 +# github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 github.com/hashicorp/hcl2/hcl github.com/hashicorp/hcl2/hcl/hclsyntax github.com/hashicorp/hcl2/hcldec