{"record":{"id":"fb76c3e9824f1211","repo":"hashicorp/packer","slug":"malformed-datasource-reference-q-data-sources-mu","errorCode":null,"errorMessage":"malformed datasource reference %q, data sources must be composed of 3 parts","messagePattern":"malformed datasource reference %q, data sources must be composed of 3 parts","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/types.refstring.go","lineNumber":79,"sourceCode":"\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\",\n\t\tType:  parts[1],\n\t\tName:  parts[2],\n\t}, nil\n}\n\n// getComponentByRef gets a registered component from the configuration from a refString\nfunc (cfg *PackerConfig) getComponentByRef(rs refString) (interface{}, error) {\n\tswitch rs.MType {\n\tcase \"data\":\n\t\tfor _, ds := range cfg.Datasources {\n\t\t\tif ds.Type != rs.Type {\n\t\t\t\tcontinue\n\t\t\t}","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/types.refstring.go#L61-L97","documentation":"newDataSourceRefString handles the 'data.*' branch of NewRefString and requires exactly three dot-separated segments: data.<type>.<name>. This error is thrown when a datasource reference has the right prefix ('data') but the wrong number of segments, so the datasource type and/or name cannot be identified.","triggerScenarios":"Calling NewRefString with 'data.<name>' (2 parts, type missing), 'data.<type>' alone, 'data.<type>.<name>.<extra>' (4+ parts), or bare 'data' (1 part).","commonSituations":"Writing ${data.my_ami} in a template instead of ${data.amazon-ami.my_ami}; copy-pasting a var/local style two-part reference for a datasource; accidentally appending an attribute like data.amazon-ami.my_ami.id into a context that expects only the block reference.","solutions":["Use the full three-part form: data.<datasource-type>.<datasource-name>, e.g. data.amazon-ami.my_ami.","If you need an output field of the datasource, reference the specific attribute after resolving the block, not inside the refString itself.","Count the dots: exactly two dots are required in a data.* reference.","If the type label is unknown, check the datasource plugin name in the template's required_plugins block and use that as <type>."],"exampleFix":"// before\nrs, err := NewRefString(\"data.my_ami\")\n// after\nrs, err := NewRefString(\"data.amazon-ami.my_ami\")","handlingStrategy":"validation","validationCode":"func validDataSourceRef(s string) bool {\n\tparts := strings.Split(s, \".\")\n\treturn len(parts) == 3 && parts[0] == \"data\"\n}\n// guard before: NewRefString(\"data.amazon-ami.my_ami\")","typeGuard":"func isThreePartDataRef(s string) bool {\n\tparts := strings.Split(s, \".\")\n\treturn len(parts) == 3 && parts[0] == \"data\" && parts[1] != \"\" && parts[2] != \"\"\n}","tryCatchPattern":"rs, err := NewRefString(ref)\nif err != nil {\n\treturn fmt.Errorf(\"malformed data source ref %q (need data.<type>.<name>): %w\", ref, err)\n}","preventionTips":["Memorize the shape: data.<plugin-type>.<block-name> — two dots, three segments.","Do not append attribute names (.id, .name) to a refString; resolve the block first.","When referencing a var/local, do not reuse the two-part form for data sources."],"tags":["hcl2template","refstring","datasource","parsing"],"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"}