|
4 | 4 | "log" |
5 | 5 | "slices" |
6 | 6 | "strings" |
| 7 | + "sync" |
7 | 8 |
|
8 | 9 | "github.com/bazelbuild/bazel-gazelle/walk" |
9 | 10 | ) |
@@ -43,31 +44,57 @@ func GetSourceRegularFiles(rel string) ([]string, error) { |
43 | 44 | if err != nil { |
44 | 45 | return nil, err |
45 | 46 | } |
| 47 | + if len(d.Subdirs) == 0 { |
| 48 | + return d.RegularFiles, nil |
| 49 | + } |
| 50 | + |
| 51 | + // Use channels to collect results from parallel goroutines. |
| 52 | + // Gazelle may populate the initial directory cache of the initially walked directories |
| 53 | + // but incremental runs will not have walked lazy-indexed subdirectories. |
| 54 | + resultChan := make(chan string, len(d.Subdirs)) |
| 55 | + wg := &sync.WaitGroup{} |
46 | 56 |
|
47 | 57 | if rel != "" { |
48 | 58 | rel = rel + "/" |
49 | 59 | } |
50 | | - return getSourceRegularSubFiles(rel, "", d, d.RegularFiles[:]) |
| 60 | + collectSourceRegularSubFiles(wg, rel, "", d, resultChan) |
| 61 | + |
| 62 | + // Close the channel when all goroutines are done |
| 63 | + go func() { |
| 64 | + wg.Wait() |
| 65 | + close(resultChan) |
| 66 | + }() |
| 67 | + |
| 68 | + // Collect results from all goroutines |
| 69 | + files := d.RegularFiles[:] |
| 70 | + for res := range resultChan { |
| 71 | + files = append(files, res) |
| 72 | + } |
| 73 | + slices.Sort(files) |
| 74 | + return files, nil |
51 | 75 | } |
52 | 76 |
|
53 | | -func getSourceRegularSubFiles(base, rel string, d walk.DirInfo, files []string) ([]string, error) { |
| 77 | +func collectSourceRegularSubFiles(wg *sync.WaitGroup, base, rel string, d walk.DirInfo, resultChan chan<- string) { |
54 | 78 | for _, sdRel := range d.Subdirs { |
55 | | - if rel != "" { |
56 | | - sdRel = rel + "/" + sdRel |
57 | | - } |
| 79 | + wg.Add(1) |
| 80 | + go func(sdRel string) { |
| 81 | + defer wg.Done() |
58 | 82 |
|
59 | | - sdInfo, _ := walk.GetDirInfo(base + sdRel) |
60 | | - |
61 | | - // Recurse into subdirectories that do not have a BUILD file just like a |
62 | | - // bazel BUILD glob() would. |
63 | | - if sdInfo.File == nil { |
64 | | - for _, f := range sdInfo.RegularFiles { |
65 | | - files = append(files, sdRel+"/"+f) |
| 83 | + if rel != "" { |
| 84 | + sdRel = rel + "/" + sdRel |
66 | 85 | } |
67 | 86 |
|
68 | | - files, _ = getSourceRegularSubFiles(base, sdRel, sdInfo, files) |
69 | | - } |
70 | | - } |
| 87 | + sdInfo, _ := walk.GetDirInfo(base + sdRel) |
71 | 88 |
|
72 | | - return files, nil |
| 89 | + // Recurse into subdirectories that do not have a BUILD file just like a |
| 90 | + // bazel BUILD glob() would. |
| 91 | + if sdInfo.File == nil { |
| 92 | + for _, f := range sdInfo.RegularFiles { |
| 93 | + resultChan <- sdRel + "/" + f |
| 94 | + } |
| 95 | + |
| 96 | + collectSourceRegularSubFiles(wg, base, sdRel, sdInfo, resultChan) |
| 97 | + } |
| 98 | + }(sdRel) |
| 99 | + } |
73 | 100 | } |
0 commit comments