-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat: add fatcontext linter #4583
Conversation
Hey, thank you for opening your first Pull Request ! |
51179bf
to
c9f847d
Compare
Thanks for this linter. I got same issue with a very long ctx in a loop last week |
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements.
Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
I can tell you your linter helped me to spot 4 errors in my code. Thanks |
7662be2
to
bc03389
Compare
First, I understand the goal of this linter. But I have a problem with the fact to "fight" against one of the classical govet rules: shadow The shadowing is a well-known problem, it's weird to suggest shadowing variables instead of just using a dedicated one. |
Contexts are often named E.g.: ctx := context.Background()
for /* cond */ {
ctx := context.WithValue(ctx, "key", "val")
// ...
} will likely become ctx := context.Background()
for /* cond */ {
ctx = context.WithValue(ctx, "key", "val")
// ...
} or even ctx := context.Background()
for /* cond */ {
ctx := context.WithValue(ctx, "key", "val") //nolint
// ...
} where the shadowing would be more appropriate (or the definition of a new variable). In our case, we disabled the shadowing linter on contexts specifically because developers in the team found it too noisy/annoying, not realizing we were exposing ourselves to another kind of issue. Also, we prefer keeping the name |
As I said, I already understand the goal of this linter.
This is your story with shadowing, context, and naming, I understand. To be clear, I suggest not incited to shadow but to fix the problem: inside the message not talking about shadow but about nested context. I know that you implemented I also suggest renaming your linter (ex: fatcontext). |
eaecb28
to
22ea82f
Compare
That's much clearer, thanks for taking the time to explain. I changed the linter according to your suggestions, released v0.2.0 and updated the PR. |
You still have a usage of |
22ea82f
to
f9a76f2
Compare
🤦🏻♂️ fixed in v0.2.2 👍🏻 EDIT: curious, how to suggest fixes with golangci-lint if |
First, at some point, we will support it. Currently, auto-fixing is limited to a few linters:
|
For reference the tracking issue to support |
@bombsimon the topic is in the top 5 of my backlog, for now, I waiting for bug fixes of v1.57 but after that, I will start again with my multiple PRs every day 😸 |
I feel some interception with https://github.com/kkHAIKE/contextcheck 🤔 |
Tested today on our codebase at work, all reports were real cases except for one intentional (a function which returns the context it modifies). It also properly detected known cases. |
@Antonboom |
Hello!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I create a context and want to send it to each element, I think this is not a frequent but valid case, is it not? // start tracing
newCtx, span := otel.Tracer(fmt.Sprintf("saga: %s", s.name)).Start(ctx, fmt.Sprintf("saga: %s", s.name))
defer span.End()
for _, step := range initSteps {
step.ctx = newCtx // nested context in loop (fatcontext)
g.Go(step.Run)
} |
@batazor in this case, it looks like |
As described in a blog post I wrote (https://gabnotes.org/fat-contexts/), we've been bitten by contexts not shadowed inside loops.
This linter addresses this issue.
Github repo: https://github.com/Crocmagnon/fatcontext