Skip to content

Commit fb7743f

Browse files
authored
Merge pull request #277 from jpbetz/format-only-imports
Performance: Use FormatOnly on imports
2 parents a7b603a + 4ef5de9 commit fb7743f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

v2/generator/execute.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
"golang.org/x/tools/imports"
29+
2930
"k8s.io/gengo/v2/namer"
3031
"k8s.io/gengo/v2/types"
3132
"k8s.io/klog/v2"
@@ -114,7 +115,13 @@ func assembleGoFile(w io.Writer, f *File) {
114115
}
115116

116117
func importsWrapper(src []byte) ([]byte, error) {
117-
return imports.Process("", src, nil)
118+
opt := imports.Options{
119+
Comments: true,
120+
TabIndent: true,
121+
TabWidth: 8,
122+
FormatOnly: true, // Disable the insertion and deletion of imports
123+
}
124+
return imports.Process("", src, &opt)
118125
}
119126

120127
func NewGoFile() *DefaultFileType {

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

v2/generator/import_tracker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestNewImportTracker(t *testing.T) {
7272
{Name: types.Name{Package: "bar.com/external/pkg"}},
7373
},
7474
expectedImports: []string{
75-
`pkg "bar.com/external/pkg"`,
75+
`externalpkg "bar.com/external/pkg"`,
7676
},
7777
},
7878
}

0 commit comments

Comments
 (0)