ctxbackground: resolve Background() calls by package identity, not identifier text#38854
ctxbackground: resolve Background() calls by package identity, not identifier text#38854Copilot wants to merge 4 commits into
Background() calls by package identity, not identifier text#38854Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Background() calls by package identity, not identifier text
There was a problem hiding this comment.
Pull request overview
This PR improves the ctxbackground analyzer so context.Background() calls are detected by resolving the selector base to the imported package (types.PkgName → imported package path), rather than relying on the literal identifier text context, eliminating alias-import false negatives and shadowing false positives.
Changes:
- Updated
isContextBackgroundCallto take*analysis.Passand usepass.TypesInfo.ObjectOf+*types.PkgNameresolution to confirm the stdlib"context"package. - Added regression coverage for aliased imports and shadowed
contextidentifiers in linter testdata. - Updated golden output for suggested-fix expectations.
Show a summary per file
| File | Description |
|---|---|
| pkg/linters/ctxbackground/ctxbackground.go | Switch context.Background() detection to package-identity resolution via TypesInfo. |
| pkg/linters/ctxbackground/testdata/src/ctxbackground/ctxbackground.go | Add aliased-import and shadowing regression cases to the analyzer’s test inputs. |
| pkg/linters/ctxbackground/testdata/src/ctxbackground/ctxbackground.go.golden | Update expected suggested-fix output for the new regression cases. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 2
| import ( | ||
| "context" | ||
| ctxpkg "context" | ||
| ) |
There was a problem hiding this comment.
Moved the aliased-import regression case into its own file ctxbackground_alias.go (with a matching ctxbackground_alias.go.golden). It now imports only ctxpkg "context", and ctxbackground.go is back to a single "context" import.
| // flagged: aliased context import still resolves to context package | ||
| func DoWorkAliasedImport(ctx context.Context) { | ||
| _ = ctx // want `use the context.Context parameter instead of context.Background\(\)` | ||
| } |
There was a problem hiding this comment.
Addressed by splitting the alias case into ctxbackground_alias.go with a dedicated ctxbackground_alias.go.golden. The golden keeps import ctxpkg "context" because ctxpkg.Context is still referenced in the function parameter after the fix, so FormatSourceRemoveImports leaves it in place. The import section now correctly matches the post-fix source.
|
@copilot run pr-finisher skill |
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
``
|
|
@copilot review all comments and address unresolved review feedback.
|
|
@copilot summarize the remaining blockers and confirm merge readiness.
|
ctxbackgroundmixed two detection strategies:context.Contextparameters were identified by type identity, whilecontext.Background()calls were identified by the literal identifier namecontext. That caused alias-import false negatives and shadowing false positives.Analyzer precision
*analysis.PassintoisContextBackgroundCall.pass.TypesInfo.ObjectOf*types.PkgNamepkgName.Imported().Path() == "context"Regression coverage
import ctxpkg "context"+ctxpkg.Background()is reported.contextwithBackground()method is not reported.pr-sous-chef run: https://github.com/github/gh-aw/actions/runs/27434456691