Skip to content

Commit cc16dce

Browse files
committed
fix: Fixing report run duplication error
1 parent 9bc14f7 commit cc16dce

File tree

6 files changed

+199
-131
lines changed

6 files changed

+199
-131
lines changed

internal/discovery/dependencydiscovery.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,21 @@ func (dd *DependencyDiscovery) dependencyToDiscover(
290290
// Record in report as excluded external dependency
291291
if dd.report != nil {
292292
absPath := util.CleanPath(depPath)
293-
run, _ := dd.report.EnsureRun(absPath)
294-
_ = dd.report.EndRun(run.Path, report.WithResult(report.ResultExcluded), report.WithReason(report.ReasonExcludeExternal))
293+
294+
run, err := dd.report.EnsureRun(l, absPath)
295+
if err != nil {
296+
l.Errorf("Error ensuring run for excluded external dependency %s: %v", absPath, err)
297+
}
298+
299+
err = dd.report.EndRun(
300+
l,
301+
run.Path,
302+
report.WithResult(report.ResultExcluded),
303+
report.WithReason(report.ReasonExcludeExternal),
304+
)
305+
if err != nil {
306+
l.Errorf("Error ending run for excluded external dependency %s: %v", absPath, err)
307+
}
295308
}
296309

297310
dd.markSeen(depPath)

internal/report/report.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"slices"
99
"sync"
1010
"time"
11+
12+
"github.com/gruntwork-io/terragrunt/pkg/log"
1113
)
1214

1315
// Report captures data for a report/summary.
@@ -129,7 +131,7 @@ var ErrRunAlreadyExists = errors.New("run already exists")
129131

130132
// AddRun adds a run to the report.
131133
// If the run already exists, it returns the ErrRunAlreadyExists error.
132-
func (r *Report) AddRun(run *Run) error {
134+
func (r *Report) AddRun(l log.Logger, run *Run) error {
133135
r.mu.Lock()
134136
defer r.mu.Unlock()
135137

@@ -139,6 +141,8 @@ func (r *Report) AddRun(run *Run) error {
139141
}
140142
}
141143

144+
l.Debugf("Adding report run %s", run.Path)
145+
142146
r.Runs = append(r.Runs, run)
143147

144148
return nil
@@ -169,22 +173,26 @@ func (r *Report) GetRun(path string) (*Run, error) {
169173
// EnsureRun tries to get a run from the report.
170174
// If the run does not exist, it creates a new run and adds it to the report, then returns the run.
171175
// This is useful when a run is being ended that might not have been started due to exclusion, etc.
172-
func (r *Report) EnsureRun(path string) (*Run, error) {
176+
func (r *Report) EnsureRun(l log.Logger, path string) (*Run, error) {
173177
run, err := r.GetRun(path)
174178
if err == nil {
179+
l.Debugf("Report run %s already exists, returning existing run", path)
180+
175181
return run, nil
176182
}
177183

178184
if !errors.Is(err, ErrRunNotFound) {
179185
return run, err
180186
}
181187

188+
l.Debugf("Report run %s not found, creating new run", path)
189+
182190
run, err = NewRun(path)
183191
if err != nil {
184192
return run, err
185193
}
186194

187-
if err = r.AddRun(run); err != nil {
195+
if err = r.AddRun(l, run); err != nil {
188196
return run, err
189197
}
190198

@@ -195,7 +203,7 @@ func (r *Report) EnsureRun(path string) (*Run, error) {
195203
// If the run does not exist, it returns the ErrRunNotFound error.
196204
// By default, the run is assumed to have succeeded. To change this, pass WithResult to the function.
197205
// If the run has already ended from an early exit, it does nothing.
198-
func (r *Report) EndRun(path string, endOptions ...EndOption) error {
206+
func (r *Report) EndRun(l log.Logger, path string, endOptions ...EndOption) error {
199207
r.mu.Lock()
200208
defer r.mu.Unlock()
201209

@@ -231,6 +239,8 @@ func (r *Report) EndRun(path string, endOptions ...EndOption) error {
231239
endOption(run)
232240
}
233241

242+
l.Debugf("Ending report run %s with result %s", path, run.Result)
243+
234244
return nil
235245
}
236246

0 commit comments

Comments
 (0)