playground: show consistent formatting of errors for gofmt mode
Fixed playground for the source.Format to append prog.go before the
error. This fixes the error highlighting on the web page.
Fixes golang/go#23201
Fixes golang/go#17187
Change-Id: I72b58190eb1edaf1b79ad61b5a4530b7d4fb73ee
Reviewed-on: https://go-review.googlesource.com/99118
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/fmt.go b/fmt.go
index 571c68a..09f50f2 100644
--- a/fmt.go
+++ b/fmt.go
@@ -6,8 +6,10 @@
import (
"encoding/json"
+ "fmt"
"go/format"
"net/http"
+ "strings"
"golang.org/x/tools/imports"
)
@@ -24,13 +26,17 @@
err error
)
if r.FormValue("imports") != "" {
- out, err = imports.Process("prog.go", in, nil)
+ out, err = imports.Process(progName, in, nil)
} else {
out, err = format.Source(in)
}
var resp fmtResponse
if err != nil {
resp.Error = err.Error()
+ // Prefix the error returned by format.Source.
+ if !strings.HasPrefix(resp.Error, progName) {
+ resp.Error = fmt.Sprintf("%v:%v", progName, resp.Error)
+ }
} else {
resp.Body = string(out)
}
diff --git a/sandbox.go b/sandbox.go
index cf1ea8c..dbc54f1 100644
--- a/sandbox.go
+++ b/sandbox.go
@@ -30,7 +30,12 @@
"github.com/bradfitz/gomemcache/memcache"
)
-const maxRunTime = 2 * time.Second
+const (
+ maxRunTime = 2 * time.Second
+
+ // progName is the program name in compiler errors
+ progName = "prog.go"
+)
type request struct {
Body string
@@ -113,9 +118,9 @@
if _, ok := err.(*exec.ExitError); ok {
// Return compile errors to the user.
- // Rewrite compiler errors to refer to 'prog.go'
+ // Rewrite compiler errors to refer to progName
// instead of '/tmp/sandbox1234/main.go'.
- errs := strings.Replace(string(out), in, "prog.go", -1)
+ errs := strings.Replace(string(out), in, progName, -1)
// "go build", invoked with a file name, puts this odd
// message before any compile errors; strip it.
@@ -285,7 +290,7 @@
package test
func main() {
- println("test")
+ println("test")
}
`, want: "", errors: "package name must be main"},
{prog: `