Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkgs/mcpx/internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func readDir(dir string, opt Options, explicit bool) (Resolved, error) {
if entryInfo.Mode()&fs.ModeSymlink != 0 {
entryInfo, err = os.Stat(entryPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
// A dangling link has no regular target to read.
if errors.Is(err, fs.ErrNotExist) || isSymlinkLoop(err) {
// Dangling and looping links have no regular target to read.
continue
}
return fmt.Errorf("reading %s: %w", entryPath, err)
Expand Down
30 changes: 30 additions & 0 deletions pkgs/mcpx/internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,36 @@ func TestDirectorySkipsDanglingLinkAndFollowsLinkOutsideRoot(t *testing.T) {
}
}

func TestDirectorySkipsSymlinkLoops(t *testing.T) {
for _, tc := range []struct {
name string
links map[string]string
}{
{"self", map[string]string{"loop.ts": "loop.ts"}},
{"pair", map[string]string{"a.ts": "b.ts", "b.ts": "a.ts"}},
} {
t.Run(tc.name, func(t *testing.T) {
root := t.TempDir()
regular := filepath.Join(root, "regular.ts")
if err := os.WriteFile(regular, []byte("regular"), 0o644); err != nil {
t.Fatal(err)
}
for name, target := range tc.links {
if err := os.Symlink(target, filepath.Join(root, name)); err != nil {
t.Skipf("symlinks are unavailable: %v", err)
}
}
got, err := source.Resolve("@dir:"+root, opts(root))
if err != nil {
t.Fatal(err)
}
if len(got.Files) != 1 || got.Files[0] != regular {
t.Fatalf("got files %v, want [%s]", got.Files, regular)
}
})
}
}

func TestResolveAllJoinsInOrder(t *testing.T) {
dir := t.TempDir()
_ = os.WriteFile(filepath.Join(dir, "second.ts"), []byte("SECOND"), 0o644)
Expand Down
5 changes: 5 additions & 0 deletions pkgs/mcpx/internal/source/symlink_loop_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !unix

package source

func isSymlinkLoop(error) bool { return false }
12 changes: 12 additions & 0 deletions pkgs/mcpx/internal/source/symlink_loop_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build unix

package source

import (
"errors"
"syscall"
)

func isSymlinkLoop(err error) bool {
return errors.Is(err, syscall.ELOOP)
}
Loading