Skip to content

Commit

Permalink
fix(cli): unescape runfiles entries from manifest #799
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhauser committed Jan 22, 2025
1 parent 68cf6f1 commit 8fcf3f9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/aspect/outputs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/bazel",
"//pkg/bazel/runfiles",
"//pkg/ioutils",
"@com_github_alphadose_haxmap//:haxmap",
"@com_github_rogpeppe_go_internal//dirhash",
Expand Down
3 changes: 2 additions & 1 deletion pkg/aspect/outputs/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/alphadose/haxmap"
"github.com/aspect-build/aspect-cli/pkg/bazel"
r "github.com/aspect-build/aspect-cli/pkg/bazel/runfiles"
"github.com/rogpeppe/go-internal/dirhash"

concurrently "github.com/tejzpr/ordered-concurrently/v3"
Expand Down Expand Up @@ -73,7 +74,7 @@ func AddRunfilesHash(hashFiles map[string][]string, label string, manifestPath s
// execroot/path /some/absolute/path
entry := strings.Split(fileScanner.Text(), " ")
// key := entry[0]
abspath := entry[1]
abspath := r.Unescape(entry[1])
fileinfo, err := os.Stat(abspath)

if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/bazel/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "runfiles",
srcs = ["runfiles.go"],
importpath = "github.com/aspect-build/aspect-cli/pkg/bazel/runfiles",
visibility = ["//visibility:public"],
)

go_test(
name = "runfiles_test",
srcs = ["runfiles_test.go"],
deps = [
":runfiles",
"@com_github_stretchr_testify//require",
],
)
33 changes: 33 additions & 0 deletions pkg/bazel/runfiles/runfiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package runfiles

// Unescape the behavior of the C++ implementation
// https://github.com/bazelbuild/bazel/blob/master/src/main/tools/build-runfiles.cc#L107
func Unescape(path string) string {
var result []rune
runes := []rune(path)

for i := 0; i < len(runes); i++ {
if runes[i] == '\\' && i+1 < len(runes) {
switch runes[i+1] {
case 's':
result = append(result, ' ')
case 'n':
result = append(result, '\n')
case 'b':
result = append(result, '\\')
default:
// For escaped backslash (\\), output single backslash
if runes[i+1] == '\\' {
result = append(result, '\\')
} else {
// For any other escaped character, preserve both the backslash and the character
result = append(result, '\\', runes[i+1])
}
}
i++
} else {
result = append(result, runes[i])
}
}
return string(result)
}
32 changes: 32 additions & 0 deletions pkg/bazel/runfiles/runfiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package runfiles_test

import (
"github.com/aspect-build/aspect-cli/pkg/bazel/runfiles"
"github.com/stretchr/testify/require"
"testing"
)

func TestUnescape(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello\\sworld", "hello world"},
{"new\\nline", "new\nline"},
{"back\\bslash", "back\\slash"},
{"double\\\\slash", "double\\slash"},
{"raw\\xsequence", "raw\\xsequence"},
{"no\\", "no\\"}, // Handles trailing backslash
{"\\s\\n\\b", " \n\\"},
{"", ""},
{"normal text", "normal text"},
{"\\s\\s\\s", " "},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := runfiles.Unescape(tt.input)
require.Equal(t, tt.expected, got)
})
}
}

0 comments on commit 8fcf3f9

Please sign in to comment.