{"record":{"id":"463889350ced494c","repo":"hashicorp/packer","slug":"unsupported-reftype-q-must-be-either-data-lo","errorCode":null,"errorMessage":"unsupported reftype %q, must be either 'data', 'local' or 'var'","messagePattern":"unsupported reftype %q, must be either 'data', 'local' or 'var'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/types.refstring.go","lineNumber":66,"sourceCode":"\t}\n\n\treturn refString{}, fmt.Errorf(\"unsupported refstring %q, must be of 'data', 'local' or 'var' type\", t)\n}\n\nfunc NewRefString(rs string) (refString, error) {\n\tparts := strings.Split(rs, \".\")\n\n\tswitch parts[0] {\n\tcase \"local\", \"var\":\n\t\treturn refString{\n\t\t\tMType: parts[0],\n\t\t\tName:  parts[1],\n\t\t}, nil\n\tcase \"data\":\n\t\treturn newDataSourceRefString(parts)\n\t}\n\n\treturn refString{}, fmt.Errorf(\"unsupported reftype %q, must be either 'data', 'local' or 'var'\", parts[0])\n}\n\nfunc (rs refString) String() string {\n\tif rs.Type == \"\" {\n\t\treturn fmt.Sprintf(\"%s.%s\", rs.MType, rs.Name)\n\t}\n\n\treturn fmt.Sprintf(\"%s.%s.%s\", rs.MType, rs.Type, rs.Name)\n}\n\nfunc newDataSourceRefString(parts []string) (refString, error) {\n\tif len(parts) != 3 {\n\t\treturn refString{}, fmt.Errorf(\"malformed datasource reference %q, data sources must be composed of 3 parts\",\n\t\t\tstrings.Join(parts, \".\"))\n\t}\n\n\treturn refString{\n\t\tMType: \"data\",","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/types.refstring.go#L48-L84","documentation":"NewRefString parses a dotted reference string (data.<type>.<name>, var.<name>, local.<name>) into a refString struct. This error is thrown when the first segment of the string is none of 'data', 'local', or 'var', meaning the string does not reference a component type Packer knows how to resolve. It is a parse-time guard so unsupported roots fail fast instead of producing a bogus refString.","triggerScenarios":"Calling NewRefString (directly or via NewRefStringFromDep with a traversal whose root is not data/local/var) with a string whose first dot-segment is something else, e.g. \"resource.foo\", \"aws_instance.web\", \"myvar.x\", or an empty string (parts[0] == \"\").","commonSituations":"Typo in a template reference like ${dataa.source} or forgetting the prefix entirely and writing just the datasource name; using Terraform-style roots unsupported by Packer such as module.* or resource.*; programmatic/plugin code building dependency strings by hand with the wrong prefix.","solutions":["Fix the reference string to start with 'data.', 'local.', or 'var.' as appropriate.","If referencing a datasource, use the full three-part form data.<type>.<name> (e.g. data.amazon-ami.my_ami).","If the refString is built programmatically, prefix it with the correct component type before calling NewRefString, or use NewRefStringFromDep with a valid hcl.Traversal.","Check for typos and extra/missing dots that shift which segment becomes parts[0]."],"exampleFix":"// before\nrs, err := NewRefString(\"amazon-ami.my_ami\")\n// after\nrs, err := NewRefString(\"data.amazon-ami.my_ami\")","handlingStrategy":"validation","validationCode":"func validRefString(s string) bool {\n\tparts := strings.Split(s, \".\")\n\tswitch parts[0] {\n\tcase \"local\", \"var\":\n\t\treturn len(parts) == 2\n\tcase \"data\":\n\t\treturn len(parts) == 3\n\t}\n\treturn false\n}\n// guard: if !validRefString(ref) { fix before NewRefString(ref) }","typeGuard":"func isSupportedRefRoot(s string) bool {\n\troot := strings.SplitN(s, \".\", 2)[0]\n\treturn root == \"data\" || root == \"local\" || root == \"var\"\n}","tryCatchPattern":"rs, err := NewRefString(ref)\nif err != nil {\n\treturn fmt.Errorf(\"invalid reference %q: %w\", ref, err)\n}","preventionTips":["Always prefix references with data./local./var. when building them programmatically.","Prefer NewRefStringFromDep with a real hcl.Traversal over hand-concatenating strings.","Add a unit test asserting every reference your code generates parses via NewRefString."],"tags":["hcl2template","refstring","parsing","packer"],"backgroundTag":"invalid-reference-syntax","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"}