Skip to content

Commit

Permalink
Copy loop iterator var for use by goroutine
Browse files Browse the repository at this point in the history
This fixes a bug where only the last dependency would be downloaded.

https:/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Feb 18, 2021
1 parent cd63c20 commit 5f4f91e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/helm/dependency_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/Masterminds/semver/v3"
securejoin "github.com/cyphar/filepath-securejoin"
Expand Down Expand Up @@ -57,6 +58,8 @@ type DependencyManager struct {
// Dependencies contains a list of dependencies, and the respective
// repository the dependency can be found at.
Dependencies []*DependencyWithRepository

mu sync.Mutex
}

// Build compiles and builds the dependencies of the Chart.
Expand All @@ -66,7 +69,8 @@ func (dm *DependencyManager) Build(ctx context.Context) error {
}

errs, ctx := errgroup.WithContext(ctx)
for _, item := range dm.Dependencies {
for _, i := range dm.Dependencies {
item := i
errs.Go(func() error {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -123,7 +127,10 @@ func (dm *DependencyManager) addLocalDependency(dpr *DependencyWithRepository) e
return err
}

dm.mu.Lock()
dm.Chart.AddDependency(ch)
dm.mu.Unlock()

return nil
}

Expand All @@ -147,7 +154,10 @@ func (dm *DependencyManager) addRemoteDependency(dpr *DependencyWithRepository)
return err
}

dm.mu.Lock()
dm.Chart.AddDependency(ch)
dm.mu.Unlock()

return nil
}

Expand Down

0 comments on commit 5f4f91e

Please sign in to comment.