-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
context.go
64 lines (51 loc) · 1.63 KB
/
context.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
package lint
import (
"context"
"fmt"
"github.com/golangci/golangci-lint/internal/pkgcache"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/goanalysis/load"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type ContextBuilder struct {
cfg *config.Config
pkgLoader *PackageLoader
fileCache *fsutils.FileCache
pkgCache *pkgcache.Cache
loadGuard *load.Guard
}
func NewContextBuilder(cfg *config.Config, pkgLoader *PackageLoader,
fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard,
) *ContextBuilder {
return &ContextBuilder{
cfg: cfg,
pkgLoader: pkgLoader,
fileCache: fileCache,
pkgCache: pkgCache,
loadGuard: loadGuard,
}
}
func (cl *ContextBuilder) Build(ctx context.Context, log logutils.Log, linters []*linter.Config) (*linter.Context, error) {
pkgs, deduplicatedPkgs, err := cl.pkgLoader.Load(ctx, linters)
if err != nil {
return nil, fmt.Errorf("failed to load packages: %w", err)
}
if len(deduplicatedPkgs) == 0 {
return nil, fmt.Errorf("%w: running `go mod tidy` may solve the problem", exitcodes.ErrNoGoFiles)
}
ret := &linter.Context{
Packages: deduplicatedPkgs,
// At least `unused` linters works properly only on original (not deduplicated) packages,
// see https://github.com/golangci/golangci-lint/pull/585.
OriginalPackages: pkgs,
Cfg: cl.cfg,
Log: log,
FileCache: cl.fileCache,
PkgCache: cl.pkgCache,
LoadGuard: cl.loadGuard,
}
return ret, nil
}