Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions internal/ibazel/ibazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,26 +628,15 @@ func (i *IBazel) watchFiles(toWatch []string, watcher common.Watcher) {
i.filesWatched[watcher] = filesWatched
}

func (i *IBazel) labelsToWatch(labels []string) ([]string, error) {
localRepositories, err := i.realLocalRepositoryPaths()
if err != nil {
return nil, err
}

workspacePath, err := i.workspaceFinder.FindWorkspace()
if err != nil {
log.Errorf("Error finding workspace: %v", err)
return nil, err
}

func resolveLabels(labels []string, localRepositories map[string]string, workspacePath string) []string {
toWatch := make([]string, 0, len(labels))
for _, label := range labels {
if strings.HasPrefix(label, "@") {
repo, target := parseTarget(label)
if realPath, ok := localRepositories[repo]; ok {
label = strings.Replace(target, ":", string(filepath.Separator), 1)
toWatch = append(toWatch, filepath.Join(realPath, label))
break
continue
}
continue
}
Expand All @@ -658,8 +647,22 @@ func (i *IBazel) labelsToWatch(labels []string) ([]string, error) {
label = strings.Replace(strings.TrimPrefix(label, "//"), ":", string(filepath.Separator), 1)
toWatch = append(toWatch, filepath.Join(workspacePath, label))
}
return toWatch
}

func (i *IBazel) labelsToWatch(labels []string) ([]string, error) {
localRepositories, err := i.realLocalRepositoryPaths()
if err != nil {
return nil, err
}

workspacePath, err := i.workspaceFinder.FindWorkspace()
if err != nil {
log.Errorf("Error finding workspace: %v", err)
return nil, err
}

return toWatch, nil
return resolveLabels(labels, localRepositories, workspacePath), nil
}

func (i *IBazel) queryArgs(args ...string) []string {
Expand Down
63 changes: 63 additions & 0 deletions internal/ibazel/ibazel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,66 @@ func TestParseTarget(t *testing.T) {
})
}
}

func TestResolveLabels(t *testing.T) {
log.SetTesting(t)

localRepositories := map[string]string{
"nested": "/home/user/nested",
}
workspacePath := "/home/user/workspace"

tests := []struct {
name string
labels []string
want []string
}{
{
name: "local label only",
labels: []string{"//:src/main.sh"},
want: []string{filepath.Join(workspacePath, "src/main.sh")},
},
{
name: "external repo label resolved to local path",
labels: []string{"@nested//:lib.sh"},
want: []string{filepath.Join("/home/user/nested", "lib.sh")},
},
{
name: "unknown external repo skipped",
labels: []string{"@unknown//:foo.sh"},
want: []string{},
},
{
name: "//external label skipped",
labels: []string{"//external/foo:bar.sh"},
want: []string{},
},
{
name: "local repo match does not stop processing subsequent labels",
labels: []string{"@nested//:lib.sh", "//:src/main.sh"},
want: []string{
filepath.Join("/home/user/nested", "lib.sh"),
filepath.Join(workspacePath, "src/main.sh"),
},
},
{
name: "multiple local repo matches all resolved",
labels: []string{"@nested//:lib.sh", "@nested//:other.sh"},
want: []string{
filepath.Join("/home/user/nested", "lib.sh"),
filepath.Join("/home/user/nested", "other.sh"),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := resolveLabels(test.labels, localRepositories, workspacePath)
if len(got) == 0 && len(test.want) == 0 {
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("resolveLabels(%v) = %v, want %v", test.labels, got, test.want)
}
})
}
}