Skip to content

Commit b57df30

Browse files
authored
Silence config warning on empty rules when policies are defined (#3972)
1 parent f4175e4 commit b57df30

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

private/bufpkg/bufcheck/client.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (c *client) Lint(
124124
lintOptions.pluginConfigs,
125125
nil, // policyConfig.
126126
lintOptions.relatedCheckConfigs,
127+
len(lintOptions.policyConfigs) > 0, // hasPolicyConfigs.
127128
)
128129
if err != nil {
129130
return err
@@ -150,7 +151,8 @@ func (c *client) Lint(
150151
policyLintConfig,
151152
pluginConfigs,
152153
policyConfig,
153-
nil, // relatedCheckConfigs.
154+
nil, // relatedCheckConfigs.
155+
true, // hasPolicyConfigs.
154156
)
155157
if err != nil {
156158
return err
@@ -177,6 +179,7 @@ func (c *client) lint(
177179
pluginConfigs []bufconfig.PluginConfig,
178180
policyConfig bufconfig.PolicyConfig,
179181
relatedCheckConfigs []bufconfig.CheckConfig,
182+
hasPolicyConfigs bool,
180183
) ([]*annotation, error) {
181184
if lintConfig.Disabled() {
182185
return nil, nil
@@ -195,7 +198,11 @@ func (c *client) lint(
195198
if err != nil {
196199
return nil, err
197200
}
198-
logRulesConfig(c.logger, config.rulesConfig)
201+
configName := bufconfig.DefaultBufYAMLFileName
202+
if policyConfig != nil {
203+
configName = policyConfig.Name()
204+
}
205+
logRulesConfig(c.logger, configName, config.rulesConfig, hasPolicyConfigs)
199206
files, err := descriptor.FileDescriptorsForProtoFileDescriptors(imageToProtoFileDescriptors(image))
200207
if err != nil {
201208
// An Image may be invalid if it does not contain all of the required dependencies.
@@ -254,6 +261,7 @@ func (c *client) Breaking(
254261
nil, // policyConfig.
255262
breakingOptions.excludeImports,
256263
breakingOptions.relatedCheckConfigs,
264+
len(breakingOptions.policyConfigs) > 0, // hasPolicyConfigs.
257265
)
258266
if err != nil {
259267
return err
@@ -282,7 +290,8 @@ func (c *client) Breaking(
282290
pluginConfigs,
283291
policyConfig,
284292
breakingOptions.excludeImports,
285-
nil, // relatedCheckConfigs.
293+
nil, // relatedCheckConfigs.
294+
true, // hasPolicyConfigs.
286295
)
287296
if err != nil {
288297
return err
@@ -311,6 +320,7 @@ func (c *client) breaking(
311320
policyConfig bufconfig.PolicyConfig,
312321
excludeImports bool,
313322
relatedCheckConfigs []bufconfig.CheckConfig,
323+
hasPolicyConfigs bool,
314324
) ([]*annotation, error) {
315325
if breakingConfig.Disabled() {
316326
return nil, nil
@@ -335,7 +345,11 @@ func (c *client) breaking(
335345
if err != nil {
336346
return nil, err
337347
}
338-
logRulesConfig(c.logger, config.rulesConfig)
348+
configName := bufconfig.DefaultBufYAMLFileName
349+
if policyConfig != nil {
350+
configName = policyConfig.Name()
351+
}
352+
logRulesConfig(c.logger, configName, config.rulesConfig, hasPolicyConfigs)
339353
fileDescriptors, err := descriptor.FileDescriptorsForProtoFileDescriptors(imageToProtoFileDescriptors(image))
340354
if err != nil {
341355
// An Image may be invalid if it does not contain all of the required dependencies.
@@ -402,6 +416,7 @@ func (c *client) ConfiguredRules(
402416
if err != nil {
403417
return nil, err
404418
}
419+
logRulesConfig(c.logger, "", rulesConfig, len(configuredRulesOptions.policyConfigs) > 0)
405420
allRules := rulesForRuleIDs(rules, rulesConfig.RuleIDs)
406421
policies, err := c.getPolicies(ctx, configuredRulesOptions.policyConfigs)
407422
if err != nil {
@@ -436,7 +451,6 @@ func (c *client) ConfiguredRules(
436451
}
437452
allRules = append(allRules, rulesForRuleIDs(policyRules, policyRulesConfig.RuleIDs)...)
438453
}
439-
logRulesConfig(c.logger, rulesConfig)
440454
return allRules, nil
441455
}
442456

private/bufpkg/bufcheck/rules_config.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func rulesConfigForCheckConfig(
4747
)
4848
}
4949

50-
func logRulesConfig(logger *slog.Logger, rulesConfig *rulesConfig) {
50+
func logRulesConfig(logger *slog.Logger, configName string, rulesConfig *rulesConfig, hasPolicyConfigs bool) {
5151
logger.Debug("rulesConfig", slog.Any("ruleIDs", rulesConfig.RuleIDs))
52-
if len(rulesConfig.RuleIDs) == 0 {
53-
logger.Warn("No " + rulesConfig.RuleType.String() + " rules are configured.")
52+
if len(rulesConfig.RuleIDs) == 0 && !hasPolicyConfigs {
53+
logger.Warn("No " + rulesConfig.RuleType.String() + " rules are configured in " + configName + ".")
5454
}
55-
warnReferencedDeprecatedIDs(logger, rulesConfig)
56-
warnUnusedPlugins(logger, rulesConfig)
55+
warnReferencedDeprecatedIDs(logger, configName, rulesConfig)
56+
warnUnusedPlugins(logger, configName, rulesConfig)
5757
}
5858

5959
type rulesConfig struct {
@@ -344,28 +344,30 @@ func newRulesConfig(
344344

345345
// *** JUST USED WITHIN THIS FILE ***
346346

347-
func warnReferencedDeprecatedIDs(logger *slog.Logger, rulesConfig *rulesConfig) {
347+
func warnReferencedDeprecatedIDs(logger *slog.Logger, configName string, rulesConfig *rulesConfig) {
348348
warnReferencedDeprecatedIDsForIDType(
349349
logger,
350+
configName,
350351
rulesConfig.ReferencedDeprecatedRuleIDToReplacementIDs,
351352
"Rule",
352353
"rules",
353354
)
354355
warnReferencedDeprecatedIDsForIDType(
355356
logger,
357+
configName,
356358
rulesConfig.ReferencedDeprecatedCategoryIDToReplacementIDs,
357359
"Category",
358360
"categories",
359361
)
360362
}
361363

362-
func warnUnusedPlugins(logger *slog.Logger, rulesConfig *rulesConfig) {
364+
func warnUnusedPlugins(logger *slog.Logger, configName string, rulesConfig *rulesConfig) {
363365
if len(rulesConfig.UnusedPluginNameToRuleIDs) == 0 {
364366
return
365367
}
366368
unusedPluginNames := xslices.MapKeysToSortedSlice(rulesConfig.UnusedPluginNameToRuleIDs)
367369
var sb strings.Builder
368-
_, _ = sb.WriteString("Your buf.yaml has plugins added which have no rules configured:\n\n")
370+
_, _ = sb.WriteString("Your " + configName + " has plugins added which have no rules configured:\n\n")
369371
for _, unusedPluginName := range unusedPluginNames {
370372
_, _ = sb.WriteString("\t - ")
371373
_, _ = sb.WriteString(unusedPluginName)
@@ -390,6 +392,7 @@ func warnUnusedPlugins(logger *slog.Logger, rulesConfig *rulesConfig) {
390392

391393
func warnReferencedDeprecatedIDsForIDType(
392394
logger *slog.Logger,
395+
configName string,
393396
referencedDeprecatedIDToReplacementIDs map[string]map[string]struct{},
394397
capitalizedIDType string,
395398
pluralIDType string,
@@ -406,24 +409,26 @@ func warnReferencedDeprecatedIDsForIDType(
406409
}
407410
var specialCallout string
408411
if deprecatedID == "DEFAULT" {
409-
specialCallout = `
412+
specialCallout = fmt.Sprintf(`
410413
411414
The concept of a default rule has been introduced. A default rule is a rule that will be run
412-
if no rules are explicitly configured in your buf.yaml. Run buf config ls-lint-rules or
415+
if no rules are explicitly configured in your %s. Run buf config ls-lint-rules or
413416
buf config ls-breaking-rules to see which rules are defaults. With this introduction, having a category
414417
also named DEFAULT is confusing, as while it happens that all the rules in the DEFAULT category
415418
are also default rules, the name has become overloaded.
416-
`
419+
`, configName)
417420
}
418421
logger.Warn(
419422
fmt.Sprintf(
420-
"%s %s referenced in your buf.yaml is deprecated.%s%s\n\tAs with all buf changes, this change is backwards-compatible: %s will continue to work.\n\tWe recommend replacing %s in your buf.yaml, but no action is immediately necessary.",
423+
"%s %s referenced in your %s is deprecated.%s%s\n\tAs with all buf changes, this change is backwards-compatible: %s will continue to work.\n\tWe recommend replacing %s in your %s, but no action is immediately necessary.",
421424
capitalizedIDType,
422425
deprecatedID,
426+
configName,
423427
replaceString,
424428
specialCallout,
425429
deprecatedID,
426430
deprecatedID,
431+
configName,
427432
),
428433
)
429434
}

0 commit comments

Comments
 (0)