Skip to content

Commit b93cdb4

Browse files
committed
fix error messages
1 parent 0712134 commit b93cdb4

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

gazelle/python/generate.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ var (
4848
buildFilenames = []string{"BUILD", "BUILD.bazel"}
4949
)
5050

51+
// Returns the mapped kind, or kind if no mapping is configured with the map_kind directive.
52+
func getMappedKind(c *config.Config, kind string) string {
53+
if mapped, ok := c.KindMap[kind]; ok {
54+
return mapped.KindName
55+
}
56+
return kind
57+
}
58+
5159
// kindMatches returns whether r matches the canonical Python rule kind `expected`, respecting `# gazelle:map_kind` and
5260
// `# gazelle:alias_kind` directives in the config.Config c.
5361
func kindMatches(r *rule.Rule, expected string, c *config.Config) bool {
54-
k := r.Kind()
55-
if mapped, ok := c.KindMap[expected]; ok {
56-
return k == mapped.KindName || c.AliasMap[k] == expected
57-
}
58-
return k == expected || c.AliasMap[k] == expected
62+
kind := r.Kind()
63+
return kind == getMappedKind(c, expected) || c.AliasMap[kind] == expected
5964
}
6065

6166
func matchesAnyGlob(s string, globs []string) bool {
@@ -285,7 +290,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
285290
if err := ensureNoCollision(args.Config, args.File, pyBinaryTargetName, pyBinaryKind); err != nil {
286291
fqTarget := label.New("", args.Rel, pyBinaryTargetName)
287292
log.Printf("failed to generate target %q of kind %q: %v",
288-
fqTarget.String(), pyBinaryKind, err)
293+
fqTarget.String(), getMappedKind(args.Config, pyBinaryKind), err)
289294
continue
290295
}
291296

@@ -335,7 +340,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
335340
fqTarget := label.New("", args.Rel, pyLibraryTargetName)
336341
err := fmt.Errorf("failed to generate target %q of kind %q: %w. "+
337342
"Use the '# gazelle:%s' directive to change the naming convention.",
338-
fqTarget.String(), pyLibraryKind, err, pythonconfig.LibraryNamingConvention)
343+
fqTarget.String(), getMappedKind(args.Config, pyLibraryKind), err, pythonconfig.LibraryNamingConvention)
339344
collisionErrors.Add(err)
340345
}
341346

@@ -389,7 +394,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
389394
fqTarget := label.New("", args.Rel, pyBinaryTargetName)
390395
err := fmt.Errorf("failed to generate target %q of kind %q: %w. "+
391396
"Use the '# gazelle:%s' directive to change the naming convention.",
392-
fqTarget.String(), pyBinaryKind, err, pythonconfig.BinaryNamingConvention)
397+
fqTarget.String(), getMappedKind(args.Config, pyBinaryKind), err, pythonconfig.BinaryNamingConvention)
393398
collisionErrors.Add(err)
394399
}
395400

@@ -427,7 +432,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
427432
if err := ensureNoCollision(args.Config, args.File, conftestTargetname, pyLibraryKind); err != nil {
428433
fqTarget := label.New("", args.Rel, conftestTargetname)
429434
err := fmt.Errorf("failed to generate target %q of kind %q: %w. ",
430-
fqTarget.String(), pyLibraryKind, err)
435+
fqTarget.String(), getMappedKind(args.Config, pyLibraryKind), err)
431436
collisionErrors.Add(err)
432437
}
433438

@@ -465,7 +470,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
465470
fqTarget := label.New("", args.Rel, pyTestTargetName)
466471
err := fmt.Errorf("failed to generate target %q of kind %q: %w. "+
467472
"Use the '# gazelle:%s' directive to change the naming convention.",
468-
fqTarget.String(), pyTestKind, err, pythonconfig.TestNamingConvention)
473+
fqTarget.String(), getMappedKind(args.Config, pyTestKind), err, pythonconfig.TestNamingConvention)
469474
collisionErrors.Add(err)
470475
}
471476

0 commit comments

Comments
 (0)