Skip to content

Commit 8cf2d63

Browse files
committed
gopls/internal/golang: add condition for enabling package move
If the package name does not match its directory basename, we shouldn't support renaming to move the package. Don't return the entire package path in PrepareRename. This is a follow-up to address https://go-review.git.corp.google.com/c/tools/+/708655/comment/408e97ee_31e00e80/ Change-Id: I74548153a2c4af7b417e132c7d88dd797871f5fe Reviewed-on: https://go-review.googlesource.com/c/tools/+/710275 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 1f054fd commit 8cf2d63

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

gopls/internal/golang/rename.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,16 @@ func prepareRenamePackageName(ctx context.Context, snapshot *cache.Snapshot, pgf
215215
return nil, err
216216
}
217217

218-
text := string(meta.Name)
219-
if snapshot.Options().PackageMove {
220-
text = string(meta.PkgPath)
218+
pkgName := string(meta.Name)
219+
fullPath := string(meta.PkgPath)
220+
text := pkgName
221+
// Before displaying the full package path, verify that the PackageMove
222+
// setting is enabled and that the package name matches its directory
223+
// basename. Checking the value of meta.Module above ensures that the
224+
// current view is either a GoMod or a GoWork view, which are the only views
225+
// for which we should enable package move.
226+
if snapshot.Options().PackageMove && path.Base(fullPath) == pkgName {
227+
text = fullPath
221228
}
222229
return &PrepareItem{
223230
Range: rng,

gopls/internal/test/marker/testdata/rename/prepare_move.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ go 1.20
1212

1313
-- b/b.go --
1414
package b //@ preparerename("b", "golang.org/lsptests/b")
15+
16+
-- a/other.go --
17+
package other //@ preparerename("other", "other") // package move disabled when the package name does not match its directory base name

0 commit comments

Comments
 (0)