golangci-lint reports undefined (typecheck) for a type declared in another file
Fix it fast
Most common cause, 1 of 3golangci-lint is given file paths instead of the package
Pass package patterns such as ./... rather than file names.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v2.15.0
hooks:
- id: golangci-lintShort answer
undefined: User (typecheck) from golangci-lint is a compile error, not a lint finding: the type checker never saw the file that declares User. The linter got single file names instead of the package, or a build constraint or disabled cgo excluded that file. Run golangci-lint run ./... with your build tags. Applies to golangci-lint v2.15 and Go 1.27.
Which cause do you have?
Ask Go which files of the package it includes and which it skips because of build constraints. If the declaring file is under included, the linter is being handed too few files (cause 1). If it is under ignored, a tag, an OS suffix or cgo keeps it out (cause 2 or 3).
go list -f 'included: {{.GoFiles}} {{.CgoFiles}} ignored: {{.IgnoredGoFiles}}' ./handlersThen find the line you see in this list and open its fix:
- 1golangci-lint is given file paths instead of the package
The declaring file is listed under included, and the command, editor setting or git hook passes .go file names to golangci-lint - 2The declaring file is excluded by a build tag or an OS suffix
go list shows the file under ignored, and it starts with a //go:build line or has a name like store_linux.go - 3cgo is disabled, so a file that imports "C" is skipped
The declaring file contains import "C", and go env CGO_ENABLED prints 0 in the lint job
What does the error look like?
handlers/user.go:14:9: undefined: User (typecheck)go build ./... succeeds, but golangci-lint still reports undefined (typecheck) for a struct from a sibling file
Cause 1 of 3
golangci-lint is given file paths instead of the package
You seeThe declaring file is listed under included, and the command, editor setting or git hook passes .go file names to golangci-lint
Check
golangci-lint prints undefined (typecheck) for a type in a sibling file when it analyses only part of a package. Its FAQ describes typecheck as a front-end for Go compiler errors and says that running on a single file or a group of files that depend on other files of the project cannot compile, so it cannot be analysed either.
go build ./... && golangci-lint run ./...Fix
Pass package patterns such as ./... rather than file names. For pre-commit, use the hooks published in the golangci-lint repository: they set pass_filenames to false and narrow the report to new code with --new-from-rev HEAD.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v2.15.0
hooks:
- id: golangci-lintVerify
A run over ./... prints no typecheck lines, and the hook passes on the same commit.
golangci-lint run ./...Confirmed by golangci-lint.run
Cause 2 of 3
The declaring file is excluded by a build tag or an OS suffix
You seego list shows the file under ignored, and it starts with a //go:build line or has a name like store_linux.go
Check
A //go:build line makes Go include the file only when its tags are satisfied, and a name ending in _GOOS or _GOARCH adds the same kind of condition. Linting without that tag, or on another operating system, leaves the file out, so every type it declares turns into undefined.
head -n 3 handlers/user_store.goFix
Run the linter with the tags your build uses, on the command line or under run.build-tags in the version 2 config. For a type every platform needs, move it out of the OS-specific file into one without a suffix.
# .golangci.yml
version: "2"
run:
build-tags:
- integrationVerify
go list puts the file under included when the tag is set, and the typecheck line disappears.
go list -tags integration -f '{{.GoFiles}}' ./handlers && golangci-lint run --build-tags integration ./...Confirmed by pkg.go.dev
Cause 3 of 3
cgo is disabled, so a file that imports "C" is skipped
You seeThe declaring file contains import "C", and go env CGO_ENABLED prints 0 in the lint job
Check
Go treats import "C" as an implied cgo build constraint, so with cgo disabled those files are not built at all. The cgo docs add that cgo is off by default when cross-compiling or when no C compiler such as gcc or clang is on PATH.
go env CGO_ENABLED CCFix
Install a C compiler in the lint image and set CGO_ENABLED=1 for the job, or keep pure Go types in a file that does not import "C" so they exist in every build.
# Debian or Ubuntu based CI image
apt-get update && apt-get install -y gcc
CGO_ENABLED=1 golangci-lint run ./...Verify
go env CGO_ENABLED prints 1 in the job, and the cgo file is listed under included.
CGO_ENABLED=1 go list -f '{{.CgoFiles}}' ./handlersConfirmed by pkg.go.dev
How do I stop it coming back?
- Run golangci-lint in CI over ./... with the same build tags and CGO_ENABLED value as your release build.
- Use the golangci-lint-full hook in CI and the golangci-lint hook locally; neither passes file names to the linter.
- Keep types shared across platforms in files without OS suffixes or //go:build lines.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.