Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

golangci-lint reports undefined (typecheck) for a type declared in another file

Your error

handlers/user.go:14:9: undefined: User (typecheck)1 more line it can show

Fix it fast

Most common cause, 1 of 3

golangci-lint is given file paths instead of the package

Pass package patterns such as ./... rather than file names.

Fix
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/golangci/golangci-lint
    rev: v2.15.0
    hooks:
      - id: golangci-lint
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to golangci-lint v2.15.0, Go 1.27, Go 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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).

Run this first
go list -f 'included: {{.GoFiles}} {{.CgoFiles}}  ignored: {{.IgnoredGoFiles}}' ./handlers

Then find the line you see in this list and open its fix:

  1. 1golangci-lint is given file paths instead of the packageThe declaring file is listed under included, and the command, editor setting or git hook passes .go file names to golangci-lint
  2. 2The declaring file is excluded by a build tag or an OS suffixgo list shows the file under ignored, and it starts with a //go:build line or has a name like store_linux.go
  3. 3cgo is disabled, so a file that imports "C" is skippedThe 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.

Check
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.

Fix
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/golangci/golangci-lint
    rev: v2.15.0
    hooks:
      - id: golangci-lint

Verify

A run over ./... prints no typecheck lines, and the hook passes on the same commit.

Verify
golangci-lint run ./...

Confirmed by golangci-lint.run

Did this fix it?

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.

Check
head -n 3 handlers/user_store.go

Fix

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.

Fix
# .golangci.yml
version: "2"
run:
  build-tags:
    - integration

Verify

go list puts the file under included when the tag is set, and the typecheck line disappears.

Verify
go list -tags integration -f '{{.GoFiles}}' ./handlers && golangci-lint run --build-tags integration ./...

Confirmed by pkg.go.dev

Did this fix it?

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.

Check
go env CGO_ENABLED CC

Fix

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.

Fix
# 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.

Verify
CGO_ENABLED=1 go list -f '{{.CgoFiles}}' ./handlers

Confirmed by pkg.go.dev

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.