/
golang.go
387 lines (361 loc) · 11.6 KB
/
golang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package lsp
import (
"bytes"
"compress/gzip"
"fmt"
goast "go/ast"
goparser "go/parser"
"go/token"
"io"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/kralicky/tools-lite/gopls/pkg/protocol"
"github.com/kralicky/tools-lite/pkg/diff"
"github.com/kralicky/tools-lite/pkg/gocommand"
"github.com/kralicky/tools-lite/pkg/imports"
"golang.org/x/mod/module"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
type GoLanguageDriver struct {
processEnv *imports.ProcessEnv
moduleResolver *imports.ModuleResolver
knownAlternativePackages [][]diff.Edit
localModDir, localModName string
}
var requiredGoEnvVars = []string{"GO111MODULE", "GOFLAGS", "GOINSECURE", "GOMOD", "GOMODCACHE", "GONOPROXY", "GONOSUMDB", "GOPATH", "GOPROXY", "GOROOT", "GOSUMDB", "GOWORK"}
func NewGoLanguageDriver(workdir string) *GoLanguageDriver {
env := map[string]string{}
for _, key := range requiredGoEnvVars {
if v, ok := os.LookupEnv(key); ok {
env[key] = v
}
}
procEnv := &imports.ProcessEnv{
GocmdRunner: &gocommand.Runner{},
Env: env,
ModFlag: "readonly",
WorkingDir: workdir,
}
res, err := procEnv.GetResolver()
if err != nil || res == nil {
slog.Warn("failed to create module resolver", "error", err)
return nil
}
resolver := res.(*imports.ModuleResolver)
modDir, modName := resolver.ModInfo(workdir)
return &GoLanguageDriver{
processEnv: procEnv,
moduleResolver: resolver,
localModDir: modDir,
localModName: modName,
}
}
func (s *GoLanguageDriver) RefreshModules() {
s.moduleResolver.ClearForNewScan()
}
func (s *GoLanguageDriver) HasGoModule() bool {
return s.localModDir != "" && s.localModName != ""
}
type ParsedGoFile struct {
*goast.File
Fset *token.FileSet
Filename string
}
func (f ParsedGoFile) Position(pos token.Pos) protocol.Position {
position := f.Fset.Position(pos)
return protocol.Position{
Line: uint32(position.Line) - 1,
Character: uint32(position.Column) - 1,
}
}
func (s *GoLanguageDriver) FindGeneratedFiles(uri protocol.DocumentURI, fd protoreflect.FileDescriptor) ([]ParsedGoFile, error) {
pkgPath := fd.Options().(*descriptorpb.FileOptions).GetGoPackage()
if pkgPath == "" {
var err error
pkgPath, err = s.ImplicitGoPackagePath(uri.Path())
if err != nil {
return nil, err
}
}
var pkgNameAlias string
if strings.Contains(pkgPath, ";") {
// path/to/package;alias
pkgPath, pkgNameAlias, _ = strings.Cut(pkgPath, ";")
} else if !strings.Contains(pkgPath, "/") && !strings.Contains(pkgPath, ".") {
// alias only
implicitPath, err := s.ImplicitGoPackagePath(uri.Path())
if err != nil {
return nil, err
}
pkgPath, pkgNameAlias = implicitPath, pkgPath
}
mod, dir := s.moduleResolver.FindPackage(pkgPath)
if mod == nil {
return nil, fmt.Errorf("no package found for %s", pkgPath)
}
fset := token.NewFileSet()
pkgs, _ := goparser.ParseDir(fset, dir, func(fi fs.FileInfo) bool {
if strings.HasSuffix(fi.Name(), "_test.go") {
return false
}
return strings.HasSuffix(fi.Name(), ".pb.go")
}, goparser.ParseComments)
var res []ParsedGoFile
for _, pkg := range pkgs {
if pkgNameAlias != "" && pkg.Name != pkgNameAlias {
continue
}
for filename, f := range pkg.Files {
preamble, ok := ParseGeneratedPreamble(f)
if !ok || preamble.Source != fd.Path() {
continue
}
res = append(res, ParsedGoFile{
File: f,
Fset: fset,
Filename: filename,
})
}
}
return res, nil
}
type GoModuleImportResults struct {
Module *gocommand.ModuleJSON
DirInModule string
SourceExists bool
SourcePath string
KnownAltPath string
}
func (s *GoLanguageDriver) ImportFromGoModule(importName string) (GoModuleImportResults, error) {
last := strings.LastIndex(importName, "/")
if last == -1 {
return GoModuleImportResults{}, fmt.Errorf("%w: %s", os.ErrNotExist, "not a go import")
}
filename := importName[last+1:]
if !strings.HasSuffix(filename, ".proto") {
return GoModuleImportResults{}, fmt.Errorf("%w: %s", os.ErrNotExist, "not a .proto file")
}
// check if the path (excluding the filename) is a well-formed go module
importPath := importName[:last]
if err := module.CheckImportPath(importPath); err != nil {
return GoModuleImportResults{}, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
var knownAltPath string
pkgData, dir := s.moduleResolver.FindPackage(importPath)
if pkgData == nil || dir == "" {
for _, edits := range s.knownAlternativePackages {
edited, err := diff.Apply(importPath, edits)
if err == nil {
pkgData, dir = s.moduleResolver.FindPackage(edited)
if pkgData != nil && dir != "" {
knownAltPath = path.Join(edited, filename)
goto edit_success
}
}
}
return GoModuleImportResults{}, fmt.Errorf("%w: %s", os.ErrNotExist, "no packages found")
}
edit_success:
// We now have a valid go package. First check if there's a .proto file in the package.
// If there is, we're done.
if _, err := os.Stat(filepath.Join(dir, filename)); err == nil {
// thank god
return GoModuleImportResults{
Module: pkgData,
DirInModule: dir,
SourceExists: true,
SourcePath: filepath.Join(dir, filename),
}, nil
}
res := GoModuleImportResults{
Module: pkgData,
DirInModule: dir,
SourceExists: false,
// even if there is no source, this may have been edited from a known
// alternative pattern, in which case we'll know how to resolve it later.
KnownAltPath: knownAltPath,
}
return res, nil
}
func (s *GoLanguageDriver) ImplicitGoPackagePath(filename string) (string, error) {
// check if there is a known go module at the path
relativePath, err := filepath.Rel(s.localModDir, filename)
if err != nil {
return "", err
}
// it's in the same module, so we can use the module name
return path.Join(s.localModName, path.Dir(relativePath)), nil
}
func (s *GoLanguageDriver) SynthesizeFromGoSource(importName string, res GoModuleImportResults) (desc *descriptorpb.FileDescriptorProto, _err error) {
// buckle up
fset := token.NewFileSet()
packages, err := goparser.ParseDir(fset, res.DirInModule, func(fi fs.FileInfo) bool {
if strings.HasSuffix(fi.Name(), "_test.go") {
return false
}
return strings.HasSuffix(fi.Name(), ".pb.go") && !strings.HasSuffix(fi.Name(), "_grpc.pb.go")
}, goparser.ParseComments)
if err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
if len(packages) != 1 {
return nil, fmt.Errorf("wrong number of packages found: %d", len(packages))
}
var rawDescByteArray *goast.Object
PACKAGES:
for _, pkg := range packages {
// we're looking for the byte array that contains the raw file descriptor
// it's named "file_<filename>_rawDesc" where <filename> is the import path
// used when compiling the generated code, with slashes replaced by underscores.
// e.g. file_example_com_foo_bar_baz_proto_rawDesc => "example.com/foo/bar/baz.proto"
// only one catch: the go package path is not necessarily the same as the import path.
// luckily, there's a comment at the top of the file that tells us what the import path is.
// it looks like "// source: example.com/foo/bar/baz.proto"
for filename, f := range pkg.Files {
// find the source file with the matching basename and the .pb.go extension
if strings.TrimSuffix(path.Base(filename), ".pb.go") != strings.TrimSuffix(path.Base(importName), ".proto") {
// generated from a different proto file
continue
}
preamble, ok := ParseGeneratedPreamble(f)
if !ok || preamble.Source == "" {
continue
}
// found a possible match, check if there's a symbol with the right name
symbolName := fmt.Sprintf("file_%s_rawDesc", strings.ReplaceAll(strings.ReplaceAll(preamble.Source, "/", "_"), ".", "_"))
object := f.Scope.Lookup(symbolName)
if object != nil && object.Kind == goast.Var {
// found it!
rawDescByteArray = object
break PACKAGES
}
}
}
if rawDescByteArray == nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, "could not find file descriptor in package")
}
// we have the raw descriptor byte array, which is just a bunch of hex numbers in a slice
// which we can decode from the ast.
// The ast for the byte array will look like:
// *ast.Object {
// Kind: var
// Name: "file_<filename>_rawDesc"
// Decl: *ast.ValueSpec {
// Values: []ast.Expr (len = 1) {
// 0: *ast.CompositeLit {
// Elts: []ast.Expr (len = {len}) {
// 0: *ast.BasicLit {
// Value: "0x0a"
// }
// 1: *ast.BasicLit {
// Value: "0x2c"
// }
// ...
elements := rawDescByteArray.Decl.(*goast.ValueSpec).Values[0].(*goast.CompositeLit).Elts
buf := bytes.NewBuffer(make([]byte, 0, 4096))
for _, b := range elements {
str := b.(*goast.BasicLit).Value
i, err := strconv.ParseUint(str, 0, 8)
if err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
buf.WriteByte(byte(i))
}
// now we have a byte array containing the raw file descriptor, which we can unmarshal
// into a FileDescriptorProto.
// the buffer may or may not be gzipped, so we need to check that first.
fd, err := DecodeRawFileDescriptor(buf.Bytes())
if err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
if fd.GetName() != importName {
// this package uses an alternate import path. we need to keep track of this
// in case any of its dependencies use a similar path structure.
alternateImportPath := fd.GetName()
resolvedImportPath := importName
edits := diff.Strings(alternateImportPath, resolvedImportPath)
s.knownAlternativePackages = append(s.knownAlternativePackages, edits)
}
return fd, nil
}
func DecodeRawFileDescriptor(data []byte) (*descriptorpb.FileDescriptorProto, error) {
var reader io.Reader = bytes.NewReader(data)
if bytes.HasPrefix(data, []byte{0x1f, 0x8b}) {
var err error
reader, err = gzip.NewReader(reader)
if err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
}
decompressedBytes, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
fd := &descriptorpb.FileDescriptorProto{}
if err := proto.Unmarshal(decompressedBytes, fd); err != nil {
return nil, fmt.Errorf("%w: %s", os.ErrNotExist, err)
}
return fd, nil
}
type PreambleInfo struct {
GeneratedBy string
Versions map[string]string
Source string
}
var (
doNotEditRegex = regexp.MustCompile(`^// Code generated(?:\s*by\s+(.+)\.)?.*DO NOT EDIT\.?$`)
versionEntryRegex = regexp.MustCompile(`^// [\s-]+([^\s]+)\s+([^\s]+)$`)
)
func ParseGeneratedPreamble(f *goast.File) (PreambleInfo, bool) {
var info PreambleInfo
var group *goast.CommentGroup
COMMENTS:
for _, cg := range f.Comments {
for _, comment := range cg.List {
if comment.Pos() > f.Package {
break
}
matches := doNotEditRegex.FindStringSubmatch(comment.Text)
if len(matches) == 0 {
continue
}
if len(matches) > 1 {
info.GeneratedBy = matches[1]
}
group = cg
break COMMENTS
}
}
if group == nil {
return PreambleInfo{}, false
}
if len(group.List) == 1 {
return info, true
}
for i, comment := range group.List[1:] {
switch {
case strings.HasPrefix(comment.Text, "// versions:"):
for j := i + 1; j < len(group.List); j++ {
matches := versionEntryRegex.FindStringSubmatch(group.List[j].Text)
if len(matches) != 3 {
break
}
if info.Versions == nil {
info.Versions = make(map[string]string)
}
info.Versions[matches[1]] = matches[2]
}
case strings.HasPrefix(comment.Text, "// source:"):
info.Source = strings.TrimSpace(strings.TrimPrefix(comment.Text, "// source:"))
}
}
return info, true
}