Merge pull request #25381 from hashicorp/alisdair/013upgrade-preserves-more-comments

command: Fix 0.13upgrade to preserve more comments
This commit is contained in:
Alisdair McDiarmid 2020-06-25 12:56:34 -04:00 committed by GitHub
commit f6f707c669
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -404,7 +404,18 @@ command and dealing with them before running this command again.
} else { } else {
attributesObject = cty.EmptyObjectVal attributesObject = cty.EmptyObjectVal
} }
// If this block already has an entry for this local name, we only
// want to replace it if it's semantically different
if existing := body.GetAttribute(localName); existing != nil {
bytes := existing.Expr().BuildTokens(nil).Bytes()
expr, _ := hclsyntax.ParseExpression(bytes, "", hcl.InitialPos)
value, _ := expr.Value(nil)
if !attributesObject.RawEquals(value) {
body.SetAttributeValue(localName, attributesObject) body.SetAttributeValue(localName, attributesObject)
}
} else {
body.SetAttributeValue(localName, attributesObject)
}
// If we don't have a source attribute, manually construct a commented // If we don't have a source attribute, manually construct a commented
// block explaining what to do // block explaining what to do

View File

@ -9,8 +9,15 @@ terraform {
source = "hashicorp/bar" source = "hashicorp/bar"
version = "0.5.0" version = "0.5.0"
} }
# An explicit requirement
baz = {
# Comment inside the block should stay
source = "foo/baz"
}
# Foo is required
foo = { foo = {
source = "hashicorp/foo" source = "hashicorp/foo"
version = "1.0.0"
} }
} }
} }

View File

@ -6,5 +6,15 @@ terraform {
required_providers { required_providers {
# Pin bar to this version # Pin bar to this version
bar = "0.5.0" bar = "0.5.0"
# An explicit requirement
baz = {
# Comment inside the block should stay
source = "foo/baz"
}
# Foo is required
foo = {
# This comment sadly won't make it
version = "1.0.0"
}
} }
} }