-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgodoc.go
More file actions
454 lines (371 loc) · 11.2 KB
/
godoc.go
File metadata and controls
454 lines (371 loc) · 11.2 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package godoc
import (
"context"
"fmt"
"go/ast"
"go/doc"
"go/token"
"go/types"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/packages"
)
// Godoc handles the extraction of Go package documentation.
type Godoc struct {
goos string
goarch string
workdir string
ctx context.Context
loadPkg func(string, string, bool) (*doc.Package, *token.FileSet, *types.Info, *packageAST, string, *packages.Module, string, error)
checkDep func(string, string) (string, func(), error)
depCache *sync.Map
}
// New creates a new [Godoc] with the specified configuration.
func New(opts ...Option) Godoc {
g := Godoc{
workdir: ".", // Default
ctx: context.Background(),
depCache: &sync.Map{},
}
g.SetOptions(opts...)
if g.ctx == nil {
g.ctx = context.Background()
}
g.loadPkg = g.loadDocPkg
g.checkDep = g.checkModuleDep
return g
}
// context returns the effective context for operations.
func (d *Godoc) context() context.Context {
if d == nil || d.ctx == nil {
return context.Background()
}
return d.ctx
}
// Load loads documentation for a Go package or a specific selector within it.
//
// If sel is empty, it loads the entire package documentation.
// Otherwise, it loads documentation for the specified selector (type, method,
// function, const, or var).
//
// For remote packages, it may add them to the current module to fetch the
// documentation.
//
// Version specifies the module version to use; if empty, uses the latest.
func (d *Godoc) Load(importPath, sel, version string) (Result, error) {
if err := validateInputs(importPath, sel); err != nil {
return nil, err
}
if sel == "" {
pkgDoc, _, err := d.getOrLoadPkg(importPath, version)
if err != nil {
return nil, err
}
return pkgDoc, nil
}
symDoc, pkgPath, err := d.getOrLoadSymbol(importPath, sel, version)
if err != nil {
return nil, err
}
if symDoc.ImportPath == "" {
// Defensive: ensure import path metadata is populated for results
// originating from cache.
symDoc.ImportPath = pkgPath
}
return symDoc, nil
}
// getOrLoadPkg gets package doc from cache (or loads it if not cached).
func (d *Godoc) getOrLoadPkg(importPath, version string) (PackageDoc, string, error) {
cache, err := getCache()
if err != nil {
return PackageDoc{}, "", err
}
expected := getPkgVersion(importPath, version)
key := getCacheKey(importPath, expected, "")
if entry, ok := getValidCacheEntry(cache, key); ok {
if entry.Package != nil {
if isRemoteImportPath(importPath) {
return *entry.Package, entry.Package.ImportPath, nil
}
if entry.GoVersion == runtime.Version() || (entry.GoVersion == "" && expected == runtime.Version()) {
if entry.GoVersion == "" {
entry.GoVersion = runtime.Version()
cache.Set(key, entry)
}
return *entry.Package, entry.Package.ImportPath, nil
}
}
// stale entry or missing package payload; fall through to rebuild
}
pkgDoc, _, pkgPath, actualVersion, meta, err := d.buildDoc(importPath, version, false)
if err != nil {
return PackageDoc{}, "", err
}
entry := cacheEntry{
Package: &pkgDoc,
cacheMetadata: meta,
}
keys := uniqKeys(key, getCacheKey(importPath, "", ""))
if actualVersion != "" {
keys = append(keys, getCacheKey(importPath, actualVersion, ""))
}
if err := setCacheEntry(cache, entry, keys...); err != nil {
return PackageDoc{}, "", err
}
return pkgDoc, pkgPath, nil
}
// getOrLoadSymbol gets symbol doc from cache (or loads it if not cached).
func (d *Godoc) getOrLoadSymbol(importPath, sel, version string) (SymbolDoc, string, error) {
cache, err := getCache()
if err != nil {
return SymbolDoc{}, "", err
}
expected := getPkgVersion(importPath, version)
key := getCacheKey(importPath, expected, sel)
if entry, ok := getValidCacheEntry(cache, key); ok {
if entry.Symbol != nil {
if isRemoteImportPath(importPath) {
return *entry.Symbol, entry.Symbol.ImportPath, nil
}
if entry.GoVersion == runtime.Version() || (entry.GoVersion == "" && expected == runtime.Version()) {
if entry.GoVersion == "" {
entry.GoVersion = runtime.Version()
cache.Set(key, entry)
}
return *entry.Symbol, entry.Symbol.ImportPath, nil
}
}
// stale entry or missing symbol payload; fall through to rebuild
}
_, symbols, pkgPath, actualVersion, meta, err := d.buildDoc(importPath, version, true)
if err != nil {
return SymbolDoc{}, "", err
}
symDoc, ok := symbols[sel]
if !ok {
return SymbolDoc{}, pkgPath, fmt.Errorf("selector %q not found in %q", sel, pkgPath)
}
entry := cacheEntry{
Symbol: &symDoc,
cacheMetadata: meta,
}
keys := uniqKeys(key, getCacheKey(importPath, "", sel))
if actualVersion != "" {
keys = append(keys, getCacheKey(importPath, actualVersion, sel))
}
if err := setCacheEntry(cache, entry, keys...); err != nil {
return SymbolDoc{}, "", err
}
return symDoc, pkgPath, nil
}
// buildDoc loads and builds documentation for the specified import path and
// version.
func (d *Godoc) buildDoc(importPath, version string, needSymbols bool) (PackageDoc, map[string]SymbolDoc, string, string, cacheMetadata, error) {
version = strings.TrimSpace(version)
if d.loadPkg == nil {
d.loadPkg = d.loadDocPkg
}
if d.checkDep == nil {
d.checkDep = d.checkModuleDep
}
var symbols, symbols2 map[string]SymbolDoc
needTypes := needSymbols
dpkg, fset, typesInfo, astInfo, pkgPath, module, _, err := d.loadPkg(importPath, "", needTypes)
if err == nil {
if !needTypes && pkgRequiresTypesInfo(dpkg) {
dpkgTyped, fsetTyped, typesInfoTyped, astInfoTyped, pkgPathTyped, moduleTyped, _, typedErr := d.loadPkg(importPath, "", true)
if typedErr == nil {
dpkg = dpkgTyped
fset = fsetTyped
typesInfo = typesInfoTyped
pkgPath = pkgPathTyped
module = moduleTyped
astInfo = astInfoTyped
} else {
// Preserve original package data if type-enriched load fails.
_ = typedErr
}
}
pkgDoc := toPkgDoc(dpkg, fset, typesInfo, astInfo, pkgPath)
if needSymbols {
symbols = buildSymbolIndex(dpkg, fset, typesInfo, astInfo, pkgPath)
}
meta := deriveCacheMetadata(module, version)
if isRemoteImportPath(importPath) {
if meta.ModuleVersion == "" && version != "" {
meta.ModuleVersion = version
}
if meta.ModuleVersion != "" {
meta.GoVersion = ""
}
}
return pkgDoc, symbols, pkgPath, version, meta, nil
}
modDir, cleanup, err2 := d.checkDep(importPath, version)
if err2 != nil {
return PackageDoc{}, nil, "", "", cacheMetadata{}, fmt.Errorf("local load failed (%w) and module dependency setup failed (%w)", err, err2)
}
if cleanup != nil && modDir != d.workdir {
defer cleanup()
}
dpkg2, fset2, typesInfo2, astInfo2, pkgPath2, module2, _, err3 := d.loadPkg(importPath, modDir, true)
if err3 != nil {
return PackageDoc{}, nil, "", "", cacheMetadata{}, fmt.Errorf("load with module dependency failed: %w", err3)
}
pkgDoc := toPkgDoc(dpkg2, fset2, typesInfo2, astInfo2, pkgPath2)
if needSymbols {
symbols2 = buildSymbolIndex(dpkg2, fset2, typesInfo2, astInfo2, pkgPath2)
}
actualVersion := getVersionFromMod(modDir, importPath)
if actualVersion == "" {
actualVersion = version
}
meta := deriveCacheMetadata(module2, actualVersion)
if isRemoteImportPath(importPath) {
if meta.ModuleVersion == "" && actualVersion != "" {
meta.ModuleVersion = actualVersion
}
if meta.ModuleVersion != "" {
meta.GoVersion = ""
}
}
return pkgDoc, symbols2, pkgPath2, actualVersion, meta, nil
}
// loadDocPkg loads documentation for a Go package.
func (d *Godoc) loadDocPkg(importPath, dir string, needTypes bool) (*doc.Package, *token.FileSet, *types.Info, *packageAST, string, *packages.Module, string, error) {
ctx := d.context()
cfg := &packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
packages.NeedSyntax |
packages.NeedCompiledGoFiles |
packages.NeedModule,
Env: append(os.Environ(), "GOWORK=off"),
Dir: dir, // empty = current working directory/module
Context: ctx,
}
if needTypes {
cfg.Mode |= packages.NeedTypes | packages.NeedTypesInfo
}
// load from GOROOT
if dir == "." && importPath != "." && !strings.Contains(importPath, "/") {
cfg.Dir = ""
}
if d.goos != "" {
cfg.Env = append(cfg.Env, "GOOS="+d.goos)
}
if d.goarch != "" {
cfg.Env = append(cfg.Env, "GOARCH="+d.goarch)
}
pkgs, err := packages.Load(cfg, importPath)
if err != nil {
return nil, nil, nil, nil, "", nil, "", err
}
var hasErrors bool
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
hasErrors = true
break
}
}
if hasErrors {
return nil, nil, nil, nil, "", nil, "", fmt.Errorf("build/load errors for %q", importPath)
}
var p *packages.Package
for _, cand := range pkgs {
if len(cand.Syntax) > 0 {
p = cand
break
}
}
if p == nil {
return nil, nil, nil, nil, "", nil, "", fmt.Errorf("no syntax found for %q", importPath)
}
var files []*ast.File
for i, f := range p.Syntax {
fn := p.GoFiles[i]
if strings.HasSuffix(fn, "_test.go") {
continue
}
files = append(files, f)
}
dpkg, err := doc.NewFromFiles(p.Fset, files, p.PkgPath)
if err != nil {
return nil, nil, nil, nil, "", nil, "", err
}
astInfo := buildPkgAST(p, files)
return dpkg, p.Fset, p.TypesInfo, astInfo, p.PkgPath, p.Module, cfg.Dir, nil
}
// checkModuleDep ensures the target import is available for loading.
//
// If the importPath is already in the go.mod of the specified dir, uses that
// dir. Otherwise, creates a temp module and adds the import there.
func (d *Godoc) checkModuleDep(importPath, version string) (string, func(), error) {
modFilePath := filepath.Join(d.workdir, "go.mod")
data, err := os.ReadFile(modFilePath)
if err == nil {
f, err := modfile.Parse(modFilePath, data, nil)
if err == nil {
for _, r := range f.Require {
if r.Mod.Path == importPath {
if version == "" || r.Mod.Version == version {
return d.workdir, nil, nil
}
// Version mismatch, fallback to temp
}
}
}
// go.mod exists but importPath not present/parse error, fallback to temp.
}
targetKey := importPath
if version != "" {
targetKey = importPath + "@" + version
}
if cache := d.depCache; cache != nil {
if cached, ok := cache.Load(targetKey); ok {
if useWorkdir, _ := cached.(bool); useWorkdir {
return d.workdir, nil, nil
}
} else {
target := importPath
if version != "" {
target = targetKey
}
if err := d.runGo(d.workdir, "list", target); err == nil {
cache.Store(targetKey, true)
return d.workdir, nil, nil
}
cache.Store(targetKey, false)
}
} else {
target := importPath
if version != "" {
target = targetKey
}
if err := d.runGo(d.workdir, "list", target); err == nil {
return d.workdir, nil, nil
}
}
tempDir, err := os.MkdirTemp("", "godoc-*")
if err != nil {
return "", nil, err
}
cleanup := func() { _ = os.RemoveAll(tempDir) }
if err := d.runGo(tempDir, "mod", "init", filepath.Base(tempDir)); err != nil {
cleanup()
return "", nil, fmt.Errorf("go mod init failed: %w", err)
}
target := importPath
if version != "" {
target = importPath + "@" + version
}
if err := d.runGo(tempDir, "get", target); err != nil {
cleanup()
return "", nil, fmt.Errorf("go get %q failed: %w", target, err)
}
return tempDir, cleanup, nil
}