{"record":{"id":"d1763c27cabfbf7d","repo":"golang/go","slug":"can-t-decode-xml-document-using-charset-q","errorCode":null,"errorMessage":"can't decode XML document using charset %q","messagePattern":"can't decode XML document using charset %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/vcs/discovery.go","lineNumber":25,"sourceCode":"import (\n\t\"encoding/xml\"\n\t\"fmt\"\n\t\"io\"\n\t\"strings\"\n)\n\n// charsetReader returns a reader that converts from the given charset to UTF-8.\n// Currently it only supports UTF-8 and ASCII. Otherwise, it returns a meaningful\n// error which is printed by go get, so the user can find why the package\n// wasn't downloaded if the encoding is not supported. Note that, in\n// order to reduce potential errors, ASCII is treated as UTF-8 (i.e. characters\n// greater than 0x7f are not rejected).\nfunc charsetReader(charset string, input io.Reader) (io.Reader, error) {\n\tswitch strings.ToLower(charset) {\n\tcase \"utf-8\", \"ascii\":\n\t\treturn input, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"can't decode XML document using charset %q\", charset)\n\t}\n}\n\n// parseMetaGoImports returns meta imports from the HTML in r.\n// Parsing ends at the end of the <head> section or the beginning of the <body>.\nfunc parseMetaGoImports(r io.Reader, mod ModuleMode) ([]metaImport, error) {\n\td := xml.NewDecoder(r)\n\td.CharsetReader = charsetReader\n\td.Strict = false\n\tvar imports []metaImport\n\tfor {\n\t\tt, err := d.RawToken()\n\t\tif err != nil {\n\t\t\tif err != io.EOF && len(imports) == 0 {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tbreak\n\t\t}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/vcs/discovery.go#L7-L43","documentation":"During VCS discovery (`go get`), the go tool fetches the vanity import path's HTML page and parses <meta> tags with an XML decoder. The CharsetReader callback only accepts UTF-8 and ASCII; any other declared charset is rejected so the user can see why the package wasn't downloaded.","triggerScenarios":"`go get` of a vanity import path whose hosting HTML page declares a charset other than UTF-8/ASCII in its XML declaration or <meta charset=...> tag (e.g., ISO-8859-1, Windows-1252, Shift_JIS).","commonSituations":"Legacy hosting servers sending Latin-1 encoded pages; misconfigured Content-Type or XML declaration on a vanity import page; a repo host that serves an unusual encoding.","solutions":["Check the repository's discovery page encoding: `curl -sL <vanity-url> | head -5`","If you control the hosting page, ensure it declares charset=UTF-8 or ASCII","Use the full VCS URL directly with `go get` via a replace directive or GOFLAGS workaround","Report the encoding issue to the vanity path maintainer"],"exampleFix":"<!-- before: page declares non-UTF-8 charset -->\n<meta charset=\"ISO-8859-1\">\n<meta name=\"go-import\" content=\"example.com/mymod git https://github.com/me/mymod\">\n\n<!-- after: use UTF-8 -->\n<meta charset=\"UTF-8\">\n<meta name=\"go-import\" content=\"example.com/mymod git https://github.com/me/mymod\">","handlingStrategy":"try-catch","validationCode":"# Pre-check the discovery page encoding before go get\nURL=\"https://$(echo $IMPORT_PATH | cut -d/ -f1)\"\nENCODING=$(curl -sIL \"$URL\" | grep -i content-type | grep -oiP 'charset=\\K[^;\\s]+' || echo 'unknown')\nif [ \"$ENCODING\" != 'utf-8' ] && [ \"$ENCODING\" != 'ascii' ] && [ \"$ENCODING\" != 'us-ascii' ] && [ \"$ENCODING\" != 'unknown' ]; then\n  echo \"WARNING: page uses unsupported charset $ENCODING\"\nfi","typeGuard":null,"tryCatchPattern":"# Catch the charset error and provide guidance\nERR=$(go get $IMPORT_PATH 2>&1)\nif echo \"$ERR\" | grep -q 'can\\'t decode XML document using charset'; then\n  CHARSET=$(echo \"$ERR\" | grep -oP 'charset \\K[^\"]+')\n  echo \"Discovery page uses unsupported charset: $CHARSET\"\n  echo \"Contact the vanity path maintainer or use a direct VCS URL\"\nfi","preventionTips":["Ensure vanity import pages declare charset=UTF-8","Use direct VCS URLs via replace directives if discovery pages are broken","Test discovery pages with curl before scripting go get"],"tags":["encoding","vcs","network","xml","charset","discovery"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}