@@ -55,6 +55,9 @@ export class GuardService implements SettingsConfigurable, Closeable {
5555 // Track custom messages from rules files
5656 private readonly ruleCustomMessages = new Map < string , string > ( ) ;
5757
58+ // Cache loaded rules
59+ private enabledRules : GuardRule [ ] = [ ] ;
60+
5861 // Validation queuing for concurrent requests
5962 private readonly validationQueue : ValidationQueueEntry [ ] = [ ] ;
6063 private readonly activeValidations = new Map < string , Promise < GuardViolation [ ] > > ( ) ;
@@ -77,6 +80,15 @@ export class GuardService implements SettingsConfigurable, Closeable {
7780
7881 // Initialize rule configuration with current settings
7982 this . ruleConfiguration . updateFromSettings ( this . settings ) ;
83+
84+ // Load initial rules
85+ this . getEnabledRulesByConfiguration ( )
86+ . then ( ( rules ) => {
87+ this . enabledRules = rules ;
88+ } )
89+ . catch ( ( error ) => {
90+ this . log . error ( `Failed to load initial rules: ${ extractErrorMessage ( error ) } ` ) ;
91+ } ) ;
8092 }
8193
8294 /**
@@ -91,6 +103,15 @@ export class GuardService implements SettingsConfigurable, Closeable {
91103
92104 this . settings = settingsManager . getCurrentSettings ( ) . diagnostics . cfnGuard ;
93105
106+ // Load rules with current settings
107+ this . getEnabledRulesByConfiguration ( )
108+ . then ( ( rules ) => {
109+ this . enabledRules = rules ;
110+ } )
111+ . catch ( ( error ) => {
112+ this . log . error ( `Failed to load rules during configuration: ${ extractErrorMessage ( error ) } ` ) ;
113+ } ) ;
114+
94115 // Subscribe to diagnostics settings changes
95116 this . settingsSubscription = settingsManager . subscribe ( 'diagnostics' , ( newDiagnosticsSettings ) => {
96117 this . onSettingsChanged ( newDiagnosticsSettings . cfnGuard ) ;
@@ -112,6 +133,17 @@ export class GuardService implements SettingsConfigurable, Closeable {
112133 const rulesFileChanged = previousSettings . rulesFile !== newSettings . rulesFile ;
113134
114135 if ( packListChanged || rulesFileChanged ) {
136+ // Clear maps only when rule configuration actually changes
137+ this . ruleToPacksMap . clear ( ) ;
138+ this . ruleCustomMessages . clear ( ) ;
139+ // Preload rules with new settings
140+ this . getEnabledRulesByConfiguration ( )
141+ . then ( ( rules ) => {
142+ this . enabledRules = rules ;
143+ } )
144+ . catch ( ( error ) => {
145+ this . log . error ( `Failed to preload rules after settings change: ${ extractErrorMessage ( error ) } ` ) ;
146+ } ) ;
115147 this . revalidateAllDocuments ( ) ;
116148 }
117149 }
@@ -182,15 +214,13 @@ export class GuardService implements SettingsConfigurable, Closeable {
182214 // Continue with validation but log the issues
183215 }
184216
185- const enabledRules = await this . getEnabledRulesByConfiguration ( ) ;
186-
187- if ( enabledRules . length === 0 ) {
217+ if ( this . enabledRules . length === 0 ) {
188218 this . publishDiagnostics ( uri , [ ] ) ;
189219 return ;
190220 }
191221
192222 // Execute Guard validation with queuing for concurrent requests
193- const violations = await this . queueValidation ( uri , content , enabledRules ) ;
223+ const violations = await this . queueValidation ( uri , content , this . enabledRules ) ;
194224
195225 // Convert violations to LSP diagnostics
196226 const diagnostics = this . convertViolationsToDiagnostics ( uri , violations ) ;
@@ -443,7 +473,7 @@ export class GuardService implements SettingsConfigurable, Closeable {
443473 /**
444474 * Process the validation queue
445475 */
446- private async processValidationQueue ( ) : Promise < void > {
476+ private processValidationQueue ( ) : void {
447477 if ( this . isProcessingQueue || this . validationQueue . length === 0 ) {
448478 return ;
449479 }
@@ -468,10 +498,10 @@ export class GuardService implements SettingsConfigurable, Closeable {
468498 continue ;
469499 }
470500
471- const enabledRules = await this . getEnabledRulesByConfiguration ( ) ;
472-
473501 // Execute the validation
474- this . executeValidation ( entry . uri , entry . content , enabledRules ) . then ( entry . resolve ) . catch ( entry . reject ) ;
502+ this . executeValidation ( entry . uri , entry . content , this . enabledRules )
503+ . then ( entry . resolve )
504+ . catch ( entry . reject ) ;
475505 }
476506 } finally {
477507 this . isProcessingQueue = false ;
@@ -521,12 +551,6 @@ export class GuardService implements SettingsConfigurable, Closeable {
521551 private async getEnabledRulesByConfiguration ( ) : Promise < GuardRule [ ] > {
522552 const enabledRules : GuardRule [ ] = [ ] ;
523553
524- // Track which packs each rule comes from for proper reporting
525- this . ruleToPacksMap . clear ( ) ;
526-
527- // Clear custom messages when getting new rules
528- this . ruleCustomMessages . clear ( ) ;
529-
530554 // If rulesFile is specified, load rules from file
531555 if ( this . settings . rulesFile ) {
532556 try {
0 commit comments