Skip to content

Commit e64847c

Browse files
committed
📌 Pin viper module to v1.12.0 in go.mod
I've been trying to get `golangci-lint` to work with `go tool`. The only issue I've run into so far is related to dependency versions. I'm importing the `golangci-lint` module into a module that also imports `viper`. However, while `golangci-lint` imports `v1.12.0`, my module imports `v1.19.0` (the latest version). When I run `go tool golangci-lint` the Go compiler compiles `golangci-lint` with `[email protected]`, which breaks part of the `revive` linter (see issue golangci#3280). It possibly breaks parts of other linters, but my specific configuration did not reveal it. I'm currently working around this by removing the configuration that `golangci-lint` can't handle when using `[email protected]`. However I believe we can get the Go compiler to use `[email protected]` for `golangci-lint` and whatever other version for any module that would import `golangci-lint`. The trick is to add a replacement to `golangci-lint`'s `go.mod`: ```gomod replace github.com/spf13/viper => github.com/spf13/viper v1.12.0 ``` I've experimented with this fix by using Go workspaces and it seems to work as desired. The parent module can still use `[email protected]` and the issue in the `revive` linter does not occur. This change requires disabling the `gomoddirectives` linter in a couple of `golangci-lint`'s tests. Those tests are completely unrelated to that specific linter or to `go.mod`, so I figure it's fine.
1 parent 3f6f904 commit e64847c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

‎go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ require (
135135
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f
136136
)
137137

138+
// When another module imports golangci-lint, they may require a version of
139+
// Viper above 1.12.0. The Go compiler will then use the higher version, which
140+
// will break golangci-lint. This replacement ensures that golangci-lint uses
141+
// the version it requires without impacting modules that import golangci-lint.
142+
// See https://github.com/golangci/golangci-lint/issues/3280 for details on why
143+
// golangci-lint is pinned to Viper 1.12.0.
144+
replace github.com/spf13/viper => github.com/spf13/viper v1.12.0
145+
138146
require (
139147
github.com/Masterminds/semver/v3 v3.3.1 // indirect
140148
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect

‎test/run_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ func TestCgoOk(t *testing.T) {
129129
"--timeout=3m",
130130
"--show-stats=false",
131131
"--default=all",
132+
// We need to disable gomoddirectives because it fails on our own go.mod.
133+
"--disable=gomoddirectives",
132134
).
133135
WithTargetPath(testdataDir, "cgo").
134136
Runner().
@@ -345,6 +347,8 @@ linters:
345347
WithArgs(
346348
"--show-stats=false",
347349
"--default=all",
350+
// We need to disable gomoddirectives because it fails on our own go.mod.
351+
"--disable=gomoddirectives",
348352
).
349353
WithTargetPath(testdataDir, "unsafe").
350354
WithBinPath(binPath).

0 commit comments

Comments
 (0)