| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/cache" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
"golang.org/x/tools/gopls/internal/settings" |
| 19 |
"golang.org/x/tools/internal/event" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, pkgDir protocol.DocumentURI) (map[protocol.DocumentURI][]*cache.Diagnostic, error) { |
| 27 |
outDir, err := os.MkdirTemp("", fmt.Sprintf("gopls-%d.details", os.Getpid())) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
defer func() { |
| 32 |
if err := os.RemoveAll(outDir); err != nil { |
| 33 |
event.Error(ctx, "cleaning details dir", err) |
| 34 |
} |
| 35 |
}() |
| 36 |
|
| 37 |
outDirURI := protocol.URIFromPath(outDir) |
| 38 |
|
| 39 |
|
| 40 |
if !strings.HasPrefix(outDir, "/") { |
| 41 |
outDirURI = protocol.DocumentURI(strings.Replace(string(outDirURI), "file:///", "file://", 1)) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkgDir.Path(), "test", []string{ |
| 48 |
"-c", |
| 49 |
"-vet=off", |
| 50 |
fmt.Sprintf("-gcflags=-json=0,%s", outDirURI), |
| 51 |
fmt.Sprintf("-o=%s", os.DevNull), |
| 52 |
".", |
| 53 |
}) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
defer cleanupInvocation() |
| 58 |
_, err = snapshot.View().GoCommandRunner().Run(ctx, *inv) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
files, err := findJSONFiles(outDir) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
reports := make(map[protocol.DocumentURI][]*cache.Diagnostic) |
| 67 |
var parseError error |
| 68 |
for _, fn := range files { |
| 69 |
uri, diagnostics, err := parseDetailsFile(fn, snapshot.Options()) |
| 70 |
if err != nil { |
| 71 |
|
| 72 |
parseError = err |
| 73 |
} |
| 74 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 75 |
if err != nil || fh == nil { |
| 76 |
continue |
| 77 |
} |
| 78 |
if pkgDir != fh.URI().Dir() { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
continue |
| 84 |
} |
| 85 |
reports[fh.URI()] = diagnostics |
| 86 |
} |
| 87 |
return reports, parseError |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func parseDetailsFile(filename string, options *settings.Options) (protocol.DocumentURI, []*cache.Diagnostic, error) { |
| 92 |
buf, err := os.ReadFile(filename) |
| 93 |
if err != nil { |
| 94 |
return "", nil, err |
| 95 |
} |
| 96 |
var ( |
| 97 |
uri protocol.DocumentURI |
| 98 |
i int |
| 99 |
diagnostics []*cache.Diagnostic |
| 100 |
) |
| 101 |
type metadata struct { |
| 102 |
File string `json:"file,omitempty"` |
| 103 |
} |
| 104 |
for dec := json.NewDecoder(bytes.NewReader(buf)); dec.More(); { |
| 105 |
|
| 106 |
if i == 0 { |
| 107 |
i++ |
| 108 |
m := new(metadata) |
| 109 |
if err := dec.Decode(m); err != nil { |
| 110 |
return "", nil, err |
| 111 |
} |
| 112 |
if !strings.HasSuffix(m.File, ".go") { |
| 113 |
continue |
| 114 |
} |
| 115 |
uri = protocol.URIFromPath(m.File) |
| 116 |
continue |
| 117 |
} |
| 118 |
d := new(protocol.Diagnostic) |
| 119 |
if err := dec.Decode(d); err != nil { |
| 120 |
return "", nil, err |
| 121 |
} |
| 122 |
d.Tags = []protocol.DiagnosticTag{} |
| 123 |
msg := d.Code.(string) |
| 124 |
if msg != "" { |
| 125 |
msg = fmt.Sprintf("%s(%s)", msg, d.Message) |
| 126 |
} |
| 127 |
if !showDiagnostic(msg, d.Source, options) { |
| 128 |
continue |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
zeroIndexedRange := func(rng protocol.Range) protocol.Range { |
| 137 |
return protocol.Range{ |
| 138 |
Start: protocol.Position{ |
| 139 |
Line: rng.Start.Line - 1, |
| 140 |
Character: rng.Start.Character - 1, |
| 141 |
}, |
| 142 |
End: protocol.Position{ |
| 143 |
Line: rng.End.Line - 1, |
| 144 |
Character: rng.End.Character - 1, |
| 145 |
}, |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
var related []protocol.DiagnosticRelatedInformation |
| 150 |
for _, ri := range d.RelatedInformation { |
| 151 |
related = append(related, protocol.DiagnosticRelatedInformation{ |
| 152 |
Location: ri.Location.URI.Location(zeroIndexedRange(ri.Location.Range)), |
| 153 |
Message: ri.Message, |
| 154 |
}) |
| 155 |
} |
| 156 |
diagnostic := &cache.Diagnostic{ |
| 157 |
URI: uri, |
| 158 |
Range: zeroIndexedRange(d.Range), |
| 159 |
Message: msg, |
| 160 |
Severity: d.Severity, |
| 161 |
Source: cache.CompilerOptDetailsInfo, |
| 162 |
Tags: d.Tags, |
| 163 |
Related: related, |
| 164 |
} |
| 165 |
diagnostics = append(diagnostics, diagnostic) |
| 166 |
i++ |
| 167 |
} |
| 168 |
return uri, diagnostics, nil |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
func showDiagnostic(msg, source string, o *settings.Options) bool { |
| 174 |
if source != "go compiler" { |
| 175 |
return false |
| 176 |
} |
| 177 |
if o.Annotations == nil { |
| 178 |
return true |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
switch { |
| 201 |
case strings.HasPrefix(msg, "canInline") || |
| 202 |
strings.HasPrefix(msg, "cannotInline") || |
| 203 |
strings.HasPrefix(msg, "inlineCall"): |
| 204 |
return o.Annotations[settings.Inline] |
| 205 |
case strings.HasPrefix(msg, "escape") || msg == "leak": |
| 206 |
return o.Annotations[settings.Escape] |
| 207 |
case strings.HasPrefix(msg, "nilcheck"): |
| 208 |
return o.Annotations[settings.Nil] |
| 209 |
case strings.HasPrefix(msg, "isInBounds") || |
| 210 |
strings.HasPrefix(msg, "isSliceInBounds"): |
| 211 |
return o.Annotations[settings.Bounds] |
| 212 |
} |
| 213 |
return false |
| 214 |
} |
| 215 |
|
| 216 |
func findJSONFiles(dir string) ([]string, error) { |
| 217 |
ans := []string{} |
| 218 |
f := func(path string, fi os.FileInfo, _ error) error { |
| 219 |
if fi.IsDir() { |
| 220 |
return nil |
| 221 |
} |
| 222 |
if strings.HasSuffix(path, ".json") { |
| 223 |
ans = append(ans, path) |
| 224 |
} |
| 225 |
return nil |
| 226 |
} |
| 227 |
err := filepath.Walk(dir, f) |
| 228 |
return ans, err |
| 229 |
} |