From 4a4b8373866a2cfc054897c9a2c1ad1fe37516bd Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 3 Sep 2024 15:28:54 -0400 Subject: [PATCH] hcl2template: rename `Name`->`LocalName` for local Local variables had an attribute called Name with the name of the local variable. However, when producing an error while walking the DAG of local/datasources, if an error is encountered during validation, the raw structure of the vertex was printed out, making the error message produced hard to understand. Therefore in order to clean it up, we rename the `Name` attribute for Local variables as `LocalName`, and introduce a `Name()` function for that block so that the complete name of the variable is clearly reported. --- hcl2template/parser.go | 8 ++++---- hcl2template/types.packer_config.go | 20 ++++++++++---------- hcl2template/types.refstring.go | 2 +- hcl2template/types.variables.go | 10 +++++++--- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index a11c5dd15..5c1460419 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -362,7 +362,7 @@ func (cfg *PackerConfig) detectBuildPrereqDependencies() hcl.Diagnostics { Severity: hcl.DiagError, Summary: "failed to process local dependency", Detail: fmt.Sprintf("An error occurred while processing a dependency for local variable %s: %s", - loc.Name, err), + loc.LocalName, err), }) continue } @@ -373,7 +373,7 @@ func (cfg *PackerConfig) detectBuildPrereqDependencies() hcl.Diagnostics { Severity: hcl.DiagError, Summary: "failed to register local dependency", Detail: fmt.Sprintf("An error occurred while registering %q as a dependency for local variable %s: %s", - rs, loc.Name, err), + rs, loc.LocalName, err), }) } } @@ -394,7 +394,7 @@ func (cfg *PackerConfig) buildPrereqsDAG() (*dag.AcyclicGraph, error) { } for _, local := range cfg.LocalBlocks { v := retGraph.Add(local) - verticesMap[fmt.Sprintf("local.%s", local.Name)] = v + verticesMap[fmt.Sprintf("local.%s", local.LocalName)] = v } // Connect the vertices together @@ -411,7 +411,7 @@ func (cfg *PackerConfig) buildPrereqsDAG() (*dag.AcyclicGraph, error) { } } for _, loc := range cfg.LocalBlocks { - locName := fmt.Sprintf("local.%s", loc.Name) + locName := fmt.Sprintf("local.%s", loc.LocalName) for _, dep := range loc.dependencies { retGraph.Connect( diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index ae27d4ecc..fadffe295 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -207,8 +207,8 @@ func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) { diags = append(diags, moreDiags...) for name, attr := range attrs { locals = append(locals, &LocalBlock{ - Name: name, - Expr: attr.Expr, + LocalName: name, + Expr: attr.Expr, }) } } @@ -219,7 +219,7 @@ func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) { func (c *PackerConfig) localByName(local string) (*LocalBlock, error) { for _, loc := range c.LocalBlocks { - if loc.Name != local { + if loc.LocalName != local { continue } @@ -260,7 +260,7 @@ func (c *PackerConfig) evaluateLocalVariables(locals []*LocalBlock) hcl.Diagnost Summary: "failed to extract dependency name from traversal ref", Detail: fmt.Sprintf("while preparing for evaluation of local variable %q, "+ "a dependency was unable to be converted to a refString. "+ - "This is likely a Packer bug, please consider reporting it.", local.Name), + "This is likely a Packer bug, please consider reporting it.", local.Name()), }) continue } @@ -290,19 +290,19 @@ func (c *PackerConfig) checkForDuplicateLocalDefinition() hcl.Diagnostics { localNames := map[string]*LocalBlock{} for _, block := range c.LocalBlocks { - loc, ok := localNames[block.Name] + loc, ok := localNames[block.LocalName] if ok { diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Duplicate local definition", Detail: fmt.Sprintf("Local variable %q is defined twice in your templates. Other definition found at %q", - block.Name, loc.Expr.Range()), + block.LocalName, loc.Expr.Range()), Subject: block.Expr.Range().Ptr(), }) continue } - localNames[block.Name] = block + localNames[block.LocalName] = block } return diags @@ -333,7 +333,7 @@ func (c *PackerConfig) recursivelyEvaluateLocalVariable(local *LocalBlock, depth Severity: hcl.DiagError, Summary: "failed to get local variable", Detail: fmt.Sprintf("While evaluating %q, its dependency %q was not found, is it defined?", - local.Name, dep.String()), + local.Name(), dep.String()), }) } @@ -355,8 +355,8 @@ func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostic if moreDiags.HasErrors() { return diags } - cfg.LocalVariables[local.Name] = &Variable{ - Name: local.Name, + cfg.LocalVariables[local.LocalName] = &Variable{ + Name: local.LocalName, Sensitive: local.Sensitive, Values: []VariableAssignment{{ Value: value, diff --git a/hcl2template/types.refstring.go b/hcl2template/types.refstring.go index 3199c9905..67c6a8f5f 100644 --- a/hcl2template/types.refstring.go +++ b/hcl2template/types.refstring.go @@ -100,7 +100,7 @@ func (cfg *PackerConfig) getComponentByRef(rs refString) (interface{}, error) { return nil, fmt.Errorf("failed to get datasource '%s.%s': component unknown", rs.Type, rs.Name) case "local": for _, loc := range cfg.LocalBlocks { - if loc.Name != rs.Name { + if loc.LocalName != rs.Name { continue } return loc, nil diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index b9fb0776b..140421e6c 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -25,8 +25,8 @@ const badIdentifierDetail = "A name must start with a letter or underscore and m // The "locals" block itself is not represented, because it serves only to // provide context for us to interpret its contents. type LocalBlock struct { - Name string - Expr hcl.Expression + LocalName string + Expr hcl.Expression // When Sensitive is set to true Packer will try its best to hide/obfuscate // the variable from the output stream. By replacing the text. Sensitive bool @@ -43,6 +43,10 @@ type LocalBlock struct { evaluated bool } +func (l LocalBlock) Name() string { + return fmt.Sprintf("local.%s", l.LocalName) +} + // VariableAssignment represents a way a variable was set: the expression // setting it and the value of that expression. It helps pinpoint were // something was set in diagnostics. @@ -303,7 +307,7 @@ func decodeLocalBlock(block *hcl.Block) (*LocalBlock, hcl.Diagnostics) { } l := &LocalBlock{ - Name: name, + LocalName: name, } if attr, exists := content.Attributes["sensitive"]; exists {