{"record":{"id":"79bfddd47ed3501f","repo":"hashicorp/packer","slug":"failed-to-get-datasource-s-s-component-unknow","errorCode":null,"errorMessage":"failed to get datasource '%s.%s': component unknown","messagePattern":"failed to get datasource '(.+?)\\.(.+?)': component unknown","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/types.refstring.go","lineNumber":103,"sourceCode":"\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}\n\t\t\tif ds.DSName != rs.Name {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn ds, nil\n\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}","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/types.refstring.go#L85-L121","documentation":"getComponentByRef resolves a parsed refString against the blocks registered in the PackerConfig. For a 'data' refString it scans cfg.Datasources for a block whose Type and DSName both match; this error is thrown when no registered datasource matches, i.e. the referenced data.<type>.<name> block is unknown to the configuration.","triggerScenarios":"Calling getComponentByRef (via recursivelyEvaluateLocalVariable while evaluating a local) with a data refString whose Type/Name pair is not present in cfg.Datasources — the datasource block was never declared, or was declared with a different type or name.","commonSituations":"Typo in the datasource name or type in ${data.type.name}; datasource block deleted or renamed while references remain; datasource declared in a different template/file not included in this build; plugin type label mismatch (e.g. 'amazon-ami' vs 'amazon-ami' with different source path).","solutions":["Add the missing datasource block to the template, e.g. data \"amazon-ami\" \"my_ami\" { ... }.","Verify the reference's type and name exactly match the datasource block's labels (case-sensitive).","If the block was renamed, update every ${data.<type>.<name>} reference to the new name.","Confirm the file containing the datasource block is part of the template being built."],"exampleFix":"// before (reference with no matching block)\nlocals { ami_id = data.amazon-ami.my_ami.id }\n// after (declare the block)\ndata \"amazon-ami\" \"my_ami\" { filters = { name = \"ubuntu-*\" } }\nlocals { ami_id = data.amazon-ami.my_ami.id }","handlingStrategy":"validation","validationCode":"// before evaluating, verify the datasource block exists in the parsed config\nfound := false\nfor _, ds := range cfg.Datasources {\n\tif ds.Type == dsType && ds.DSName == dsName {\n\t\tfound = true\n\t\tbreak\n\t}\n}\nif !found {\n\treturn fmt.Errorf(\"datasource %s.%s not declared in template\", dsType, dsName)\n}","typeGuard":"func hasDatasource(cfg *hcl2template.PackerConfig, dsType, dsName string) bool {\n\tfor _, ds := range cfg.Datasources {\n\t\tif ds.Type == dsType && ds.DSName == dsName {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}","tryCatchPattern":"comp, err := cfg.getComponentByRef(rs)\nif err != nil {\n\treturn fmt.Errorf(\"cannot resolve %s: %w — check the data block is declared with matching type/name labels\", rs, err)\n}","preventionTips":["Keep datasource type/name labels in sync with every ${data.type.name} usage.","Run packer validate (or packer inspect) on templates before build to catch unknown references.","After renaming a datasource block, grep the template for the old name."],"tags":["hcl2template","datasource","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"}