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

Linter for incorrect var capture #104835

Closed
wants to merge 1 commit into from

Conversation

uthark
Copy link
Contributor

@uthark uthark commented Sep 8, 2021

What type of PR is this?

/kind bug

What this PR does / why we need it:

See #98213

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Just enabling linter and fixing few flakes.
Thinking of a better way to introduce an allowlist for existing violations. Seems like golangci doesn't support it (See upstream issue golangci/golangci-lint#913)

Does this PR introduce a user-facing change?

No.


Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 8, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @uthark. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 8, 2021
@k8s-ci-robot k8s-ci-robot added area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 8, 2021
@omertuc
Copy link
Contributor

omertuc commented Sep 8, 2021

I'm not entirely qualified so take everything I say with a grain of salt, but the way I understand things is -

exportloopref may be an over-sensitive solution to this (#98213) problem. iiuc sometimes it's reasonable to pass &loopvar to a function to avoid a copy, it's not always a bad thing. The issues start when that function actually stores that address and have it be used after the loop var is already changed on the next iterations or completely gone.

Same thing can be said about closures capturing the loop var and then being ran only after the loop var is already changed on the next iterations - which is what happens with Ginkgo - it "collects" all the closures created by the loop and runs them only after the loop is done, and at that point the loop var contains the final iterable value, and thus the same value is being used by all the closures that were created. But in contrast, when using the built-in go testing package - there's a similar common pattern of creating test-case closures inside a loop with the closures referencing the loop var. But with the built-in go testing package this is (mostly*) not a problem because the closures get to run before the loop iteration is over. They're not being collected like in Ginkgo. So, if I understand correctly, exportloopref would incorrectly flag those legitimate uses as linter violations.


*I think the built in go testing package allows running tests in parallel, I think that may actually start to cause problems, not sure about this though

Copy link
Member

@neolit123 neolit123 left a comment

Choose a reason for hiding this comment

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

SGTM, but up to the /hack maintainers to decide.

/ok-to-test

- unused
- varcheck
- ineffassign
- exportloopref
Copy link
Member

Choose a reason for hiding this comment

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

exportloopref seems to be a new addition?

-E unused \
-E varcheck \
-E ineffassign
golangci-lint run --config "${KUBE_ROOT}/hack/.golangci.yaml"
Copy link
Member

Choose a reason for hiding this comment

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

my take: the benefit is minimal at this point, but having a config vs flags seems better in general.

@@ -121,6 +121,7 @@ func CheckAuditList(el auditinternal.EventList, expected []AuditEvent) (missing
expectations := newAuditEventTracker(expected)

for _, e := range el.Items {
e := e
Copy link
Member

Choose a reason for hiding this comment

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

these "add local var in a loop" changes seem fine.
great if there aren't more.

Copy link
Member

Choose a reason for hiding this comment

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

hm, we pass e by ref on line below. And this may result in bad behavior if testEventFromInternal will run any goroutine or will keep the ref for later use.

Copy link
Member

Choose a reason for hiding this comment

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

/hold

This may lead to an issue, #106371, is the change forced by linter?

Copy link
Member

Choose a reason for hiding this comment

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

that issue fixed exactly this problem, we have to capture the variable or the reference will only use the last value, or am I getting wrong your concern?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I read the PR backwards. Adding it makes a lot of sense. Sorry for the noise

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 12, 2021
@neolit123
Copy link
Member

/release-note-none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Oct 12, 2021
@@ -0,0 +1,12 @@
---
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 should put a comment at the top pointing to the upstream docs for this config format?

Copy link
Member

Choose a reason for hiding this comment

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

this got split out in another PR working on some other class of lints, upon rebase the change in this file should just be adding the new linter 😅

@BenTheElder
Copy link
Member

/approve
/retest

@BenTheElder
Copy link
Member

/triage accepted
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Nov 9, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BenTheElder, uthark

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 9, 2021
@BenTheElder
Copy link
Member

xref another golangci PR that will have merge conflicts #103723

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Nov 9, 2021

@uthark: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-verify 703a107 link true /test pull-kubernetes-verify

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 15, 2021
@aojea
Copy link
Member

aojea commented Nov 16, 2021

there are a few more issues that need to be fixed

running golangci-lint 
test/e2e/framework/pods.go:271:11: exporting a pointer for the loop variable e (exportloopref)
				ev = &e
				      ^
test/e2e/framework/util.go:1394:50: exporting a pointer for the loop variable watchEventIndex (exportloopref)
					foundEventIndexes[actualWatchEventIndex] = &watchEventIndex
					                                            ^
test/e2e/apimachinery/custom_resource_definition.go:207:15: exporting a pointer for the loop variable g (exportloopref)
					group = &g
					         ^
test/e2e/apimachinery/custom_resource_definition.go:217:17: exporting a pointer for the loop variable v (exportloopref)
					version = &v
					           ^
test/e2e/apimachinery/custom_resource_definition.go:235:17: exporting a pointer for the loop variable v (exportloopref)
					version = &v
					           ^
test/e2e/apimachinery/webhook.go:125:15: exporting a pointer for the loop variable g (exportloopref)
					group = &g
					         ^
test/e2e/apimachinery/webhook.go:135:17: exporting a pointer for the loop variable v (exportloopref)
					version = &v
					           ^
test/e2e/auth/service_accounts.go:568:43: exporting a pointer for the loop variable tc (exportloopref)
				pod.Spec.SecurityContext.RunAsUser = &tc.wantUID
				                                      ^
test/e2e/auth/service_accounts.go:571:41: exporting a pointer for the loop variable tc (exportloopref)
				pod.Spec.SecurityContext.FSGroup = &tc.wantGID
				                                    ^
test/e2e/autoscaling/cluster_size_autoscaling.go:1253:27: exporting a pointer for the loop variable node (exportloopref)
			nodes = append(nodes, &node)
			                       ^
test/e2e/storage/nfs_persistent_volume-disruptive.go:126:20: exporting a pointer for the loop variable node (exportloopref)
					clientNode = &node
					              ^
test/e2e/storage/volume_provisioning.go:971:42: exporting a pointer for the loop variable pv (exportloopref)
				remainingPVs = append(remainingPVs, &pv)
				                                     ^
pkg/proxy/userspace/proxier_test.go:117:19: exporting a pointer for the loop variable port (exportloopref)
			servicePort = &port
			               ^
plugin/pkg/admission/securitycontext/scdeny/admission_test.go:143:31: exporting a pointer for the loop variable test (exportloopref)
		pod.Spec.SecurityContext = &test.securityContext
		                            ^
test/e2e_node/restart_test.go:57:39: exporting a pointer for the loop variable pod (exportloopref)
			runningPods = append(runningPods, &pod)
			                                   ^
+++ exit code: 1
+++ error: 1
FAILED   verify-golangci-lint.sh	436s

@k8s-ci-robot
Copy link
Contributor

@uthark: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 17, 2021
@dims
Copy link
Member

dims commented Jan 14, 2022

this PR has been on hold for about 2 weeks. Can you please see how to unblock this by reviewing comments etc? or please close this out (Trying to declutter our open list of PRs)

@dims
Copy link
Member

dims commented Feb 11, 2022

This PR is older than 4 weeks and needs as rebase.

Please rebase the PR against latest master and reopen if still needed.

/close

@k8s-ci-robot
Copy link
Contributor

@dims: Closed this PR.

In response to this:

This PR is older than 4 weeks and needs as rebase.

Please rebase the PR against latest master and reopen if still needed.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. kind/bug Categorizes issue or PR as related to a bug. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants