From 29c6fdd33ef8984e1643dbdda00ebab30f6c1728 Mon Sep 17 00:00:00 2001 From: Antoine Wendlinger Date: Fri, 30 Jan 2026 10:35:56 +0100 Subject: [PATCH 1/2] fix: adding an external_test when an internal one exists And vice versa. When an existing BUILD file exists with an internal test rule, and a new external test file is added, puku silently ignores the new test file. Code-wise, `generate.updater.allocateSources` does detect the new file and creates a new external test rule, but gives it the same name as the existing internal test rule, which results in `generate.updater.updateSources` to ignore the new rule. This fixes the issue by looking at existing rule names, and adding `external_` to the new test rule name if a rule with the desired name already exists. --- generate/generate.go | 24 ++++++++++++++++++++++-- generate/generate_test.go | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/generate/generate.go b/generate/generate.go index 8ad5a7d..e4ea89a 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -439,6 +439,11 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma return nil, err } + existingRuleNames := map[string]struct{}{} + for _, r := range rules { + existingRuleNames[r.Name()] = struct{}{} + } + var newRules []*edit.Rule for _, src := range unallocated { importedFile := sources[src] @@ -465,7 +470,9 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma } } if rule == nil { - name := filepath.Base(pkgDir) + baseName := filepath.Base(pkgDir) + name := baseName + isExternal_ := importedFile.IsExternal(filepath.Join(u.plzConf.ImportPath(), pkgDir)) kind := "go_library" if importedFile.IsTest() { name += "_test" @@ -475,11 +482,24 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma kind = "go_binary" name = "main" } + _, nameExists := existingRuleNames[name] + if nameExists { + if importedFile.IsTest() { + if isExternal_ { + name = fmt.Sprintf("%s_external_test", baseName) + } else { + name = fmt.Sprintf("%s_internal_test", baseName) + } + } else { + return nil, fmt.Errorf("rule with name %s already exists in %s", name, pkgDir) + } + } rule = edit.NewRule(edit.NewRuleExpr(kind, name), kinds.DefaultKinds[kind], pkgDir) - if importedFile.IsExternal(filepath.Join(u.plzConf.ImportPath(), pkgDir)) { + if isExternal_ { setExternal(rule) } newRules = append(newRules, rule) + existingRuleNames[name] = struct{}{} } rule.AddSrc(src) diff --git a/generate/generate_test.go b/generate/generate_test.go index 9dcb924..c7456d7 100644 --- a/generate/generate_test.go +++ b/generate/generate_test.go @@ -51,7 +51,7 @@ func TestAllocateSources(t *testing.T) { require.NoError(t, err) require.Len(t, newRules, 1) - assert.Equal(t, "foo_test", newRules[0].Name()) + assert.Equal(t, "foo_external_test", newRules[0].Name()) assert.ElementsMatch(t, []string{"external_test.go"}, mustGetSources(t, u, newRules[0])) assert.ElementsMatch(t, []string{"foo.go", "bar.go"}, mustGetSources(t, u, rules[0])) From 123c307507900d266db3a3e048bb2f52b165cb61 Mon Sep 17 00:00:00 2001 From: Antoine Wendlinger Date: Fri, 30 Jan 2026 11:36:26 +0100 Subject: [PATCH 2/2] fix lint --- generate/generate.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/generate/generate.go b/generate/generate.go index e4ea89a..00edb79 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -472,7 +472,7 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma if rule == nil { baseName := filepath.Base(pkgDir) name := baseName - isExternal_ := importedFile.IsExternal(filepath.Join(u.plzConf.ImportPath(), pkgDir)) + isExternal := importedFile.IsExternal(filepath.Join(u.plzConf.ImportPath(), pkgDir)) kind := "go_library" if importedFile.IsTest() { name += "_test" @@ -485,7 +485,7 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma _, nameExists := existingRuleNames[name] if nameExists { if importedFile.IsTest() { - if isExternal_ { + if isExternal { name = fmt.Sprintf("%s_external_test", baseName) } else { name = fmt.Sprintf("%s_internal_test", baseName) @@ -495,7 +495,7 @@ func (u *updater) allocateSources(conf *config.Config, pkgDir string, sources ma } } rule = edit.NewRule(edit.NewRuleExpr(kind, name), kinds.DefaultKinds[kind], pkgDir) - if isExternal_ { + if isExternal { setExternal(rule) } newRules = append(newRules, rule)