Skip to content

go: golangci-lint sees Go generated from other targets - #23742

Open
frekw wants to merge 1 commit into
pantsbuild:mainfrom
frekw:frekw/golangci-lint-generated-go
Open

frekw wants to merge 1 commit into
pantsbuild:mainfrom
frekw:frekw/golangci-lint-generated-go

Conversation

@frekw

@frekw frekw commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This PR was written primarily by Claude Code (Claude Opus 5.5). I reviewed the change and its tests before opening it.

Fixes #23707.

Problem

golangci-lint runs the real go tooling over a copy of the module, so every package a linted package imports has to exist as files in the sandbox. The rule copies the plain sources of the transitive closure and never runs codegen. A go_package that imports Go generated from a protobuf_sources target compiles and tests fine under Pants, but pants lint fails typecheck with no required module provides package .... Every other linter on that package then fails with it.

Solution

For each target in the closure that the module's import path mapping knows about and that isn't a go_package, the rule hydrates its sources with enable_codegen=True for GoPackageSourcesField. It then writes the generated files where go looks for the package: the go.mod directory joined with the import path relative to the module path.

Codegen writes its output beside its own sources (a .pb.go next to its .proto), and that is usually not the same directory, so relocating matters. In the new test the proto sits in protos/ with go_package = "example.com/lint/gen/greeting", and the generated file has to land in gen/greeting/.

Nothing in the rule is protobuf-specific. It uses the same address_to_import_path mapping that dependency inference uses, so any backend that plugs into GoModuleImportPathsMappingsHook gets the same treatment.

Generated packages whose import path is outside the module path are left out, as before. No directory in the module can provide them, so placing them would need a replace directive or a go.work, which is a bigger change.

Not included

go vet has the same gap, as the issue notes. It also copies only the linted packages' own sources, not the rest of the closure, and runs from the build root in a single partition. Fixing it properly is a separate change, so I left it out.

Testing

test_generated_go in rules_integration_test.py fails on main with the typecheck error above and passes with this change. The other golangci-lint tests pass unchanged.

🤖 Generated with Claude Code

golangci-lint copied only the plain sources of the closure, so a package
importing Go generated from e.g. protobuf_sources failed typecheck. Hydrate
codegen for GoPackageSourcesField and place each generated package at the
directory go resolves its import path to.

Fixes pantsbuild#23707.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tdyas
tdyas requested a review from jgranstrom November 2, 2026 05:00
@benjyw

benjyw commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution. We've just branched for 2.34.x, so merging this pull request now will come out in 2.35.x, please move the release notes updates to docs/notes/2.35.x.md if that's appropriate.

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.

golangci-lint fails typecheck on Go packages that import generated protobuf code

2 participants