Skip to content

Commit 5f4f91e

Browse files
committed
Copy loop iterator var for use by goroutine
This fixes a bug where only the last dependency would be downloaded. https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent cd63c20 commit 5f4f91e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

internal/helm/dependency_manager.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path/filepath"
2525
"strings"
26+
"sync"
2627

2728
"github.com/Masterminds/semver/v3"
2829
securejoin "github.com/cyphar/filepath-securejoin"
@@ -57,6 +58,8 @@ type DependencyManager struct {
5758
// Dependencies contains a list of dependencies, and the respective
5859
// repository the dependency can be found at.
5960
Dependencies []*DependencyWithRepository
61+
62+
mu sync.Mutex
6063
}
6164

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

6871
errs, ctx := errgroup.WithContext(ctx)
69-
for _, item := range dm.Dependencies {
72+
for _, i := range dm.Dependencies {
73+
item := i
7074
errs.Go(func() error {
7175
select {
7276
case <-ctx.Done():
@@ -123,7 +127,10 @@ func (dm *DependencyManager) addLocalDependency(dpr *DependencyWithRepository) e
123127
return err
124128
}
125129

130+
dm.mu.Lock()
126131
dm.Chart.AddDependency(ch)
132+
dm.mu.Unlock()
133+
127134
return nil
128135
}
129136

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

157+
dm.mu.Lock()
150158
dm.Chart.AddDependency(ch)
159+
dm.mu.Unlock()
160+
151161
return nil
152162
}
153163

0 commit comments

Comments
 (0)