|
5 | 5 | package hooks
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "crypto/rand" |
| 9 | + "encoding/json" |
8 | 10 | "fmt"
|
| 11 | + "io" |
| 12 | + "log" |
| 13 | + "math/big" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + "runtime" |
| 17 | + "strings" |
| 18 | + "sync" |
| 19 | + "time" |
| 20 | + "unicode" |
9 | 21 |
|
10 | 22 | "github.com/sergi/go-diff/diffmatchpatch"
|
11 | 23 | "golang.org/x/tools/internal/lsp/diff"
|
12 | 24 | "golang.org/x/tools/internal/span"
|
13 | 25 | )
|
14 | 26 |
|
| 27 | +// structure for saving information about diffs |
| 28 | +// while the new code is being rolled out |
| 29 | +type diffstat struct { |
| 30 | + Before, After int |
| 31 | + Oldedits, Newedits int |
| 32 | + Oldtime, Newtime time.Duration |
| 33 | + Stack string |
| 34 | + Msg string `json:",omitempty"` // for errors |
| 35 | + Ignored int `json:",omitempty"` // numbr of skipped records with 0 edits |
| 36 | +} |
| 37 | + |
| 38 | +var ( |
| 39 | + mu sync.Mutex // serializes writes and protects ignored |
| 40 | + difffd io.Writer |
| 41 | + ignored int // lots of the diff calls have 0 diffs |
| 42 | +) |
| 43 | + |
| 44 | +var fileonce sync.Once |
| 45 | + |
| 46 | +func (s *diffstat) save() { |
| 47 | + // save log records in a file in os.TempDir(). |
| 48 | + // diff is frequently called with identical strings, so |
| 49 | + // these are somewhat compressed out |
| 50 | + fileonce.Do(func() { |
| 51 | + fname := filepath.Join(os.TempDir(), fmt.Sprintf("gopls-diff-%x", os.Getpid())) |
| 52 | + fd, err := os.Create(fname) |
| 53 | + if err != nil { |
| 54 | + // now what? |
| 55 | + } |
| 56 | + difffd = fd |
| 57 | + }) |
| 58 | + |
| 59 | + mu.Lock() |
| 60 | + defer mu.Unlock() |
| 61 | + if s.Oldedits == 0 && s.Newedits == 0 { |
| 62 | + if ignored < 15 { |
| 63 | + // keep track of repeated instances of no diffs |
| 64 | + // but only print every 15th |
| 65 | + ignored++ |
| 66 | + return |
| 67 | + } |
| 68 | + s.Ignored = ignored + 1 |
| 69 | + } else { |
| 70 | + s.Ignored = ignored |
| 71 | + } |
| 72 | + ignored = 0 |
| 73 | + // it would be really nice to see why diff was called |
| 74 | + _, f, l, ok := runtime.Caller(2) |
| 75 | + if ok { |
| 76 | + var fname string |
| 77 | + fname = filepath.Base(f) // diff is only called from a few places |
| 78 | + s.Stack = fmt.Sprintf("%s:%d", fname, l) |
| 79 | + } |
| 80 | + x, err := json.Marshal(s) |
| 81 | + if err != nil { |
| 82 | + log.Print(err) // failure to print statistics should not stop gopls |
| 83 | + } |
| 84 | + fmt.Fprintf(difffd, "%s\n", x) |
| 85 | +} |
| 86 | + |
| 87 | +// save encrypted versions of the broken input and return the file name |
| 88 | +// (the saved strings will have the same diff behavior as the user's strings) |
| 89 | +func disaster(before, after string) string { |
| 90 | + // encrypt before and after for privacy. (randomized monoalphabetic cipher) |
| 91 | + // got will contain the substitution cipher |
| 92 | + // for the runes in before and after |
| 93 | + got := map[rune]rune{} |
| 94 | + for _, r := range before { |
| 95 | + got[r] = ' ' // value doesn't matter |
| 96 | + } |
| 97 | + for _, r := range after { |
| 98 | + got[r] = ' ' |
| 99 | + } |
| 100 | + repl := initrepl(len(got)) |
| 101 | + i := 0 |
| 102 | + for k := range got { // randomized |
| 103 | + got[k] = repl[i] |
| 104 | + i++ |
| 105 | + } |
| 106 | + // use got to encrypt before and after |
| 107 | + subst := func(r rune) rune { return got[r] } |
| 108 | + first := strings.Map(subst, before) |
| 109 | + second := strings.Map(subst, after) |
| 110 | + |
| 111 | + // one failure per session is enough, and more private. |
| 112 | + // this saves the last one. |
| 113 | + fname := fmt.Sprintf("%s/gopls-failed-%x", os.TempDir(), os.Getpid()) |
| 114 | + fd, err := os.Create(fname) |
| 115 | + defer fd.Close() |
| 116 | + _, err = fd.Write([]byte(fmt.Sprintf("%s\n%s\n", string(first), string(second)))) |
| 117 | + if err != nil { |
| 118 | + // what do we tell the user? |
| 119 | + return "" |
| 120 | + } |
| 121 | + // ask the user to send us the file, somehow |
| 122 | + return fname |
| 123 | +} |
| 124 | + |
| 125 | +func initrepl(n int) []rune { |
| 126 | + repl := make([]rune, 0, n) |
| 127 | + for r := rune(0); len(repl) < n; r++ { |
| 128 | + if unicode.IsLetter(r) || unicode.IsNumber(r) { |
| 129 | + repl = append(repl, r) |
| 130 | + } |
| 131 | + } |
| 132 | + // randomize repl |
| 133 | + rdr := rand.Reader |
| 134 | + lim := big.NewInt(int64(len(repl))) |
| 135 | + for i := 1; i < n; i++ { |
| 136 | + v, _ := rand.Int(rdr, lim) |
| 137 | + k := v.Int64() |
| 138 | + repl[i], repl[k] = repl[k], repl[i] |
| 139 | + } |
| 140 | + return repl |
| 141 | +} |
| 142 | + |
| 143 | +// BothDiffs edits calls both the new and old diffs, checks that the new diffs |
| 144 | +// change before into after, and attempts to preserve some statistics. |
| 145 | +func BothDiffs(uri span.URI, before, after string) (edits []diff.TextEdit, err error) { |
| 146 | + // The new diff code contains a lot of internal checks that panic when they |
| 147 | + // fail. This code catches the panics, or other failures, tries to save |
| 148 | + // the failing example (and ut wiykd ask the user to send it back to us, and |
| 149 | + // changes options.newDiff to 'old', if only we could figure out how.) |
| 150 | + stat := diffstat{Before: len(before), After: len(after)} |
| 151 | + now := time.Now() |
| 152 | + Oldedits, oerr := ComputeEdits(uri, before, after) |
| 153 | + if oerr != nil { |
| 154 | + stat.Msg += fmt.Sprintf("old:%v", oerr) |
| 155 | + } |
| 156 | + stat.Oldedits = len(Oldedits) |
| 157 | + stat.Oldtime = time.Since(now) |
| 158 | + defer func() { |
| 159 | + if r := recover(); r != nil { |
| 160 | + disaster(before, after) |
| 161 | + edits, err = Oldedits, oerr |
| 162 | + } |
| 163 | + }() |
| 164 | + now = time.Now() |
| 165 | + Newedits, rerr := diff.NComputeEdits(uri, before, after) |
| 166 | + stat.Newedits = len(Newedits) |
| 167 | + stat.Newtime = time.Now().Sub(now) |
| 168 | + got := diff.ApplyEdits(before, Newedits) |
| 169 | + if got != after { |
| 170 | + stat.Msg += "FAIL" |
| 171 | + disaster(before, after) |
| 172 | + stat.save() |
| 173 | + return Oldedits, oerr |
| 174 | + } |
| 175 | + stat.save() |
| 176 | + return Newedits, rerr |
| 177 | +} |
| 178 | + |
15 | 179 | func ComputeEdits(uri span.URI, before, after string) (edits []diff.TextEdit, err error) {
|
16 | 180 | // The go-diff library has an unresolved panic (see golang/go#278774).
|
17 | 181 | // TODO(rstambler): Remove the recover once the issue has been fixed
|
|
0 commit comments