Skip to content

Commit fadac40

Browse files
authored
Merge pull request #39 from AkihiroSuda/dev
pkg/analyzer/analyzer.go: split modpath.Guesser
2 parents a4eb20d + 81b8b76 commit fadac40

File tree

3 files changed

+71
-57
lines changed

3 files changed

+71
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import 'github.com/lmittmann/tint': module 'github.com/lmittmann/tint@v1.0.7' do
3737
### GitHub API rate limit
3838
gosocialcheck uses the GitHub API for the following operations:
3939
- Fetch git tags, via `api.github.com`.
40-
- Fetch `go.mod` and `go.sum`, via `http://raw.githubusercontent.com`.
40+
- Fetch `go.mod` and `go.sum`, via `https://raw.githubusercontent.com`.
4141

4242
These API calls often fails unless the API token is set.
4343

pkg/analyzer/analyzer.go

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"path/filepath"
1414
"strconv"
1515
"strings"
16-
"sync"
1716

1817
"golang.org/x/mod/modfile"
1918
"golang.org/x/mod/module"
@@ -30,8 +29,8 @@ type Opts struct {
3029

3130
func New(ctx context.Context, opts Opts) (*analysis.Analyzer, error) {
3231
inst := &instance{
33-
Opts: opts,
34-
modDirs: make(map[string]string),
32+
Opts: opts,
33+
gusser: modpath.NewGuesser(),
3534
}
3635
a := &analysis.Analyzer{
3736
Name: "gosocialcheck",
@@ -46,13 +45,12 @@ func New(ctx context.Context, opts Opts) (*analysis.Analyzer, error) {
4645

4746
type instance struct {
4847
Opts
49-
modDirs map[string]string // key: MODULE@VER
50-
mu sync.RWMutex
48+
gusser *modpath.Guesser
5149
}
5250

5351
func run(ctx context.Context, inst *instance) func(*analysis.Pass) (any, error) {
5452
return func(pass *analysis.Pass) (any, error) {
55-
modDir, err := inst.guessModuleDir(pass)
53+
modDir, err := inst.gusser.GuessModuleDir(pass)
5654
if err != nil {
5755
return nil, err
5856
}
@@ -152,53 +150,3 @@ func parseGoSum(r io.Reader) (map[string]string, error) {
152150
}
153151
return res, sc.Err()
154152
}
155-
156-
// guessModuleDir guess the directory that contains go.mod and go.sum.
157-
// This function might not be robust.
158-
//
159-
// A workaround for https://github.com/golang/go/issues/73878
160-
func (inst *instance) guessModuleDir(pass *analysis.Pass) (string, error) {
161-
if pass.Module == nil {
162-
return "", errors.New("got nil module")
163-
}
164-
mod := pass.Module.Path
165-
modVer := pass.Module.Version
166-
inst.mu.RLock()
167-
k := mod
168-
if modVer != "" {
169-
k += "@" + modVer
170-
}
171-
v := inst.modDirs[k]
172-
inst.mu.RUnlock()
173-
if v != "" {
174-
return v, nil
175-
}
176-
if len(pass.Files) == 0 {
177-
return "", fmt.Errorf("%s: got no files", mod)
178-
}
179-
var sawGoBuildDir bool
180-
for _, f := range pass.Files {
181-
ff := pass.Fset.File(f.Pos())
182-
file := ff.Name()
183-
fileSlash := filepath.ToSlash(file)
184-
if strings.Contains(fileSlash, "/go-build/") {
185-
// tmp file like /Users/suda/Library/Caches/go-build/a0/a0f5d4693b09f2e3e24d18608f43e8540c5c52248877ef966df196f36bed5dfb-d
186-
sawGoBuildDir = true
187-
}
188-
if strings.Contains(fileSlash, modpath.StripMajorVersion(mod)) {
189-
dir, err := modpath.DirFromFileAndMod(file, mod, modVer)
190-
if err != nil {
191-
return "", err
192-
}
193-
slog.Debug("guessed module dir", "mod", mod, "modVer", modVer, "dir", dir)
194-
inst.mu.Lock()
195-
inst.modDirs[k] = dir
196-
inst.mu.Unlock()
197-
return dir, nil
198-
}
199-
}
200-
if sawGoBuildDir {
201-
return "", nil
202-
}
203-
return "", fmt.Errorf("could not guess the directory of module %s", k)
204-
}

pkg/modpath/modpath.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package modpath
22

33
import (
4+
"errors"
45
"fmt"
6+
"log/slog"
57
"path/filepath"
68
"regexp"
79
"strings"
10+
"sync"
11+
12+
"golang.org/x/tools/go/analysis"
813
)
914

1015
func DirFromFileAndMod(filePath, mod, modVer string) (string, error) {
@@ -24,3 +29,64 @@ func DirFromFileAndMod(filePath, mod, modVer string) (string, error) {
2429
func StripMajorVersion(pkg string) string {
2530
return regexp.MustCompile(`(/v[0-9]+)$`).ReplaceAllString(pkg, "")
2631
}
32+
33+
func NewGuesser() *Guesser {
34+
return &Guesser{
35+
modDirs: make(map[string]string),
36+
}
37+
}
38+
39+
type Guesser struct {
40+
modDirs map[string]string // key: MODULE@VER
41+
mu sync.RWMutex
42+
}
43+
44+
// GuessModuleDir guess the directory that contains go.mod and go.sum.
45+
// This function might not be robust.
46+
//
47+
// A workaround for https://github.com/golang/go/issues/73878
48+
func (guesser *Guesser) GuessModuleDir(pass *analysis.Pass) (string, error) {
49+
if pass.Module == nil {
50+
return "", errors.New("got nil module")
51+
}
52+
mod := pass.Module.Path
53+
modVer := pass.Module.Version
54+
guesser.mu.RLock()
55+
k := mod
56+
if modVer != "" {
57+
k += "@" + modVer
58+
}
59+
v := guesser.modDirs[k]
60+
guesser.mu.RUnlock()
61+
if v != "" {
62+
return v, nil
63+
}
64+
if len(pass.Files) == 0 {
65+
return "", fmt.Errorf("%s: got no files", mod)
66+
}
67+
var sawGoBuildDir bool
68+
for _, f := range pass.Files {
69+
ff := pass.Fset.File(f.Pos())
70+
file := ff.Name()
71+
fileSlash := filepath.ToSlash(file)
72+
if strings.Contains(fileSlash, "/go-build/") {
73+
// tmp file like /Users/suda/Library/Caches/go-build/a0/a0f5d4693b09f2e3e24d18608f43e8540c5c52248877ef966df196f36bed5dfb-d
74+
sawGoBuildDir = true
75+
}
76+
if strings.Contains(fileSlash, StripMajorVersion(mod)) {
77+
dir, err := DirFromFileAndMod(file, mod, modVer)
78+
if err != nil {
79+
return "", err
80+
}
81+
slog.Debug("guessed module dir", "mod", mod, "modVer", modVer, "dir", dir)
82+
guesser.mu.Lock()
83+
guesser.modDirs[k] = dir
84+
guesser.mu.Unlock()
85+
return dir, nil
86+
}
87+
}
88+
if sawGoBuildDir {
89+
return "", nil
90+
}
91+
return "", fmt.Errorf("could not guess the directory of module %s", k)
92+
}

0 commit comments

Comments
 (0)