gogithub-actionslintergofmt

File is not `gofmt`-ed with `-s`: why is this happening and how to resolve it?


We use a linter (for Golang) that run through a Github Actions workflow every time we open or update a Pull Request on our repository.

It recently started to return the following error:

File is not `gofmt`-ed with `-s` (gofmt)

After what happened in this other PR to the file pkg/api/api/go.
(EDIT: link added to evaluate and eventually reproduce the error)

Evidences:

original commit

linter output

I would like to understand what was the source of this error, as well as how to resolve it?


Solution

  • Source of the error

    It seems this error can be returned when the file is not properly formatted according to Go rules.

    For example: If you accidentally used tab indentation rather than spaces.

    EDIT: blackgreen's answer gives more accurate details about the source of the error


    How to resolve it

    You can use the following Go command:

    gofmt -s -w <path_to_file>.go

    ... then commit the code.

    Note that in my case: gofmt -w pkg/api/api.go was enough to resolve the problem (without the -s flag, which I found strange as the error specifically asked for the -s).

    Source 1 + Source 2