{"record":{"id":"0c16173199bd67ea","repo":"golang/go","slug":"syntax-error","errorCode":null,"errorMessage":"syntax error","messagePattern":"syntax error","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/imports/read.go","lineNumber":44,"sourceCode":"var bom = []byte{0xef, 0xbb, 0xbf}\n\nfunc newImportReader(b *bufio.Reader) *importReader {\n\t// Remove leading UTF-8 BOM.\n\t// Per https://golang.org/ref/spec#Source_code_representation:\n\t// a compiler may ignore a UTF-8-encoded byte order mark (U+FEFF)\n\t// if it is the first Unicode code point in the source text.\n\tif leadingBytes, err := b.Peek(3); err == nil && bytes.Equal(leadingBytes, bom) {\n\t\tb.Discard(3)\n\t}\n\treturn &importReader{b: b}\n}\n\nfunc isIdent(c byte) bool {\n\treturn 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '_' || c >= utf8.RuneSelf\n}\n\nvar (\n\terrSyntax = errors.New(\"syntax error\")\n\terrNUL    = errors.New(\"unexpected NUL in input\")\n)\n\n// syntaxError records a syntax error, but only if an I/O error has not already been recorded.\nfunc (r *importReader) syntaxError() {\n\tif r.err == nil {\n\t\tr.err = errSyntax\n\t}\n}\n\n// readByte reads the next byte from the input, saves it in buf, and returns it.\n// If an error occurs, readByte records the error in r.err and returns 0.\nfunc (r *importReader) readByte() byte {\n\tc, err := r.b.ReadByte()\n\tif err == nil {\n\t\tr.buf = append(r.buf, c)\n\t\tif c == 0 {\n\t\t\terr = errNUL","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/imports/read.go#L26-L62","documentation":"The import reader encountered malformed Go syntax while scanning a source file for import declarations. The sentinel error errSyntax is set when the reader hits unexpected tokens or structure while parsing comments, build tags, the package clause, or import blocks. This is a lightweight parser-level error from the imports reader, not a full Go compiler syntax error — it fires during the fast import-scanning pass.","triggerScenarios":"The importReader in imports/read.go encounters invalid token sequences while scanning — a malformed identifier in an import path, unexpected keyword in package or import position, unbalanced parentheses in an import block, or invalid characters where syntax tokens are expected. readByte or parsing logic calls syntaxError() which sets r.err = errSyntax.","commonSituations":"A .go file with syntax errors that hasn't been compiled yet; a file being scanned during go list or goimports that has partial or templated content; encoding issues with BOM or invalid UTF-8; a generated file that was incompletely written; a file with embedded code in comments that confuses the scanner.","solutions":["Open the source file in question and fix the syntax error — check for unbalanced parens, missing quotes, or invalid tokens","Run gofmt -w or go vet on the file to identify the exact syntax error location","If the file is generated, regenerate it from the source template","Check for encoding issues — ensure UTF-8 without BOM (the reader skips BOM but other encoding issues may persist)","Temporarily exclude the file from scanning if it's not valid Go (e.g., a template file with a .go extension)"],"exampleFix":"// before: malformed import block causes syntax error\n// package main\n// import (\n//     \"fmt\"\n//     \"os\"\n// // missing closing paren\n\n// after: fix the syntax\n// package main\n// import (\n//     \"fmt\"\n//     \"os\"\n// )","handlingStrategy":"validation","validationCode":"// Validate Go source files before scanning by attempting a parse.\nimport \"go/parser\"\n\nfunc isValidGoSource(path string) error {\n    fset := token.NewFileSet()\n    _, err := parser.ParseFile(fset, path, nil, parser.AllErrors)\n    return err // nil means syntactically valid\n}\n\n// For faster pre-checks without full parsing:\nfunc quickSyntaxCheck(path string) error {\n    data, err := os.ReadFile(path)\n    if err != nil {\n        return err\n    }\n    // Check for balanced parentheses in import blocks\n    // Check for BOM (the import reader skips it, but other tools may not)\n    if len(data) >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF {\n        return fmt.Errorf(\"%s: BOM detected, remove it\", path)\n    }\n    return nil\n}","typeGuard":"// This is a scanning error, returned as the sentinel errSyntax.\n// Detect by message since the sentinel is package-private:\nfunc isImportSyntaxError(err error) bool {\n    return err != nil && (err.Error() == \"syntax error\" || strings.Contains(err.Error(), \"syntax error\"))\n}","tryCatchPattern":"// For tooling that uses the import scanner:\n// err := importReader.Err()\n// if isImportSyntaxError(err) {\n//     // The source file has syntax errors.\n//     // Skip it or report to the user.\n//     log.Printf(\"skipping %s: syntax error\", path)\n//     continue\n// }\n//\n// For end users: this error surfaces during go list, go build, goimports.\n// Fix the source file's syntax to resolve it.","preventionTips":["Run gofmt -w on all Go files before building to catch syntax issues early","Run go vet as part of CI to detect malformed source files","Ensure generated files are fully written before the scanner reaches them","Avoid naming non-Go files with .go extensions (use .go.tmpl for templates)","Check for encoding issues — ensure UTF-8 without BOM in all source files","Use goimports instead of manual import management to avoid syntax errors in import blocks"],"tags":["go","parser","imports","syntax-error","source-code"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}