简体   繁体   中英

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:

原始提交

短绒输出

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM