Skip to content

Commit 817e827

Browse files
authored
refactor: debug/tracel logging improvements in js+orion generate+import (#116)
### Changes are visible to end-users: no ### Test plan - Covered by existing test cases
1 parent eabb699 commit 817e827

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

language/js/resolve.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (*typeScriptLang) Name() string { return LanguageName }
4242
// Determine what rule (r) outputs which can be imported.
4343
// For TypeScript this is all the import-paths pointing to files within the rule.
4444
func (ts *typeScriptLang) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
45-
BazelLog.Debugf("Imports(%s): //%s:%s", LanguageName, f.Pkg, r.Name())
45+
BazelLog.Tracef("Imports(%s): //%s:%s", LanguageName, f.Pkg, r.Name())
4646

4747
switch r.Kind() {
4848
case TsProtoLibraryKind:
@@ -63,12 +63,16 @@ func (ts *typeScriptLang) sourceFileImports(c *config.Config, r *rule.Rule, f *r
6363

6464
infoAttr := r.PrivateAttr("ts_project_info")
6565
if infoAttr != nil && infoAttr.(*TsProjectInfo).sources != nil {
66+
BazelLog.Debugf("Imports(%s): //%s:%s (generated %s)", LanguageName, f.Pkg, r.Name(), r.Kind())
67+
6668
srcsSet := infoAttr.(*TsProjectInfo).sources
6769
srcs = make([]string, 0, srcsSet.Size())
6870
for it := srcsSet.Iterator(); it.Next(); {
6971
srcs = append(srcs, it.Value().(string))
7072
}
7173
} else {
74+
BazelLog.Debugf("Imports(%s): //%s:%s (non-generated %s)", LanguageName, f.Pkg, r.Name(), r.Kind())
75+
7276
sourceFiles, err := common.GetSourceRegularFiles(f.Pkg)
7377
if err != nil {
7478
BazelLog.Errorf("Failed to fetch source files %s:%s - %v", f.Pkg, r.Name(), err)
@@ -124,6 +128,8 @@ func (ts *typeScriptLang) sourceFileImports(c *config.Config, r *rule.Rule, f *r
124128
}
125129

126130
func (ts *typeScriptLang) tsconfigImports(r *rule.Rule, f *rule.File) []resolve.ImportSpec {
131+
BazelLog.Debugf("Imports(%s): //%s:%s (%s)", LanguageName, f.Pkg, r.Name(), r.Kind())
132+
127133
// Only the tsconfig file itself is exposed.
128134
// The output is the same as the ts_config(src) input.
129135
return []resolve.ImportSpec{
@@ -143,6 +149,8 @@ func (ts *typeScriptLang) protoLibraryImports(r *rule.Rule, f *rule.File) []reso
143149
return nil
144150
}
145151

152+
BazelLog.Debugf("Imports(%s): //%s:%s (%s)", LanguageName, f.Pkg, r.Name(), r.Kind())
153+
146154
protoSrcs := protoSrcsAttr.([]string)
147155
dtsOutputs := []string{}
148156

language/orion/resolver.go

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ const extGeneratedRules = "__starzelle_generated_rules"
4646
const extGeneratedRulesPkg = "__starzelle_generated_rules_pkg"
4747

4848
func (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.
7666
func (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

Comments
 (0)