fix: use custom realpath function in copy_directory and copy_to_directory since github.com/yookoala/realpath has bugs on Windows (#338)

This commit is contained in:
Greg Magolan 2023-01-16 21:46:50 -08:00 committed by GitHub
parent 25a3040180
commit de8d9cae3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 22 deletions

View File

@ -13,7 +13,6 @@ def go_dependencies():
sum = "h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=",
version = "v4.6.0",
)
go_repository(
name = "com_github_google_go_cmp",
build_file_proto_mode = "disable_global",
@ -21,14 +20,6 @@ def go_dependencies():
sum = "h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=",
version = "v0.5.8",
)
go_repository(
name = "com_github_yookoala_realpath",
build_file_proto_mode = "disable_global",
importpath = "github.com/yookoala/realpath",
sum = "h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ=",
version = "v1.0.0",
)
go_repository(
name = "org_golang_x_exp",
build_file_proto_mode = "disable_global",

1
go.mod
View File

@ -4,6 +4,5 @@ go 1.19
require (
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/yookoala/realpath v1.0.0
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
)

2
go.sum
View File

@ -1,6 +1,4 @@
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ=
github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Ya9AIoYBpE=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=

View File

@ -1,6 +1,9 @@
package common
import (
"fmt"
"os"
"path"
"path/filepath"
)
@ -14,3 +17,27 @@ func FileRel(basepath, targpath string) (string, error) {
return filepath.ToSlash(r), nil
}
// github.com/yookoala/realpath has bugs on Windows;
// this function assumes that the path passed in is a symlink
func Realpath(p string) (string, error) {
t, err := os.Readlink(p)
if err != nil {
return "", fmt.Errorf("readlink %s failed: %w", p, err)
}
if !path.IsAbs(t) {
t = path.Join(path.Dir(p), t)
}
info, err := os.Lstat(t)
if err != nil {
return "", fmt.Errorf("lstat %s failed: %w", p, err)
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
return Realpath(t)
}
return t, nil
}

View File

@ -5,10 +5,7 @@ go_library(
srcs = ["main.go"],
importpath = "github.com/aspect-build/bazel-lib/tools/copy_directory",
visibility = ["//visibility:public"],
deps = [
"//tools/common",
"@com_github_yookoala_realpath//:go_default_library",
],
deps = ["//tools/common"],
)
go_binary(

View File

@ -9,7 +9,6 @@ import (
"sync"
"github.com/aspect-build/bazel-lib/tools/common"
"github.com/yookoala/realpath"
)
type pathSet map[string]bool
@ -46,7 +45,7 @@ func copyDir(src string, dst string) error {
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
linkPath, err := realpath.Realpath(p)
linkPath, err := common.Realpath(p)
if err != nil {
return err
}

View File

@ -8,7 +8,6 @@ go_library(
deps = [
"//tools/common",
"@com_github_bmatcuk_doublestar_v4//:doublestar",
"@com_github_yookoala_realpath//:go_default_library",
"@org_golang_x_exp//maps",
],
)

View File

@ -14,7 +14,6 @@ import (
"github.com/aspect-build/bazel-lib/tools/common"
"github.com/bmatcuk/doublestar/v4"
"github.com/yookoala/realpath"
"golang.org/x/exp/maps"
)
@ -155,7 +154,7 @@ func copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
linkPath, err := realpath.Realpath(p)
linkPath, err := common.Realpath(p)
if err != nil {
return err
}
@ -341,7 +340,7 @@ func copyPaths(cfg *config) error {
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// On Windows, filepath.WalkDir doesn't like directory symlinks so we must
// call filepath.WalkDir on the realpath
realpath, err := realpath.Realpath(file.Path)
realpath, err := common.Realpath(file.Path)
if err != nil {
return err
}