Skip to content

Commit 1ab5edf

Browse files
committed
Use depguard linter to forbid dependencies
Introduce the depguard linter to forbid required dependencies in the submodules. This is a clean way to achieve what we had before in the go.mod by using a replacement and pointing the forbidden package to a virtual non-existent directory "FORBIDDEN_DEPENDENCY". Now, if a package requires a forbidden dependency, the linter will fail, rather than having Go fail to compile/fetch dependencies. Signed-off-by: Ivan Valdes <[email protected]>
1 parent 92ac034 commit 1ab5edf

File tree

7 files changed

+62
-52
lines changed

7 files changed

+62
-52
lines changed

api/go.mod

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,3 @@ require (
2626
google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 // indirect
2727
gopkg.in/yaml.v3 v3.0.1 // indirect
2828
)
29-
30-
// Bad imports are sometimes causing attempts to pull that code.
31-
// This makes the error more explicit.
32-
replace (
33-
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
34-
go.etcd.io/etcd/api/v3 => ./FORBIDDEN_DEPENDENCY
35-
go.etcd.io/etcd/pkg/v3 => ./FORBIDDEN_DEPENDENCY
36-
go.etcd.io/etcd/tests/v3 => ./FORBIDDEN_DEPENDENCY
37-
go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY
38-
)

client/v3/go.mod

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,3 @@ replace (
4646
go.etcd.io/etcd/api/v3 => ../../api
4747
go.etcd.io/etcd/client/pkg/v3 => ../pkg
4848
)
49-
50-
// Bad imports are sometimes causing attempts to pull that code.
51-
// This makes the error more explicit.
52-
replace (
53-
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
54-
go.etcd.io/etcd/pkg/v3 => ./FORBIDDEN_DEPENDENCY
55-
go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY
56-
go.etcd.io/tests/v3 => ./FORBIDDEN_DEPENDENCY
57-
)

etcdctl/go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,3 @@ replace (
5454
go.etcd.io/etcd/client/v3 => ../client/v3
5555
go.etcd.io/etcd/pkg/v3 => ../pkg
5656
)
57-
58-
// Bad imports are sometimes causing attempts to pull that code.
59-
// This makes the error more explicit.
60-
replace (
61-
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
62-
go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY
63-
go.etcd.io/tests/v3 => ./FORBIDDEN_DEPENDENCY
64-
)

etcdutl/go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ replace (
1212
go.etcd.io/etcd/server/v3 => ../server
1313
)
1414

15-
// Bad imports are sometimes causing attempts to pull that code.
16-
// This makes the error more explicit.
17-
replace (
18-
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
19-
go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY
20-
go.etcd.io/tests/v3 => ./FORBIDDEN_DEPENDENCY
21-
)
22-
2315
require (
2416
github.com/coreos/go-semver v0.3.1
2517
github.com/dustin/go-humanize v1.0.1

pkg/go.mod

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,3 @@ require (
3232
)
3333

3434
replace go.etcd.io/etcd/client/pkg/v3 => ../client/pkg
35-
36-
// Bad imports are sometimes causing attempts to pull that code.
37-
// This makes the error more explicit.
38-
// Etcd contains lots of packages and dependency relationship.
39-
// Shouldn't import unnecessary dependencies
40-
replace (
41-
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
42-
go.etcd.io/etcd/api/v3 => ./FORBIDDEN_DEPENDENCY
43-
go.etcd.io/etcd/tests/v3 => ./FORBIDDEN_DEPENDENCY
44-
go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY
45-
)

server/go.mod

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,3 @@ replace (
8282
go.etcd.io/etcd/client/v3 => ../client/v3
8383
go.etcd.io/etcd/pkg/v3 => ../pkg
8484
)
85-
86-
// Bad imports are sometimes causing attempts to pull that code.
87-
// This makes the error more explicit.
88-
replace go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
89-
90-
replace go.etcd.io/tests/v3 => ./FORBIDDEN_DEPENDENCY

tools/.golangci.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ version: "2"
33
linters:
44
default: none
55
enable: # please keep this alphabetized
6+
- depguard # Implement forbidden dependencies
67
- errorlint
78
- ineffassign
89
- nakedret
@@ -17,6 +18,67 @@ linters:
1718
- usetesting
1819
- whitespace
1920
settings:
21+
depguard:
22+
rules:
23+
api:
24+
list-mode: lax
25+
files:
26+
# Check files inside the api module/directory, but skip the
27+
# server/etcdserver/api directory. Note that this is configured
28+
# this way as depguard doesn't support relative paths. The path
29+
# that it uses is absolute where it runs, so we need ** at the
30+
# beginning of the path.
31+
# Refer to: https://github.com/OpenPeeDeeP/depguard?tab=readme-ov-file#config
32+
# > Should always prefix a file glob with **/ as files are matched
33+
# > against absolute paths.
34+
# Could be simplified if they resolve issue:
35+
# https://github.com/OpenPeeDeeP/depguard/issues/54
36+
- '**/api/**/*.go'
37+
- '!**/server/etcdserver/api/**/*.go'
38+
deny:
39+
- pkg: go.etcd.io/etcd/api/v3$
40+
- pkg: go.etcd.io/etcd/pkg/v3
41+
- pkg: go.etcd.io/etcd/v3
42+
- pkg: go.etcd.io/tests/v3
43+
client:
44+
list-mode: lax
45+
files:
46+
- "**/client/v3/**/*.go"
47+
deny:
48+
- pkg: go.etcd.io/etcd/pkg/v3
49+
- pkg: go.etcd.io/etcd/v3
50+
- pkg: go.etcd.io/tests/v3
51+
etcdctl:
52+
list-mode: lax
53+
files:
54+
- "**/etcdctl/**/*.go"
55+
deny:
56+
- pkg: go.etcd.io/etcd/v3
57+
- pkg: go.etcd.io/tests/v3
58+
etcdutl:
59+
list-mode: lax
60+
files:
61+
- "**/etcdutl/**/*.go"
62+
deny:
63+
- pkg: go.etcd.io/etcd/v3
64+
- pkg: go.etcd.io/tests/v3
65+
server:
66+
list-mode: lax
67+
files:
68+
- "**/server/**/*.go"
69+
deny:
70+
- pkg: go.etcd.io/etcd/v3
71+
- pkg: go.etcd.io/tests/v3
72+
pkg:
73+
list-mode: lax
74+
files:
75+
- "**/pkg/**/*.go"
76+
- "!**/client/pkg/**/*.go"
77+
- "!**/tools/rw-heatmaps/pkg/**/*.go"
78+
deny:
79+
- pkg: go.etcd.io/etcd/api/v3
80+
- pkg: go.etcd.io/etcd/v3
81+
- pkg: go.etcd.io/tests/v3
2082
nakedret:
2183
# Align with https://github.com/alexkohler/nakedret/blob/v1.0.2/cmd/nakedret/main.go#L10
2284
max-func-lines: 5

0 commit comments

Comments
 (0)