Skip to content

dev: split ContextLoader #4516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type runCommand struct {
debugf logutils.DebugFunc
reportData *report.Data

contextLoader *lint.ContextLoader
goenv *goutil.Env
contextBuilder *lint.ContextBuilder
goenv *goutil.Env

fileCache *fsutils.FileCache
lineCache *fsutils.LineCache
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *runCommand) persistentPostRunE(_ *cobra.Command, _ []string) error {
return nil
}

func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
if err != nil {
Expand Down Expand Up @@ -211,8 +211,11 @@ func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
return fmt.Errorf("failed to build packages cache: %w", err)
}

c.contextLoader = lint.NewContextLoader(c.cfg, c.log.Child(logutils.DebugKeyLoader), c.goenv,
c.lineCache, c.fileCache, pkgCache, load.NewGuard())
guard := load.NewGuard()

pkgLoader := lint.NewPackageLoader(c.log.Child(logutils.DebugKeyLoader), c.cfg, args, c.goenv, guard)

c.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, c.fileCache, pkgCache, guard)

if err = initHashSalt(c.buildInfo.Version, c.cfg); err != nil {
return fmt.Errorf("failed to init hash salt: %w", err)
Expand Down Expand Up @@ -374,7 +377,7 @@ func (c *runCommand) runAnalysis(ctx context.Context, args []string) ([]result.I
return nil, err
}

lintCtx, err := c.contextLoader.Load(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)
lintCtx, err := c.contextBuilder.Build(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)
if err != nil {
return nil, fmt.Errorf("context loading failed: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (c *Config) Validate() error {
c.LintersSettings.Validate,
c.Linters.Validate,
c.Output.Validate,
c.Run.Validate,
}

for _, v := range validators {
Expand Down
20 changes: 18 additions & 2 deletions pkg/config/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package config

import "time"
import (
"fmt"
"slices"
"strings"
"time"
)

// Run encapsulates the config options for running the linter analysis.
type Run struct {
Expand All @@ -26,6 +31,17 @@ type Run struct {
// Deprecated: use Output.ShowStats instead.
ShowStats bool `mapstructure:"show-stats"`

// It's obtain by flags and use for the tests and the context loader.
// Only used by skipDirs processor. TODO(ldez) remove it in next PR.
Args []string // Internal needs.
}

func (r *Run) Validate() error {
// go help modules
allowedMods := []string{"mod", "readonly", "vendor"}

if r.ModulesDownloadMode != "" && !slices.Contains(allowedMods, r.ModulesDownloadMode) {
return fmt.Errorf("invalid modules download path %s, only (%s) allowed", r.ModulesDownloadMode, strings.Join(allowedMods, "|"))
}

return nil
}
75 changes: 75 additions & 0 deletions pkg/config/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRun_Validate(t *testing.T) {
testCases := []struct {
desc string
settings *Run
}{
{
desc: "modules-download-mode: mod",
settings: &Run{
ModulesDownloadMode: "mod",
},
},
{
desc: "modules-download-mode: readonly",
settings: &Run{
ModulesDownloadMode: "readonly",
},
},
{
desc: "modules-download-mode: vendor",
settings: &Run{
ModulesDownloadMode: "vendor",
},
},
{
desc: "modules-download-mode: empty",
settings: &Run{
ModulesDownloadMode: "",
},
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := test.settings.Validate()
require.NoError(t, err)
})
}
}

func TestRun_Validate_error(t *testing.T) {
testCases := []struct {
desc string
settings *Run
expected string
}{
{
desc: "modules-download-mode: invalid",
settings: &Run{
ModulesDownloadMode: "invalid",
},
expected: "invalid modules download path invalid, only (mod|readonly|vendor) allowed",
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := test.settings.Validate()
require.EqualError(t, err, test.expected)
})
}
}
64 changes: 64 additions & 0 deletions pkg/lint/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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/golinters/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, 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
}
1 change: 0 additions & 1 deletion pkg/lint/linter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Context struct {

Cfg *config.Config
FileCache *fsutils.FileCache
LineCache *fsutils.LineCache
Log logutils.Log

PkgCache *pkgcache.Cache
Expand Down
Loading