Enable a linter that will force consistent commas for expressions

The following discussion from !2141 (merged) should be addressed:

  • @ggeorgiev_ started a discussion: (+1 comment)

    @pedropombeiro I left a few comments about inconsistent commas, however, I don't feel strongly about them and we could leave them as is. Instead, is there a linter that could force such consistency on us?


Example:

A

	var a []int
	a = append(
		a,
		1,
		2,
		3,
	)

B:

	var a []int
	a = append(
		a,
		1,
		2,
		3)

The linter should force either one of these styles on us

Proposal

Desired logic

Build a linter plugin that implements the following logic:

  • For multi-line function declarations:
    • Report when arguments are not listed separately on each line.
    • Report when arguments are listed separately on each line, but the last argument isn't followed by a comma/new-line.
    • Report when it could fit on a single line.
  • For multi-line call expressions:
    • Report when first argument is on same line as function call.
    • Report when last argument isn't followed by a comma/new-line.

Optional: Auto-fix

A helpful tool render a Go AST is http://goast.yuroyoro.net/

Deployment

That's the easy part. That less clear cut part is how to build, deploy and maintain a custom plugin that is not merged into the golangci-lint codebase.

Basically there are 2 hurdles:

  • the regular golangci-lint that people normally have installed won’t work as it can’t run plugins, you need to build from source with CGO_ENABLED=1.
  • both the linter and plugins must be built with the same Go compiler version
  • more generally, whatever dependencies a linter plugin uses must have exactly the same go.mod versions as the host golangci-lint

The proposal is to build both golangci-lint and the plugin in a Docker image, pushed that to a Docker repository and use that instead of the official image in the code_quality job. That will give us assistance in CI jobs which will help already with MR reviews.

Follow-up issue

The follow-up issue is handling local (non-CI) linting. We'll need to build and maintain and distribute a golangci-lint executable and linter library for Linux and Darwin.

Edited by Pedro Pombeiro