Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Visibility extension overwrite on descent (fixes #1467) #1472

Merged
merged 1 commit into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion language/bazel/visibility/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,21 @@ func (*visibilityExtension) Configure(c *config.Config, _ string, f *rule.File)
return
}

var newVisTargets []string
for _, d := range f.Directives {
switch d.Key {
case _directiveName:
for _, target := range strings.Split(d.Value, ",") {
cfg.visibilityTargets = append(cfg.visibilityTargets, target)
newVisTargets = append(newVisTargets, target)
}
}
}

// if visibility targets were specified, overwrite the config
if len(newVisTargets) != 0 {
cfg.visibilityTargets = newVisTargets
}

c.Exts[_extName] = cfg
}

Expand Down
61 changes: 61 additions & 0 deletions language/bazel/visibility/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,64 @@ func Test_NoRuleIfNoBuildFile(t *testing.T) {
t.Fatal("expected array of length 0")
}
}

func Test_MultipleDirectivesAcrossFilesSupercede(t *testing.T) {
testVis1 := "//src:__subpackages__"
testVis2 := "//src2:__subpackages__"
file1, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(`
# gazelle:default_visibility %s
`, testVis1)))
if err != nil {
t.Fatalf("expected not nil - %+v", err)
}
file2, err := rule.LoadData("path/path", "pkg", []byte(fmt.Sprintf(`
# gazelle:default_visibility %s
`, testVis2)))
if err != nil {
t.Fatalf("expected not nil - %+v", err)
}

cfg := config.New()
ext := visibility.NewLanguage()
ext.Configure(cfg, "path", file1)

// clone the config as if we were decending through Walk
cfg2 := cfg.Clone()
ext.Configure(cfg2, "path/path", file2)

res2 := ext.GenerateRules(language.GenerateArgs{
Config: cfg2,
File: rule.EmptyFile("path/path/file", "pkg"),
})

if len(res2.Gen) != 1 {
t.Fatal("expected array of length 1")
}
if len(res2.Imports) != 1 {
t.Fatal("expected array of length 1")
}
if len(res2.Gen[0].AttrStrings("default_visibility")) != 1 {
t.Fatal("expected array of length 1")
}
if testVis2 != res2.Gen[0].AttrStrings("default_visibility")[0] {
t.Fatal("expected returned visibility to match '//src2:__subpackages__'")
}

res1 := ext.GenerateRules(language.GenerateArgs{
Config: cfg,
File: rule.EmptyFile("path/file", "pkg"),
})

if len(res1.Gen) != 1 {
t.Fatal("expected array of length 1")
}
if len(res1.Imports) != 1 {
t.Fatal("expected array of length 1")
}
if len(res1.Gen[0].AttrStrings("default_visibility")) != 1 {
t.Fatal("expected array of length 1")
}
if testVis1 != res1.Gen[0].AttrStrings("default_visibility")[0] {
t.Fatal("expected returned visibility to match '//src:__subpackages__'")
}
}