Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gha: add golangci-lint job, fix warnings; bump API version #46

Merged
merged 14 commits into from
Feb 17, 2021

Conversation

kolyshkin
Copy link
Contributor

@kolyshkin kolyshkin commented Feb 10, 2021

This

  • reworks the source layout to ensure linters don't choke;
  • slightly improves Makefile and running tests;
  • introduces golangci-lint from Makefile as well as for GitHub Actions;
  • fixes all golangci-lint warnings found;
    • ⚠️ this includes bumping API to v5 since we changed the prototypes;
  • adds a small go mod tidy verify job to GHA;
  • enables some more linters and fixes their warnings (suggested by @rst0git)

See individual commits for more details.

Description for changelog:

 - API bumped to v5
 - methods (such as Dump, Restore etc.) now require a pointer to rpc.CriuOpts as the first argument

@kolyshkin kolyshkin changed the title gha: add golangci-lint job [WIP] gha: add golangci-lint job Feb 10, 2021
@kolyshkin
Copy link
Contributor Author

OK, this can't be done until we have a mix of .c and .go files. Trying to fix...

This is to fix a warning from golangci-lint. As we do not have a way to
return the error here, deliberately ignore it.

Signed-off-by: Kir Kolyshkin <[email protected]>
1. Use log instead of fmt.Printf().

2. Use log.Fatal* that does os.Exit(1) for us.

3. Always print the err on error.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin force-pushed the golangci branch 4 times, most recently from 7f3f204 to 48407e2 Compare February 16, 2021 00:37
1. Remove the 'install.tools' target. In different version of Golang,
   "go get" has different meaning.  In particular, since go 1.13 or so,
   go get in a package directory adds the new package to go.mod/go.sum.
   We don't want do add golint into magic-gen deps, so let's remove it,
   as well as the machinery to find out GOBIN.

2. Remove the 'lint' target. Linting will be done from the main Makefile
   in the next commit.

Signed-off-by: Kir Kolyshkin <[email protected]>
Many Go source code tools, including linters, do not like when
the sources for multiple binaries reside in the same directory.
They don't like it when the unrelated .c files are found, too.

Reorganise the source tree to follow the "one binary -- one directory"
rule, which allows us to use golangci-lint.

Now,

1. Enable golangci-lint from Makefile.

2. Improve piggie handling in Makefile. In particular:
   - do not rely on `pidof piggie`, get it from the run;
   - always kill piggie instance(s) at the end.

   NOTE that we can't use $PID for kill because after restore
   the PID is different.

3. Remove the 'install.tools' target. In different version of
   Golang, "go get" has different meaning.  In particular, since go
   1.13 or so, go get in a package directory adds the new package to
   go.mod/go.sum.  We don't want do add golint into magic-gen deps, so
   let's remove it, as well as the machinery to find out GOBIN.

4. Add removing the image dir after successful test.

5. Fix the phony targets list.

6. Remove @ from some commands, as we want to see what's happening.

Signed-off-by: Kir Kolyshkin <[email protected]>
...and add a badge to README.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin force-pushed the golangci branch 2 times, most recently from 84d2799 to ffc8aa6 Compare February 16, 2021 00:53
@kolyshkin kolyshkin changed the title [WIP] gha: add golangci-lint job gha: add golangci-lint job, fix warnings Feb 16, 2021
This fixes warnings like this one:

> main.go:12:2: SA1019: package github.com/golang/protobuf/proto is deprecated: Use the "google.golang.org/protobuf/proto" package instead. (staticcheck)
> 	"github.com/golang/protobuf/proto"
> 	^

(and there's no functional or API change as far as I can see).

Note that github.org/google/protobuf is still there in go.mod
as it is used by the .pb.go files. Apparently it is there only to ensure
backward compatibility with something like:

> import proto "github.com/golang/protobuf/proto"
> // This is a compile-time assertion that a sufficiently up-to-date version
> // of the legacy proto package is being used.
> const _ = proto.ProtoPackageIsVersion4

Signed-off-by: Kir Kolyshkin <[email protected]>
rpc.CriuOpts contains a mutex and therefore should not ever be passed by
value (the other reason is it probably uses too much stack space --
though I haven't checked that).

This fixes a bunch of warnings from govet and copylocks linters, like:

> main.go:190:26: copylocks: Dump passes lock by value: github.com/checkpoint-restore/go-criu/v4/rpc.CriuOpts contains google.golang.org/protobuf/internal/impl.MessageState contains sync.Mutex (govet)
> func (c *Criu) Dump(opts rpc.CriuOpts, nfy Notify) error {
>                          ^
> main.go:195:29: copylocks: Restore passes lock by value: github.com/checkpoint-restore/go-criu/v4/rpc.CriuOpts contains google.golang.org/protobuf/internal/impl.MessageState contains sync.Mutex (govet)
> func (c *Criu) Restore(opts rpc.CriuOpts, nfy Notify) error {
>                             ^

Fix the prototypes, and adjust all the callers accordingly.

Finally, as this changes the public API, bump the version to v5.

Signed-off-by: Kir Kolyshkin <[email protected]>
The meaning of it is to make sure `go mod tidy` was run after
any module-related changes.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin changed the title gha: add golangci-lint job, fix warnings gha: add golangci-lint job, fix warnings; bump API version Feb 16, 2021
@kolyshkin
Copy link
Contributor Author

@adrianreber @rst0git PTAL. Unfortunately I had to change the public API and thus bump v4 to v5 -- I see no way around it.

@kolyshkin kolyshkin mentioned this pull request Feb 16, 2021
@adrianreber
Copy link
Member

Thanks. Looks good to me. I have no problem doing a v5 based release. @rst0git what do you think about these changes?

Makefile Show resolved Hide resolved
Brought to you by gofumpt v0.1.0.

Signed-off-by: Kir Kolyshkin <[email protected]>
> main.go:48:9: G204: Subprocess launched with function call as argument or cmd arguments (gosec)
> 	cmd := exec.Command(c.swrkPath, args...)
>	       ^

This warning is targeted more towards the code audit.

Signed-off-by: Kir Kolyshkin <[email protected]>
> test/main.go:27:2: G109: Potential Integer overflow made by strconv.Atoi result conversion to int16/32 (gosec)
> 	pid, _ := strconv.Atoi(pidS)
>	^

Add error handling while at it.

Signed-off-by: Kir Kolyshkin <[email protected]>
> test/main.go:33:49: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
> 		return fmt.Errorf("can't open image dir: %v", err)
> 		                                              ^
> test/main.go:55:38: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
> 		return fmt.Errorf("dump fail: %v", err)
> 		                                   ^

Note that %w requires go >= 1.13
(https://blog.golang.org/go1.13-errors).

Signed-off-by: Kir Kolyshkin <[email protected]>
> phaul/images.go:14:48: preparePhaulImages - result 1 (error) is always nil (unparam)
> func preparePhaulImages(wdir string) (*images, error) {
>                                                ^

Signed-off-by: Kir Kolyshkin <[email protected]>
1. Skip go code which is autogenerated.

2. In addition to the default set of enabled linters, enable
   some more, from the following presets:
 - bugs
 - performance
 - unused
 - format

This actually enables the following additional linters:
 - asciicheck
 - bodyclose
 - errorlint
 - exhaustive
 - exportloopref
 - gofmt
 - gofumpt
 - goimports
 - gosec (gas)
 - makezero
 - maligned
 - noctx
 - prealloc
 - rowserrcheck
 - scopelint
 - sqlclosecheck
 - unparam

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
Copy link
Contributor Author

(CI for 1.16-rc1 is broken as it was removed; fix in #47)

@rst0git rst0git merged commit d7b552a into checkpoint-restore:master Feb 17, 2021
@adrianreber
Copy link
Member

@rst0git Do you want to tag the new release? 5.0.0?

@kolyshkin kolyshkin mentioned this pull request Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants