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

go/ast: SortImports does not change the (*ast.File).Imports field #69694

Open
mateusz834 opened this issue Sep 28, 2024 · 2 comments · May be fixed by #69695
Open

go/ast: SortImports does not change the (*ast.File).Imports field #69694

mateusz834 opened this issue Sep 28, 2024 · 2 comments · May be fixed by #69695

Comments

@mateusz834
Copy link
Member

Reproducer:

func TestSortImports(t *testing.T) {
	const src = `package test

import (
	"test"
	"test" // test comment
)
`

	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution)
	if err != nil {
		t.Fatal(err)
	}

	ast.SortImports(fset, f)

	importDeclSpecCount := len(f.Decls[0].(*ast.GenDecl).Specs)
	if importDeclSpecCount != 1 {
		t.Fatalf("len(f.Decls[0].(*ast.GenDecl).Specs) = %v; want = 1", importDeclSpecCount)
	}

	if len(f.Imports) != 1 {
		t.Fatalf("len(f.Imports) = %v; want = 1", len(f.Imports)) // fails with len(f.Imports) = 2; want = 1
	}
}

Currently ast.SortImports only updates the f.Decls[0].(*ast.GenDecl), the f.Imports field is untouched.

CC @griesemer

@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/616340 mentions this issue: go/ast: update (*File).Imports field in SortImports

@mateusz834 mateusz834 changed the title go/ast: SortImports does change the (*ast.File).Imports field go/ast: SortImports does not change the (*ast.File).Imports field Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants