Skip to content

Add go mod init to quick-start #1835

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/docs/usage/faq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ Long answer:

Because the first run caches type information. All subsequent runs will be fast.
Usually this options is used during development on local machine and compilation was already performed.

## How does `golangci-lint` find go source files?

`golangci-lint` uses go modules to find golang files. If your project is a go module, it should *just work*.
Copy link
Member

@ldez ldez Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golangci-lint uses go modules to find golang files.

This is false or at least not precise: the use of go modules by default is related to the behavior of your local go version.

go1.11
$ go version
go version go1.11.13 linux/amd64
$ env | rg GO111MODULE
$ go env | grep GO111MODULE
$ golangci-lint run
go1.12
$ go version
go version go1.12.17 linux/amd64
$ env | rg GO111MODULE
$ go env | grep GO111MODULE
$ golangci-lint run
go1.13
$ go version
go version go1.13.15 linux/amd64
$ env | rg GO111MODULE
$ go env | grep GO111MODULE
GO111MODULE=""
$ golangci-lint run
go1.14
$ go version
go version go1.14.15 linux/amd64
$ env | rg GO111MODULE
$ go env | grep GO111MODULE
GO111MODULE=""
$ golangci-lint run
go1.15
$ go version
go version go1.15.9 linux/amd64
$ env | rg GO111MODULE
$ go env | grep GO111MODULE
GO111MODULE=""
$ golangci-lint run
go1.16
$ go version
go version go1.16.1 linux/amd64
$ go env | grep GO111MODULE
GO111MODULE=""
$ golangci-lint run

ERRO Running error: context loading failed: no go files to analyze 

In go1.16 the go modules are on by default instead of auto or off

Also note:

We plan to drop support for GOPATH mode in Go 1.17. In other words, Go 1.17 will ignore GO111MODULE.

https://blog.golang.org/go116-module-changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this specifically state this applies to go 1.16 and up?

Copy link
Member

@ldez ldez Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relation between the local go version and GO111MODULE exists since go1.11.

If GO111MODULE is on, even with go<1.16, golangci-lint will use Go modules.

go1.11
$ go version
go version go1.11.13 linux/amd64
$ GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: go: cannot determine module path for source directory /home/ldez/sources/go/src/github.com/golangci/sandbox (outside GOPATH, no import comments) 
go1.12
$ go version
go version go1.12.17 linux/amd64
$GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: go: cannot determine module path for source directory /home/ldez/sources/go/src/github.com/golangci/sandbox (outside GOPATH, no import comments) 
go1.13
$ go version
go version go1.13.15 linux/amd64
$ GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: no go files to analyze 
go1.14
$ go version
go version go1.14.15 linux/amd64
$ GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: no go files to analyze
go1.15
$ go version
go version go1.15.9 linux/amd64
$ GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: no go files to analyze
go1.16
$ go version
go version go1.16.1 linux/amd64
$ GO111MODULE=on golangci-lint run
ERRO Running error: context loading failed: no go files to analyze

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be precise, golangci-lint relies on Go internal behavior, there is no GO111MODULE related code inside golangci-lint code base.


If your project is not a go module, you can still use `golangci-lint` for your project. You can do this by setting the environment variable `GO111MODULE=off` when you run `golangci-lint`. Make sure you set this in your CI toolchain appropriately.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your project is not a go module, you can still use golangci-lint for your project.

Same thing here.