command/format: improve "source" of error messages regarding missing arguments (#20907)

* vendor: update hcl2 dependency
* command/format: revert diagnostic format behavior if snippet or highlight range is empty
This commit is contained in:
Kristin Laemmert 2019-04-03 14:04:59 -04:00 committed by GitHub
parent 87f141000b
commit f8a5e17d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 30 deletions

View File

@ -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,9 +101,6 @@ 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)
@ -112,7 +110,6 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color
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

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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 {

View File

@ -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.

2
vendor/modules.txt vendored
View File

@ -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