golangci/golangci-lint · error

can't parse patch: %w

Error message

can't parse patch: %w

What it means

ExtractDiagnosticFromPatch parses the unified diff produced by the formatter using diffpkg.ParseMultiFileDiff. If the patch text is not parseable, the error is wrapped as "can't parse patch". This is normally an internal consistency failure between the diff generator and parser.

Source

Thrown at pkg/goformatters/internal/diff.go:221

			originalNumber: currentOriginalLineNumber + 1,
			typ:            diffLineAdded,
			data:           "",
		}
		diffLines = append(diffLines, dl)
	}

	return diffLines
}

func ExtractDiagnosticFromPatch(
	pass *analysis.Pass,
	file *ast.File,
	patch []byte,
	logger logutils.Log,
) error {
	diffs, err := diffpkg.ParseMultiFileDiff(patch)
	if err != nil {
		return fmt.Errorf("can't parse patch: %w", err)
	}

	if len(diffs) == 0 {
		return fmt.Errorf("got no diffs from patch parser: %s", patch)
	}

	ft := pass.Fset.File(file.Pos())

	adjLine := pass.Fset.PositionFor(file.Pos(), false).Line - pass.Fset.PositionFor(file.Pos(), true).Line

	for _, d := range diffs {
		if len(d.Hunks) == 0 {
			logger.Warnf("Got no hunks in diff %+v", d)
			continue
		}

		for _, hunk := range d.Hunks {
			p := hunkChangesParser{log: logger}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Update golangci-lint and its x/tools diff dependency
  2. Rename offending files to ASCII/safe names and retry
  3. Apply the formatter directly to the file to bypass diff reporting
  4. Capture the patch (enable debug output) and file an upstream issue if reproducible

Example fix

// before: file with problematic name
my file (v2).go
// after: rename to a safe name
my_file_v2.go
Defensive patterns

Strategy: fallback

Try / catch

// degrade gracefully: log patch and continue
if err := ExtractDiagnosticFromPatch(pass, file, patch, logger); err != nil {
    logger.Warnf("skipping unparseable patch: %v", err)
}

Prevention

When it happens

Trigger: diffpkg.ParseMultiFileDiff(patch) returns an error inside ExtractDiagnosticFromPatch — malformed unified diff output, e.g. non-standard headers produced by the diff step for odd file names or binary/empty content.

Common situations: Filenames with spaces/unicode that confuse diff headers; formatter emitting a non-unified diff; encoding issues; version mismatch between diff generator and parser libraries.

Related errors


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