@@ -17,8 +17,10 @@ package bufcheck
1717import (
1818 "bytes"
1919 "context"
20+ "errors"
2021 "fmt"
2122 "io"
23+ "io/fs"
2224 "log/slog"
2325 "strings"
2426
@@ -386,7 +388,7 @@ func (c *client) ConfiguredRules(
386388 for _ , option := range options {
387389 option .applyToConfiguredRules (configuredRulesOptions )
388390 }
389- allRules , allCategories , err := c .allRulesAndCategories (
391+ rules , categories , err := c .allRulesAndCategories (
390392 ctx ,
391393 checkConfig .FileVersion (),
392394 configuredRulesOptions .pluginConfigs ,
@@ -396,12 +398,46 @@ func (c *client) ConfiguredRules(
396398 if err != nil {
397399 return nil , err
398400 }
399- rulesConfig , err := rulesConfigForCheckConfig (checkConfig , allRules , allCategories , ruleType , configuredRulesOptions .relatedCheckConfigs )
401+ rulesConfig , err := rulesConfigForCheckConfig (checkConfig , rules , categories , ruleType , configuredRulesOptions .relatedCheckConfigs )
400402 if err != nil {
401403 return nil , err
402404 }
405+ allRules := rulesForRuleIDs (rules , rulesConfig .RuleIDs )
406+ policies , err := c .getPolicies (ctx , configuredRulesOptions .policyConfigs )
407+ if err != nil {
408+ return nil , err
409+ }
410+ for index , policy := range policies {
411+ policyConfig := configuredRulesOptions .policyConfigs [index ]
412+ pluginConfigs , err := policyToBufConfigPluginConfigs (policy )
413+ if err != nil {
414+ return nil , err
415+ }
416+ // Load the check config for the rule type.
417+ var policyCheckConfig bufconfig.CheckConfig
418+ switch ruleType {
419+ case check .RuleTypeLint :
420+ policyCheckConfig , err = policyToBufConfigLintConfig (policy , policyConfig )
421+ case check .RuleTypeBreaking :
422+ policyCheckConfig , err = policyToBufConfigBreakingConfig (policy , policyConfig )
423+ default :
424+ return nil , fmt .Errorf ("unknown check.RuleType: %v" , ruleType )
425+ }
426+ if err != nil {
427+ return nil , err
428+ }
429+ policyRules , policyCategories , err := c .allRulesAndCategories (ctx , policyCheckConfig .FileVersion (), pluginConfigs , policyConfig , false )
430+ if err != nil {
431+ return nil , err
432+ }
433+ policyRulesConfig , err := rulesConfigForCheckConfig (policyCheckConfig , policyRules , policyCategories , ruleType , nil )
434+ if err != nil {
435+ return nil , err
436+ }
437+ allRules = append (allRules , rulesForRuleIDs (policyRules , policyRulesConfig .RuleIDs )... )
438+ }
403439 logRulesConfig (c .logger , rulesConfig )
404- return rulesForRuleIDs ( allRules , rulesConfig . RuleIDs ) , nil
440+ return allRules , nil
405441}
406442
407443func (c * client ) AllRules (
@@ -420,6 +456,22 @@ func (c *client) AllRules(
420456 if err != nil {
421457 return nil , err
422458 }
459+ policies , err := c .getPolicies (ctx , allRulesOptions .policyConfigs )
460+ if err != nil {
461+ return nil , err
462+ }
463+ for index , policy := range policies {
464+ policyConfig := allRulesOptions .policyConfigs [index ]
465+ pluginConfigs , err := policyToBufConfigPluginConfigs (policy )
466+ if err != nil {
467+ return nil , err
468+ }
469+ policyRules , _ , err := c .allRulesAndCategories (ctx , fileVersion , pluginConfigs , policyConfig , false )
470+ if err != nil {
471+ return nil , err
472+ }
473+ rules = append (rules , policyRules ... )
474+ }
423475 return rulesForType (rules , ruleType ), nil
424476}
425477
@@ -580,6 +632,12 @@ func (c *client) getPlugins(ctx context.Context, pluginConfigs []bufconfig.Plugi
580632 pluginRefs := xslices .IndexedToValues (indexedPluginRefs )
581633 pluginKeys , err := pluginKeyProvider .GetPluginKeysForPluginRefs (ctx , pluginRefs , bufplugin .DigestTypeP1 )
582634 if err != nil {
635+ if errors .Is (err , fs .ErrNotExist ) {
636+ if policyConfig != nil {
637+ return nil , fmt .Errorf ("unable to resolve plugins for policy %q: %w" , policyConfig .Name (), err )
638+ }
639+ return nil , fmt .Errorf ("unable to resolve plugins: %w" , err )
640+ }
583641 return nil , err
584642 }
585643 pluginDatas , err := pluginDataProvider .GetPluginDatasForPluginKeys (ctx , pluginKeys )
0 commit comments