diff --git a/internal/gps/strip_vendor.go b/internal/gps/strip_vendor.go index 1b224d0e6a..aaaf9bcd22 100644 --- a/internal/gps/strip_vendor.go +++ b/internal/gps/strip_vendor.go @@ -23,11 +23,7 @@ func stripVendor(path string, info os.FileInfo, err error) error { // If the file is a symlink to a directory, delete the symlink. if (info.Mode() & os.ModeSymlink) != 0 { - realInfo, err := os.Stat(path) - if err != nil { - return err - } - if realInfo.IsDir() { + if realInfo, err := os.Stat(path); err == nil && realInfo.IsDir() { return os.Remove(path) } } diff --git a/internal/gps/strip_vendor_nonwindows_test.go b/internal/gps/strip_vendor_nonwindows_test.go index a8eb9ae928..8ff55155b0 100644 --- a/internal/gps/strip_vendor_nonwindows_test.go +++ b/internal/gps/strip_vendor_nonwindows_test.go @@ -82,6 +82,31 @@ func TestStripVendorSymlinks(t *testing.T) { }, })) + t.Run("broken vendor symlink", stripVendorTestCase(fsTestCase{ + before: filesystemState{ + dirs: []fsPath{ + {"package"}, + }, + links: []fsLink{ + { + path: fsPath{"package", "vendor"}, + to: "nonexistence", + }, + }, + }, + after: filesystemState{ + dirs: []fsPath{ + {"package"}, + }, + links: []fsLink{ + { + path: fsPath{"package", "vendor"}, + to: "nonexistence", + }, + }, + }, + })) + t.Run("chained symlinks", stripVendorTestCase(fsTestCase{ before: filesystemState{ dirs: []fsPath{ diff --git a/internal/gps/strip_vendor_windows.go b/internal/gps/strip_vendor_windows.go index 5e51513590..c6b0a13346 100644 --- a/internal/gps/strip_vendor_windows.go +++ b/internal/gps/strip_vendor_windows.go @@ -37,11 +37,7 @@ func stripVendor(path string, info os.FileInfo, err error) error { return filepath.SkipDir case symlink: - realInfo, err := os.Stat(path) - if err != nil { - return err - } - if realInfo.IsDir() { + if realInfo, err := os.Stat(path); err == nil && realInfo.IsDir() { return os.Remove(path) } diff --git a/internal/gps/strip_vendor_windows_test.go b/internal/gps/strip_vendor_windows_test.go index 7569a48741..67ff900299 100644 --- a/internal/gps/strip_vendor_windows_test.go +++ b/internal/gps/strip_vendor_windows_test.go @@ -90,6 +90,31 @@ func TestStripVendorSymlinks(t *testing.T) { }, })) + t.Run("broken vendor symlink", stripVendorTestCase(fsTestCase{ + before: filesystemState{ + dirs: []fsPath{ + {"package"}, + }, + links: []fsLink{ + { + path: fsPath{"package", "vendor"}, + to: "nonexistence", + }, + }, + }, + after: filesystemState{ + dirs: []fsPath{ + {"package"}, + }, + links: []fsLink{ + { + path: fsPath{"package", "vendor"}, + to: "nonexistence", + }, + }, + }, + })) + t.Run("chained symlinks", stripVendorTestCase(fsTestCase{ // Curiously, if a symlink on windows points to *another* symlink which // eventually points at a directory, we'll correctly remove that first