{"record":{"id":"d584f5b758152721","repo":"hashicorp/packer","slug":"unsupported-component-q-only-vars-locals-and-d","errorCode":null,"errorMessage":"Unsupported component: %q, only vars, locals and datasources can be fetched by their refString","messagePattern":"Unsupported component: %q, only vars, locals and datasources can be fetched by their refString","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/types.refstring.go","lineNumber":120,"sourceCode":"\t\t}\n\t\treturn nil, fmt.Errorf(\"failed to get datasource '%s.%s': component unknown\", rs.Type, rs.Name)\n\tcase \"local\":\n\t\tfor _, loc := range cfg.LocalBlocks {\n\t\t\tif loc.LocalName != rs.Name {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn loc, nil\n\t\t}\n\tcase \"var\":\n\t\tfor _, val := range cfg.InputVariables {\n\t\t\tif val.Name != rs.Name {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn val, nil\n\t\t}\n\t}\n\n\treturn nil, fmt.Errorf(\"Unsupported component: %q, only vars, locals and datasources can be fetched by their refString\", rs)\n}\n\nfunc (ds *DatasourceBlock) RegisterDependency(rs refString) error {\n\tswitch rs.MType {\n\tcase \"data\", \"local\":\n\t\tds.Dependencies = append(ds.Dependencies, rs)\n\t// NOOP: vars are always evaluated beforehand for datasources\n\tcase \"var\":\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported dependency type %q; datasources can only depend on local, var or data.\", rs.MType)\n\t}\n\n\treturn nil\n}\n\nfunc (loc *LocalBlock) RegisterDependency(rs refString) error {\n\tswitch rs.MType {\n\tcase \"data\", \"local\":","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/types.refstring.go#L102-L138","documentation":"getComponentByRef is the terminal fallthrough of the refString resolver: after checking data/local/var cases, if the component was not found in the matching collection (or the MType was somehow something else), it returns this error stating that only vars, locals, and datasources can be fetched by refString. It covers both an unsupported refString type slipping through and a local/var name that has no matching block.","triggerScenarios":"Calling getComponentByRef with a 'local' refString whose LocalName is absent from cfg.LocalBlocks, or a 'var' refString whose Name is absent from cfg.InputVariables — the switch falls through to the generic error; also any refString reaching here with a non-data/local/var MType.","commonSituations":"Referencing ${local.foo} or ${var.foo} that was never declared (typo or removed block); referencing a variable defined only in a var-file not passed via -var-file; forgetting to declare an input variable that is used in a local expression.","solutions":["Declare the missing local or variable block in the template (e.g. variable \"foo\" { ... } or locals { foo = ... }).","Check the name for typos and exact casing against the declared block.","If the variable lives in a var-file, pass it with -var-file or -var on the packer build command.","Confirm the reference is meant to be data/local/var; other component types (builders, provisioners, post-processors) cannot be referenced via refString."],"exampleFix":"// before (undeclared variable)\nlocals { region = var.aws_region }\n// after\nvariable \"aws_region\" { type = string, default = \"us-east-1\" }\nlocals { region = var.aws_region }","handlingStrategy":"validation","validationCode":"// verify the referenced local/var is declared before evaluation\nname := \"aws_region\" // from rs.Name\nok := false\nfor _, v := range cfg.InputVariables {\n\tif v.Name == name { ok = true; break }\n}\nfor _, l := range cfg.LocalBlocks {\n\tif l.LocalName == name { ok = true; break }\n}\nif !ok {\n\treturn fmt.Errorf(\"local/var %q is not declared in the template\", name)\n}","typeGuard":"func hasLocalOrVar(cfg *hcl2template.PackerConfig, name string) bool {\n\tfor _, v := range cfg.InputVariables {\n\t\tif v.Name == name { return true }\n\t}\n\tfor _, l := range cfg.LocalBlocks {\n\t\tif l.LocalName == name { return true }\n\t}\n\treturn false\n}","tryCatchPattern":"comp, err := cfg.getComponentByRef(rs)\nif err != nil {\n\treturn fmt.Errorf(\"unresolvable reference %s: %w — declare the variable/local block or pass the missing -var/-var-file\", rs, err)\n}","preventionTips":["Declare every variable used by locals/other blocks; prefer required variables with no default only when intentional.","Always pass var-files explicitly on CI builds so referenced vars exist.","Run packer validate before build to surface undeclared locals/vars."],"tags":["hcl2template","refstring","missing-block","reference-resolution"],"backgroundTag":"unknown-reference","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}