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 various golangci linter issues #977

Merged
merged 1 commit into from Mar 25, 2021

Conversation

katiewasnothere
Copy link
Contributor

@katiewasnothere katiewasnothere commented Mar 18, 2021

This PR fixes the various linter issues discovered in #975.

Many of the fixes involved adding a comment to exclude code from specific linters, see No Lint.

For warnings relating to deadcode, the related code was removed. Warnings that related to ineffectual value assignment for context variables, I excluded the line from the ineffassign and staticcheck linters. This was to help ensure future log statements that may be made in these functions use the correct ctx value. For warnings relating to not checking the error code of a function call, I excluded the errcheck linter for that line, since it was likely intentional to ignore the error. If this is not the case, we can fix the relevant areas.

To see a list of the default checks made with golangci, see here.

Signed-off-by: Kathryn Baldauf kabaldau@microsoft.com

@katiewasnothere katiewasnothere requested a review from a team as a code owner March 18, 2021 02:08
@katiewasnothere
Copy link
Contributor Author

@kevpar PTAL when you get the chance

@kevpar
Copy link
Member

kevpar commented Mar 18, 2021

Any chance for a brief overview of what these are? e.g. are 90% of these "remove unused code"?

@dcantah
Copy link
Contributor

dcantah commented Mar 18, 2021

The fact that our old linter complained about this PR made me giggle

@slonopotamus
Copy link
Contributor

slonopotamus commented Mar 18, 2021

And that's why I didn't want to change linter in #970 but instead make it a separate step :)

image

@katiewasnothere katiewasnothere force-pushed the linter_fixes branch 2 times, most recently from 7dd1134 to b91db64 Compare March 18, 2021 22:33
Copy link
Contributor

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -460,7 +455,7 @@ func (s *service) DiagPid(ctx context.Context, req *shimdiag.PidRequest) (*shimd
if s == nil {
return nil, nil
}
ctx, span := trace.StartSpan(ctx, "DiagPid")
ctx, span := trace.StartSpan(ctx, "DiagPid") //nolint:ineffassign,staticcheck
Copy link
Contributor

Choose a reason for hiding this comment

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

curious; what was the linter complaining about here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It complained because the call to StartSpan returns a new ctx, which isn't used again in the function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah!

would something like this work for those as well?

Suggested change
ctx, span := trace.StartSpan(ctx, "DiagPid") //nolint:ineffassign,staticcheck
_, span := trace.StartSpan(ctx, "DiagPid")

Copy link
Member

Choose a reason for hiding this comment

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

Can we do _, span := ... and remove the nolint?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, @thaJeztah commented right before me :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can't add new functions on the context struct type since it's defined in a different imported package that we don't own.

Copy link
Member

Choose a reason for hiding this comment

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

If we keep the nolint directives in, and then later change the code such that they are no longer needed (e.g. start using ctx), will the linter tell us "hey, this directive isn't doing anything"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not from what I've seen

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong preference here. I would probably go with _, span := ..., and rely on re-creating the ctx variable later if we end up needing it, but leaving the nolint directives in seems okay to me as well.

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 I'm going to keep the nolint directives in for now and if it becomes annoying we can take them out.

}
<-containerExitCh
err = containerExitErr
Copy link
Contributor

Choose a reason for hiding this comment

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

do you know why this was here? (or was this just copy/pasta from another block?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like that was something @jstarks wrote two years ago, so maybe he can comment haha

@kevpar
Copy link
Member

kevpar commented Mar 19, 2021

Any chance for a brief overview of what these are? e.g. are 90% of these "remove unused code"?

@katiewasnothere just wanted to make sure you saw this.

@katiewasnothere
Copy link
Contributor Author

katiewasnothere commented Mar 19, 2021

Any chance for a brief overview of what these are? e.g. are 90% of these "remove unused code"?

@kevpar I updated the PR description with some info. Is there something else that I should add that would make it clearer?

Spec: s,
HostingSystem: parent,
NetworkNamespace: netNS,
ScaleCPULimitsToSandbox: shimOpts.ScaleCpuLimitsToSandbox,
Copy link
Member

Choose a reason for hiding this comment

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

Why did we remove this assignment?

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 linter was complaining because below we check if shimOpts is nil, so it didn't like this direct access to it. But also, we set it additionally right below this struct creation, so we only need one :D

Copy link
Member

Choose a reason for hiding this comment

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

I was very confused, because I thought we already fixed this in #913... Turns out the issue was re-introduced, probably due to a bad merge, here. :(

Copy link
Contributor

Choose a reason for hiding this comment

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

I cry

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like it was noticed just before / after merge 🙈 #915 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought I force pushed to fix before checking in 😭 😭 😭. Well I've gotta give it to myself, I both fixed it and then broke it again. This deserves a medal

Copy link
Contributor

Choose a reason for hiding this comment

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

💩 happens; you did that to test if the linters work, correct? 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

100%, don't let anyone tell you otherwise 😉

cmd/ncproxy/main.go Outdated Show resolved Hide resolved
@@ -572,10 +566,12 @@ func startContainerShim(c *container, pidFile, logFile string) error {
if err != nil {
return err
}
defer shim.Release()
defer func() {
_ = shim.Release()
Copy link
Member

Choose a reason for hiding this comment

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

This is obnoxious. Maybe we need to think about turning off the error check lint in the future.

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 it's valuable to have on. I think we should just find a better way to handle cases where we don't really care about the error. Maybe log a warning? Probably depends on the case though.

Signed-off-by: Kathryn Baldauf <kabaldau@microsoft.com>
@katiewasnothere katiewasnothere merged commit f496574 into microsoft:master Mar 25, 2021
@katiewasnothere katiewasnothere deleted the linter_fixes branch March 25, 2021 21:49
@thaJeztah
Copy link
Contributor

Thanks @katiewasnothere !!

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.

None yet

7 participants