{"id":"eaa85c9ef575f6df","repo":"gohugoio/hugo","slug":"failed-to-decode-module-download-result-w","errorCode":null,"errorMessage":"failed to decode module download result: %w","messagePattern":"failed to decode module download result: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/client.go","lineNumber":473,"sourceCode":"\t// No valid cache, need to query.\n\targs := []string{\"mod\", \"download\", \"-json\", fmt.Sprintf(\"%s@%s\", path, version)}\n\n\tb := &bytes.Buffer{}\n\terr := c.runGo(context.Background(), b, args...)\n\tif err != nil {\n\t\t// The -json flag makes Go output error details as JSON to stdout\n\t\t// even on failure. Try to extract the error message from the JSON output.\n\t\tif jsonErr := extractGoModDownloadError(b.Bytes()); jsonErr != \"\" {\n\t\t\treturn nil, fmt.Errorf(\"failed to download module %s@%s: %s: %s\", path, version, err, jsonErr)\n\t\t}\n\t\treturn nil, fmt.Errorf(\"failed to download module %s@%s: %w\", path, version, err)\n\t}\n\n\tjsonResult := b.Bytes()\n\n\tm := &goModule{}\n\tif err := json.NewDecoder(bytes.NewReader(jsonResult)).Decode(m); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode module download result: %w\", err)\n\t}\n\n\tif m.Error != nil {\n\t\treturn nil, errors.New(m.Error.Err)\n\t}\n\n\t// Cache the successful result if it has a Dir field.\n\tif m.Dir != \"\" {\n\t\t_ = c.ccfg.ModuleQueriesCache.SetBytes(cacheKey, jsonResult)\n\t}\n\n\treturn m, nil\n}\n\n// isDirNonEmpty returns true if dir exists and contains at least one file or subdirectory.\nfunc (c *Client) isDirNonEmpty(dir string) bool {\n\tf, err := c.fs.Open(dir)\n\tif err != nil {","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/client.go#L455-L491","documentation":"Returned when `go mod download -json` succeeded (exit 0) but the JSON it wrote to stdout could not be decoded into Hugo's goModule struct. It signals corrupted or unexpected output from the Go toolchain rather than a download failure per se.","triggerScenarios":"json.Decoder failing on `go mod download -json` output: non-JSON noise interleaved on stdout (wrappers, GODEBUG output), a truncated buffer, or a Go version emitting an incompatible format.","commonSituations":"Shell wrappers or scripts around the `go` binary that print to stdout, GOFLAGS injecting flags that change output, or very new/old Go releases changing the -json schema.","solutions":["Run `go mod download -json <path>@<version>` manually and inspect the raw output.","Remove wrappers/aliases around the go binary that print extra output.","Unset GOFLAGS/GODEBUG values that alter go command output.","Upgrade Hugo and Go to compatible recent versions."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isDecodeErr(err error) bool {\n    var syntaxErr *json.SyntaxError\n    var typeErr *json.UnmarshalTypeError\n    return errors.As(err, &syntaxErr) || errors.As(err, &typeErr)\n}","tryCatchPattern":"if err := client.Download(mods); err != nil {\n    if isDecodeErr(err) {\n        // go tool output was malformed — likely go version mismatch or GOFLAGS polluting stdout\n        return fmt.Errorf(\"unexpected 'go mod download -json' output; check Go version compatibility: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep the Go toolchain within the version range Hugo supports — decode failures usually mean the -json output shape changed","Ensure GOFLAGS/GODEBUG don't inject non-JSON text into the go command's stdout","Report the raw output alongside the error when filing issues instead of retrying"],"tags":["go-modules","hugo-mod","json-decode","toolchain"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}