Skip to content

Commit b3280ef

Browse files
committed
review(round2): dedup foreign-region pairs and name override in errors
Two adversarial-review fixes for the foreign-regions feature: - EffectiveForeignRegions now dedups identical marker pairs. A pair declared both top-level and on a matching override (or repeated in a list) previously produced duplicate protected ranges and duplicate MDS073 diagnostics, because the check path (Run) only dedups diagnostics when a repo-scoped rule is enabled and MDS073 is not one. The linear scan keeps the common no-region path allocation-free. - checkForeignRegionList takes a location label so a malformed marker pair on an override reports overrides[i].foreign-regions[j] instead of an ambiguous top-level foreign-regions[j]. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwKxDxhjkTQPPkBrNhsrNG
1 parent 162105b commit b3280ef

2 files changed

Lines changed: 80 additions & 10 deletions

File tree

internal/config/foreignregion.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,43 @@ func EffectiveForeignRegions(cfg *Config, filePath string) []ForeignRegion {
1616
return nil
1717
}
1818
var out []ForeignRegion
19-
out = append(out, cfg.ForeignRegions...)
19+
for _, r := range cfg.ForeignRegions {
20+
if !containsForeignRegion(out, r) {
21+
out = append(out, r)
22+
}
23+
}
2024
for _, o := range cfg.Overrides {
2125
if len(o.ForeignRegions) == 0 {
2226
continue
2327
}
24-
if matchesAny(o.Patterns(), filePath) {
25-
out = append(out, o.ForeignRegions...)
28+
if !matchesAny(o.Patterns(), filePath) {
29+
continue
30+
}
31+
for _, r := range o.ForeignRegions {
32+
if !containsForeignRegion(out, r) {
33+
out = append(out, r)
34+
}
2635
}
2736
}
2837
return out
2938
}
3039

40+
// containsForeignRegion reports whether list already holds an identical
41+
// marker pair. EffectiveForeignRegions dedups with it so a pair declared
42+
// both top-level and on a matching override (or repeated within a list)
43+
// contributes one protected span and one MDS073 diagnostic, not two —
44+
// the check path does not otherwise dedup per-file diagnostics. The
45+
// marker-pair lists are short (a handful of entries), so the linear scan
46+
// is cheaper than allocating a set on this per-file hot path.
47+
func containsForeignRegion(list []ForeignRegion, r ForeignRegion) bool {
48+
for _, e := range list {
49+
if e == r {
50+
return true
51+
}
52+
}
53+
return false
54+
}
55+
3156
// validateConfigSemantics runs the post-parse structural checks that
3257
// depend on the fully-decoded config: kind graph validity and
3358
// foreign-region marker-pair well-formedness. Kept together so
@@ -44,31 +69,36 @@ func validateConfigSemantics(cfg *Config) error {
4469
// (the scanner could never tell which line opens and which closes a
4570
// region). It checks the top-level list and every override's list.
4671
func validateForeignRegions(cfg *Config) error {
47-
if err := checkForeignRegionList(cfg.ForeignRegions); err != nil {
72+
if err := checkForeignRegionList("foreign-regions", cfg.ForeignRegions); err != nil {
4873
return err
4974
}
5075
for i := range cfg.Overrides {
51-
if err := checkForeignRegionList(cfg.Overrides[i].ForeignRegions); err != nil {
76+
label := fmt.Sprintf("overrides[%d].foreign-regions", i)
77+
if err := checkForeignRegionList(label, cfg.Overrides[i].ForeignRegions); err != nil {
5278
return err
5379
}
5480
}
5581
return nil
5682
}
5783

58-
func checkForeignRegionList(regions []ForeignRegion) error {
84+
// checkForeignRegionList validates one marker-pair list. label names the
85+
// list's location in the config ("foreign-regions" or
86+
// "overrides[i].foreign-regions") so an error points at the offending
87+
// override rather than an ambiguous top-level index.
88+
func checkForeignRegionList(label string, regions []ForeignRegion) error {
5989
for i, r := range regions {
6090
start := strings.TrimSpace(r.Start)
6191
end := strings.TrimSpace(r.End)
6292
if start == "" {
63-
return fmt.Errorf("foreign-regions[%d]: start marker must not be empty", i)
93+
return fmt.Errorf("%s[%d]: start marker must not be empty", label, i)
6494
}
6595
if end == "" {
66-
return fmt.Errorf("foreign-regions[%d]: end marker must not be empty", i)
96+
return fmt.Errorf("%s[%d]: end marker must not be empty", label, i)
6797
}
6898
if start == end {
6999
return fmt.Errorf(
70-
"foreign-regions[%d]: start and end markers must differ (both %q)",
71-
i, start)
100+
"%s[%d]: start and end markers must differ (both %q)",
101+
label, i, start)
72102
}
73103
}
74104
return nil

internal/config/foreignregion_more_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,46 @@ func TestParseForeignRegionsOverrideInvalidRejected(t *testing.T) {
5252
assert.Contains(t, err.Error(), "start marker must not be empty")
5353
}
5454

55+
// TestParseForeignRegionsOverrideErrorNamesOverride reports which
56+
// override carries a malformed marker pair, not an ambiguous top-level
57+
// index.
58+
func TestParseForeignRegionsOverrideErrorNamesOverride(t *testing.T) {
59+
yml := `overrides:
60+
- glob: ["README.md"]
61+
- glob: ["AGENTS.md"]
62+
foreign-regions:
63+
- start: "<!-- gen:start -->"
64+
end: ""
65+
`
66+
_, err := ParseBytes([]byte(yml))
67+
require.Error(t, err)
68+
assert.Contains(t, err.Error(), "overrides[1].foreign-regions[0]")
69+
assert.Contains(t, err.Error(), "end marker must not be empty")
70+
}
71+
72+
// TestEffectiveForeignRegionsDedupes collapses a marker pair declared
73+
// both top-level and on a matching override into a single entry, so the
74+
// check path (which does not dedup per-file MDS073 diagnostics) does not
75+
// double-report or double-protect it.
76+
func TestEffectiveForeignRegionsDedupes(t *testing.T) {
77+
cfg := &Config{
78+
ForeignRegions: []ForeignRegion{{Start: "<!-- a -->", End: "<!-- b -->"}},
79+
Overrides: []Override{
80+
{
81+
Glob: []string{"AGENTS.md"},
82+
ForeignRegions: []ForeignRegion{
83+
{Start: "<!-- a -->", End: "<!-- b -->"}, // duplicate of top-level
84+
{Start: "<!-- c -->", End: "<!-- d -->"}, // distinct
85+
},
86+
},
87+
},
88+
}
89+
got := EffectiveForeignRegions(cfg, "AGENTS.md")
90+
require.Len(t, got, 2)
91+
assert.Equal(t, ForeignRegion{Start: "<!-- a -->", End: "<!-- b -->"}, got[0])
92+
assert.Equal(t, ForeignRegion{Start: "<!-- c -->", End: "<!-- d -->"}, got[1])
93+
}
94+
5595
// TestCopyForeignRegionsNil returns nil for a nil input.
5696
func TestCopyForeignRegionsNil(t *testing.T) {
5797
assert.Nil(t, copyForeignRegions(nil))

0 commit comments

Comments
 (0)