This document describes different linters & formatters that are applied in this project and how to work with them in local development.
All linters also run on our CI pipelines - see the dedicated Github Actions for implementation.
The Go code in this project is linted using the golangci-lint linters aggregator, which covers a wide range of linting issues.
All the default linters are turned on, as well as a few additional ones. You may inspect the configuration file used and compare it to the list of linters and their description by golangci-lint
.
Thanks to .vscode/settings.json, linting should work automatically in VSCode. The --fix
flag will try to solve linting issues automatically, however please note that sometimes it will not be able to do so (e.g. bad variable naming).
If you are experiencing issues or suspect linting is not working correctly, go to Code -> Settings/Preferences... -> Settings, and then search for "go lint tool". Then,
- Under Go: Lint Tool make sure "golangci-lint" is chosen,
- Under Go: Lint On Save make sure "package" is chosen.
The linter should discover the configuration file automatically.
In case you want to invoke golangci-lint
from the command line, you may install it with
brew install golangci-lint
brew upgrade golangci-lint
Then, simply run golangci-lint run ./...
in any of the Go modules in this project to obtain a full list of linting errors, if exist. You may pass the --fix
flag here as well in order to fix issues automatically where possible.
NOTE if you encounter an error such as
ERRO Running error: 1 error occurred:
* can't run linter goanalysis_metalinter: goimports: can't extract issues from gofmt diff output...
Try to install/update diffutils
with brew install diffutils
, then try again (source).
It seems that gofmt
(which run as part of golangci-lint
) is against max line length formatting (see Github issue).
However, one of the linters, lll
, is configured to report lines longer than 80 characters as a linting issue.
(Adapted from here)
In order to automatically break long lines in VSCode, do the following:
- Install
golines
(Github) by running
go install github.com/segmentio/golines@latest
- Make sure your
PATH
includes yourGOPATH
- in your.zshrc
(or compatible), add
export GOPATH="$HOME/go"
PATH="$GOPATH/bin:$PATH"
- Install the
Run On Save
VSCode plugin - In VSCode, go to Code -> Settings/Preferences... -> Settings, and then search for "runonsave". Under Emeraldwalk: Runonsave, click the link to edit on
settings.json
, and modify it to include the following:
// rest of config...
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.go$",
"cmd": "golines ${file} -w -m 80"
}
]
}
// rest of config...
- Restart VSCode; your files should now be automatically formatted according to the linter rules.
We use Hadolint in order to keep our Dockerfiles linted. It implements the Best practices for writing Dockerfiles from the official Docker documentation.
Install Hadolint on your local machine:
brew install hadolint
Run on our single current Dockerfile:
hadolint Dockerfiles/amd64/Dockerfile
Install this VSCode extension in order to see linting warnings & errors right within the IDE. You need to install Hadolint on your machine in order for this extension to work.