gci icon indicating copy to clipboard operation
gci copied to clipboard

"C" package causes `gci` to output invalid suggestion

Open VirrageS opened this issue 2 years ago • 5 comments

What version of GCI are you using?

gci version 0.11.0

Reproduce Steps

1. Save following program to `main.go`
2. Run `gci diff main.go`

Note that it only happens when there is some additional (default) package present.

package main

import (
	"C"
	"fmt"

	_ "golang.org/x/example/hello/reverse"
)

func init() {
	var x C.int
	fmt.Print(x)
}

What did you expect to see?

No diff.

What did you see instead?

--- main.go
+++ main.go
@@ -7,6 +7,12 @@
        _ "golang.org/x/example/hello/reverse"
 )
 
+import (
+       "fmt"
+
+       _ "golang.org/x/example/hello/reverse"
+)
+
 func init() {
        var x C.int
        fmt.Print(x)

VirrageS avatar Aug 29 '23 12:08 VirrageS

As noted in the description, following program doesn't produce any diff:

package main

import (
	"C"
	"fmt"
)

func init() {
	var x C.int
	fmt.Print(x)
}

VirrageS avatar Aug 29 '23 12:08 VirrageS

Because GCI consider it is a C block as a whole, need some time to think how to fix it.

For now, you should split C import as single.

daixiang0 avatar Sep 18 '23 05:09 daixiang0

@daixiang0 when i use the gci with https://golangci-lint.run/usage/linters/#gci, there is a error like: image image How do i need to configure it? Thanks a lot

SimFG avatar Sep 20 '23 09:09 SimFG

@SimFG I cannot reproduce it:

root:[gci]$ ./dist/gci diff -s standard -s default --custom-order gci-bug/main.go
--- gci-bug/main.go
+++ gci-bug/main.go
@@ -1,7 +1,8 @@
 package main

 import "C"
+
 import (
-       "sort"
        "fmt"
+       "sort"
 )

root:[gci]$ cat gci-bug/main.go
package main

import "C"
import (
        "sort"
        "fmt"
)

daixiang0 avatar Sep 21 '23 00:09 daixiang0

thnaks a lot

SimFG avatar Sep 21 '23 01:09 SimFG