{"record":{"id":"72e4ee493221dcda","repo":"gohugoio/hugo","slug":"failed-to-open-image-for-decode-w","errorCode":null,"errorMessage":"failed to open image for decode: %w","messagePattern":"failed to open image for decode: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/image.go","lineNumber":475,"sourceCode":"\t\ttargetPath := i.relTargetPathFromConfig(conf, i.getSpec().Imaging.Cfg.SourceHash)\n\t\tci.setTargetPath(targetPath)\n\t\tci.Format = conf.TargetFormat\n\t\tci.setMediaType(conf.TargetFormat.MediaType())\n\n\t\treturn ci, converted, nil\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn img, nil\n}\n\n// DecodeImage decodes the image source into an Image.\n// This for internal use only.\nfunc (i *imageResource) DecodeImage() (image.Image, error) {\n\tf, err := i.ReadSeekCloser()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to open image for decode: %w\", err)\n\t}\n\tdefer f.Close()\n\n\treturn i.getSpec().Imaging.Codec.DecodeFormat(i.Format, f)\n}\n\nfunc (i *imageResource) clone(img image.Image) *imageResource {\n\tspec := i.baseResource.Clone().(baseResource)\n\n\tvar image *images.Image\n\tif img != nil {\n\t\timage = i.WithImage(img)\n\t} else {\n\t\timage = i.WithSpec(spec)\n\t}\n\n\tir := &imageResource{\n\t\tImage:        image,","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/resources/image.go#L457-L493","documentation":"Returned by imageResource.DecodeImage when i.ReadSeekCloser() fails to open the underlying image source for reading. This wraps the open/read error with %w. ReadSeekCloser opens the resource's content (file, remote, or embedded) so failures here mean the source bytes are inaccessible before decoding even starts.","triggerScenarios":"Processing an image resource whose backing file was deleted/moved after registration, a remote image resource whose fetch fails, a resource on a filesystem (afero) that errors on Open, or permission denied on the source path.","commonSituations":"Source asset removed between build setup and image processing, a module/remote resource that 404s, a permissions issue on the working filesystem, or a symlink that dangles.","solutions":["Confirm the image source file exists and is readable at build time.","For remote/module resources, verify the URL resolves and network/cache is healthy.","Unwrap the error to distinguish a missing file (os.ErrNotExist) from a permission error."],"exampleFix":"// before: source deleted after resource registration\n{{ $img := $res.Resize \"100x\" }}  // fails: file gone\n\n// after: ensure asset exists / pin via module\n{{ $img := resources.Get \"images/logo.png\" }}\n{{ with $img }}{{ .Resize \"100x\" }}{{ end }}","handlingStrategy":"validation","validationCode":"// Confirm the source resource is readable before decoding.\nfunc canOpen(res resource.Source) error {\n    r, err := res.ReadSeekCloser()\n    if err != nil { return err }\n    r.Close()\n    return nil\n}","typeGuard":null,"tryCatchPattern":"img, err := ir.DecodeImage()\nif err != nil {\n    if errors.Is(err, os.ErrNotExist) { /* source missing */ }\n    return err\n}","preventionTips":["Verify image assets exist and are readable at build time.","For remote resources, validate the URL/cache before processing.","Check file permissions on the working afero filesystem."],"tags":["image-processing","io","resource","file-access"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}