diff --git a/refactor/rename/mvpkg.go b/refactor/rename/mvpkg.go index cd416c56f9..638d0cfc25 100644 --- a/refactor/rename/mvpkg.go +++ b/refactor/rename/mvpkg.go @@ -145,7 +145,9 @@ func subpackages(ctxt *build.Context, srcDir string, dir string) map[string]bool log.Fatalf("unexpected error in ForEachPackage: %v", err) } - if !strings.HasPrefix(pkg, path.Join(dir, "")) { + // Only process the package or a sub-package + if !(strings.HasPrefix(pkg, dir) && + (len(pkg) == len(dir) || pkg[len(dir)] == '/')) { return } diff --git a/refactor/rename/mvpkg_test.go b/refactor/rename/mvpkg_test.go index 7f5b135381..38e8a90ee0 100644 --- a/refactor/rename/mvpkg_test.go +++ b/refactor/rename/mvpkg_test.go @@ -214,6 +214,23 @@ var _ a.T }, }, + // References into subpackages where directories have overlapped names + { + ctxt: fakeContext(map[string][]string{ + "foo": {}, + "foo/a": {`package a`}, + "foo/aa": {`package bar`}, + "foo/c": {`package c; import _ "foo/bar";`}, + }), + from: "foo/a", to: "foo/spam", + want: map[string]string{ + "/go/src/foo/spam/0.go": `package spam +`, + "/go/src/foo/aa/0.go": `package bar`, + "/go/src/foo/c/0.go": `package c; import _ "foo/bar";`, + }, + }, + // External test packages { ctxt: buildutil.FakeContext(map[string]map[string]string{ @@ -398,11 +415,13 @@ var _ foo.T } moveDirectory = func(from, to string) error { for path, contents := range got { - if strings.HasPrefix(path, from) { - newPath := strings.Replace(path, from, to, 1) - delete(got, path) - got[newPath] = contents + if !(strings.HasPrefix(path, from) && + (len(path) == len(from) || path[len(from)] == filepath.Separator)) { + continue } + newPath := strings.Replace(path, from, to, 1) + delete(got, path) + got[newPath] = contents } return nil }