Skip to content

Replace context.Background() → t.Context() #3532

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jandubois
Copy link
Member

@jandubois jandubois commented May 12, 2025

To quiet down "usetesting" linter.

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
@jandubois
Copy link
Member Author

I guess this is an argument to switch to Go 1.24: the t.Context() doesn't exist in Go 1.23, but using context.Background() in the test with 1.24 throws a linter error.

@jandubois jandubois marked this pull request as draft May 12, 2025 06:54
@afbjorklund
Copy link
Member

afbjorklund commented May 12, 2025

but using context.Background() in the test with 1.24 throws a linter error.

The linter says that it will disable itself if you run it with an older version?

https://golangci-lint.run/usage/linters/#usetesting

So I guess the entire sub-linter should be removed, until the go version is bumped

i.e. context-background: false

@jandubois
Copy link
Member Author

So I guess the entire sub-linter should be removed, until the go version is bumped

i.e. context-background: false

Makes sense to me; we just need to remember to enable it again when we bump the Go version...

@afbjorklund
Copy link
Member

afbjorklund commented May 12, 2025

I think there were three of them (for go1.24), but not sure if we ran into the others

Templating the linter config sounds like overkill to me, maybe a reminder comment?

@nirs
Copy link
Member

nirs commented May 22, 2025

Is this a real issue? do we need timeouts or cancellation support in the tests? If not the best way is to disable the linter setting or the entire linter. Using all possible linters is not good idea.

@jandubois
Copy link
Member Author

Is this a real issue?

Depends on what you mean by "real".

do we need timeouts or cancellation support in the tests?

No

If not the best way is to disable the linter setting or the entire linter. Using all possible linters is not good idea.

Why not wait until we switch to Go 1.24, apply this PR, and be done with it?

@nirs
Copy link
Member

nirs commented May 22, 2025

Why not wait until we switch to Go 1.24, apply this PR, and be done with it?

Sounds good but it does not solve the linter issue.

@jandubois
Copy link
Member Author

Sounds good but it does not solve the linter issue.

Why wouldn't it? That is the main point of the PR...

@nirs
Copy link
Member

nirs commented May 22, 2025

Sounds good but it does not solve the linter issue.

Why wouldn't it? That is the main point of the PR...

How waiting silences the linter? It is not enabled yet and you want to enable it?

@jandubois
Copy link
Member Author

jandubois commented May 22, 2025

How waiting silences the linter? It is not enabled yet and you want to enable it?

The linter disables itself in 1.23, but will show warnings in 1.24. That's why we can't enable it yet.

Because to silence the warnings in 1.24 we have to make use of features that only exist in 1.24, so will break compilation (and linting) in 1.23.

But once we stop supporting 1.23 we can apply the changes and enable the linter. It will not show warnings in 1.24 because they will be fixed.

This PR makes the changes that require 1.24; it is not compatible with 1.23.

@jandubois
Copy link
Member Author

I recommend that we switch to 1.24 once Lima 1.1.x has shown itself to be stable, e.g. no significant bug reports in the next 2 weeks.

We currently can't upgrade our yq dependency to the latest release because it requires 1.24 (see #3530).

@alexandear
Copy link
Member

I can't reproduce the usetesting issues when running golangci-lint run locally.

Details

$ go version
go version go1.24.3 darwin/arm64

$ golangci-lint version
golangci-lint has version 2.1.6 built with go1.24.2 from eabc263 on 2025-05-04T15:36:41Z

$ golangci-lint cache clean

$ golangci-lint run
pkg/vz/vz_driver_darwin.go:206:2: directive `//nolint:revive // error-strings` is unused for linter "revive" (nolintlint)
        //nolint:revive // error-strings
        ^
1 issues:
* nolintlint: 1

The issues appear only when I run golangci-lint on the downloader package:

$ golangci-lint run pkg/downloader/*.go
pkg/downloader/downloader_test.go:47:23: context.Background() could be replaced by t.Context() in TestDownloadRemote (usetesting)
                        r, err := Download(context.Background(), localPath, dummyRemoteFileURL)
                                           ^
pkg/downloader/downloader_test.go:52:22: context.Background() could be replaced by t.Context() in TestDownloadRemote (usetesting)
                        r, err = Download(context.Background(), localPath, dummyRemoteFileURL)
                                          ^
pkg/downloader/downloader_test.go:59:23: context.Background() could be replaced by t.Context() in TestDownloadRemote (usetesting)
                        _, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
                                           ^
pkg/downloader/downloader_test.go:62:23: context.Background() could be replaced by t.Context() in TestDownloadRemote (usetesting)
                        r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
                                           ^

Perhaps, in this case, golangci-lint can't determine the Go file version and assumes it's Go 1.24.


Could we create a separate issue for upgrading to Go 1.24? That way, we can track all related work and any necessary workarounds in one place.

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.

4 participants