diff --git a/plugin/auto/walk.go b/plugin/auto/walk.go index 4967fcff0..5a46682bd 100644 --- a/plugin/auto/walk.go +++ b/plugin/auto/walk.go @@ -44,12 +44,14 @@ func (a Auto) Walk() error { cleanPath := filepath.Clean(path) if previous, ok := seen[origin]; ok && previous != cleanPath { log.Warningf("Multiple zone files match origin %q: using %q instead of %q", origin, path, previous) + toDelete[origin] = false + return nil } seen[origin] = cleanPath if z, ok := a.Z[origin]; ok { toDelete[origin] = false - z.SetFile(path) + z.SetFile(cleanPath) return nil } diff --git a/plugin/auto/walk_test.go b/plugin/auto/walk_test.go index 5331429e3..03e519b81 100644 --- a/plugin/auto/walk_test.go +++ b/plugin/auto/walk_test.go @@ -149,6 +149,37 @@ func TestWalkWarnsForDuplicateOrigin(t *testing.T) { } } +func TestWalkKeepsFirstMatchingFileForOrigin(t *testing.T) { + dir := t.TempDir() + zone := filepath.Join(dir, "example.org.zone") + backup := filepath.Join(dir, "example.org.zone.bak-20260528") + + for _, path := range []string{zone, backup} { + if err := os.WriteFile(path, []byte(zoneContent), 0644); err != nil { + t.Fatal(err) + } + } + + a := Auto{ + loader: loader{ + directory: dir, + re: regexp.MustCompile(`(.*)\.zone`), + template: `${1}`, + }, + Zones: &Zones{}, + } + + a.Walk() + if got := a.Z["example.org."].File(); got != zone { + t.Fatalf("zone file = %q, want %q", got, zone) + } + + a.Walk() + if got := a.Z["example.org."].File(); got != zone { + t.Fatalf("zone file after reload = %q, want %q", got, zone) + } +} + func createFiles(t *testing.T) (string, error) { t.Helper() dir := t.TempDir()