@@ -21,17 +21,12 @@ import (
2121
2222// Run runs the find command.
2323func 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
0 commit comments