Skip to content

Commit 71acbc6

Browse files
authored
Merge pull request #579 from 9999years/fix-578
Fix when tree root is symlink
2 parents 7234560 + a22a307 commit 71acbc6

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

cmd/format/format.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
223223
func 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+
}

cmd/root_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,44 @@ func TestRunInSubdir(t *testing.T) {
20992099
}
21002100
}
21012101

2102+
// Check that supplying paths on the command-line works when an element of the
2103+
// project root is a symlink.
2104+
//
2105+
// Regression test for #578.
2106+
//
2107+
// See: https://github.com/numtide/treefmt/issues/578
2108+
func TestProjectRootIsSymlink(t *testing.T) {
2109+
as := require.New(t)
2110+
2111+
tempDir := t.TempDir()
2112+
realRoot := filepath.Join(tempDir, "/real-root")
2113+
test.TempExamplesInDir(t, realRoot)
2114+
2115+
symlinkRoot := filepath.Join(tempDir, "/project-root")
2116+
err := os.Symlink(realRoot, symlinkRoot)
2117+
as.NoError(err)
2118+
2119+
test.ChangeWorkDir(t, symlinkRoot)
2120+
2121+
// basic config
2122+
cfg := &config.Config{
2123+
FormatterConfigs: map[string]*config.Formatter{
2124+
"echo": {
2125+
Command: "echo",
2126+
Includes: []string{"*"},
2127+
},
2128+
},
2129+
}
2130+
2131+
configPath := filepath.Join(symlinkRoot, "/treefmt.toml")
2132+
test.WriteConfig(t, configPath, cfg)
2133+
2134+
treefmt(t,
2135+
withArgs("-c", "go/main.go"),
2136+
withNoError(t),
2137+
)
2138+
}
2139+
21022140
type options struct {
21032141
args []string
21042142
env map[string]string

0 commit comments

Comments
 (0)