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 suggested fixes that move comments incorrectly #67336

Closed
lfolger opened this issue May 13, 2024 · 4 comments
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@lfolger
Copy link
Contributor

lfolger commented May 13, 2024

Go version

n/a

Output of go env in your module/workspace:

n/a

What did you do?

This seems like a lot to parse. The important part is (I think) that the analyzer tries to inline a function that requires two new imports one of which is renamed.

func TestImport(t *testing.T) {
	files := map[string]string{
		"define/my/typ/foo.go": `
		package typ
		type T int
		`,

		"some/other/pkg/foo.go": `
		package pkg
		import "context"
		import "define/my/typ"
		func Foo(typ.T) context.Context{ return nil }
		`,

		"one/more/pkg/foo.go": `
		package pkg
		func Bar() {}
		`,

		"to/be/inlined/foo.go": `
			package inlined

			import "context"
			import "some/other/pkg"
			import "define/my/typ"

			// inlineme
			func Baz(ctx context.Context) context.Context {
				return pkg.Foo(typ.T(5))
			}
		`,

		"b/c/foo.go": `
package c
import (
	"context"
	"to/be/inlined"
	"one/more/pkg"
)

const (
	// This is a variable
	someConst = 5
)

func foo() {
	inlined.Baz(context.TODO()) // want "inline call of inlined.Baz"
	pkg.Bar()
}
		`,
		"b/c/foo.go.golden": `package c
import (
	"context"
	"one/more/pkg"
	pkg0 "some/other/pkg"
	
	// This is za variable
	"define/my/typ"
)

const (
	someConst = 5
)

func foo() {
	var _ context.Context = context.TODO()
	pkg0.Foo(typ.T(5)) // want "inline call of inlined.Baz"
	pkg.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?

The test failed with:

=== RUN   TestImport
    ...analysistest/analysistest.go:263: suggested fixes failed for /tmp/analysistest883636049/src/b/c/foo.go:
        --- /tmp/analysistest883636049/src/b/c/foo.go.golden
        +++ actual
        @@ -4,12 +4,12 @@
                "context"
                "one/more/pkg"
                pkg0 "some/other/pkg"
        -
        +
        +       // This is a variable
                "define/my/typ"
         )

        -const (
        -       // This is a variable
        +const (
                someConst = 5
         )

--- FAIL: TestImport (1.93s)

Sometimes the test fails with a different error due to https://go.dev/issue/67335.

What did you expect to see?

I expected the test to pass.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 13, 2024
@gopherbot gopherbot added this to the Unreleased milestone May 13, 2024
@lfolger
Copy link
Contributor Author

lfolger commented May 13, 2024

cc: @adonovan

@adonovan adonovan modified the milestones: Unreleased, gopls/v0.16.0 May 13, 2024
@adonovan adonovan added Refactoring Issues related to refactoring tools gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 13, 2024
@adonovan
Copy link
Member

Thanks for the helpful testcase. I suspect this is just another symptom of not storing comments in the tree; that is, #20744.

@findleyr
Copy link
Member

Deferring this until we have a better solution for comments.

@findleyr findleyr modified the milestones: gopls/v0.17.0, gopls/v0.18.0 Nov 15, 2024
@findleyr findleyr added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label Nov 21, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/629979 mentions this issue: internal/refactor/inline: fix comment movement due to added imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools 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