Handle concatenated lists when checking if sources are already used in the BUILD file.
PiperOrigin-RevId: 217188751
diff --git a/ts_auto_deps/updater/updater.go b/ts_auto_deps/updater/updater.go
index 945420c..65d9a28 100644
--- a/ts_auto_deps/updater/updater.go
+++ b/ts_auto_deps/updater/updater.go
@@ -784,14 +784,13 @@
// attrName from the given set of sources.
func removeSourcesUsed(bld *build.File, ruleKind, attrName string, srcs srcSet) {
for _, rule := range buildRules(bld, ruleKind) {
- ruleSrcs := rule.AttrStrings(attrName)
- for _, s := range ruleSrcs {
+ for s, _ := range srcs {
+ pkg := filepath.Dir(bld.Path)
+ // Handles ":foo.ts" references, and concatenated lists [foo.ts] + [bar.ts]
// TODO(martinprobst): What to do about sources that don't seem to exist?
- if strings.HasPrefix(s, ":") {
- s = s[1:] // Recognize ":foo.ts" style references to sources
+ if edit.ListFind(rule.Attr(attrName), s, pkg) != nil {
+ delete(srcs, s)
}
- // Might be generated srcs.
- delete(srcs, s)
}
}
}
diff --git a/ts_auto_deps/updater/updater_test.go b/ts_auto_deps/updater/updater_test.go
index c8d1e9b..d243ffb 100644
--- a/ts_auto_deps/updater/updater_test.go
+++ b/ts_auto_deps/updater/updater_test.go
@@ -324,6 +324,81 @@
}
}
+func TestRemoveSourcesUsed(t *testing.T) {
+ tests := []struct {
+ name string
+ buildFile string
+ ruleKind string
+ attrName string
+ srcs srcSet
+ expectedSrcs srcSet
+ }{
+ {
+ name: "RemovesSources",
+ buildFile: `ts_library(name = "lib", srcs = ["foo.ts", "bar.ts"])`,
+ ruleKind: "ts_library",
+ attrName: "srcs",
+ srcs: map[string]bool{"foo.ts": true},
+ expectedSrcs: map[string]bool{},
+ },
+ {
+ name: "WrongRuleKind",
+ buildFile: `ts_library(name = "lib", srcs = ["foo.ts", "bar.ts"])`,
+ ruleKind: "ng_module",
+ attrName: "srcs",
+ srcs: map[string]bool{"foo.ts": true},
+ expectedSrcs: map[string]bool{"foo.ts": true},
+ },
+ {
+ name: "WrongAttrName",
+ buildFile: `ts_library(name = "lib", srcs = ["foo.ts", "bar.ts"])`,
+ ruleKind: "ts_library",
+ attrName: "deps",
+ srcs: map[string]bool{"foo.ts": true},
+ expectedSrcs: map[string]bool{"foo.ts": true},
+ },
+ {
+ name: "MultipleRules",
+ buildFile: `ts_library(name = "lib", srcs = ["foo.ts"])
+ ts_library(name = "lib2", srcs = ["bar.ts"])`,
+ ruleKind: "ts_library",
+ attrName: "srcs",
+ srcs: map[string]bool{"foo.ts": true, "bar.ts": true},
+ expectedSrcs: map[string]bool{},
+ },
+ {
+ name: "ConcatenatedLists",
+ buildFile: `ts_library(name = "lib", srcs = ["foo.ts"] + ["bar.ts"])`,
+ ruleKind: "ts_library",
+ attrName: "srcs",
+ srcs: map[string]bool{"foo.ts": true, "bar.ts": true},
+ expectedSrcs: map[string]bool{},
+ },
+ {
+ name: "ColonReferences",
+ buildFile: `ts_library(name = "lib", srcs = [":foo.ts", "bar.ts"])`,
+ ruleKind: "ts_library",
+ attrName: "srcs",
+ srcs: map[string]bool{"foo.ts": true},
+ expectedSrcs: map[string]bool{},
+ },
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ bld, err := build.ParseBuild("foo/bar/BUILD",
+ []byte(test.buildFile))
+ if err != nil {
+ t.Fatalf("parse failure: %v", err)
+ }
+
+ removeSourcesUsed(bld, test.ruleKind, test.attrName, test.srcs)
+ if !reflect.DeepEqual(test.srcs, test.expectedSrcs) {
+ t.Errorf("expected removeSourcesUsed() = %v, expected %v", test.srcs, test.expectedSrcs)
+ }
+ })
+ }
+}
+
func TestUpdateWebAssets(t *testing.T) {
ctx := context.Background()
bld, err := build.ParseBuild("foo/bar/BUILD",