Skip to content

fix: add go.mod hash to the cache salt #5739

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 2 commits into from
Apr 16, 2025
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
43 changes: 41 additions & 2 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (

"github.com/fatih/color"
"github.com/gofrs/flock"
"github.com/ldez/grignotin/goenv"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/automaxprocs/maxprocs"
"golang.org/x/mod/sumdb/dirhash"
"gopkg.in/yaml.v3"

"github.com/golangci/golangci-lint/v2/internal/cache"
Expand Down Expand Up @@ -216,7 +218,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {

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

if err = initHashSalt(c.buildInfo.Version, c.cfg); err != nil {
if err = initHashSalt(c.log.Child(logutils.DebugKeyGoModSalt), c.buildInfo.Version, c.cfg); err != nil {
return fmt.Errorf("failed to init hash salt: %w", err)
}

Expand Down Expand Up @@ -618,7 +620,7 @@ func formatMemory(memBytes uint64) string {

// Related to cache.

func initHashSalt(version string, cfg *config.Config) error {
func initHashSalt(logger logutils.Log, version string, cfg *config.Config) error {
binSalt, err := computeBinarySalt(version)
if err != nil {
return fmt.Errorf("failed to calculate binary salt: %w", err)
Expand All @@ -629,9 +631,18 @@ func initHashSalt(version string, cfg *config.Config) error {
return fmt.Errorf("failed to calculate config salt: %w", err)
}

goModSalt, err := computeGoModSalt()
if err != nil {
// NOTE: missing go.mod must be ignored.
logger.Warnf("Failed to calculate go.mod salt: %v", err)
}

b := bytes.NewBuffer(binSalt)
b.Write(configSalt)
b.WriteString(goModSalt)

cache.SetSalt(b)

return nil
}

Expand All @@ -648,15 +659,19 @@ func computeBinarySalt(version string) ([]byte, error) {
if err != nil {
return nil, err
}

f, err := os.Open(p)
if err != nil {
return nil, err
}

defer f.Close()

h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return nil, err
}

return h.Sum(nil), nil
}

Expand All @@ -678,5 +693,29 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
if _, err := h.Write(configData.Bytes()); err != nil {
return nil, err
}

return h.Sum(nil), nil
}

func computeGoModSalt() (string, error) {
values, err := goenv.Get(context.Background(), goenv.GOMOD)
if err != nil {
return "", fmt.Errorf("failed to get goenv: %w", err)
}

goModPath := filepath.Clean(values[goenv.GOMOD])

data, err := os.ReadFile(goModPath)
if err != nil {
return "", fmt.Errorf("failed to read go.mod: %w", err)
}

sum, err := dirhash.Hash1([]string{goModPath}, func(string) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(data)), nil
})
if err != nil {
return "", fmt.Errorf("failed to compute go.sum: %w", err)
}

return sum, nil
}
12 changes: 9 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/ldez/grignotin/goenv"
"github.com/ldez/grignotin/gomod"
"golang.org/x/mod/modfile"

"github.com/golangci/golangci-lint/v2/pkg/logutils"
)

// defaultGoVersion the value should be "oldstable" - 1.
Expand Down Expand Up @@ -102,16 +104,16 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
return v1.GreaterThanOrEqual(l)
}

func detectGoVersion(ctx context.Context) string {
return cmp.Or(detectGoVersionFromGoMod(ctx), defaultGoVersion)
func detectGoVersion(ctx context.Context, log logutils.Log) string {
return cmp.Or(detectGoVersionFromGoMod(ctx, log), defaultGoVersion)
}

// detectGoVersionFromGoMod tries to get Go version from go.mod.
// It returns `toolchain` version if present,
// else it returns `go` version if present,
// else it returns `GOVERSION` version if present,
// else it returns empty.
func detectGoVersionFromGoMod(ctx context.Context) string {
func detectGoVersionFromGoMod(ctx context.Context, log logutils.Log) string {
values, err := goenv.Get(ctx, goenv.GOMOD, goenv.GOVERSION)
if err != nil {
values = map[string]string{
Expand All @@ -128,6 +130,10 @@ func detectGoVersionFromGoMod(ctx context.Context) string {
return parseGoVersion(values[goenv.GOVERSION])
}

if file.Module != nil {
log.Infof("Module name %q", file.Module.Mod.Path)
}

// The toolchain exists only if 'toolchain' version > 'go' version.
// If 'toolchain' version <= 'go' version, `go mod tidy` will remove 'toolchain' version from go.mod.
if file.Toolchain != nil && file.Toolchain.Name != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (l *Loader) checkConfigurationVersion() error {

func (l *Loader) handleGoVersion() {
if l.cfg.Run.Go == "" {
l.cfg.Run.Go = detectGoVersion(context.Background())
l.cfg.Run.Go = detectGoVersion(context.Background(), l.log)
}

l.cfg.Linters.Settings.Govet.Go = l.cfg.Run.Go
Expand Down
1 change: 1 addition & 0 deletions pkg/logutils/logutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const envDebug = "GL_DEBUG"

const (
DebugKeyBinSalt = "bin_salt"
DebugKeyGoModSalt = "gomod_salt"
DebugKeyConfigReader = "config_reader"
DebugKeyEmpty = ""
DebugKeyEnabledLinters = "enabled_linters"
Expand Down
Loading