From 41e4fedabf548f766c66d98078ca924d0f20d6a4 Mon Sep 17 00:00:00 2001 From: Ethan Davidson <31261035+EthanThatOneKid@users.noreply.github.com> Date: Wed, 21 Jun 2023 11:04:24 -0700 Subject: [PATCH] Fix incorrect usage of `sync.WaitGroup` in `RulesFromFile` (#7) * `RulesFromFile`: fix `sync.WaitGroup` usage --- rules.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rules.go b/rules.go index 155ed52..256fdea 100644 --- a/rules.go +++ b/rules.go @@ -102,21 +102,22 @@ func RulesFromFile(pathname string, ranges []Range, visited map[string]struct{}, return nil, errors.Wrapf(err, "failed to parse rules for file %s", pathname) } + var innerWg sync.WaitGroup // WaitGroup for inner goroutines for _, rule := range rules { for _, target := range rule.Targets { if target.File == nil { continue } - wg.Add(1) + innerWg.Add(1) go func(pathname string) { - defer wg.Done() + defer innerWg.Done() if _, ok := visited[pathname]; ok { return } - moreRules, err := RulesFromFile(pathname, nil, visited, wg, options) + moreRules, err := RulesFromFile(pathname, nil, visited, &innerWg, options) if err != nil { return } @@ -126,5 +127,9 @@ func RulesFromFile(pathname string, ranges []Range, visited map[string]struct{}, } } + // Wait for all inner goroutines to complete before returning. + innerWg.Wait() + + // Add rules to the map. return rules, nil }