golangci/golangci-lint · error

failed to parse file: %w

Error message

failed to parse file: %w

What it means

The generated-file matcher parses each candidate file with go/parser (package clause only, with comments) to find the '// Code generated ...' doc comment. If the file is not valid Go, ParseFile fails and this wrapped error is returned. golangci-lint cannot decide generated status for a file it cannot parse.

Source

Thrown at pkg/result/processors/exclusion_generated_file_matcher.go:53

	mode string
}

func NewGeneratedFileMatcher(mode string) *GeneratedFileMatcher {
	return &GeneratedFileMatcher{
		debugf: logutils.Debug(logutils.DebugKeyGeneratedFileFilter),
		mode:   mode,
	}
}

func (p *GeneratedFileMatcher) IsGeneratedFile(filepath string, src any) (bool, error) {
	if p.mode == config.GeneratedModeDisable {
		return false, nil
	}

	file, err := parser.ParseFile(token.NewFileSet(), filepath, src, parser.PackageClauseOnly|parser.ParseComments)
	if err != nil {
		return false, fmt.Errorf("failed to parse file: %w", err)
	}

	if p.mode == config.GeneratedModeStrict {
		return isGeneratedFileStrict(file), nil
	}

	doc := getComments(file)

	return p.isGeneratedFileLax(doc), nil
}

// isGeneratedFileLax reports whether the source file is generated code.
// The function uses a bit laxer rules than isGeneratedFileStrict to match more generated code.
// See https://github.com/golangci/golangci-lint/issues/48 and https://github.com/golangci/golangci-lint/issues/72.
func (p *GeneratedFileMatcher) isGeneratedFileLax(doc string) bool {
	markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile, genSwaggerCodegen}

	doc = strings.ToLower(doc)

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Fix the syntax error in the flagged file (run gofmt/go vet locally to locate it)
  2. Regenerate the file with its code generator at the correct version
  3. Ensure only valid .go files are linted (adjust skip-files/exclusions)
  4. If the file is intentionally not Go, rename its extension
Defensive patterns

Strategy: validation

Validate before calling

// Go: pre-check that the file is parseable Go before treating it as generated-candidate
fset := token.NewFileSet()
if _, err := parser.ParseFile(fset, path, nil, parser.PackageClauseOnly|parser.ParseComments); err != nil {
    return fmt.Errorf("not valid Go, skip generated check: %w", err)
}

Try / catch

gen, err := m.IsGeneratedFile(path, nil)
if err != nil {
    var perr scanner.ErrorList
    if errors.As(err, &perr) { log.Printf("parse errors in %s: %v", path, perr) }
    return err
}

Prevention

When it happens

Trigger: IsGeneratedFile is called (from shouldPassIssue, process, or formatStdIn) with filepath whose contents are not parseable Go — invalid syntax, empty/corrupt file, or a non-Go file fed to the processor.

Common situations: Broken generated code committed to the repo; template-rendered .go files with placeholder syntax errors; mixing file extensions (e.g. linting .go files that contain JSON); tool version mismatch producing partially-written files during codegen.

Understand the failure class

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/20b28e8e97b7b579. Report an issue: GitHub.