diff --git a/internal/smartlink/smartlink.go b/internal/smartlink/smartlink.go index feb5134fa5..c101abde5e 100644 --- a/internal/smartlink/smartlink.go +++ b/internal/smartlink/smartlink.go @@ -40,6 +40,8 @@ func LinkContents(src, dest string) error { // Link creates a link from src to target. MS decided to support Symlinks but only if you opt into developer mode (go figure), // which we cannot reasonably force on our users. So on Windows we will instead create dirs and hardlinks. func Link(src, dest string) error { + originalSrc := src + var err error src, dest, err = resolvePaths(src, dest) if err != nil { @@ -47,6 +49,16 @@ func Link(src, dest string) error { } if fileutils.IsDir(src) { + if fileutils.IsSymlink(originalSrc) { + // If the original src is a symlink, the resolved src is no longer a symlink and could point + // to a parent directory, resulting in a recursive directory structure. + // Avoid any potential problems by simply linking the original link to the target. + // Links to directories are okay on Linux and macOS, but will fail on Windows. + // If we ever get here on Windows, the artifact being deployed is bad and there's nothing we + // can do about it except receive the report from Rollbar and report it internally. + return linkFile(originalSrc, dest) + } + if err := fileutils.Mkdir(dest); err != nil { return errs.Wrap(err, "could not create directory %s", dest) } diff --git a/internal/smartlink/smartlink_lin_mac.go b/internal/smartlink/smartlink_lin_mac.go index b19f45d411..edc065f280 100644 --- a/internal/smartlink/smartlink_lin_mac.go +++ b/internal/smartlink/smartlink_lin_mac.go @@ -5,16 +5,10 @@ package smartlink import ( "os" - - "github.com/ActiveState/cli/internal/errs" - "github.com/ActiveState/cli/internal/fileutils" ) // file will create a symlink from src to dest, and falls back on a hardlink if no symlink is available. // This is a workaround for the fact that Windows does not support symlinks without admin privileges. func linkFile(src, dest string) error { - if fileutils.IsDir(src) { - return errs.New("src is a directory, not a file: %s", src) - } return os.Symlink(src, dest) } diff --git a/internal/smartlink/smartlink_test.go b/internal/smartlink/smartlink_test.go new file mode 100644 index 0000000000..50a772254f --- /dev/null +++ b/internal/smartlink/smartlink_test.go @@ -0,0 +1,75 @@ +package smartlink + +import ( + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/ActiveState/cli/internal/fileutils" + "github.com/stretchr/testify/require" +) + +func TestLinkContentsWithCircularLink(t *testing.T) { + srcDir, err := os.MkdirTemp("", "src") + require.NoError(t, err) + defer os.RemoveAll(srcDir) + + destDir, err := os.MkdirTemp("", "dest") + require.NoError(t, err) + defer os.RemoveAll(destDir) + + // Create test file structure: + // src/ + // ├── regular.txt + // └── subdir/ + // ├── circle -> subdir (circular link) + // └── subfile.txt + + testFile := filepath.Join(srcDir, "regular.txt") + err = os.WriteFile(testFile, []byte("test content"), 0644) + require.NoError(t, err) + + subDir := filepath.Join(srcDir, "subdir") + err = os.Mkdir(subDir, 0755) + require.NoError(t, err) + + subFile := filepath.Join(subDir, "subfile.txt") + err = os.WriteFile(subFile, []byte("sub content"), 0644) + require.NoError(t, err) + + circularLink := filepath.Join(subDir, "circle") + err = os.Symlink(subDir, circularLink) + require.NoError(t, err) + + err = LinkContents(srcDir, destDir) + if runtime.GOOS == "windows" { + require.Error(t, err) + return // hard links to directories are not allowed on Windows + } + require.NoError(t, err) + + // Verify file structure. + destFile := filepath.Join(destDir, "regular.txt") + require.FileExists(t, destFile) + content, err := os.ReadFile(destFile) + require.NoError(t, err) + require.Equal(t, "test content", string(content)) + + destSubFile := filepath.Join(destDir, "subdir", "subfile.txt") + require.FileExists(t, destSubFile) + subContent, err := os.ReadFile(destSubFile) + require.NoError(t, err) + require.Equal(t, "sub content", string(subContent)) + + destCircular := filepath.Join(destDir, "subdir", "circle") + require.FileExists(t, destCircular) + target, err := fileutils.ResolveUniquePath(destCircular) + require.NoError(t, err) + srcCircular := filepath.Join(srcDir, "subdir") + if runtime.GOOS == "darwin" { + srcCircular, err = fileutils.ResolveUniquePath(srcCircular) // needed for full $TMPDIR resolution + require.NoError(t, err) + } + require.Equal(t, target, srcCircular) +}