Skip to content

Commit d3b28a5

Browse files
committed
Compile regex and explain why it is used
1 parent b9d85b8 commit d3b28a5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

vendor.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ func findPackagesInSourceCode(root string) map[string]bool {
181181
return packages
182182
}
183183

184+
// Compile the regular expression once
185+
var majorVersionSuffixRegex = regexp.MustCompile(`^/v[1-9][0-9]*(/|$)`)
186+
184187
// findPackagesForModule returns the submodules of a given module that are actually used in the source code
185188
func findPackagesForModule(modulePath string, usedPackages map[string]bool) []string {
186189
var packages []string
@@ -190,9 +193,12 @@ func findPackagesForModule(modulePath string, usedPackages map[string]bool) []st
190193
if strings.HasPrefix(pkg, modulePath) {
191194
// Extract the part after modulePath
192195
suffix := pkg[len(modulePath):]
193-
matched, _ := regexp.MatchString(`^/v[1-9][0-9]*(/|$)`, suffix)
194196

195-
if !matched {
197+
// If `suffix` begins with a major version suffix then we do not have the right module
198+
// path. For example, if the module path is `example.com/mymodule` and the package path
199+
// is `example.com/mymodule/v2/submodule` then we should not consider it a match - it
200+
// is really a match for the module `example.com/mymodule/v2`.
201+
if !majorVersionSuffixRegex.MatchString(suffix) {
196202
packages = append(packages, pkg)
197203
}
198204
}

0 commit comments

Comments
 (0)