Skip to content

Commit

Permalink
Improve absolute path handling on Windows
Browse files Browse the repository at this point in the history
In the presence of forward-slash paths with drive letters.
They can come from environment variable expansion in the proto
configuration.
  • Loading branch information
Nils Wireklint authored and Nils Wireklint committed Feb 7, 2024
1 parent 9de9f10 commit 9cfab0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/filesystem/local_directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,34 @@ func newLocalDirectory(absPath string, openReparsePoint bool) (DirectoryCloser,
return newLocalDirectoryFromHandle(handle)
}

// Convert forward-slash drive-letter paths to absolute drive-letter paths.
// This can come from how build directories are configured for bb-worker on Windows.
// Where a combination of 'git-bash' and environment variable expansion in jsonnet can create paths
// that the `filepath` does not detect as absolute.
//
// Example: /C:/...
// Should be C:/...
func CanonicalizeAbsolutePath(path string) string {
if len(path) >= 4 && path[0] == '/' && path[2] == ':' && path[3] == '/' {
path = path[1:]
}

return path
}

func NewLocalDirectory(path string) (DirectoryCloser, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
var absPath string
var err error

path = CanonicalizeAbsolutePath(path)

if filepath.IsAbs(path) {
absPath = filepath.FromSlash(path)
} else {
absPath, err = filepath.Abs(path)
if err != nil {
return nil, err
}
}
absPath = "\\??\\" + absPath
return newLocalDirectory(absPath, true)
Expand Down
7 changes: 7 additions & 0 deletions pkg/filesystem/path/resolve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package path

import (
"runtime"
"strings"

"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -38,6 +39,12 @@ func (rs *resolverState) push(scopeWalker ScopeWalker, path string) error {
path = stripOneOrMoreSlashes(path)
absolute = true
}
if runtime.GOOS == "windows" {
// filepath.IsAbs
if len(path) > 3 && path[1] == ':' && path[2] == '/' {
absolute = true
}
}

// Push the path without any leading slashes onto the stack, so
// that its components may be processed. Apply them against the
Expand Down

0 comments on commit 9cfab0c

Please sign in to comment.