Skip to content

Commit

Permalink
Fix import path prefix collisions (#8)
Browse files Browse the repository at this point in the history
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>/"
  • Loading branch information
nikhita authored and marwan-at-work committed Jul 5, 2019
1 parent bb619e9 commit b41fc69
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion major/major.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package major

import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
Expand Down Expand Up @@ -131,7 +132,7 @@ func updateImportPath(p *packages.Package, old, new string) error {
var rewritten bool
for _, i := range syn.Imports {
imp := strings.Replace(i.Path.Value, `"`, ``, 2)
if strings.HasPrefix(imp, old) {
if strings.HasPrefix(imp, fmt.Sprintf("%s/", old)) || imp == old {
newImp := strings.Replace(imp, old, new, 1)
rewrote := astutil.RewriteImport(p.Fset, syn, imp, newImp)
if rewrote {
Expand Down

0 comments on commit b41fc69

Please sign in to comment.