{"record":{"id":"4b28f184d03f1615","repo":"hashicorp/packer","slug":"failed-to-create-cyclonedx-encoder-w","errorCode":null,"errorMessage":"failed to create CycloneDX encoder: %w","messagePattern":"failed to create CycloneDX encoder: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sbom/generator_syft.go","lineNumber":71,"sourceCode":"\tsbomResult, err := syft.CreateSBOM(ctx, src, sbomCfg)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create SBOM: %w\", err)\n\t}\n\n\treturn g.encodeToFormat(sbomResult)\n}\n\n// encodeToFormat encodes the SBOM to the requested format.\nfunc (g *Generator) encodeToFormat(sbomData *sbom.SBOM) ([]byte, error) {\n\tswitch g.config.Format {\n\tcase FormatCycloneDX:\n\t\tcfg := cyclonedxjson.DefaultEncoderConfig()\n\t\tcfg.Pretty = true\n\t\tencoder, err := cyclonedxjson.NewFormatEncoderWithConfig(\n\t\t\tcfg,\n\t\t)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create CycloneDX encoder: %w\", err)\n\t\t}\n\t\treturn format.Encode(*sbomData, encoder)\n\n\tcase FormatSPDX:\n\t\tcfg := spdxjson.DefaultEncoderConfig()\n\t\tcfg.Pretty = true\n\t\tencoder, err := spdxjson.NewFormatEncoderWithConfig(\n\t\t\tcfg,\n\t\t)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create SPDX encoder: %w\", err)\n\t\t}\n\t\treturn format.Encode(*sbomData, encoder)\n\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported format: %s (supported: cyclonedx, spdx)\", g.config.Format)\n\t}\n}","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/internal/sbom/generator_syft.go#L53-L89","documentation":"When encoding the completed SBOM as CycloneDX JSON, cyclonedxjson.NewFormatEncoderWithConfig failed to build the encoder, wrapped as `failed to create CycloneDX encoder: %w`. This indicates a mismatch between the encoder config and the Syft version — with DefaultEncoderConfig it is rare and usually a library/version integration problem rather than user input.","triggerScenarios":"Syft library upgrade changing NewFormatEncoderWithConfig signature/behavior; config struct fields set to values the encoder constructor rejects; build-tag or vendoring inconsistencies causing an incompatible cyclonedxjson package.","commonSituations":"Partial dependency upgrades (go.mod pinning different syft module versions); custom fork/vendor drift; a Syft release where the CycloneDX encoder constructor gained validation.","solutions":["Read the wrapped cause from %w for the constructor's actual complaint","Run go mod tidy / go mod vendor to align syft module versions","Upgrade or pin github.com/anchore/syft to a known-good version","Fall back to the default encoder path or the SPDX encoder if CycloneDX encoding is broken in that version"],"exampleFix":"// before\nimport \"github.com/anchore/syft/syft/format/cyclonedxjson\" // version drift vs syft core\n// after\n// go.mod: pin one consistent version\ngo get github.com/anchore/syft@v1.x.y && go mod tidy\n// then rebuild; if still failing:\nencoder, err := cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())","handlingStrategy":"fallback","validationCode":"// no pre-call validation possible; constructor depends on library internals","typeGuard":null,"tryCatchPattern":"out, err := gen.Generate(ctx)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to create CycloneDX encoder\") {\n        log.Warn(\"CycloneDX encoder unavailable; retrying with SPDX\")\n        cfg.Format = sbom.FormatSPDX\n        return sbom.NewGenerator(cfg).Generate(ctx)\n    }\n    return err\n}","preventionTips":["Pin a single syft version across go.mod and run go mod tidy in CI","Test SBOM generation after every syft upgrade","Keep vendored/cyclonedxjson package in sync with the syft core module"],"tags":["go","syft","cyclonedx","dependency-version","sbom"],"backgroundTag":"encoder-creation-failed","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"}