Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/refactor/eg: Using ./... only matches one file even though there are many more #70414

Open
connorszczepaniak-wk opened this issue Nov 18, 2024 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@connorszczepaniak-wk
Copy link

connorszczepaniak-wk commented Nov 18, 2024

Go version

go version go1.23.3 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/connorszczepaniak/Library/Caches/go-build'
GOENV='/Users/connorszczepaniak/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/connorszczepaniak/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/connorszczepaniak/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/connorszczepaniak/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.3.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/connorszczepaniak/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.3.darwin-arm64/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.3'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/connorszczepaniak/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/path/to/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/14/dl88pgbs11766nqnslc5ry8h0000gs/T/go-build3037559288=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I have a template looks approximately like this in abstract:

package mypkg

import (
	"testing"

	"github.com/myRepo/x/internal/a"
	"github.com/myRepo/x/internal/a/b"
)

func before(t testing.TB, widget *b.Widget, name string) string {
	return a.FunctionToReplace(t, widget, name, "", -1)
}

func after(t testing.TB, widget *b.Widget, name string) string {
	return a.NewFunction().SomethingElse(name).Another(t, widget)
}

I'm attempting to run eg -w -t template.go github.com/myRepo/... to replace all usages in this module.

What did you see happen?

When I run eg -w -t template.go ./... from the root of github.com/myRepo, I notice that only one usage gets replaced although there are many, many more in packages matched by this pattern. If I run eg -w -t template.go github.com/myRepo/x/internal/some/other/pkg, where this package contains a lot of call sites of the old function, then all of the matches appear and get replaced.

Maybe not relevant, but I noticed that for some specific packages that would be included in github.com/myRepo/..., there are errors that occur when running eg.

// when running eg -w -t template.go github.com/myRepo/x/internal/a/...
eg: template.go:11:45: cannot use widget (variable of type *b.Widget) as *b.Widget value in argument to a.FunctionToReplace

// when running eg -w -t template.go github.com/myRepo/x/internal/a/subpkg
eg: template.go:6:2: could not import github.com/myRepo/x/internal/a (package "github.com/myRepo/x/internal/a" not found)

What did you expect to see?

All call sites within the list of packages matched by github.com/myRepo/... to be replaced.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Nov 18, 2024
@gopherbot gopherbot added this to the Unreleased milestone Nov 18, 2024
@connorszczepaniak-wk
Copy link
Author

I've noticed this same issue with an even simpler template as well:

package mypkg

import (
	"github.com/myRepo/x/internal/a"
)

func before() a.SomeStruct {
	return a.SomeStruct{}
}

func after() a.SomeStruct{} {
	return a.SomeStruct{Field1: true}
}

@connorszczepaniak-wk
Copy link
Author

Looked a bit deeper at the second case. It seems to be that the struct literal from before doesn't match the struct literal at the usages; namely the call to matchType here returns false even though the two are named types with the same package and same name.

If I dig into that a bit more, it looks like we fail in types.Identical when comparing named types here; even though their origins seem to be deep equal, they are not the same pointer (which is what gets compared here).

@dmitshur
Copy link
Contributor

CC @golang/tools-team.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants