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

fix some linting failures and configure golangci-lint #922

Merged
merged 14 commits into from
Jun 6, 2024

Conversation

dklimpel
Copy link
Contributor

@dklimpel dklimpel commented May 22, 2024

Checklist
  • make test-all (UNIX) passes. CI will also test this
  • unit and/or integration tests are included (if applicable)
  • documentation is changed or added (if applicable)

Description of change

Fix some linting failures and add new go linting to makefile.

I am not a go expert. Please have a deeper look inside.

See a list of lint failures at:

Copy link
Member

@aelsabbahy aelsabbahy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this, l wonder if CI should run make lint to be consistent with local dev story.

For example, this PR is currently passing the golangci-lint, but failing Travis do to the gofmt check. Ideally, GitHub actions would catch both.

@@ -73,7 +77,9 @@ func (s *ServiceSystemd) Running() (bool, error) {
return false, nil
}
cmd := util.NewCommand("systemctl", "-q", "is-active", s.service)
cmd.Run()
if err := cmd.Run(); err != nil {
return false, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should probably bubble up the error:

return false, err

golangci-lint:
$(info INFO: Starting build $@)
go install github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint run --timeout 5m $(pkgs)

lint:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we change this to also run golangci-lint.

lint: golangci-lint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think golint does not exist anymore and golint can be replaced by golangci-lint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, didn't even notice that, yeah.. we can replace golint in the lint target with golangci-lint that makes more sense.

So much has changed in the go ecosystem since Goss was originally written.

@dklimpel dklimpel marked this pull request as draft May 23, 2024 05:11
@dklimpel
Copy link
Contributor Author

For example, this PR is currently passing the golangci-lint, but failing Travis do to the gofmt check. Ideally, GitHub actions would catch both.

I have added fmt to pipeline.
Github currently does not find it because it only looks at modified code, and this line is currently not included in the diff. If you check everything, the pipeline fails always until all errors are fixed. A bit of a dilemma.

only-new-issues: true

@dklimpel
Copy link
Contributor Author

I do not not knwo why it is failing.

image

Perhaps a Travis CI problem :(

Copy link
Member

@aelsabbahy aelsabbahy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, small comment on golint and make targets.

The travis-ci is failing because cmd.Run() will return an error on non-zero exit status. So would need to either:

  1. Ignore the errors and skip linting those calls
  2. Add a check for ExitError and ignore it
  3. Refactor cmd.Run() to ignore ExitError and the consumer code has to do a check for Status if they care.

golangci-lint:
$(info INFO: Starting build $@)
go install github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint run --timeout 5m $(pkgs)

lint:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, didn't even notice that, yeah.. we can replace golint in the lint target with golangci-lint that makes more sense.

So much has changed in the go ecosystem since Goss was originally written.

Copy link
Member

@aelsabbahy aelsabbahy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can promote this out of draft.

At some point, I'll run a golangci-lint run and fix all the findings.

curious, what are the pros/cons of using golangci-lint-action vs just running make lint in a github action?

I assume the former provides some unique benefits/flags, but requires making sure the local make lint and the job are in sync, (same version, same flags, etc.).

Anyways, correct me if I'm wrong, but putting everything behind make should make it easier to move off of github if that ends up being another transition in the future?

.github/workflows/golangci-lint.yaml Show resolved Hide resolved
Comment on lines -54 to +56
ctx := context.WithValue(context.Background(), "id", a.ID())
ctx := context.WithValue(context.Background(), idKey{}, a.ID())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was:

resource/addr.go:54:49: SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
	ctx := context.WithValue(context.Background(), "id", a.ID())

@dklimpel
Copy link
Contributor Author

Looks great, small comment on golint and make targets.

The travis-ci is failing because cmd.Run() will return an error on non-zero exit status. So would need to either:

  1. Ignore the errors and skip linting those calls
  2. Add a check for ExitError and ignore it
  3. Refactor cmd.Run() to ignore ExitError and the consumer code has to do a check for Status if they care.

I have decided to "Ignore the errors and skip linting those calls"

@dklimpel
Copy link
Contributor Author

curious, what are the pros/cons of using golangci-lint-action vs just running make lint in a github action?

I assume the former provides some unique benefits/flags, but requires making sure the local make lint and the job are in sync, (same version, same flags, etc.).

Anyways, correct me if I'm wrong, but putting everything behind make should make it easier to move off of github if that ends up being another transition in the future?

I think the github actions are actively developed further and certainly optimised for performance and caching. For this you have to maintain makefile and action twice. The github action also runs without problems on forked branches. With the makefile, each developer has to make sure that it works, which hopefully it does out of the box.

Anyways, correct me if I'm wrong, but putting everything behind make should make it easier to move off of github if that ends up being another transition in the future?

I think so, too.
The dual maintenance is currently limited to the golint version

@dklimpel
Copy link
Contributor Author

Sorry, the PR has become a little more complicated than planned. In the end, the currently configured linting has now been fully implemented and the pipeline no longer only needs to check the diff (only-new-issues: true)

@dklimpel dklimpel marked this pull request as ready for review May 31, 2024 17:39
@dklimpel dklimpel changed the title fix some linting failures fix some linting failures and configure golangci-lint May 31, 2024
Copy link
Member

@aelsabbahy aelsabbahy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I assume this is ready to merge.

I can enable some of the commented out lintes and fix some of the issues as follow up PRs.

marked this as approved, if you're ready for this to be merged just post a comment here and I'll merge it.

@aelsabbahy
Copy link
Member

Minor question:

The github action also runs without problems on forked branches. With the makefile, each developer has to make sure that it works, which hopefully it does out of the box.

Why does make lint not work on forked branches in github?

@dklimpel
Copy link
Contributor Author

dklimpel commented Jun 4, 2024

marked this as approved, if you're ready for this to be merged just post a comment here and I'll merge it.

Yes, is ready for merging.

@dklimpel
Copy link
Contributor Author

dklimpel commented Jun 4, 2024

Why does make lint not work on forked branches in github?

I think I was thinking too much. The crucial point is that the Travis CI only runs when you create a PR. Otherwise you would have to have your own Travis account. But for development it would also be good to run the CI beforehand creating a PR. For example, I sometimes develop directly in GitHub without a computer that has make and co installed. It's good to get direct feedback.

@aelsabbahy aelsabbahy merged commit 6a8fc96 into goss-org:master Jun 6, 2024
2 checks passed
@dklimpel dklimpel deleted the lint_code branch June 7, 2024 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants