mirror of
https://github.com/golang/sys.git
synced 2026-02-08 19:56:04 +03:00
windows/mkwinsyscall: write source to temp file if formatting fails
This change writes the unformatted Go source code to a temp file if "format.Source" fails. Print the temp file path to the console to make it easy to find. The source code is what causes formatting errors, and it can be difficult to diagnose them without this context. Fixes golang/go#57925 Change-Id: Ifa4d8a6e8bc5006357b0bc88afce5ba1d6fe0a48 Reviewed-on: https://go-review.googlesource.com/c/sys/+/463216 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
committed by
Gopher Robot
parent
71da690494
commit
4112509618
@@ -871,6 +871,22 @@ func (src *Source) Generate(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeTempSourceFile(data []byte) (string, error) {
|
||||
f, err := os.CreateTemp("", "mkwinsyscall-generated-*.go")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = f.Write(data)
|
||||
if closeErr := f.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
os.Remove(f.Name()) // best effort
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, "usage: mkwinsyscall [flags] [path ...]\n")
|
||||
flag.PrintDefaults()
|
||||
@@ -897,7 +913,12 @@ func main() {
|
||||
|
||||
data, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Printf("failed to format source: %v", err)
|
||||
f, err := writeTempSourceFile(buf.Bytes())
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write unformatted source to file: %v", err)
|
||||
}
|
||||
log.Fatalf("for diagnosis, wrote unformatted source to %v", f)
|
||||
}
|
||||
if *filename == "" {
|
||||
_, err = os.Stdout.Write(data)
|
||||
|
||||
Reference in New Issue
Block a user