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:
parent
87f141000b
commit
f8a5e17d3d
|
@ -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
|
// Make sure the snippet includes the highlight. This should be true
|
||||||
// for any reasonable diagnostic, but we'll make sure.
|
// for any reasonable diagnostic, but we'll make sure.
|
||||||
snippetRange = hcl.RangeOver(snippetRange, highlightRange)
|
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
|
var src []byte
|
||||||
if sources != nil {
|
if sources != nil {
|
||||||
|
@ -77,13 +85,6 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color
|
||||||
file, offset := parseRange(src, highlightRange)
|
file, offset := parseRange(src, highlightRange)
|
||||||
|
|
||||||
headerRange := 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)
|
contextStr := hcled.ContextString(file, offset-1)
|
||||||
if contextStr != "" {
|
if contextStr != "" {
|
||||||
|
@ -100,18 +101,14 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
beforeRange, highlightedRange, afterRange := lineRange.PartitionAround(highlightRange)
|
beforeRange, highlightedRange, afterRange := lineRange.PartitionAround(highlightRange)
|
||||||
if highlightedRange.Empty() {
|
before := beforeRange.SliceBytes(src)
|
||||||
fmt.Fprintf(&buf, "%4d: %s\n", lineRange.Start.Line, sc.Bytes())
|
highlighted := highlightedRange.SliceBytes(src)
|
||||||
} else {
|
after := afterRange.SliceBytes(src)
|
||||||
before := beforeRange.SliceBytes(src)
|
fmt.Fprintf(
|
||||||
highlighted := highlightedRange.SliceBytes(src)
|
&buf, color.Color("%4d: %s[underline]%s[reset]%s\n"),
|
||||||
after := afterRange.SliceBytes(src)
|
lineRange.Start.Line,
|
||||||
fmt.Fprintf(
|
before, highlighted, after,
|
||||||
&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()
|
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) {
|
func parseRange(src []byte, rng hcl.Range) (*hcl.File, int) {
|
||||||
filename := rng.Filename
|
filename := rng.Filename
|
||||||
offset := rng.Start.Byte
|
offset := rng.Start.Byte
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -60,7 +60,7 @@ require (
|
||||||
github.com/hashicorp/go-version v1.1.0
|
github.com/hashicorp/go-version v1.1.0
|
||||||
github.com/hashicorp/golang-lru v0.5.0 // indirect
|
github.com/hashicorp/golang-lru v0.5.0 // indirect
|
||||||
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
|
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/hil v0.0.0-20190212112733-ab17b08d6590
|
||||||
github.com/hashicorp/logutils v1.0.0
|
github.com/hashicorp/logutils v1.0.0
|
||||||
github.com/hashicorp/memberlist v0.1.0 // indirect
|
github.com/hashicorp/memberlist v0.1.0 // indirect
|
||||||
|
|
2
go.sum
2
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-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 h1:C3vhYEXk8ihs+Xvq093axRyYhfLERrZ6Uv5tfRw9yvw=
|
||||||
github.com/hashicorp/hcl2 v0.0.0-20190327223817-3fb4ed0d9260/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek=
|
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 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI=
|
||||||
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE=
|
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=
|
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
|
||||||
|
|
|
@ -58,8 +58,8 @@ simple_attr = "val"
|
||||||
}
|
}
|
||||||
emptySrcRng := &SourceRange{
|
emptySrcRng := &SourceRange{
|
||||||
Filename: "test.tf",
|
Filename: "test.tf",
|
||||||
Start: SourcePos{Line: 33, Column: 1, Byte: 440},
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
||||||
End: SourcePos{Line: 33, Column: 1, Byte: 440},
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
|
|
@ -279,7 +279,11 @@ func (b *Body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Body) MissingItemRange() hcl.Range {
|
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.
|
// Attributes is the collection of attribute definitions within a body.
|
||||||
|
|
|
@ -307,7 +307,7 @@ github.com/hashicorp/hcl/hcl/scanner
|
||||||
github.com/hashicorp/hcl/hcl/strconv
|
github.com/hashicorp/hcl/hcl/strconv
|
||||||
github.com/hashicorp/hcl/json/scanner
|
github.com/hashicorp/hcl/json/scanner
|
||||||
github.com/hashicorp/hcl/json/token
|
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
|
||||||
github.com/hashicorp/hcl2/hcl/hclsyntax
|
github.com/hashicorp/hcl2/hcl/hclsyntax
|
||||||
github.com/hashicorp/hcl2/hcldec
|
github.com/hashicorp/hcl2/hcldec
|
||||||
|
|
Loading…
Reference in New Issue