@@ -221,23 +221,22 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
221221// Symlinks are allowed in `paths` and we resolve them here, since
222222// the readers will ignore symlinks.
223223func resolvePaths (paths []string , walkType walk.Type , treeRoot string ) error {
224- for i , path := range paths {
225- log .Debugf ("Resolving path '%s': %v" , path , walkType )
224+ // Note: `treeRoot` may itself be or contain a symlink (e.g. it is in
225+ // `$TMPDIR` on macOS or a user has set a symlink to shorten the repository
226+ // path for path length restrictions), so we resolve it here first.
227+ //
228+ // See: https://github.com/numtide/treefmt/issues/578
229+ treeRoot , err := resolvePath (walkType , treeRoot )
230+ if err != nil {
231+ return fmt .Errorf ("error computing absolute path of %s: %w" , treeRoot , err )
232+ }
226233
227- absolutePath , err := filepath .Abs (path )
234+ for i , path := range paths {
235+ absolutePath , err := resolvePath (walkType , path )
228236 if err != nil {
229237 return fmt .Errorf ("error computing absolute path of %s: %w" , path , err )
230238 }
231239
232- if walkType != walk .Stdin {
233- realPath , err := filepath .EvalSymlinks (absolutePath )
234- if err != nil {
235- return fmt .Errorf ("path %s not found: %w" , absolutePath , err )
236- }
237-
238- absolutePath = realPath
239- }
240-
241240 relativePath , err := filepath .Rel (treeRoot , absolutePath )
242241 if err != nil {
243242 return fmt .Errorf ("error computing relative path from %s to %s: %w" , treeRoot , absolutePath , err )
@@ -252,3 +251,24 @@ func resolvePaths(paths []string, walkType walk.Type, treeRoot string) error {
252251
253252 return nil
254253}
254+
255+ // Resolve a path to an absolute path, resolving symlinks if necessary.
256+ func resolvePath (walkType walk.Type , path string ) (string , error ) {
257+ log .Debugf ("Resolving path '%s': %v" , path , walkType )
258+
259+ absolutePath , err := filepath .Abs (path )
260+ if err != nil {
261+ return "" , fmt .Errorf ("error computing absolute path of %s: %w" , path , err )
262+ }
263+
264+ if walkType != walk .Stdin {
265+ realPath , err := filepath .EvalSymlinks (absolutePath )
266+ if err != nil {
267+ return "" , fmt .Errorf ("path %s not found: %w" , absolutePath , err )
268+ }
269+
270+ absolutePath = realPath
271+ }
272+
273+ return absolutePath , nil
274+ }
0 commit comments