Skip to content

Commit e58dfd3

Browse files
committed
gopls/internal/golang: package move: handle mixed build configs
When there are files in a metadata.Package that are ignored because they are not in the package with the current build configuration, we should not allow moving that package. Change-Id: I2cc4f078113ce370cdafaee2333ffabc0befd673 Reviewed-on: https://go-review.googlesource.com/c/tools/+/721040 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 72b42f2 commit e58dfd3

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

gopls/internal/golang/rename.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func Rename(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, pp pro
452452

453453
var (
454454
editMap map[protocol.DocumentURI][]diff.Edit
455-
newPkgDir string // declared here so it can be used later to rename the package's enclosing directory
455+
newPkgDir string // declared here so it can be used later to move the package's contents to the new directory
456456
)
457457
if inPackageName {
458458
countRenamePackage.Inc()

gopls/internal/golang/rename_check.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,14 @@ func checkPackageRename(opts *settings.Options, curPkg *cache.Package, f file.Ha
927927
if newName == curPkg.String() || newName == string(curPkg.Metadata().Name) {
928928
return f.URI().DirPath(), curPkg.Metadata().Name, curPkg.Metadata().PkgPath, nil
929929
}
930+
931+
// If there are any Go files in the package that are not in the compiled package
932+
// with the current build config, we should not allow a package move.
933+
ignored := hasIgnoredGoFiles(curPkg)
934+
if ignored {
935+
return "", "", "", fmt.Errorf("moving a package with ignored files is not supported")
936+
}
937+
930938
// TODO(mkalil): support relative paths
931939
if build.IsLocalImport(newName) {
932940
return "", "", "", fmt.Errorf("specifying relative paths in package rename not yet supported")
@@ -981,6 +989,18 @@ func checkPackageRename(opts *settings.Options, curPkg *cache.Package, f file.Ha
981989
return newPkgDir, newPkgName, PackagePath(newName), nil
982990
}
983991

992+
// hasIgnoredGoFiles returns true if the input pkg contains any Go files that
993+
// are not part of package pkg with the current build configuration.
994+
func hasIgnoredGoFiles(pkg *cache.Package) bool {
995+
// If any ignored files are Go files, don't allow a package move.
996+
for _, f := range pkg.Metadata().IgnoredFiles {
997+
if strings.HasSuffix(f.Path(), ".go") {
998+
return true
999+
}
1000+
}
1001+
return false
1002+
}
1003+
9841004
// isLocal reports whether obj is local to some function.
9851005
// Precondition: not a struct field or interface method.
9861006
func isLocal(obj types.Object) bool {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ import "golang.org/lsptests/rename/nine/nine/nine"
102102
-- othermoddir/go.mod --
103103
module mod.com/other
104104

105+
-- foo/windows.go --
106+
//go:build windows
107+
package foo
108+
109+
-- foo/default.go --
110+
//go:build !windows
111+
package foo //@ renameerr("foo", "golang.org/lsptests/rename/foo2", re"ignored files")
105112

106113
-- @moveDownTwoDirs/nine/nine/nine/eight/eight.go --
107114
@@ -0,0 +1 @@

0 commit comments

Comments
 (0)