Skip to content

Commit b41fc69

Browse files
nikhitamarwan-at-work
authored andcommitted
Fix import path prefix collisions (#8)
If two import paths have the same prefix, versioned imports are not generated correctly. For example, consider `k8s.io/api` and `k8s.io/apimachinery`. Running `mod upgrade -tag=12 -mod-name=k8s.io/api`, will also modify apimachinery import paths to something like `"k8s.io/api/v12machinery/pkg/runtime"`. To avoid this, explicitly check if the import path to modify is: - the old import path itself - has the prefix "<old-import-path>/"
1 parent bb619e9 commit b41fc69

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

major/major.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package major
22

33
import (
44
"flag"
5+
"fmt"
56
"go/format"
67
"io/ioutil"
78
"log"
@@ -131,7 +132,7 @@ func updateImportPath(p *packages.Package, old, new string) error {
131132
var rewritten bool
132133
for _, i := range syn.Imports {
133134
imp := strings.Replace(i.Path.Value, `"`, ``, 2)
134-
if strings.HasPrefix(imp, old) {
135+
if strings.HasPrefix(imp, fmt.Sprintf("%s/", old)) || imp == old {
135136
newImp := strings.Replace(imp, old, new, 1)
136137
rewrote := astutil.RewriteImport(p.Fset, syn, imp, newImp)
137138
if rewrote {

0 commit comments

Comments
 (0)