Skip to content

Commit

Permalink
Fixes for resolving import errors (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos authored Apr 17, 2020
1 parent ebfbc8d commit dbd4ef8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
3 changes: 3 additions & 0 deletions buildifier/utils/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (d *Diagnostics) Format(format string, verbose bool) string {
w.Message,
w.URL))
}
if !f.Formatted {
output.WriteString(fmt.Sprintf("%s # reformat\n", f.Filename))
}
}
return output.String()
case "json":
Expand Down
49 changes: 38 additions & 11 deletions buildifier/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,37 @@ func containsFile(dir, file string) bool {
return false
}

// SplitFilePath splits a file path into the workspace root and package name.
// SplitRelativePath splits a path relative to the workspace root into package name and label.
// It takes the workspace root and chunks of the relative path (already split) as input arguments.
// Both output variables always have forward slashes as separators.
func SplitRelativePath(workspaceRoot string, chunks []string) (pkg, label string) {
switch chunks[len(chunks)-1] {
case "BUILD", "BUILD.bazel":
return path.Join(chunks[:len(chunks)-1]...), chunks[len(chunks)-1]
}

pkg = ""
label = path.Join(chunks...)
parent := workspaceRoot
for i, chunk := range chunks {
if i == len(chunks)-1 {
// The last chunk is a filename, not a directory
break
}
parent = filepath.Join(parent, chunk)
if containsFile(parent, "BUILD") {
pkg = path.Join(chunks[:i+1]...)
label = path.Join(chunks[i+1:]...)
}
}
return pkg, label
}

// SplitFilePath splits a file path into the workspace root, package name and label.
// Workspace root is determined as the last directory in the file path that
// contains a WORKSPACE (or WORKSPACE.bazel) file.
// Returns empty strings if no WORKSPACE file is found
// Package and label are always separated with forward slashes.
// Returns empty strings if no WORKSPACE file is found.
func SplitFilePath(filename string) (workspaceRoot, pkg, label string) {
root := "/"
if volume := filepath.VolumeName(filename); volume != "" {
Expand All @@ -107,20 +134,20 @@ func SplitFilePath(filename string) (workspaceRoot, pkg, label string) {
chunks := append([]string{""}, strings.Split(relPath, string(os.PathSeparator))...)
parent := root
workspaceIndex := -1
for i := 0; i < len(chunks)-1; i++ {
chunk := chunks[i]
for i, chunk := range chunks {
if i == len(chunks)-1 {
// The last chunk is a filename, not a directory
break
}
parent = filepath.Join(parent, chunk)
if containsFile(parent, "WORKSPACE") {
workspaceIndex = i
workspaceRoot = parent
pkg = ""
label = path.Join(chunks[i+1:]...)
}
if workspaceIndex != -1 && containsFile(parent, "BUILD") {
pkg = path.Join(chunks[workspaceIndex+1 : i+1]...)
label = path.Join(chunks[i+1:]...)
workspaceIndex = i
}
}
if workspaceIndex != -1 {
pkg, label = SplitRelativePath(workspaceRoot, chunks[workspaceIndex+1:])
}
return workspaceRoot, pkg, label
}

Expand Down

0 comments on commit dbd4ef8

Please sign in to comment.