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

2

12

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?

Tenerife answered 3/9, 2021 at 18:21 Comment(2)
I fixed it by running: golangci-lint run --fixDash
the solution here fixed my issue.Hebrides
T
30

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

Tenerife answered 3/9, 2021 at 18:21 Comment(3)
If you found a way to demonstrate the problem you should add it to the question; that will probably get it reopened.Administrator
Anybody knows if there is a way to automatically enable this in jetbrains goland, so the command don't need to be executed ?Nailbrush
@Nailbrush Configure it to reformat your code on save and then to use go fmt on reformatting code in the code style section: https://mcmap.net/q/210665/-goland-how-to-use-gofmtNierman
P
9

The -s flag in gofmt has nothing to do with formatting. It's about simplifying the code:

Try to simplify code (after applying the rewrite rule, if any).

The warning you see comes from the linter golangci-lint. Since you claim to have fixed the error by running gofmt -w, the presence of the hint "with -s" may be due to this bug: https://github.com/golangci/golangci-lint/issues/513.

The linked issue was fixed in 2019 and released with v1.17.0. You might want to check if your pipeline is using an older version.

Assuming that your file pkg/api/api.go triggered the warning just because it was not formatted, gofmt -w solves the issue because -w overwrites the file:

If a file's formatting is different from gofmt's, overwrite it with gofmt's version.

Pilkington answered 3/9, 2021 at 21:38 Comment(2)
how come the formatting went different? I have like 100 of files in the repo which I just checked out, is the only solution to gofmt on each file individually? I am using windows system in case if thats screwing the file formatting.Cung
@ObaidMaroof you don't have to reformat each file individually. You can supply directory paths to gofmt or use shell expansionPilkington

© 2022 - 2024 — McMap. All rights reserved.