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/internal/refactor/inline: analyzer generates unnecessary import renames #67281

Closed
lfolger opened this issue May 10, 2024 · 6 comments
Closed
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

@lfolger
Copy link
Contributor

lfolger commented May 10, 2024

Go version

not relevant

Output of go env in your module/workspace:

not relevant

What did you do?

func TestNoUnnecessaryImportRename(t *testing.T) {
	files := map[string]string{
		"some/other/foo/foo.go": `
			package foo

			func Bar () {
			}
		`,
		"some/package/foo/foo.go": `
			package foo

			import "some/other/foo"

			// inlineme
			func Bar () {
				foo.Bar()
			}
		`,
		"b/c/foo.go": `
package c
import (
	"some/package/foo"
)

func f() {
	foo.Bar()
}
		`,
		"b/c/foo.go.golden": `
package c
import (
	"some/other/foo"
)

func f() {
	foo.Bar()
}
		`,
	}
	dir, cleanup, err := analysistest.WriteFiles(files)
	if err != nil {
		t.Fatal(err)
	}
	analysistest.RunWithSuggestedFixes(t, dir, analyzer.Analyzer, "b/c")
	cleanup()
}

What did you see happen?

=== RUN   TestNoUnnecessaryImportRename
    .../go/analysis/analysistest/analysistest.go:523: b/c/foo.go:8:2: unexpected diagnostic: inline call of foo.Bar
    .../go/analysis/analysistest/analysistest.go:263: suggested fixes failed for /tmp/analysistest3217487681/src/b/c/foo.go:
        --- /tmp/analysistest3217487681/src/b/c/foo.go.golden
        +++ actual
        @@ -1,9 +1,9 @@
         package c

         import (
        -       "some/other/foo"
        +       foo0 "some/other/foo"
         )

         func f() {
        -       foo.Bar()
        +       foo0.Bar()
         }
--- FAIL: TestNoUnnecessaryImportRename (0.04s)

What did you expect to see?

I would expect the analyzer to not rename the import if it removes the conflicting import the same suggested fix.

This pattern is common when you use the inline analyzer to facilitate package moves and it would be nice if the analyzer doesn't leave the code base with a bunch of unecessary import renames.

@lfolger
Copy link
Contributor Author

lfolger commented May 10, 2024

cc: @adonovan

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 10, 2024
@gopherbot gopherbot added this to the Unreleased milestone May 10, 2024
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 10, 2024
@lfolger
Copy link
Contributor Author

lfolger commented Jun 10, 2024

Any update on this?

@adonovan
Copy link
Member

I have a sketch of a fix but it was a little ugly and I haven't found time to improve it. Perhaps this week? I could also share it with @timothy-king since he's been keen to get his hands dirty in this package.

@lfolger
Copy link
Contributor Author

lfolger commented Jun 10, 2024

I'm happy to take a stab at it myself if you don't have the capacity just wanted to make sure we are not duplicating work here.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/592575 mentions this issue: internal/refactor/inline: avoid unnecessary import renames

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/593035 mentions this issue: internal/refactor/inline: avoid unnecessary import renames

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

4 participants