Skip to content

Commit a7cafef

Browse files
committed
fix: hash local module replacement
1 parent e0354ac commit a7cafef

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

pkg/commands/internal/builder.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package internal
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/base64"
57
"fmt"
68
"io"
79
"os"
@@ -12,6 +14,8 @@ import (
1214
"time"
1315
"unicode"
1416

17+
"golang.org/x/mod/sumdb/dirhash"
18+
1519
"github.com/golangci/golangci-lint/v2/pkg/logutils"
1620
)
1721

@@ -173,15 +177,17 @@ func (b Builder) goModTidy(ctx context.Context) error {
173177
}
174178

175179
func (b Builder) goBuild(ctx context.Context, binaryName string) error {
176-
now := time.Now().UTC()
180+
version, err := b.createVersion(b.cfg.Version)
181+
if err != nil {
182+
return fmt.Errorf("custom version: %w", err)
183+
}
184+
185+
b.log.Infof("version: %s", version)
177186

178187
//nolint:gosec // the variable is sanitized.
179188
cmd := exec.CommandContext(ctx, "go", "build",
180189
"-ldflags",
181-
fmt.Sprintf(
182-
"-s -w -X 'main.version=%s' -X 'main.date=%s'",
183-
createVersion(b.cfg.Version, now), now.String(),
184-
),
190+
fmt.Sprintf("-s -w -X 'main.version=%s' -X 'main.date=%s'", version, time.Now().UTC().String()),
185191
"-o", binaryName,
186192
"./cmd/golangci-lint",
187193
)
@@ -243,8 +249,28 @@ func (b Builder) getBinaryName() string {
243249
return name
244250
}
245251

246-
func createVersion(orig string, now time.Time) string {
247-
return fmt.Sprintf("%s-custom-gcl-%d", sanitizeVersion(orig), now.UnixNano())
252+
func (b Builder) createVersion(orig string) (string, error) {
253+
hash := sha256.New()
254+
255+
for _, plugin := range b.cfg.Plugins {
256+
if plugin.Path == "" {
257+
continue
258+
}
259+
260+
dh, err := dirhash.HashDir(plugin.Path, "", dirhash.DefaultHash)
261+
if err != nil {
262+
return "", fmt.Errorf("hash plugin directory: %w", err)
263+
}
264+
265+
b.log.Infof("%s: %s", plugin.Path, dh)
266+
267+
hash.Write([]byte(dh))
268+
}
269+
270+
return fmt.Sprintf("%s-custom-gcl-%s",
271+
sanitizeVersion(orig),
272+
sanitizeVersion(base64.URLEncoding.EncodeToString(hash.Sum(nil))),
273+
), nil
248274
}
249275

250276
func sanitizeVersion(v string) string {

pkg/commands/internal/builder_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package internal
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
97
)
108

119
func Test_sanitizeVersion(t *testing.T) {
@@ -56,12 +54,3 @@ func Test_sanitizeVersion(t *testing.T) {
5654
})
5755
}
5856
}
59-
60-
func Test_createVersion(t *testing.T) {
61-
parse, err := time.Parse(time.RFC3339Nano, "2010-02-04T21:00:57.123456789+08:00")
62-
require.NoError(t, err)
63-
64-
version := createVersion("1.2.3", parse)
65-
66-
assert.Equal(t, "1.2.3-custom-gcl-1265288457123456789", version)
67-
}

0 commit comments

Comments
 (0)