{"record":{"id":"46755c565463aa63","repo":"apache/beam","slug":"error-writing-debug-data-to-file-after-err-v-v","errorCode":null,"errorMessage":"error writing debug data to file after err %v:%v","messagePattern":"error writing debug data to file after err (.+?):(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/cmd/starcgen/starcgen.go","lineNumber":81,"sourceCode":"\tintendedPkg = flag.String(\"package\", \"\", \"a filter on input go files. Required if inputs unset.\")\n\toutput      = flag.String(\"output\", \"\", \"output file with types to create\")\n\tids         = flag.String(\"identifiers\", \"\", \"comma separated list of package local identifiers for which to generate code\")\n\tdebug       = flag.Bool(\"debug\", false, \"print out a debugging header in the shim file to help diagnose errors\")\n)\n\n// Generate takes the typechecked inputs, and generates the shim file for the relevant\n// identifiers.\nfunc Generate(w io.Writer, filename, pkg string, ids []string, fset *token.FileSet, files []*ast.File) error {\n\te := starcgenx.NewExtractor(pkg)\n\te.Ids = ids\n\te.Debug = *debug\n\n\t// Importing from source should work in most cases.\n\timp := importer.For(\"source\", nil)\n\tif err := e.FromAsts(imp, fset, files); err != nil {\n\t\t// Always print out the debugging info to the file.\n\t\tif _, errw := w.Write(e.Bytes()); errw != nil {\n\t\t\treturn fmt.Errorf(\"error writing debug data to file after err %v:%v\", err, errw)\n\t\t}\n\t\terr = fmt.Errorf(\"error extracting from asts: %v\", err)\n\t\te.Printf(\"%v\", err)\n\t\treturn err\n\t}\n\tdata := e.Generate(filename)\n\tif err := write(w, []byte(license)); err != nil {\n\t\treturn err\n\t}\n\treturn write(w, data)\n}\n\nfunc write(w io.Writer, data []byte) error {\n\tn, err := w.Write(data)\n\tif err != nil && n < len(data) {\n\t\treturn fmt.Errorf(\"short write of data got %d, want %d\", n, len(data))\n\t}\n\treturn err","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/cmd/starcgen/starcgen.go#L63-L99","documentation":"In starcgen.Generate, after e.FromAsts reports an extraction error, the generator tries to write the accumulated debug output (e.Bytes()) to the output writer so developers can diagnose the failure. If THAT debug write itself fails, this error is returned, combining the original extraction error and the write error. It masks the primary failure path, so both values matter.","triggerScenarios":"FromAsts fails (e.g. a Go source file in --inputs cannot be imported/parsed) AND the io.Writer passed to Generate returns an error when the debug bytes are written (e.g. closed writer, disk full, invalid file).","commonSituations":"Redirecting starcgen output to a full disk or read-only file; piping to a closed pipe (e.g. `starcgen | head`); passing an already-closed os.File as the output writer.","solutions":["Fix the write target first: ensure the output file/pipe is writable and not closed or full.","Then fix the original 'error extracting from asts' reported inside the message.","Check disk space and permissions on the output path before running starcgen.","Capture full stdout/stderr instead of piping into commands that close early."],"exampleFix":"// before\nf, _ := os.Create(outPath)\nf.Close()\ngen.Generate(f, ...) // writes to closed file\n// after\nf, err := os.Create(outPath)\nif err != nil { return err }\ndefer f.Close()\ngen.Generate(f, ...)","handlingStrategy":"validation","validationCode":"if f, ok := w.(*os.File); ok {\n\tif _, err := f.Stat(); err != nil { return fmt.Errorf(\"output file unusable: %w\", err) }\n}","typeGuard":null,"tryCatchPattern":"err := gen.Generate(w, ...)\nif err != nil && strings.Contains(err.Error(), \"error writing debug data\") {\n\tlog.Printf(\"both extraction and debug write failed: %v\", err)\n}","preventionTips":["Always check errors from os.Create and defer Close until after Generate finishes.","Verify free disk space before large code-generation runs.","Avoid piping generator output into commands that close early (e.g. head)."],"tags":["go","code-generation","io","debug"],"backgroundTag":"file-write-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}