Skip to content

Commit 24995dc

Browse files
authored
Merge pull request #187 from rabbitmq/no-generate-glob-attrs
Do not generate wildcard (glob/native.glob) "srcs" in gazelle
2 parents b8d58ff + 8809850 commit 24995dc

File tree

4 files changed

+56
-195
lines changed

4 files changed

+56
-195
lines changed

gazelle/erlang_app.go

Lines changed: 20 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -115,49 +115,6 @@ func multilineList[T any](values []T) build.Expr {
115115
}
116116
}
117117

118-
func multilineGlob(glob rule.GlobValue, macro bool) build.Expr {
119-
var patternsValue build.Expr
120-
if len(glob.Patterns) < 2 {
121-
patternsValue = rule.ExprFromValue(glob.Patterns)
122-
} else {
123-
patternsValue = multilineList(glob.Patterns)
124-
}
125-
globArgs := []build.Expr{patternsValue}
126-
if len(glob.Excludes) > 0 {
127-
excludesValue := multilineList(glob.Excludes)
128-
globArgs = append(globArgs, &build.AssignExpr{
129-
LHS: &build.LiteralExpr{Token: "exclude"},
130-
Op: "=",
131-
RHS: excludesValue,
132-
})
133-
}
134-
token := "glob"
135-
if macro {
136-
token = "native.glob"
137-
}
138-
return &build.CallExpr{
139-
X: &build.LiteralExpr{Token: token},
140-
List: globArgs,
141-
ForceMultiLine: len(glob.Excludes) > 0,
142-
}
143-
}
144-
145-
func explicitPlusGlobExpr(
146-
explicit []string,
147-
glob rule.GlobValue,
148-
macro bool,
149-
) build.Expr {
150-
if len(explicit) == 0 {
151-
return multilineGlob(glob, macro)
152-
}
153-
154-
return &build.BinaryExpr{
155-
X: multilineList(explicit),
156-
Op: "+",
157-
Y: multilineGlob(glob, macro),
158-
}
159-
}
160-
161118
func (erlangApp *ErlangApp) pathFor(from, include string) string {
162119
directPath := filepath.Join(filepath.Dir(from), include)
163120
privatePath := filepath.Join("src", include)
@@ -317,18 +274,7 @@ func (erlangApp *ErlangApp) BeamFilesRules(args language.GenerateArgs, erlParser
317274
othersRule = rule.NewRule(erlangBytecodeKind, "other_beam")
318275
othersRule.SetAttr("app_name", erlangApp.Name)
319276
othersRule.SetAttr("erlc_opts", "//:"+erlcOptsRuleName)
320-
othersSrcsGen := mutable_set.Map(others.Filter(func(v *ErlangAppFileParsed) bool {
321-
return v.IsGenFile
322-
}), (*ErlangAppFileParsed).path).Values(strings.Compare)
323-
otherSrcsExcludes := mutable_set.Map(mutable_set.Union(transforms, behaviours), (*ErlangAppFileParsed).path).Values(strings.Compare)
324-
othersRule.SetAttr("srcs", explicitPlusGlobExpr(
325-
othersSrcsGen,
326-
rule.GlobValue{
327-
Patterns: []string{"src/**/*.erl"},
328-
Excludes: otherSrcsExcludes,
329-
},
330-
erlangConfig.GenerateBeamFilesMacro,
331-
))
277+
othersRule.SetAttr("srcs", multilineList(others.Values(comparePaths)))
332278
othersRule.SetAttr("hdrs", []string{":public_and_private_hdrs"})
333279
othersRule.SetAttr("dest", "ebin")
334280
if len(beamFilesGroupRules) > 0 {
@@ -480,18 +426,7 @@ func (erlangApp *ErlangApp) testBeamFilesRules(args language.GenerateArgs, erlPa
480426
othersRule.SetAttr("testonly", true)
481427
othersRule.SetAttr("app_name", erlangApp.Name)
482428
othersRule.SetAttr("erlc_opts", "//:"+testErlcOptsRuleName)
483-
othersSrcsGen := mutable_set.Map(others.Filter(func(v *ErlangAppFileParsed) bool {
484-
return v.IsGenFile
485-
}), (*ErlangAppFileParsed).path).Values(strings.Compare)
486-
otherSrcsExcludes := mutable_set.Map(mutable_set.Union(transforms, behaviours), (*ErlangAppFileParsed).path).Values(strings.Compare)
487-
othersRule.SetAttr("srcs", explicitPlusGlobExpr(
488-
othersSrcsGen,
489-
rule.GlobValue{
490-
Patterns: []string{"src/**/*.erl"},
491-
Excludes: otherSrcsExcludes,
492-
},
493-
erlangConfig.GenerateBeamFilesMacro,
494-
))
429+
othersRule.SetAttr("srcs", multilineList(others.Values(comparePaths)))
495430
othersRule.SetAttr("hdrs", []string{":public_and_private_hdrs"})
496431
othersRule.SetAttr("dest", "test")
497432
if len(beamFilesGroupRules) > 0 {
@@ -555,121 +490,52 @@ func (erlangApp *ErlangApp) allSrcsRules(args language.GenerateArgs) (rules []*r
555490
erlangConfig := erlangConfigForRel(args.Config, args.Rel)
556491

557492
srcs := rule.NewRule("filegroup", "srcs")
558-
if erlangConfig.GenerateFewerBytecodeRules {
559-
srcsGen := mutable_set.Map(erlangApp.Srcs.Filter(func(v *ErlangAppFileParsed) bool {
560-
return v.IsGenFile
561-
}), (*ErlangAppFileParsed).path).Values(strings.Compare)
562-
srcs.SetAttr("srcs", explicitPlusGlobExpr(
563-
srcsGen,
564-
rule.GlobValue{
565-
Patterns: []string{
566-
"src/**/*.erl",
567-
"src/**/*.app.src",
568-
},
569-
},
570-
erlangConfig.GenerateBeamFilesMacro,
571-
))
572-
} else {
573-
srcsSrcs := mutable_set.Union(
574-
mutable_set.Map(erlangApp.Srcs, (*ErlangAppFileParsed).path),
575-
mutable_set.Map(erlangApp.AppSrc, (*ErlangAppFile).path),
576-
).Values(strings.Compare)
577-
if len(srcsSrcs) > 0 {
578-
srcs.SetAttr("srcs", multilineList(srcsSrcs))
579-
}
493+
srcsSrcs := mutable_set.Union(
494+
mutable_set.Map(erlangApp.Srcs, (*ErlangAppFileParsed).path),
495+
mutable_set.Map(erlangApp.AppSrc, (*ErlangAppFile).path),
496+
).Values(strings.Compare)
497+
if len(srcsSrcs) > 0 {
498+
srcs.SetAttr("srcs", multilineList(srcsSrcs))
580499
}
581500
if erlangConfig.Testonly {
582501
srcs.SetAttr("testonly", true)
583502
}
584503
rules = append(rules, srcs)
585504

586505
private_hdrs := rule.NewRule("filegroup", "private_hdrs")
587-
if erlangConfig.GenerateFewerBytecodeRules {
588-
privateHdrsGen := mutable_set.Map(erlangApp.PrivateHdrs.Filter(func(v *ErlangAppFile) bool {
589-
return v.IsGenFile
590-
}), (*ErlangAppFile).path).Values(strings.Compare)
591-
private_hdrs.SetAttr("srcs", explicitPlusGlobExpr(
592-
privateHdrsGen,
593-
rule.GlobValue{
594-
Patterns: []string{"src/**/*.hrl"},
595-
},
596-
erlangConfig.GenerateBeamFilesMacro,
597-
))
598-
} else {
599-
if !erlangApp.PrivateHdrs.IsEmpty() {
600-
private_hdrs.SetAttr("srcs", multilineList(
601-
erlangApp.PrivateHdrs.Values(comparePaths2)))
602-
}
506+
if !erlangApp.PrivateHdrs.IsEmpty() {
507+
private_hdrs.SetAttr("srcs", multilineList(
508+
erlangApp.PrivateHdrs.Values(comparePaths2)))
603509
}
604510
if erlangConfig.Testonly {
605511
private_hdrs.SetAttr("testonly", true)
606512
}
607513
rules = append(rules, private_hdrs)
608514

609515
public_hdrs := rule.NewRule("filegroup", "public_hdrs")
610-
if erlangConfig.GenerateFewerBytecodeRules {
611-
publicHdrsGen := mutable_set.Map(erlangApp.PublicHdrs.Filter(func(v *ErlangAppFile) bool {
612-
return v.IsGenFile
613-
}), (*ErlangAppFile).path).Values(strings.Compare)
614-
public_hdrs.SetAttr("srcs", explicitPlusGlobExpr(
615-
publicHdrsGen,
616-
rule.GlobValue{
617-
Patterns: []string{"include/**/*.hrl"},
618-
},
619-
erlangConfig.GenerateBeamFilesMacro,
620-
))
621-
} else {
622-
if !erlangApp.PublicHdrs.IsEmpty() {
623-
public_hdrs.SetAttr("srcs", multilineList(
624-
erlangApp.PublicHdrs.Values(comparePaths2)))
625-
}
626-
516+
if !erlangApp.PublicHdrs.IsEmpty() {
517+
public_hdrs.SetAttr("srcs", multilineList(
518+
erlangApp.PublicHdrs.Values(comparePaths2)))
627519
}
628520
if erlangConfig.Testonly {
629521
public_hdrs.SetAttr("testonly", true)
630522
}
631523
rules = append(rules, public_hdrs)
632524

633525
priv := rule.NewRule("filegroup", "priv")
634-
if erlangConfig.GenerateFewerBytecodeRules {
635-
privGen := mutable_set.Map(erlangApp.Priv.Filter(func(v *ErlangAppFile) bool {
636-
return v.IsGenFile
637-
}), (*ErlangAppFile).path).Values(strings.Compare)
638-
priv.SetAttr("srcs", explicitPlusGlobExpr(
639-
privGen,
640-
rule.GlobValue{
641-
Patterns: []string{"priv/**/*"},
642-
},
643-
erlangConfig.GenerateBeamFilesMacro,
644-
))
645-
} else {
646-
if !erlangApp.Priv.IsEmpty() {
647-
priv.SetAttr("srcs", multilineList(
648-
erlangApp.Priv.Values(comparePaths2)))
649-
}
526+
if !erlangApp.Priv.IsEmpty() {
527+
priv.SetAttr("srcs", multilineList(
528+
erlangApp.Priv.Values(comparePaths2)))
650529
}
651530
if erlangConfig.Testonly {
652531
priv.SetAttr("testonly", true)
653532
}
654533
rules = append(rules, priv)
655534

656535
licenses := rule.NewRule("filegroup", "license_files")
657-
if erlangConfig.GenerateFewerBytecodeRules {
658-
licensesGen := mutable_set.Map(erlangApp.LicenseFiles.Filter(func(v *ErlangAppFile) bool {
659-
return v.IsGenFile
660-
}), (*ErlangAppFile).path).Values(strings.Compare)
661-
licenses.SetAttr("srcs", explicitPlusGlobExpr(
662-
licensesGen,
663-
rule.GlobValue{
664-
Patterns: []string{"LICENSE*"},
665-
},
666-
erlangConfig.GenerateBeamFilesMacro,
667-
))
668-
} else {
669-
if !erlangApp.LicenseFiles.IsEmpty() {
670-
licenses.SetAttr("srcs", multilineList(
671-
erlangApp.LicenseFiles.Values(comparePaths2)))
672-
}
536+
if !erlangApp.LicenseFiles.IsEmpty() {
537+
licenses.SetAttr("srcs", multilineList(
538+
erlangApp.LicenseFiles.Values(comparePaths2)))
673539
}
674540
if erlangConfig.Testonly {
675541
licenses.SetAttr("testonly", true)

test/gazelle/erlang_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,8 @@ var _ = Describe("an ErlangApp", func() {
407407
Expect(rules[2].Name()).To(Equal("other_beam"))
408408
Expect(rules[2].AttrString("app_name")).To(Equal(app.Name))
409409
Expect(rules[2].AttrString("erlc_opts")).To(Equal("//:erlc_opts"))
410-
Expect(strings.NewReplacer("\n", "", " ", "").Replace(
411-
build.FormatString(rules[2].Attr("srcs"))),
412-
).To(
413-
Equal("glob([\"src/**/*.erl\"],exclude=[\"src/bar.erl\",\"src/baz.erl\",\"src/xform.erl\",],)"),
410+
Expect(rules[2].AttrStrings("srcs")).To(
411+
ConsistOf("src/foo.erl"),
414412
)
415413
Expect(rules[2].AttrString("dest")).To(Equal("ebin"))
416414
Expect(rules[2].AttrStrings("beam")).To(

test/gazelle/testdata/basic_hex_tar_no_tests/BUILD.out

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ erlang_bytecode(
5050
"src/aten_emitter.erl",
5151
"src/aten_sink.erl",
5252
"src/aten_sup.erl",
53-
] + glob(["src/**/*.erl"]),
53+
],
5454
hdrs = [":public_and_private_hdrs"],
5555
app_name = "aten",
5656
dest = "ebin",
@@ -65,41 +65,30 @@ filegroup(
6565
filegroup(
6666
name = "srcs",
6767
srcs = [
68+
"src/aten.app.src",
6869
"src/aten.erl",
6970
"src/aten_app.erl",
7071
"src/aten_detect.erl",
7172
"src/aten_detector.erl",
7273
"src/aten_emitter.erl",
7374
"src/aten_sink.erl",
7475
"src/aten_sup.erl",
75-
] + glob([
76-
"src/**/*.app.src",
77-
"src/**/*.erl",
78-
]),
76+
],
7977
)
8078

81-
filegroup(
82-
name = "private_hdrs",
83-
srcs = glob(["src/**/*.hrl"]),
84-
)
79+
filegroup(name = "private_hdrs")
8580

86-
filegroup(
87-
name = "public_hdrs",
88-
srcs = glob(["include/**/*.hrl"]),
89-
)
81+
filegroup(name = "public_hdrs")
9082

91-
filegroup(
92-
name = "priv",
93-
srcs = glob(["priv/**/*"]),
94-
)
83+
filegroup(name = "priv")
9584

9685
filegroup(
9786
name = "license_files",
9887
srcs = [
9988
"LICENSE",
10089
"LICENSE-APACHE2",
10190
"LICENSE-MPL-RabbitMQ",
102-
] + glob(["LICENSE*"]),
91+
],
10392
)
10493

10594
filegroup(

test/gazelle/testdata/basic_rebar_compact_rules/BUILD.out

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ plt(
5252

5353
erlang_bytecode(
5454
name = "other_beam",
55-
srcs = glob(["src/**/*.erl"]),
55+
srcs = [
56+
"src/seshat.erl",
57+
"src/seshat_app.erl",
58+
"src/seshat_counters_server.erl",
59+
"src/seshat_sup.erl",
60+
],
5661
hdrs = [":public_and_private_hdrs"],
5762
app_name = "seshat",
5863
dest = "ebin",
@@ -62,7 +67,12 @@ erlang_bytecode(
6267
erlang_bytecode(
6368
name = "test_other_beam",
6469
testonly = True,
65-
srcs = glob(["src/**/*.erl"]),
70+
srcs = [
71+
"src/seshat.erl",
72+
"src/seshat_app.erl",
73+
"src/seshat_counters_server.erl",
74+
"src/seshat_sup.erl",
75+
],
6676
hdrs = [":public_and_private_hdrs"],
6777
app_name = "seshat",
6878
dest = "test",
@@ -100,30 +110,28 @@ erlang_bytecode(
100110

101111
filegroup(
102112
name = "srcs",
103-
srcs = glob([
104-
"src/**/*.app.src",
105-
"src/**/*.erl",
106-
]),
113+
srcs = [
114+
"src/seshat.app.src",
115+
"src/seshat.erl",
116+
"src/seshat_app.erl",
117+
"src/seshat_counters_server.erl",
118+
"src/seshat_sup.erl",
119+
],
107120
)
108121

109-
filegroup(
110-
name = "private_hdrs",
111-
srcs = glob(["src/**/*.hrl"]),
112-
)
122+
filegroup(name = "private_hdrs")
113123

114-
filegroup(
115-
name = "public_hdrs",
116-
srcs = glob(["include/**/*.hrl"]),
117-
)
124+
filegroup(name = "public_hdrs")
118125

119-
filegroup(
120-
name = "priv",
121-
srcs = glob(["priv/**/*"]),
122-
)
126+
filegroup(name = "priv")
123127

124128
filegroup(
125129
name = "license_files",
126-
srcs = glob(["LICENSE*"]),
130+
srcs = [
131+
"LICENSE",
132+
"LICENSE-APACHE2",
133+
"LICENSE-MPL-RabbitMQ",
134+
],
127135
)
128136

129137
filegroup(

0 commit comments

Comments
 (0)