Skip to content

Commit a97b439

Browse files
authored
Merge pull request #47 from AkihiroSuda/gomoddirectivecomments
Support `//gosocialcheck:trusted` directive to silence warnings
2 parents d8acbc3 + 64f8d3b commit a97b439

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ import 'github.com/lmittmann/tint': module 'github.com/lmittmann/tint@v1.0.7' do
3434
```
3535

3636
## Hints
37+
### Allowlist
38+
39+
Use `//gosocialcheck:trusted` [directives](https://github.com/AkihiroSuda/gomoddirectivecomments) in `go.mod` to silence alerts for trustworthy modules.
40+
41+
e.g.,
42+
```go-module
43+
//gosocialcheck:trusted
44+
require (
45+
golang.org/x/sync v0.19.0
46+
)
47+
```
48+
49+
or
50+
51+
```go-module
52+
require (
53+
golang.org/x/sync v0.19.0 //gosocialcheck:trusted
54+
)
55+
```
56+
57+
Note: The directive ignores the module version.
58+
3759
### GitHub API rate limit
3860
gosocialcheck uses the GitHub API for the following operations:
3961
- Fetch git tags, via `api.github.com`.

go.mod

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ module github.com/AkihiroSuda/gosocialcheck
44

55
go 1.24.0
66

7+
// My own packages and golang.org/x packages are trusted
8+
//gosocialcheck:trusted
9+
require (
10+
github.com/AkihiroSuda/gomoddirectivecomments v0.1.0
11+
golang.org/x/mod v0.33.0
12+
golang.org/x/sync v0.19.0 // gomodjail:unconfined
13+
golang.org/x/tools v0.41.0 // gomodjail:unconfined
14+
)
15+
716
require (
817
github.com/lmittmann/tint v1.1.3 // gomodjail:unconfined
918
github.com/spf13/cobra v1.10.2 // gomodjail:unconfined
1019
github.com/spf13/pflag v1.0.10
11-
golang.org/x/mod v0.32.0
12-
golang.org/x/sync v0.19.0 // gomodjail:unconfined
13-
golang.org/x/tools v0.41.0 // gomodjail:unconfined
1420
gopkg.in/yaml.v3 v3.0.1
1521
gotest.tools/v3 v3.5.2
1622
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/AkihiroSuda/gomoddirectivecomments v0.1.0 h1:5sKxYIkq9GGs0DTnuPNVm2Z/LmhKdTN+8QblThzTKqg=
2+
github.com/AkihiroSuda/gomoddirectivecomments v0.1.0/go.mod h1:flXOhVLWfsi4FuFhFoc9F3m7wAH2RT4aM3fVyQROKt4=
13
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
24
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
35
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -12,8 +14,8 @@ github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
1214
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
1315
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1416
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
15-
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
16-
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
17+
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
18+
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
1719
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
1820
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
1921
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=

pkg/analyzer/analyzer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616
"sync"
1717

18+
gomoddirectivecomments "github.com/AkihiroSuda/gomoddirectivecomments"
1819
"golang.org/x/mod/modfile"
1920
"golang.org/x/mod/module"
2021
"golang.org/x/tools/go/analysis"
@@ -28,6 +29,11 @@ type Opts struct {
2829
Cache *cache.Cache
2930
}
3031

32+
const (
33+
directivePolicyUntrusted = "untrusted"
34+
directivePolicyTrusted = "trusted"
35+
)
36+
3137
func New(ctx context.Context, opts Opts) (*analysis.Analyzer, error) {
3238
inst := &instance{
3339
Opts: opts,
@@ -76,6 +82,10 @@ func run(ctx context.Context, inst *instance) func(*analysis.Pass) (any, error)
7682
if goMod.Module.Mod.Path != pass.Module.Path {
7783
return nil, fmt.Errorf("%s: expected %q, got %q", goModFilename, pass.Module.Path, goMod.Module.Mod.Path)
7884
}
85+
policies, err := gomoddirectivecomments.Parse(goMod, "gosocialcheck", directivePolicyUntrusted)
86+
if err != nil {
87+
return nil, fmt.Errorf("failed to parse gosocialcheck directives in %q: %w", goModFilename, err)
88+
}
7989
// TODO: cache go.sum
8090
goSumFilename := filepath.Join(modDir, "go.sum")
8191
// pass.ReadFile does not support go.sum
@@ -102,6 +112,10 @@ func run(ctx context.Context, inst *instance) func(*analysis.Pass) (any, error)
102112
slog.DebugContext(ctx, "module entry not found (negligible for stdlib and local imports)", "path", p)
103113
continue
104114
}
115+
if policies[modV.Path] == directivePolicyTrusted {
116+
slog.DebugContext(ctx, "module marked as trusted via gosocialcheck:trusted directive", "path", modV.Path)
117+
continue
118+
}
105119
h1 := goSum[modV.Path+" "+modV.Version]
106120
inst.processedSumsMu.RLock()
107121
_, h1Processed := inst.processedSums[h1]

0 commit comments

Comments
 (0)