Skip to content

gci: Add trailing slash to local module #4608

Closed
@StephenBrown2

Description

@StephenBrown2

Welcome

Description of the problem

In my company's package, there are a few repositories that are named similarly, because they are related, and one is imported in the other (for example, a separate proto repository).

e.g.:
Main program: github.com/Company/main-program
Dependency: github.com/Company/main-program-dependency

This unfortunately confuses gci when trying to use the localmodule section, as imports from both repos get grouped as one.

e.g:

import (
  "fmt"
  "errors"

  "github.com/Company/main-program/package"
  "github.com/Company/main-program-dependency/other"
)

When they should be separate, e.g:

import (
  "fmt"
  "errors"

  "github.com/Company/main-program-dependency/other"

  "github.com/Company/main-program/package"
)

Adding the local module as it's own custom section with the trailing slash gets around this (i.e. prefix("github.com/Company/main-program/")), but I would prefer to rely in the linter's detection so I can use the same config across multiple repos in our org.

This could be done by modifying https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/gci.go#L169 to be:

v.Path = info.Path + "/"

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version v1.57.3-0.20240402181201-2dd62d125014 built with go1.21.8 from (unknown, modified: ?, mod sum: "h1:jmz5tBAul6h97PilGm8Sd+n9wWfIWduO67xL9AAOF0k=") on (unknown)

Configuration

---
linters:
  enable:
    - gci
linters-settings:
  gci:
    sections:
      - standard
      - default
      - blank
      - dot
      - "prefix(github.com/Company/)"
      - localmodule # Package name being "github.com/Company/main-program"

Go environment

$ go version && go env
go version go1.21.8 linux/amd64
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY='github.com/Company'
GONOSUMDB='github.com/Company'
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE='github.com/Company'
GOPROXY='direct'
GOROOT='/usr/lib/golang'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/golang/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.8'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2947470614=/tmp/go-build -gno-record-gcc-switches'

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
# paste output here

A minimal reproducible example or link to a public repository

An example, non-functioning, code sample.

package main

import (
	"fmt"

	"github.com/Company/main-program-dependency/other"
	"github.com/Company/main-program/pkg"
)

func main() {
	fmt.Println("Hello, World!")
	pkg.Package(other.Name)
}

Expected:

package main

import (
	"fmt"

	"github.com/Company/main-program-dependency/other"

	"github.com/Company/main-program/pkg"
)

func main() {
	fmt.Println("Hello, World!")
	pkg.Package(other.Name)
}

Validation

  • Yes, I've included all information above (version, config, etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    dependenciesRelates to an upstream dependency

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions