Skip to content

Commit 077900d

Browse files
committed
chore: pr comments
1 parent 6bfb74f commit 077900d

File tree

7 files changed

+47
-74
lines changed

7 files changed

+47
-74
lines changed

cli/commands/find/find.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ import (
2121

2222
// Run runs the find command.
2323
func Run(ctx context.Context, l log.Logger, opts *Options) error {
24-
// Ensure WorkingDir is absolute to avoid filepath.Rel errors when comparing with absolute unit paths
25-
if !filepath.IsAbs(opts.WorkingDir) {
26-
absWorkingDir, err := filepath.Abs(opts.WorkingDir)
27-
if err != nil {
28-
return errors.Errorf("failed to get absolute path for working directory %s: %w", opts.WorkingDir, err)
29-
}
30-
31-
opts.WorkingDir = util.CleanPath(absWorkingDir)
32-
// Also update the underlying TerragruntOptions
33-
opts.TerragruntOptions.WorkingDir = opts.WorkingDir
34-
opts.TerragruntOptions.RootWorkingDir = opts.WorkingDir
24+
// Use RootWorkingDir which is already absolute (set by initialSetup in cli/commands/commands.go)
25+
// We never want to use filepath.Abs() here as it depends on the process PWD
26+
// Fall back to WorkingDir for tests that don't go through initialSetup
27+
absWorkingDir := opts.RootWorkingDir
28+
if absWorkingDir == "" {
29+
absWorkingDir = opts.WorkingDir
3530
}
3631

3732
d, err := discovery.NewForDiscoveryCommand(discovery.DiscoveryCommandOptions{
@@ -104,7 +99,7 @@ func Run(ctx context.Context, l log.Logger, opts *Options) error {
10499
}, func(ctx context.Context) error {
105100
var convErr error
106101

107-
foundComponents, convErr = discoveredToFound(components, opts)
102+
foundComponents, convErr = discoveredToFound(components, opts, absWorkingDir)
108103

109104
return convErr
110105
})
@@ -137,7 +132,7 @@ type FoundComponent struct {
137132
Reading []string `json:"reading,omitempty"`
138133
}
139134

140-
func discoveredToFound(components component.Components, opts *Options) (FoundComponents, error) {
135+
func discoveredToFound(components component.Components, opts *Options, absWorkingDir string) (FoundComponents, error) {
141136
foundComponents := make(FoundComponents, 0, len(components))
142137
errs := []error{}
143138

@@ -147,8 +142,7 @@ func discoveredToFound(components component.Components, opts *Options) (FoundCom
147142
}
148143

149144
if opts.QueueConstructAs != "" {
150-
if c.Kind() == component.UnitKind {
151-
unit := c.(*component.Unit)
145+
if unit, ok := c.(*component.Unit); ok {
152146
if cfg := unit.Config(); cfg != nil && cfg.Exclude != nil {
153147
if cfg.Exclude.IsActionListed(opts.QueueConstructAs) {
154148
continue
@@ -157,7 +151,7 @@ func discoveredToFound(components component.Components, opts *Options) (FoundCom
157151
}
158152
}
159153

160-
relPath, err := filepath.Rel(opts.WorkingDir, c.Path())
154+
relPath, err := filepath.Rel(absWorkingDir, c.Path())
161155
if err != nil {
162156
errs = append(errs, errors.New(err))
163157

@@ -170,17 +164,15 @@ func discoveredToFound(components component.Components, opts *Options) (FoundCom
170164
}
171165

172166
if opts.Exclude {
173-
if c.Kind() == component.UnitKind {
174-
unit := c.(*component.Unit)
167+
if unit, ok := c.(*component.Unit); ok {
175168
if cfg := unit.Config(); cfg != nil && cfg.Exclude != nil {
176169
foundComponent.Exclude = cfg.Exclude.Clone()
177170
}
178171
}
179172
}
180173

181174
if opts.Include {
182-
if c.Kind() == component.UnitKind {
183-
unit := c.(*component.Unit)
175+
if unit, ok := c.(*component.Unit); ok {
184176
if cfg := unit.Config(); cfg != nil && cfg.ProcessedIncludes != nil {
185177
foundComponent.Include = make(map[string]string, len(cfg.ProcessedIncludes))
186178
for _, v := range cfg.ProcessedIncludes {
@@ -197,7 +189,7 @@ func discoveredToFound(components component.Components, opts *Options) (FoundCom
197189
foundComponent.Reading = make([]string, len(c.Reading()))
198190

199191
for i, reading := range c.Reading() {
200-
relReadingPath, err := filepath.Rel(opts.WorkingDir, reading)
192+
relReadingPath, err := filepath.Rel(absWorkingDir, reading)
201193
if err != nil {
202194
errs = append(errs, errors.New(err))
203195
}
@@ -210,7 +202,7 @@ func discoveredToFound(components component.Components, opts *Options) (FoundCom
210202
foundComponent.Dependencies = make([]string, len(c.Dependencies()))
211203

212204
for i, dep := range c.Dependencies() {
213-
relDepPath, err := filepath.Rel(opts.WorkingDir, dep.Path())
205+
relDepPath, err := filepath.Rel(absWorkingDir, dep.Path())
214206
if err != nil {
215207
errs = append(errs, errors.New(err))
216208

cli/commands/list/list.go

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ import (
2424

2525
// Run runs the list command.
2626
func Run(ctx context.Context, l log.Logger, opts *Options) error {
27+
// Use RootWorkingDir which is already absolute (set by initialSetup in cli/commands/commands.go)
28+
// We never want to use filepath.Abs() here as it depends on the process PWD
29+
// Fall back to WorkingDir for tests that don't go through initialSetup
30+
absWorkingDir := opts.RootWorkingDir
31+
if absWorkingDir == "" {
32+
absWorkingDir = opts.WorkingDir
33+
}
34+
2735
d, err := discovery.NewForDiscoveryCommand(discovery.DiscoveryCommandOptions{
2836
WorkingDir: opts.WorkingDir,
2937
QueueConstructAs: opts.QueueConstructAs,
@@ -90,7 +98,7 @@ func Run(ctx context.Context, l log.Logger, opts *Options) error {
9098
}, func(ctx context.Context) error {
9199
var convErr error
92100

93-
listedComponents, convErr = discoveredToListed(components, opts)
101+
listedComponents, convErr = discoveredToListed(components, opts, absWorkingDir)
94102

95103
return convErr
96104
})
@@ -162,21 +170,10 @@ func (l ListedComponents) Get(path string) *ListedComponent {
162170
return nil
163171
}
164172

165-
func discoveredToListed(components component.Components, opts *Options) (ListedComponents, error) {
173+
func discoveredToListed(components component.Components, opts *Options, absWorkingDir string) (ListedComponents, error) {
166174
listedComponents := make(ListedComponents, 0, len(components))
167175
errs := []error{}
168176

169-
// Ensure working directory is absolute for reliable path relativity calculations
170-
absWorkingDir := opts.WorkingDir
171-
if !filepath.IsAbs(absWorkingDir) {
172-
var err error
173-
174-
absWorkingDir, err = filepath.Abs(absWorkingDir)
175-
if err != nil {
176-
return nil, errors.New(err)
177-
}
178-
}
179-
180177
for _, c := range components {
181178
if c.External() && !opts.External {
182179
continue
@@ -198,19 +195,7 @@ func discoveredToListed(components component.Components, opts *Options) (ListedC
198195
}
199196
}
200197

201-
// Ensure component path is absolute for reliable relativity calculation
202-
componentPath := c.Path()
203-
if !filepath.IsAbs(componentPath) {
204-
var err error
205-
206-
componentPath, err = filepath.Abs(componentPath)
207-
if err != nil {
208-
errs = append(errs, errors.New(err))
209-
continue
210-
}
211-
}
212-
213-
relPath, err := filepath.Rel(absWorkingDir, componentPath)
198+
relPath, err := filepath.Rel(absWorkingDir, c.Path())
214199
if err != nil {
215200
errs = append(errs, errors.New(err))
216201

@@ -232,19 +217,7 @@ func discoveredToListed(components component.Components, opts *Options) (ListedC
232217
listedCfg.Dependencies = make([]*ListedComponent, len(c.Dependencies()))
233218

234219
for i, dep := range c.Dependencies() {
235-
// Ensure dependency path is absolute for reliable relativity calculation
236-
depPath := dep.Path()
237-
if !filepath.IsAbs(depPath) {
238-
var err error
239-
240-
depPath, err = filepath.Abs(depPath)
241-
if err != nil {
242-
errs = append(errs, errors.New(err))
243-
continue
244-
}
245-
}
246-
247-
relDepPath, err := filepath.Rel(absWorkingDir, depPath)
220+
relDepPath, err := filepath.Rel(absWorkingDir, dep.Path())
248221
if err != nil {
249222
errs = append(errs, errors.New(err))
250223

@@ -254,8 +227,7 @@ func discoveredToListed(components component.Components, opts *Options) (ListedC
254227
depExcluded := false
255228

256229
if opts.QueueConstructAs != "" {
257-
if dep.Kind() == component.UnitKind {
258-
depUnit := dep.(*component.Unit)
230+
if depUnit, ok := dep.(*component.Unit); ok {
259231
if depCfg := depUnit.Config(); depCfg != nil && depCfg.Exclude != nil {
260232
if depCfg.Exclude.IsActionListed(opts.QueueConstructAs) {
261233
depExcluded = true

internal/discovery/discovery.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ func (d *Discovery) WithNoHidden() *Discovery {
151151
}
152152

153153
// WithSkipExternalDependencyPrompt sets the flag to skip external dependency prompts.
154-
// This is useful for discovery-only commands like find/list that don't need user interaction.
154+
// Required for discovery-only commands (find/list) that:
155+
// 1. Don't execute terraform (so external deps are safe to include)
156+
// 2. Need to run non-interactively (prompts would block automated scripts)
157+
// 3. Only show/analyze the dependency graph (no risk of modifying external state)
155158
func (d *Discovery) WithSkipExternalDependencyPrompt() *Discovery {
156159
d.skipExternalDependencyPrompt = true
157160

@@ -516,8 +519,9 @@ func Parse(
516519
parseOpts.TerragruntConfigPath = filepath.Join(parseOpts.WorkingDir, configFilename)
517520
parseOpts.OriginalTerragruntConfigPath = parseOpts.TerragruntConfigPath
518521

519-
// When suppressing parse errors, also suppress error-level logging to prevent
520-
// parsing errors from appearing in stderr during discovery
522+
// When suppressing parse errors, also suppress logger output to prevent
523+
// parsing errors from appearing in stderr during discovery.
524+
// Note: This only affects logging DURING parsing, not before or after.
521525
parsingLogger := l
522526
if suppressParseErrors {
523527
parsingLogger = l.WithOptions(log.WithOutput(io.Discard))

internal/discovery/discovery_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ func TestDiscoveryWithDependencies(t *testing.T) {
230230

231231
// nil out the parsed configurations, as it doesn't matter for this test
232232
for _, c := range components {
233-
if c.Kind() == component.UnitKind {
234-
unit := c.(*component.Unit)
233+
if unit, ok := c.(*component.Unit); ok {
235234
unit.StoreConfig(nil)
236235
}
237236
}
@@ -335,8 +334,8 @@ exclude {
335334
findUnit := func(path string) *component.Unit {
336335
for _, c := range components {
337336
if filepath.Base(c.Path()) == path {
338-
if c.Kind() == component.UnitKind {
339-
return c.(*component.Unit)
337+
if unit, ok := c.(*component.Unit); ok {
338+
return unit
340339
}
341340
}
342341
}
@@ -611,8 +610,8 @@ func TestDiscoveryIgnoreExternalDependencies(t *testing.T) {
611610

612611
for _, c := range components {
613612
if c.Path() == appDir {
614-
if c.Kind() == component.UnitKind {
615-
appCfg = c.(*component.Unit)
613+
if unit, ok := c.(*component.Unit); ok {
614+
appCfg = unit
616615
}
617616

618617
break
@@ -674,8 +673,8 @@ func TestDiscoveryPopulatesReadingField(t *testing.T) {
674673

675674
for _, c := range components {
676675
if c.Path() == appDir {
677-
if c.Kind() == component.UnitKind {
678-
appComponent = c.(*component.Unit)
676+
if unit, ok := c.(*component.Unit); ok {
677+
appComponent = unit
679678
}
680679

681680
break

internal/runner/common/unit_runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewUnitRunner(unit *component.Unit) *UnitRunner {
4141

4242
func (runner *UnitRunner) runTerragrunt(ctx context.Context, opts *options.TerragruntOptions, r *report.Report) error {
4343
logger := runner.Unit.Logger()
44+
logger.Debugf("UnitRunner.runTerragrunt called for %s, opts.RunTerragrunt=%v", runner.Unit.Path(), opts.RunTerragrunt)
4445
logger.Debugf("Running %s", runner.Unit.Path())
4546

4647
opts.Writer = component.NewUnitWriter(opts.Writer)
@@ -118,6 +119,7 @@ func (runner *UnitRunner) Run(ctx context.Context, opts *options.TerragruntOptio
118119
runner.Status = Running
119120

120121
logger := runner.Unit.Logger()
122+
logger.Infof("[VERSION-DEBUG] UnitRunner.Run called for %s, working dir: %s", runner.Unit.Path(), opts.WorkingDir)
121123

122124
if runner.Unit.AssumeAlreadyApplied() {
123125
logger.Debugf("Assuming unit %s has already been applied and skipping it", runner.Unit.Path())

internal/runner/run/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ var sourceChangeLocks = sync.Map{}
8686
// Run downloads terraform source if necessary, then runs terraform with the given options and CLI args.
8787
// This will forward all the args and extra_arguments directly to Terraform.
8888
func Run(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error {
89+
l.Infof("[VERSION-DEBUG] run.Run called for working dir: %s, command: %s", opts.WorkingDir, opts.TerraformCommand)
90+
8991
if opts.TerraformCommand == "" {
9092
return errors.New(MissingCommand{})
9193
}

internal/runner/runnerpool/runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func (r *Runner) Run(ctx context.Context, l log.Logger, opts *options.Terragrunt
261261

262262
task := func(ctx context.Context, u *component.Unit) error {
263263
execOpts := u.ExecutionOptions()
264+
l.Warnf("[VERSION-DEBUG-WARN] Runner pool task starting for unit: %s, working dir: %s", u.Path(), execOpts.WorkingDir)
264265

265266
return telemetry.TelemeterFromContext(ctx).Collect(ctx, "runner_pool_task", map[string]any{
266267
"terraform_command": execOpts.TerraformCommand,
@@ -269,6 +270,7 @@ func (r *Runner) Run(ctx context.Context, l log.Logger, opts *options.Terragrunt
269270
"terragrunt_config_path": execOpts.TerragruntConfigPath,
270271
}, func(childCtx context.Context) error {
271272
unitRunner := common.NewUnitRunner(u)
273+
l.Infof("[VERSION-DEBUG] About to call unitRunner.Run for %s", u.Path())
272274

273275
// Clone stack-level options with unit's config path to get unit-specific WorkingDir
274276
// This ensures each unit executes in its own directory

0 commit comments

Comments
 (0)