Skip to content

Commit df35488

Browse files
jpbetzthockin
andcommitted
Avoid imports of same name
This primarily benefits applyconfiguration-gen today, which accidentaly got this behavior by including the local package name in imports, which caused goimports to deconflict with it before removing it since it was unused. This commit effictively preserves that behavior now that we will not be relying on goimports to add/rename/remove imports. Co-authored-by: Tim Hockin <[email protected]>
1 parent 13ad52c commit df35488

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

v2/generator/import_tracker.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package generator
1818

1919
import (
2020
"go/token"
21+
"path/filepath"
2122
"strings"
2223

2324
"k8s.io/klog/v2"
@@ -45,7 +46,7 @@ import (
4546
func NewImportTrackerForPackage(local string, typesToAdd ...*types.Type) *namer.DefaultImportTracker {
4647
tracker := namer.NewDefaultImportTracker(types.Name{Package: local})
4748
tracker.IsInvalidType = func(*types.Type) bool { return false }
48-
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, name) }
49+
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, local, name) }
4950
tracker.PrintImport = func(path, name string) string { return name + " \"" + path + "\"" }
5051

5152
tracker.AddTypes(typesToAdd...)
@@ -56,14 +57,15 @@ func NewImportTracker(typesToAdd ...*types.Type) *namer.DefaultImportTracker {
5657
return NewImportTrackerForPackage("", typesToAdd...)
5758
}
5859

59-
func goTrackerLocalName(tracker namer.ImportTracker, t types.Name) string {
60+
func goTrackerLocalName(tracker namer.ImportTracker, localPkg string, t types.Name) string {
6061
path := t.Package
6162

6263
// Using backslashes in package names causes gengo to produce Go code which
6364
// will not compile with the gc compiler. See the comment on GoSeperator.
6465
if strings.ContainsRune(path, '\\') {
6566
klog.Warningf("Warning: backslash used in import path '%v', this is unsupported.\n", path)
6667
}
68+
localLeaf := filepath.Base(localPkg)
6769

6870
dirs := strings.Split(path, namer.GoSeperator)
6971
for n := len(dirs) - 1; n >= 0; n-- {
@@ -74,8 +76,13 @@ func goTrackerLocalName(tracker namer.ImportTracker, t types.Name) string {
7476
// packages, but aren't legal go names. So we'll sanitize.
7577
name = strings.ReplaceAll(name, ".", "")
7678
name = strings.ReplaceAll(name, "-", "")
77-
if _, found := tracker.PathOf(name); found {
78-
// This name collides with some other package
79+
if _, found := tracker.PathOf(name); found || name == localLeaf {
80+
// This name collides with some other package.
81+
// Or, this name is tne same name as the local package,
82+
// which we avoid because it can be confusing. For example,
83+
// if the local package is v1, we to avoid importing
84+
// another package using the v1 name, and instead import
85+
// it with a more qualified name, such as metav1.
7986
continue
8087
}
8188

0 commit comments

Comments
 (0)