@@ -46,8 +46,6 @@ const extGeneratedRules = "__starzelle_generated_rules"
4646const extGeneratedRulesPkg = "__starzelle_generated_rules_pkg"
4747
4848func (re * GazelleHost ) importsGenerateRules (cfg * BUILDConfig , c * config.Config , f * rule.File ) gazelleLanguage.GenerateResult {
49- BazelLog .Debugf ("Imports.GenerateRules(%s): //%s" , GazelleLanguageName , f .Pkg )
50-
5149 regularFiles , err := common .GetSourceRegularFiles (f .Pkg )
5250 if err != nil {
5351 BazelLog .Fatalf ("Error getting regular files for %s: %v" , f .Pkg , err )
@@ -61,58 +59,62 @@ func (re *GazelleHost) importsGenerateRules(cfg *BUILDConfig, c *config.Config,
6159
6260 // The RegularFiles are processed by the GazelleHost and passed to plugins.
6361 RegularFiles : regularFiles ,
64-
65- // Subdirs and GenFiles are not used by the GazelleHost, just pass empty slices.
66- Subdirs : []string {},
67- GenFiles : []string {},
68-
69- // OtherEmpty and OtherGen are not used by the GazelleHost, just pass empty slices.
70- OtherEmpty : []* rule.Rule {},
71- OtherGen : []* rule.Rule {},
7262 })
7363}
7464
7565// Determine what rule (r) outputs which can be imported.
7666func (re * GazelleHost ) Imports (c * config.Config , r * rule.Rule , f * rule.File ) []resolve.ImportSpec {
77- BazelLog .Debugf ("Imports(%s): //%s:%s" , GazelleLanguageName , f .Pkg , r .Name ())
67+ BazelLog .Tracef ("Imports(%s): //%s:%s" , GazelleLanguageName , f .Pkg , r .Name ())
68+
69+ // This rule was generated by this gazelle extension.
70+ if declaration := r .PrivateAttr (targetDeclarationKey ); declaration != nil {
71+ BazelLog .Debugf ("Imports(%s): //%s:%s (generated %s)" , GazelleLanguageName , f .Pkg , r .Name (), r .Kind ())
72+ return symbolToImportSpecList (declaration .(plugin.TargetDeclaration ).Symbols )
73+ }
7874
7975 cfg := getBUILDConfig (c , f .Pkg )
8076
81- // If GenerateRules() was not run by gazelle
82- if ! cfg .generated {
83- // With GazelleHost the Imports() functionality is provided at rule generation time, not
84- // a separate Import-extraction phase.
85- //
86- // When running partial generations this means we must manually invoke GenerateRules() if it
87- // was not invoked by gazelle as part of the partial run.
88- if c .Exts [extGeneratedRulesPkg ] != cfg .rel {
89- c .Exts [extGeneratedRules ] = re .importsGenerateRules (cfg , c , f )
90- c .Exts [extGeneratedRulesPkg ] = cfg .rel
91- }
77+ // Generation was done for this package, but this rule was not one of them.
78+ if cfg .generated {
79+ return nil
80+ }
9281
93- // Find this rule in the host-generated rules
94- genResult := c .Exts [extGeneratedRules ].(gazelleLanguage.GenerateResult )
95- for _ , genRule := range genResult .Gen {
96- if genRule .Kind () == r .Kind () && genRule .Name () == r .Name () {
97- r = genRule
98- break
99- }
100- }
82+ // Otherwise generation was not done for this package and must be manually invoked.
83+ //
84+ // With GazelleHost the Imports() functionality is provided at rule generation time, not
85+ // a separate Import-extraction phase.
86+ //
87+ // When running partial generations this means we must manually invoke GenerateRules() if it
88+ // was not invoked by gazelle as part of the partial run.
89+ if c .Exts [extGeneratedRulesPkg ] != f .Pkg {
90+ BazelLog .Debugf ("Imports.GenerateRules(%s): //%s" , GazelleLanguageName , f .Pkg )
91+ c .Exts [extGeneratedRules ] = re .importsGenerateRules (cfg , c , f )
92+ c .Exts [extGeneratedRulesPkg ] = f .Pkg
10193 }
10294
103- targetDeclarationAttr := r .PrivateAttr (targetDeclarationKey )
104- if targetDeclarationAttr == nil {
105- // This rule was not generated by this gazelle extension.
106- return nil
95+ // Find this rule in the host-generated rules
96+ genResult := c .Exts [extGeneratedRules ].(gazelleLanguage.GenerateResult )
97+ for _ , g := range genResult .Gen {
98+ if g .Kind () == r .Kind () && g .Name () == r .Name () {
99+ // TODO: what if the rule generation is different and not what is expected?
100+ // ... it is possible this directory is not being updated, and if it were updated
101+ // the result would be different.
102+ if declaration := g .PrivateAttr (targetDeclarationKey ); declaration != nil {
103+ BazelLog .Debugf ("Imports(%s): //%s:%s (not generated %s)" , GazelleLanguageName , f .Pkg , g .Name (), g .Kind ())
104+ return symbolToImportSpecList (declaration .(plugin.TargetDeclaration ).Symbols )
105+ }
106+ break
107+ }
107108 }
108109
109- targetDeclaration := targetDeclarationAttr .(plugin.TargetDeclaration )
110+ return nil
111+ }
110112
111- res := make ([]resolve.ImportSpec , 0 , len (targetDeclaration .Symbols ))
112- for _ , s := range targetDeclaration .Symbols {
113+ func symbolToImportSpecList (symbols []plugin.Symbol ) []resolve.ImportSpec {
114+ res := make ([]resolve.ImportSpec , 0 , len (symbols ))
115+ for _ , s := range symbols {
113116 res = append (res , symbolToImportSpec (s ))
114117 }
115-
116118 return res
117119}
118120
0 commit comments