gci icon indicating copy to clipboard operation
gci copied to clipboard

Expected '\t', found '\n' in import section with comments

Open bpingris opened this issue 3 years ago • 4 comments

Hi,

When using gci with golangci-lint, gci will report an error if a comment is above a package, like so:

package main

import (
	"fmt"
                               <- gci: Expected '\t', Found '\n' at main.go[line 5,col 1]
	// Foobar
	"net/http"
)

func main() {
	fmt.Println("foobar")
	fmt.Println(http.MethodGet)
}

This issue has been reported on the golangci-lint repo

.golangci.yaml

linters:
  disable-all: true
  enable:
    - gci

Gci version

$ gci -v
gci version 0.3

bpingris avatar Mar 07 '22 14:03 bpingris

Use the following to run unit tests.

with-above-comment-and-alias.in.go

package main
import (
	"fmt"

	// golang
	_ "github.com/golang"

 	// Comment here.
	_ "github.com/golang-migrate/migrate/v4/database/spanner"

	// Another comment here.
	_ "github.com/golang-migrate/migrate/v4/source/file"
	
	"github.com/daixiang0/gci"
)

with-above-comment-and-alias.out.go

package main
import (
	"fmt"

	// golang
	_ "github.com/golang"

	// Comment here.
	_ "github.com/golang-migrate/migrate/v4/database/spanner"

	// Another comment here.
	_ "github.com/golang-migrate/migrate/v4/source/file"
	
	"github.com/daixiang0/gci"
)

Run TestRun

--- FAIL: TestRun (0.00s)
    --- FAIL: TestRun/internal/testdata/with-above-comment-and-alias (0.00s)
        /gci/pkg/gci/gci_test.go:45: 
            	Error Trace:	gci_test.go:45
            	Error:      	Not equal: 
            	            	expected: "package main\nimport (\n\t\"fmt\"\n\n\t// golang\n\t_ \"github.com/golang\"\n\n\t// Comment here.\n\t_ \"github.com/golang-migrate/migrate/v4/database/spanner\"\n\n\t// Another comment here.\n\t_ \"github.com/golang-migrate/migrate/v4/source/file\"\n\t\n\t\"github.com/daixiang0/gci\"\n)\n"
            	            	actual  : "package main\nimport (\n\t\"fmt\"\n\n\t// golang\n\t_ \"github.com/golang\"\n\t// Comment here.\n\t_ \"github.com/golang-migrate/migrate/v4/database/spanner\"\n\t// Another comment here.\n\t_ \"github.com/golang-migrate/migrate/v4/source/file\"\n\n\t\"github.com/daixiang0/gci\"\n)\n"
            	            	
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -6,9 +6,7 @@
            	            	 	_ "github.com/golang"
            	            	-
            	            	 	// Required to initialize migration destination.
            	            	 	_ "github.com/golang-migrate/migrate/v4/database/spanner"
            	            	-
            	            	 	// Required to initialize file migration.
            	            	 	_ "github.com/golang-migrate/migrate/v4/source/file"
            	            	-	
            	            	+
            	            	 	"github.com/daixiang0/gci"
            	Test:       	TestRun/internal/testdata/with-above-comment-and-alias
            	Messages:   	output
FAIL
FAIL	github.com/daixiang0/gci/pkg/gci	0.113s
FAIL

Notice the difference

Expected: migrate/v4/source/file\"\n\t\n\t\"github.com/daixiang0/gci\"\n)\n"
Actual:      migrate/v4/source/file\"\n\n\t\"github.com/daixiang0/gci\"\n)\n"

IDispose avatar Mar 07 '22 17:03 IDispose

@IDispose The error here is that gci expects your code to be formatted as follows:

package main

import (
	"fmt"

	// golang
	_ "github.com/golang"
	// Comment here.
	_ "github.com/golang-migrate/migrate/v4/database/spanner"
	// Another comment here.
	_ "github.com/golang-migrate/migrate/v4/source/file"

	"github.com/daixiang0/gci"
)

The reason why it does this is configured in the accompanying .cfg.yaml. Configured there is:

sections:
  - Standard
  - Default
  - Prefix(github.com/local)
  - Prefix(github.com/daixiang0)

This means that the imports that you expect to have a newline between them are all grouped into the same category: Default. The reason why there are any whitespaces in the formatted output is that the default separator between imports in a category is a newline. Thus you need to configure gci differently to achieve the desired behavior. At the moment there is no support for having separators between entries of the same category.

ngehrsitz avatar Apr 17 '22 18:04 ngehrsitz

I don't know what happened here and whether it was in golangcli-lint or gci, but our code was fine before and now throws cryptic error messages all over the place after updating both and having to switch from local-prefixes to sections, so something broke.

Airblader avatar Apr 25 '22 07:04 Airblader

@Airblader now we need to add config in golangci-lint to make GCI works better.

daixiang0 avatar May 30 '22 03:05 daixiang0

v1.46.2 output:

pilot/pkg/config/kube/crdclient/cache_handler.go:20: File is not `gci`-ed with --skip-generated -s standard,default,prefix(istio.io/) (gci)

pilot/pkg/config/kube/crdclient/cache_handler.go:25: File is not `gci`-ed with --skip-generated -s standard,default,prefix(istio.io/) (gci)

pilot/pkg/config/kube/crdclient/cache_handler.go:26: File is not `gci`-ed with --skip-generated -s standard,default,prefix(istio.io/) (gci)

daixiang0 avatar Aug 01 '22 06:08 daixiang0