Skip to content

Commit b372af5

Browse files
committed
Go: Allow FindAllFilesWithName to use predicate functions for dirsToSkip
1 parent 2cd9bd8 commit b372af5

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

go/extractor/project/project.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ func RemoveTemporaryExtractorFiles() {
184184

185185
// Find all go.work files in the working directory and its subdirectories
186186
func findGoWorkFiles() []string {
187-
return util.FindAllFilesWithName(".", "go.work", "vendor")
187+
return util.FindAllFilesWithName(".", "go.work", util.SkipVendorChecks...)
188188
}
189189

190190
// Find all go.mod files in the specified directory and its subdirectories
191191
func findGoModFiles(root string) []string {
192-
return util.FindAllFilesWithName(root, "go.mod", "vendor")
192+
return util.FindAllFilesWithName(root, "go.mod", util.SkipVendorChecks...)
193193
}
194194

195195
// A regular expression for the Go toolchain version syntax.
@@ -547,8 +547,8 @@ func startsWithAnyOf(str string, prefixes []string) bool {
547547
// Finds Go workspaces in the current working directory.
548548
func GetWorkspaceInfo(emitDiagnostics bool) []GoWorkspace {
549549
bazelPaths := slices.Concat(
550-
util.FindAllFilesWithName(".", "BUILD", "vendor"),
551-
util.FindAllFilesWithName(".", "BUILD.bazel", "vendor"),
550+
util.FindAllFilesWithName(".", "BUILD", util.SkipVendorChecks...),
551+
util.FindAllFilesWithName(".", "BUILD.bazel", util.SkipVendorChecks...),
552552
)
553553
if len(bazelPaths) > 0 {
554554
// currently not supported

go/extractor/util/util.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,24 @@ func FindGoFiles(root string) bool {
152152
return found
153153
}
154154

155-
func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []string {
155+
// The type of check function used by `FindAllFilesWithName` to decide whether to skip the directory named by `path`.
156+
type FindAllFilesWithNameSkipCheck func(path string) bool
157+
158+
// Commonly we only want to skip `vendor` directories in `FindAllFilesWithName`. This array is a suitable
159+
// argument for `dirsToSkip` which skips `vendor` directories.
160+
var SkipVendorChecks = []FindAllFilesWithNameSkipCheck{IsGolangVendorDirectory}
161+
162+
// Returns an array of all files matching `name` within the path at `root`.
163+
// The `dirsToSkip` array contains check functions used to decide which directories to skip.
164+
func FindAllFilesWithName(root string, name string, dirsToSkip ...FindAllFilesWithNameSkipCheck) []string {
156165
paths := make([]string, 0, 1)
157166
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
158167
if err != nil {
159168
return err
160169
}
161170
if d.IsDir() {
162171
for _, dirToSkip := range dirsToSkip {
163-
if path == dirToSkip {
172+
if dirToSkip(path) {
164173
return filepath.SkipDir
165174
}
166175
}

0 commit comments

Comments
 (0)