Skip to content

Commit

Permalink
fix: Upgraded lockfile now has correct whitespace. (#90)
Browse files Browse the repository at this point in the history
* fix: Upgraded lockfile now has correct whitespace.

* fix: Correct linting error.
  • Loading branch information
Junkern authored Sep 7, 2022
1 parent 1164203 commit a41c292
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
55 changes: 49 additions & 6 deletions internal/app/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"golang.org/x/mod/sumdb/dirhash"
Expand Down Expand Up @@ -122,13 +123,10 @@ func createHclBody(hcl Lockfile) string {
barBody.SetAttributeValue("constraints", cty.StringVal(*v.Constraints))
}

var listOfHashes []cty.Value
for _, hash := range v.Hashes {
listOfHashes = append(listOfHashes, cty.StringVal(hash))
if len(v.Hashes) > 0 {
hashToks := encodeHashSetTokens(v.Hashes)
barBody.SetAttributeRaw("hashes", hashToks)
}

list := cty.ListVal(listOfHashes)
barBody.SetAttributeValue("hashes", list)
}

convertedString := bytes.NewBuffer(f.Bytes()).String()
Expand All @@ -137,6 +135,51 @@ func createHclBody(hcl Lockfile) string {
return fullString
}

// Taken from https://github.com/hashicorp/terraform/blob/aeefde7428b836646ba9622f1bb313e6dfe2ca87/internal/depsfile/locks_file.go#L454
// Based on this issue: https://github.com/hashicorp/hcl/issues/542
func encodeHashSetTokens(hashes []string) hclwrite.Tokens {
// We'll generate the source code in a low-level way here (direct
// token manipulation) because it's desirable to maintain exactly
// the layout implemented here so that diffs against the locks
// file are easy to read; we don't want potential future changes to
// hclwrite to inadvertently introduce whitespace changes here.
ret := hclwrite.Tokens{
{
Type: hclsyntax.TokenOBrack,
Bytes: []byte{'['},
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
},
}

// Although lock.hashes is a slice, we de-dupe and sort it on
// initialization so it's normalized for interpretation as a logical
// set, and so we can just trust it's already in a good order here.
for _, hash := range hashes {
hashVal := cty.StringVal(hash)
ret = append(ret, hclwrite.TokensForValue(hashVal)...)
ret = append(ret, hclwrite.Tokens{
{
Type: hclsyntax.TokenComma,
Bytes: []byte{','},
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
},
}...)
}

ret = append(ret, &hclwrite.Token{
Type: hclsyntax.TokenCBrack,
Bytes: []byte{']'},
})

return ret
}

func getCalculatedHashForProvider(fullPath string) string {
packageDir, err := filepath.EvalSymlinks(fullPath)
if err != nil {
Expand Down
17 changes: 13 additions & 4 deletions internal/app/lockfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func TestCreateHclBody(t *testing.T) {
provider "test" {
version = "1.0.0"
constraints = "1.0.0"
hashes = ["h1:test", "h1:test1"]
hashes = [
"h1:test",
"h1:test1",
]
}
`

Expand All @@ -48,12 +51,16 @@ provider "test" {
provider "test" {
version = "1.0.0"
constraints = "1.0.0"
hashes = ["h1:test"]
hashes = [
"h1:test",
]
}
provider "test" {
version = "1.0.0"
constraints = "1.0.0"
hashes = ["h1:test"]
hashes = [
"h1:test",
]
}
`

Expand All @@ -72,7 +79,9 @@ provider "test" {
provider "test" {
version = "1.0.0"
hashes = ["h1:test"]
hashes = [
"h1:test",
]
}
`

Expand Down

0 comments on commit a41c292

Please sign in to comment.