fix: fix pathing issue in new copy_to_directory binary tool on Windows (#334)

This commit is contained in:
Greg Magolan 2023-01-16 15:09:18 -08:00 committed by GitHub
parent 4dc36a97f2
commit ce043b299d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 16 deletions

View File

@ -193,8 +193,8 @@ bzl_library(
srcs = ["repositories.bzl"],
deps = [
":utils",
"//lib/private/docs:coreutils_toolchain",
"//lib/private/docs:copy_to_directory_toolchain",
"//lib/private/docs:coreutils_toolchain",
"//lib/private/docs:jq_toolchain",
"//lib/private/docs:local_config_platform",
"//lib/private/docs:yq_toolchain",

View File

@ -14,11 +14,10 @@ diff_test(
file2 = ":ls",
)
genrule(
name = "sha256sum",
outs = ["sha256sum.txt"],
srcs = ["test.bin"],
outs = ["sha256sum.txt"],
cmd = "$(COREUTILS_BIN) sha256sum $(location :test.bin) > $@",
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)
@ -29,11 +28,10 @@ assert_contains(
expected = """9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 lib/tests/coreutils/test.bin""",
)
genrule(
name = "sha512sum",
outs = ["sha512sum.txt"],
srcs = ["test.bin"],
outs = ["sha512sum.txt"],
cmd = "$(COREUTILS_BIN) sha512sum $(location :test.bin) > $@",
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)
@ -46,8 +44,8 @@ assert_contains(
genrule(
name = "wc",
outs = ["wc.txt"],
srcs = ["test.bin"],
outs = ["wc.txt"],
cmd = "$(COREUTILS_BIN) wc $(location :test.bin) > $@",
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)
@ -56,4 +54,4 @@ assert_contains(
name = "test_wc",
actual = "wc.txt",
expected = """0 1 4 lib/tests/coreutils/test.bin""",
)
)

View File

@ -14,6 +14,7 @@ go_library(
name = "common",
srcs = [
"copy.go",
"file.go",
"stamp.go",
"version.go",
],

14
tools/common/file.go Normal file
View File

@ -0,0 +1,14 @@
package common
import "path/filepath"
// Same as filepath.Rel except that it normalizes result to forward slashes
// slashes since filepath.Rel will convert to system slashes
func FileRel(basepath, targpath string) (string, error) {
r, err := filepath.Rel(basepath, targpath)
if err != nil {
return "", err
}
return filepath.ToSlash(r), nil
}

View File

@ -27,7 +27,7 @@ func copyDir(src string, dst string) error {
return err
}
r, err := filepath.Rel(src, p)
r, err := common.FileRel(src, p)
if err != nil {
return err
}

View File

@ -142,6 +142,11 @@ func copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
return err
}
r, err := common.FileRel(file.Path, p)
if err != nil {
return err
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
linkPath, err := os.Readlink(p)
@ -174,10 +179,6 @@ func copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
return copyDir(cfg, srcPaths, f)
} else {
// symlink points to a regular file
r, err := filepath.Rel(file.Path, p)
if err != nil {
return fmt.Errorf("failed to walk directory %s: %w", file.Path, err)
}
f := fileInfo{
Package: file.Package,
Path: linkPath,
@ -193,10 +194,6 @@ func copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
}
// a regular file
r, err := filepath.Rel(file.Path, p)
if err != nil {
return fmt.Errorf("failed to walk directory %s: %w", file.Path, err)
}
f := fileInfo{
Package: file.Package,
Path: p,