{"record":{"id":"0d7d7ed066f78e26","repo":"hashicorp/packer","slug":"unsupported-refstring-q-must-be-of-data-loca","errorCode":null,"errorMessage":"unsupported refstring %q, must be of 'data', 'local' or 'var' type","messagePattern":"unsupported refstring %q, must be of 'data', 'local' or 'var' type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/types.refstring.go","lineNumber":50,"sourceCode":"\t// For locals/vars this is the name of the variable to look for, while\n\t// for datasources this is the name of the block, which coupled with the\n\t// type is the identity of the datasource's execution.\n\tName string\n}\n\nfunc NewRefStringFromDep(t hcl.Traversal) (refString, error) {\n\troot := t.RootName()\n\n\tswitch root {\n\tcase \"local\", \"var\":\n\t\treturn NewRefString(fmt.Sprintf(\"%s.%s\", root, t[1].(hcl.TraverseAttr).Name))\n\tcase \"data\":\n\t\treturn NewRefString(fmt.Sprintf(\"%s.%s.%s\", root,\n\t\t\tt[1].(hcl.TraverseAttr).Name,\n\t\t\tt[2].(hcl.TraverseAttr).Name))\n\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","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/types.refstring.go#L32-L68","documentation":"NewRefStringFromDep converts an hcl.Traversal dependency (e.g. local.foo, var.bar, data.type.name) into a refString. Only the root scopes 'local', 'var', and 'data' are supported; a traversal rooted with any other identifier (or a malformed traversal missing the expected attribute segments) reaches the default case and returns this error. Callers are detectBuildPrereqDependencies and evaluateLocalVariables while scanning template expressions for build-prerequisite dependencies.","triggerScenarios":"An expression inside a local or datasource traversal rooted at an unsupported scope — e.g. referencing a build artifact, pathroot, packer, or a mis-typed root like 'vars'/'Local' — gets passed to NewRefStringFromDep. Also produced when the traversal lacks the second attribute (t[1]) for local/var or third attribute (t[2]) for data, causing an index panic before this return in pathological cases.","commonSituations":"Typing `vars.foo` or `locals.foo` instead of `var.foo` / `local.foo` in a template; referencing special Packer contexts (packer.version, pathroot) in a position where dependency detection runs; hand-written HCL fragments with unusual traversals.","solutions":["Fix the reference root to one of the supported forms: `local.<name>`, `var.<name>`, or `data.<type>.<name>`","Check for plurals/case typos: `vars` → `var`, `locals` → `local`","If you meant a special value like packer.version or pathroot, reference it via the correct Packer context variable instead of expecting dependency tracking","Run `packer validate` to catch unsupported references with source locations before building"],"exampleFix":"// before: unsupported root in a local expression\nlocal \"tag\" { value = vars.environment }   // 'vars' unsupported\n// after\nlocal \"tag\" { value = var.environment }","handlingStrategy":"validation","validationCode":"// Ensure dependency roots are only local/var/data before relying on\n// prerequisite evaluation. Scan traversals in datasource/local expressions:\n// valid roots regex: ^(local|var|data)\\.\n// e.g. grep -nE '(^|[^a-zA-Z_.])(vars|locals|pathroot)\\.' *.pkr.hcl\n// to flag unsupported roots.","typeGuard":"func isSupportedDepRoot(t hcl.Traversal) bool {\n    switch t.RootName() {\n    case \"local\", \"var\", \"data\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"rs, err := NewRefStringFromDep(traversal)\nif err != nil {\n    return fmt.Errorf(\"dependency %v is not trackable; use local.<name>, var.<name> or data.<type>.<name>: %w\", traversal, err)\n}","preventionTips":["Use exact scope keywords: `local`, `var`, `data` — not `locals`, `vars`","Reference special values (packer.version, pathroot) via their documented context variables","Run `packer validate` to catch unsupported traversals with source locations","For datasources, remember data references need all three parts: data.<type>.<name>"],"tags":["hcl2template","hcl","reference-format","validation"],"backgroundTag":"invalid-reference-format","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"}