Skip to content

Commit fafaf0d

Browse files
authored
fix(internal/aliasfix): handle import paths correctly (#10097)
The last component in import path of the old genproto path does not correspond to the import path -- usually. The import paths usually look like `google.golang.org/genproto/googleapis/analytics/admin/v1alpha`, however the imported package name is `admin`. This uses a heuristic to detect whether the last path component is a version, There are some packages that do not fit that description though, e.g. `google.golang.org/genproto/googleapis/devtools/containeranalysis/v1beta1/grafeas`.
1 parent 63f947d commit fafaf0d

File tree

8 files changed

+47
-34
lines changed

8 files changed

+47
-34
lines changed

internal/aliasfix/aliasfix.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"io/fs"
2828
"os"
29+
"path"
2930
"path/filepath"
3031
"strconv"
3132
"strings"
@@ -46,12 +47,12 @@ func ProcessPath(path string) error {
4647
}
4748
if dir.IsDir() {
4849
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
49-
if err == nil && !d.IsDir() && strings.HasSuffix(d.Name(), ".go") {
50-
err = processFile(path, nil)
51-
}
52-
if err != nil {
50+
if err != nil || d.IsDir() {
5351
return err
5452
}
53+
if strings.HasSuffix(d.Name(), ".go") {
54+
return processFile(path, nil)
55+
}
5556
return nil
5657
})
5758
if err != nil {
@@ -73,6 +74,7 @@ func processFile(name string, w io.Writer) (err error) {
7374
if err != nil {
7475
return err
7576
}
77+
7678
var modified bool
7779
for _, imp := range f.Imports {
7880
var importPath string
@@ -81,8 +83,8 @@ func processFile(name string, w io.Writer) (err error) {
8183
return err
8284
}
8385
if pkg, ok := GenprotoPkgMigration[importPath]; ok && pkg.Status == StatusMigrated {
84-
oldNamespace := importPath[strings.LastIndex(importPath, "/")+1:]
85-
newNamespace := pkg.ImportPath[strings.LastIndex(pkg.ImportPath, "/")+1:]
86+
oldNamespace := genprotoNamespace(importPath)
87+
newNamespace := path.Base(pkg.ImportPath)
8688
if imp.Name == nil && oldNamespace != newNamespace {
8789
// use old namespace for fewer diffs
8890
imp.Name = ast.NewIdent(oldNamespace)
@@ -99,26 +101,6 @@ func processFile(name string, w io.Writer) (err error) {
99101
return nil
100102
}
101103

102-
if w == nil {
103-
backup := name + ".bak"
104-
if err = os.Rename(name, backup); err != nil {
105-
return err
106-
}
107-
defer func() {
108-
if err != nil {
109-
os.Rename(backup, name)
110-
} else {
111-
os.Remove(backup)
112-
}
113-
}()
114-
var file *os.File
115-
file, err = os.Create(name)
116-
if err != nil {
117-
return err
118-
}
119-
defer file.Close()
120-
w = file
121-
}
122104
var buf bytes.Buffer
123105
if err := format.Node(&buf, fset, f); err != nil {
124106
return err
@@ -127,9 +109,32 @@ func processFile(name string, w io.Writer) (err error) {
127109
if err != nil {
128110
return err
129111
}
130-
if _, err := w.Write(b); err != nil {
112+
113+
if w != nil {
114+
_, err := w.Write(b)
131115
return err
132116
}
133117

134-
return nil
118+
backup := name + ".bak"
119+
if err = os.Rename(name, backup); err != nil {
120+
return err
121+
}
122+
defer func() {
123+
if err != nil {
124+
os.Rename(backup, name)
125+
} else {
126+
os.Remove(backup)
127+
}
128+
}()
129+
130+
return os.WriteFile(name, b, 0644)
131+
}
132+
133+
func genprotoNamespace(importPath string) string {
134+
suffix := path.Base(importPath)
135+
// if it looks like a version, then use the second from last component.
136+
if len(suffix) >= 2 && suffix[0] == 'v' && '0' <= suffix[1] && suffix[1] <= '1' {
137+
return path.Base(path.Dir(importPath))
138+
}
139+
return suffix
135140
}

internal/aliasfix/aliasfix_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ func TestGolden(t *testing.T) {
7474
}
7575
for _, tc := range tests {
7676
t.Run(tc.name, func(t *testing.T) {
77-
GenprotoPkgMigration["example.com/old/foo"] = Pkg{
77+
GenprotoPkgMigration["example.com/old/foo/v1"] = Pkg{
7878
ImportPath: "example.com/new/foopb",
7979
Status: tc.status,
8080
}
81+
GenprotoPkgMigration["example.com/old/bar/v1/bar"] = Pkg{
82+
ImportPath: "example.com/new/barpb",
83+
Status: tc.status,
84+
}
8185
var w bytes.Buffer
8286
if updateGoldens {
8387
if err := processFile(filepath.Join("testdata", tc.fileName), nil); err != nil {

internal/aliasfix/testdata/golden/input2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package golden
33
import (
44
"net"
55

6+
bar "example.com/new/barpb"
67
foo "example.com/new/foopb"
78
)
89

910
func Bar1(foo foo.Baz, addr net.Addr) {}
11+
func Bar2(foo bar.Baz, addr net.Addr) {}

internal/aliasfix/testdata/input1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package golden
22

3-
import "example.com/old/foo"
3+
import "example.com/old/foo/v1"
44

55
func Bar1(foo foo.Baz) {}

internal/aliasfix/testdata/input2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package golden
33
import (
44
"net"
55

6-
"example.com/old/foo"
6+
"example.com/old/foo/v1"
7+
"example.com/old/bar/v1/bar"
78
)
89

910
func Bar1(foo foo.Baz, addr net.Addr) {}
11+
func Bar2(foo bar.Baz, addr net.Addr) {}

internal/aliasfix/testdata/input4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package golden
22

3-
import foopb "example.com/old/foo"
3+
import foopb "example.com/old/foo/v1"
44

55
func Bar4(baz foopb.Baz) {}

internal/aliasfix/testdata/input5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package golden
33
import (
44
"net"
55

6-
blah "example.com/old/foo"
6+
blah "example.com/old/foo/v1"
77
)
88

99
func Bar2(baz blah.Baz, addr net.Addr) {}

internal/aliasfix/testdata/input6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package golden
33
import (
44
"net"
55

6-
blah "example.com/old/foo"
6+
blah "example.com/old/foo/v1"
77
)
88

99
func Bar2(baz blah.Baz, addr net.Addr) {}

0 commit comments

Comments
 (0)