|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "os/exec" |
| 16 | + "path/filepath" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/bradfitz/gomemcache/memcache" |
| 20 | +) |
| 21 | + |
| 22 | +// handleVet performs a vet check on source code, trying cache for results first. |
| 23 | +// It serves an empty response if no errors were found by the "vet" tool. |
| 24 | +func (s *server) handleVet(w http.ResponseWriter, r *http.Request) { |
| 25 | + // TODO(ysmolsky): refactor common code in this function and handleCompile. |
| 26 | + // See golang.org/issue/24535. |
| 27 | + var req request |
| 28 | + // Until programs that depend on golang.org/x/tools/godoc/static/playground.js |
| 29 | + // are updated to always send JSON, this check is in place. |
| 30 | + if b := r.FormValue("body"); b != "" { |
| 31 | + req.Body = b |
| 32 | + } else if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 33 | + s.log.Errorf("error decoding request: %v", err) |
| 34 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + resp := &response{} |
| 39 | + key := cacheKey("vet", req.Body) |
| 40 | + if err := s.cache.Get(key, resp); err != nil { |
| 41 | + if err != memcache.ErrCacheMiss { |
| 42 | + s.log.Errorf("s.cache.Get(%q, &response): %v", key, err) |
| 43 | + } |
| 44 | + resp, err = s.vetCheck(&req) |
| 45 | + if err != nil { |
| 46 | + s.log.Errorf("error checking vet: %v", err) |
| 47 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 48 | + return |
| 49 | + } |
| 50 | + if err := s.cache.Set(key, resp); err != nil { |
| 51 | + s.log.Errorf("cache.Set(%q, resp): %v", key, err) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + var buf bytes.Buffer |
| 56 | + if err := json.NewEncoder(&buf).Encode(resp); err != nil { |
| 57 | + s.log.Errorf("error encoding response: %v", err) |
| 58 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 59 | + return |
| 60 | + } |
| 61 | + if _, err := io.Copy(w, &buf); err != nil { |
| 62 | + s.log.Errorf("io.Copy(w, &buf): %v", err) |
| 63 | + return |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// vetCheck runs the "vet" tool on the source code in req.Body. |
| 68 | +// In case of no errors it returns an empty, non-nil *response. |
| 69 | +// Otherwise &response.Errors contains found errors. |
| 70 | +func (s *server) vetCheck(req *request) (*response, error) { |
| 71 | + tmpDir, err := ioutil.TempDir("", "vet") |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("error creating temp directory: %v", err) |
| 74 | + } |
| 75 | + defer os.RemoveAll(tmpDir) |
| 76 | + |
| 77 | + in := filepath.Join(tmpDir, "main.go") |
| 78 | + if err := ioutil.WriteFile(in, []byte(req.Body), 0400); err != nil { |
| 79 | + return nil, fmt.Errorf("error creating temp file %q: %v", in, err) |
| 80 | + } |
| 81 | + |
| 82 | + cmd := exec.Command("go", "vet", in) |
| 83 | + out, err := cmd.CombinedOutput() |
| 84 | + if err == nil { |
| 85 | + return &response{}, nil |
| 86 | + } |
| 87 | + |
| 88 | + if _, ok := err.(*exec.ExitError); !ok { |
| 89 | + return nil, fmt.Errorf("error vetting go source: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Rewrite compiler errors to refer to progName |
| 93 | + // instead of '/tmp/sandbox1234/main.go'. |
| 94 | + errs := strings.Replace(string(out), in, progName, -1) |
| 95 | + |
| 96 | + // "go vet", invoked with a file name, puts this odd |
| 97 | + // message before any compile errors; strip it. |
| 98 | + errs = strings.Replace(errs, "# command-line-arguments\n", "", 1) |
| 99 | + |
| 100 | + return &response{Errors: errs}, nil |
| 101 | +} |
0 commit comments