Skip to content

ctxbackground: resolve Background() calls by package identity, not identifier text#38854

Open
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-ctxbackground-precision
Open

ctxbackground: resolve Background() calls by package identity, not identifier text#38854
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-ctxbackground-precision

Conversation

Copilot AI commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

ctxbackground mixed two detection strategies: context.Context parameters were identified by type identity, while context.Background() calls were identified by the literal identifier name context. That caused alias-import false negatives and shadowing false positives.

  • Analyzer precision

    • Threaded *analysis.Pass into isContextBackgroundCall.
    • Replaced name-based matching with type-info resolution:
      • resolve selector base via pass.TypesInfo.ObjectOf
      • require *types.PkgName
      • require pkgName.Imported().Path() == "context"
    • Behavior now aligns with the analyzer’s existing type-based parameter detection.
  • Regression coverage

    • Added aliased import case: import ctxpkg "context" + ctxpkg.Background() is reported.
    • Added shadowing case: local non-package identifier named context with Background() method is not reported.
    • Updated golden output accordingly for suggested-fix expectations.
func isContextBackgroundCall(pass *analysis.Pass, call *ast.CallExpr) bool {
	sel, ok := call.Fun.(*ast.SelectorExpr)
	if !ok || sel.Sel.Name != "Background" {
		return false
	}
	ident, ok := sel.X.(*ast.Ident)
	if !ok || pass.TypesInfo == nil {
		return false
	}
	pkgName, ok := pass.TypesInfo.ObjectOf(ident).(*types.PkgName)
	return ok && pkgName.Imported().Path() == "context"
}

pr-sous-chef run: https://github.com/github/gh-aw/actions/runs/27434456691

Generated by 👨‍🍳 PR Sous Chef · 73.5 AIC · ⌖ 1.05 AIC · ⊞ 17.3K ·

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ctxbackground precision issue with context detection ctxbackground: resolve Background() calls by package identity, not identifier text Jun 12, 2026
Copilot AI requested a review from pelikhan June 12, 2026 15:33
@pelikhan pelikhan marked this pull request as ready for review June 12, 2026 15:55
Copilot AI review requested due to automatic review settings June 12, 2026 15:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 isContextBackgroundCall to take *analysis.Pass and use pass.TypesInfo.ObjectOf + *types.PkgName resolution to confirm the stdlib "context" package.
  • Added regression coverage for aliased imports and shadowed context identifiers 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

Comment on lines 3 to 6
import (
"context"
ctxpkg "context"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +34 to +37
// 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\(\)`
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot run pr-finisher skill

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

``
@copilot refresh the branch and summarize any remaining blockers.

Generated by 👨‍🍳 PR Sous Chef · 53.8 AIC · ⌖ 0.972 AIC · ⊞ 17.3K ·

@github-actions

Copy link
Copy Markdown
Contributor

@copilot review all comments and address unresolved review feedback.

Generated by 👨‍🍳 PR Sous Chef · 73.5 AIC · ⌖ 1.05 AIC · ⊞ 17.3K ·

@github-actions

Copy link
Copy Markdown
Contributor

@copilot summarize the remaining blockers and confirm merge readiness.

Generated by 👨‍🍳 PR Sous Chef · 73.5 AIC · ⌖ 1.05 AIC · ⊞ 17.3K ·

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.

3 participants